postgresql/src/backend
Tom Lane 9c703c169a Make queries' locking of indexes more consistent.
The assertions added by commit b04aeb0a0 exposed that there are some
code paths wherein the executor will try to open an index without
holding any lock on it.  We do have some lock on the index's table,
so it seems likely that there's no fatal problem with this (for
instance, the index couldn't get dropped from under us).  Still,
it's bad practice and we should fix it.

To do so, remove the optimizations in ExecInitIndexScan and friends
that tried to avoid taking a lock on an index belonging to a target
relation, and just take the lock always.  In non-bug cases, this
will result in no additional shared-memory access, since we'll find
in the local lock table that we already have a lock of the desired
type; hence, no significant performance degradation should occur.

Also, adjust the planner and executor so that the type of lock taken
on an index is always identical to the type of lock taken for its table,
by relying on the recently added RangeTblEntry.rellockmode field.
This avoids some corner cases where that might not have been true
before (possibly resulting in extra locking overhead), and prevents
future maintenance issues from having multiple bits of logic that
all needed to be in sync.  In addition, this change removes all core
calls to ExecRelationIsTargetRelation, which avoids a possible O(N^2)
startup penalty for queries with large numbers of target relations.
(We'd probably remove that function altogether, were it not that we
advertise it as something that FDWs might want to use.)

Also adjust some places in selfuncs.c to not take any lock on indexes
they are transiently opening, since we can assume that plancat.c
did that already.

In passing, change gin_clean_pending_list() to take RowExclusiveLock
not AccessShareLock on its target index.  Although it's not clear that
that's actually a bug, it seemed very strange for a function that's
explicitly going to modify the index to use only AccessShareLock.

David Rowley, reviewed by Julien Rouhaud and Amit Langote,
a bit of further tweaking by me

Discussion: https://postgr.es/m/19465.1541636036@sss.pgh.pa.us
2019-04-04 15:12:58 -04:00
..
access Make queries' locking of indexes more consistent. 2019-04-04 15:12:58 -04:00
bootstrap tableam: relation creation, VACUUM FULL/CLUSTER, SET TABLESPACE. 2019-03-28 20:01:43 -07:00
catalog GSSAPI encryption support 2019-04-03 15:02:33 -04:00
commands Allow VACUUM to be run with index cleanup disabled. 2019-04-04 15:04:43 -04:00
executor Make queries' locking of indexes more consistent. 2019-04-04 15:12:58 -04:00
foreign Update copyright for 2019 2019-01-02 12:44:25 -05:00
jit Renaming for new subscripting mechanism 2019-02-01 12:50:32 -03:00
lib Further code review for new integerset code. 2019-03-25 12:23:48 -04:00
libpq GSSAPI encryption support 2019-04-03 15:02:33 -04:00
main Replace @postgresql.org with @lists.postgresql.org for mailinglists 2019-01-19 19:06:35 +01:00
nodes Generated columns 2019-03-30 08:15:57 +01:00
optimizer Make queries' locking of indexes more consistent. 2019-04-04 15:12:58 -04:00
parser Catch syntax error in generated column definition 2019-04-01 10:46:37 +02:00
partitioning Speed up planning when partitions can be pruned at plan time. 2019-03-30 18:58:55 -04:00
po Translation updates 2018-06-25 12:37:18 +02:00
port Silence -Wimplicit-fallthrough in sysv_shmem.c. 2019-04-03 23:23:35 -07:00
postmaster Allow VACUUM to be run with index cleanup disabled. 2019-04-04 15:04:43 -04:00
regex Collations with nondeterministic comparison 2019-03-22 12:12:43 +01:00
replication Generated columns 2019-03-30 08:15:57 +01:00
rewrite Perform RLS subquery checks as the right user when going via a view. 2019-04-02 08:13:59 +01:00
snowball Update copyright for 2019 2019-01-02 12:44:25 -05:00
statistics Reduce overhead of pg_mcv_list (de)serialization 2019-04-03 21:23:40 +02:00
storage Refactor the fsync queue for wider use. 2019-04-04 23:38:38 +13:00
tcop Log all statements from a sample of transactions 2019-04-03 18:43:59 -03:00
tsearch Move hash_any prototype from access/hash.h to utils/hashutils.h 2019-03-11 13:17:50 -03:00
utils Make queries' locking of indexes more consistent. 2019-04-04 15:12:58 -04:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
Makefile Get rid of jsonpath_gram.h and jsonpath_scanner.h 2019-03-20 11:13:34 +03:00
common.mk Remove PARTIAL_LINKING build mode. 2018-03-30 17:33:04 -07:00
nls.mk Translation updates 2018-06-25 12:37:18 +02:00