postgresql/src/backend/access
Michael Paquier f99870dd86 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:08 +09:00
..
brin Ignore BRIN indexes when checking for HOT udpates 2021-11-30 20:04:38 +01:00
common Fix corruption of toast indexes with REINDEX CONCURRENTLY 2021-12-08 11:01:08 +09:00
gin Ignore BRIN indexes when checking for HOT udpates 2021-11-30 20:04:38 +01:00
gist Ignore BRIN indexes when checking for HOT udpates 2021-11-30 20:04:38 +01:00
hash Ignore BRIN indexes when checking for HOT udpates 2021-11-30 20:04:38 +01:00
heap vacuumlazy.c: fix remaining "dead tuple" references. 2021-11-30 11:40:33 -08:00
index Add hardening to catch invalid TIDs in indexes. 2021-11-04 19:54:05 -07:00
nbtree Ignore BRIN indexes when checking for HOT udpates 2021-11-30 20:04:38 +01:00
rmgrdesc Fix WAL replay in presence of an incomplete record 2021-09-29 11:21:51 -03:00
spgist Ignore BRIN indexes when checking for HOT udpates 2021-11-30 20:04:38 +01:00
table Replace RelationOpenSmgr() with RelationGetSmgr(). 2021-07-12 17:01:36 -04:00
tablesample Update copyright for 2021 2021-01-02 13:06:25 -05:00
transam Remove mention of TimeLineID update from comments 2021-12-01 14:17:24 +01:00
Makefile Introduce access/{table.h, relation.h}, for generic functions from heapam.h. 2019-01-21 10:51:36 -08:00