mirror of
https://github.com/postgres/postgres.git
synced 2026-10-23 13:29:09 +03:00
Fix propagation of indimmediate flag in index_create_copy()
index_create_copy is used to create copy definitions of existing indexes.
Currently, it passes 0 as constr_flags to index_create(), which results
in the copied index to always be created as immediate (indimmediate set
to true). For deferrable unique constraints, it means that the
transient index used during the phase 2 of REINDEX CONCURRENTLY forces
immediate constraint checks on concurrent inserts, which can cause
unexpected constraint violations based on the definition of the parent
table, inconsistently set in the copied index.
To fix this without violating the contract of constr_flags (which should
only be used when creating constraints) and without relaxing the strict
assertion in index_create(), this introduces a new index creation flag:
INDEX_CREATE_DEFERRABLE. If set, a copied index's indimmediate is set
to false, meaning that unique constraints are not enforced immediately
on insertion, but at transaction commit time.
An isolation test for REINDEX CONCURRENTLY is added, based on an
injection point waiting after phase 1 of the operation, where an index
copy has been built and is able to accept DMLs for its validation in
phase 2. The test is tentatively backpatched down to v17.
INJECTION_POINT() is outside a transaction context, which should be fine
on HEAD since 8daeaa9b64 but I suspect may cause issues in v19 and
older branches due to the wait facility depending on condition variables
and a DSM setup, but let's see what the buildfarm tells.
Author: Nitin Motiani <nitinmotiani@google.com>
Discussion: https://postgr.es/m/CAH5HC97JmjPpgiQOqW9xm8qXhNiu7zZ1Qh+FfhEESJuDv69kuQ@mail.gmail.com
Backpatch-through: 14
This commit is contained in:
@@ -702,6 +702,10 @@ UpdateIndexRelation(Oid indexoid,
|
||||
* already exists.
|
||||
* INDEX_CREATE_PARTITIONED:
|
||||
* create a partitioned index (table must be partitioned)
|
||||
* INDEX_CREATE_DEFERRABLE:
|
||||
* index supports a deferrable constraint, mark it as
|
||||
* non-immediate (indimmediate = false).
|
||||
*
|
||||
* constr_flags: flags passed to index_constraint_create
|
||||
* (only if INDEX_CREATE_ADD_CONSTRAINT is set)
|
||||
* allow_system_table_mods: allow table to be a system catalog
|
||||
@@ -1032,7 +1036,8 @@ index_create(Relation heapRelation,
|
||||
indexInfo,
|
||||
collationObjectId, classObjectId, coloptions,
|
||||
isprimary, is_exclusion,
|
||||
(constr_flags & INDEX_CONSTR_CREATE_DEFERRABLE) == 0,
|
||||
(constr_flags & INDEX_CONSTR_CREATE_DEFERRABLE) == 0 &&
|
||||
(flags & INDEX_CREATE_DEFERRABLE) == 0,
|
||||
!concurrent && !invalid,
|
||||
!concurrent);
|
||||
|
||||
@@ -1303,6 +1308,8 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId,
|
||||
List *indexColNames = NIL;
|
||||
List *indexExprs = NIL;
|
||||
List *indexPreds = NIL;
|
||||
bits16 flags = INDEX_CREATE_SKIP_BUILD | INDEX_CREATE_CONCURRENT;
|
||||
Form_pg_index indexForm;
|
||||
|
||||
indexRelation = index_open(oldIndexId, RowExclusiveLock);
|
||||
|
||||
@@ -1322,6 +1329,13 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId,
|
||||
indexTuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(oldIndexId));
|
||||
if (!HeapTupleIsValid(indexTuple))
|
||||
elog(ERROR, "cache lookup failed for index %u", oldIndexId);
|
||||
|
||||
indexForm = (Form_pg_index) GETSTRUCT(indexTuple);
|
||||
|
||||
/* Old index is deferrable, do the same for the new index */
|
||||
if (!indexForm->indimmediate)
|
||||
flags |= INDEX_CREATE_DEFERRABLE;
|
||||
|
||||
indclassDatum = SysCacheGetAttrNotNull(INDEXRELID, indexTuple,
|
||||
Anum_pg_index_indclass);
|
||||
indclass = (oidvector *) DatumGetPointer(indclassDatum);
|
||||
@@ -1428,8 +1442,8 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId,
|
||||
indclass->values,
|
||||
indcoloptions->values,
|
||||
optionDatum,
|
||||
INDEX_CREATE_SKIP_BUILD | INDEX_CREATE_CONCURRENT,
|
||||
0,
|
||||
flags,
|
||||
0, /* constr_flags */
|
||||
true, /* allow table to be a system catalog? */
|
||||
false, /* is_internal? */
|
||||
NULL);
|
||||
|
||||
@@ -65,6 +65,7 @@ extern void index_check_primary_key(Relation heapRel,
|
||||
#define INDEX_CREATE_IF_NOT_EXISTS (1 << 4)
|
||||
#define INDEX_CREATE_PARTITIONED (1 << 5)
|
||||
#define INDEX_CREATE_INVALID (1 << 6)
|
||||
#define INDEX_CREATE_DEFERRABLE (1 << 7)
|
||||
|
||||
extern Oid index_create(Relation heapRelation,
|
||||
const char *indexRelationName,
|
||||
|
||||
Reference in New Issue
Block a user