Avoid spurious waits in concurrent indexing
authorAlvaro Herrera <alvherre@alvh.no-ip.org>
Wed, 25 Nov 2020 21:21:08 +0000 (18:21 -0300)
committerAlvaro Herrera <alvherre@alvh.no-ip.org>
Wed, 25 Nov 2020 21:22:57 +0000 (18:22 -0300)
commitc98763bf51bf610b3ee7e209fc76c3ff9a6b3163
tree024e5968dee23ff8706cf8bb315e254833b6e6c7
parent314fb9baeacb3426a5d9a26132aae8d828cc0ad7
Avoid spurious waits in concurrent indexing

In the various waiting phases of CREATE INDEX CONCURRENTLY (CIC) and
REINDEX CONCURRENTLY (RC), we wait for other processes to release their
snapshots; this is necessary in general for correctness.  However,
processes doing CIC in other tables cannot possibly affect CIC or RC
done in "this" table, so we don't need to wait for those.  This commit
adds a flag in MyProc->statusFlags to indicate that the current process
is doing CIC, so that other processes doing CIC or RC can ignore it when
waiting.

Note that this logic is only valid if the index does not access other
tables.  For simplicity we avoid setting the flag if the index has a
column that's an expression, or has a WHERE predicate.  (It is possible
to have expressional or partial indexes that do not access other tables,
but figuring that out would require more work.)

This flag can potentially also be used by processes doing REINDEX
CONCURRENTLY to be skipped; and by VACUUM to ignore processes in CIC or
RC for the purposes of computing an Xmin.  That's left for future
commits.

Author: Álvaro Herrera <alvherre@alvh.no-ip.org>
Author: Dimitry Dolgov <9erthalion6@gmail.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/20200810233815.GA18970@alvherre.pgsql
src/backend/commands/indexcmds.c
src/include/storage/proc.h