postgresql/src/backend
Michael Paquier 9acea52ea3 Fix corruption of toast indexes with REINDEX CONCURRENTLY
REINDEX CONCURRENTLY run on a toast index or a toast relation could
corrupt the target indexes rebuilt, as a backend running in parallel
that manipulates toast values would directly release the lock on the
toast relation when its local operation is done, rather than releasing
the lock once the transaction that manipulated the toast values
committed.

The fix done here is simple: we now hold a ROW EXCLUSIVE lock on the
toast relation when saving or deleting a toast value until the
transaction working on them is committed, so as a concurrent reindex
happening in parallel would be able to wait for any activity and see any
new rows inserted (or deleted).

An isolation test is added to check after the case fixed here, which is
a bit fancy by design as it relies on allow_system_table_mods to rename
the toast table and its index to fixed names.  This way, it is possible
to reindex them directly without any dependency on the OID of the
underlying relation.  Note that this could not use a DO block either, as
REINDEX CONCURRENTLY cannot be run in a transaction block.  The test is
backpatched down to 13, where it is possible, thanks to c4a7a39, to use
allow_system_table_mods in a test suite.

Reported-by: Alexey Ermakov
Analyzed-by: Andres Freund, Noah Misch
Author: Michael Paquier
Reviewed-by: Nathan Bossart
Discussion: https://postgr.es/m/17268-d2fb426e0895abd4@postgresql.org
Backpatch-through: 12
2021-12-08 11:01:19 +09:00
..
access Fix corruption of toast indexes with REINDEX CONCURRENTLY 2021-12-08 11:01:19 +09:00
bootstrap Skip WAL for new relfilenodes, under wal_level=minimal. 2020-04-04 12:25:34 -07:00
catalog Avoid leaking memory during large-scale REASSIGN OWNED BY operations. 2021-12-01 13:44:47 -05:00
commands Block ALTER TABLE .. DROP NOT NULL on columns in replica identity index 2021-11-25 15:05:28 +09:00
executor Fix variable lifespan in ExecInitCoerceToDomain(). 2021-11-02 13:36:57 -04:00
foreign Update copyrights for 2020 2020-01-01 12:21:45 -05:00
jit Avoid some other O(N^2) hazards in list manipulation. 2021-11-01 16:24:40 -04:00
lib Fix typo in comment 2021-04-20 14:36:41 +02:00
libpq On Windows, also call shutdown() while closing the client socket. 2021-12-07 13:34:19 -05:00
main Add PostgreSQL home page to --help output 2020-02-28 13:12:21 +01:00
nodes Avoid O(N^2) behavior in SyncPostCheckpoint(). 2021-11-02 11:31:54 -04:00
optimizer Avoid O(N^2) behavior in SyncPostCheckpoint(). 2021-11-02 11:31:54 -04:00
parser Avoid O(N^2) behavior in SyncPostCheckpoint(). 2021-11-02 11:31:54 -04:00
partitioning Avoid using ambiguous word "non-negative" in error messages. 2021-07-28 01:21:52 +09:00
po Fix typo 2021-11-08 09:17:24 -03:00
port Reject huge_pages=on if shared_memory_type=sysv. 2021-10-26 13:04:40 +13:00
postmaster Reject extraneous data after SSL or GSS encryption handshake. 2021-11-08 11:01:43 -05:00
regex Make pg_regexec() robust against out-of-range search_start. 2021-09-11 15:19:49 -04:00
replication Doc: improve protocol spec for logical replication Type messages. 2021-11-10 13:12:58 -05:00
rewrite Fix rewriter to set hasModifyingCTE correctly on rewritten queries. 2021-09-08 12:05:43 -04:00
snowball Update copyrights for 2020 2020-01-01 12:21:45 -05:00
statistics Release memory allocated by dependency_degree 2021-09-23 18:34:01 +02:00
storage Fix parallel operations that prevent oldest xmin from advancing. 2021-11-19 09:24:00 +05:30
tcop Fix Portal snapshot tracking to handle subtransactions properly. 2021-10-01 11:10:12 -04:00
tsearch Don't leak compiled regex(es) when an ispell cache entry is dropped. 2021-03-18 21:44:43 -04:00
utils Fix memory overrun when querying pg_stat_slru 2021-11-12 21:50:08 +09:00
.gitignore Add .gitignore entries for AIX-specific intermediate build artifacts. 2015-07-08 20:44:22 -04:00
common.mk Remove PARTIAL_LINKING build mode. 2018-03-30 17:33:04 -07:00
Makefile Update copyrights for 2020 2020-01-01 12:21:45 -05:00
nls.mk Translation updates 2020-09-14 13:14:53 +02:00