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
8daeaa9b642c 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
* create a partitioned index (table must be partitioned)
* INDEX_CREATE_SUPPRESS_PROGRESS:
* don't report progress during the index build.
+ * 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)
indexInfo,
collationIds, opclassIds, 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);
List *indexColNames = NIL;
List *indexExprs = NIL;
List *indexPreds = NIL;
+ Form_pg_index indexForm;
indexRelation = index_open(oldIndexId, RowExclusiveLock);
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);
stattargets,
reloptionsDatum,
flags,
- 0,
+ 0, /* constr_flags */
true, /* allow table to be a system catalog? */
false, /* is_internal? */
NULL);
CommitTransactionCommand();
}
+ INJECTION_POINT("reindex-conc-index-built", NULL);
+
StartTransactionCommand();
/*
#define INDEX_CREATE_PARTITIONED (1 << 5)
#define INDEX_CREATE_INVALID (1 << 6)
#define INDEX_CREATE_SUPPRESS_PROGRESS (1 << 7)
+#define INDEX_CREATE_DEFERRABLE (1 << 8)
extern Oid index_create(Relation heapRelation,
const char *indexRelationName,
ISOLATION = basic \
inplace \
+ reindex_concurrently_deferred \
repack \
repack_temporal \
repack_temporal_multirange \
--- /dev/null
+Parsed test spec with 2 sessions
+
+starting permutation: reindex check_catalog begin2 write2 write_dup resolve_dup commit2 wakeup noop1
+injection_points_attach
+-----------------------
+
+(1 row)
+
+step reindex: REINDEX TABLE CONCURRENTLY reind_deferred; <waiting ...>
+step check_catalog:
+ SELECT c.relname, i.indisunique, i.indimmediate, i.indisready, i.indisvalid
+ FROM pg_class c
+ JOIN pg_index i ON i.indexrelid = c.oid
+ WHERE c.relname = 'uq_val_ccnew';
+
+relname |indisunique|indimmediate|indisready|indisvalid
+------------+-----------+------------+----------+----------
+uq_val_ccnew|t |f |t |f
+(1 row)
+
+step begin2: BEGIN;
+step write2: INSERT INTO reind_deferred VALUES (3, 9);
+step write_dup: INSERT INTO reind_deferred VALUES (4, 9);
+step resolve_dup: UPDATE reind_deferred SET val = 10 WHERE id = 4;
+step commit2: COMMIT;
+step wakeup:
+ SELECT injection_points_detach('reindex-conc-index-built');
+ SELECT injection_points_wakeup('reindex-conc-index-built');
+
+injection_points_detach
+-----------------------
+
+(1 row)
+
+injection_points_wakeup
+-----------------------
+
+(1 row)
+
+step reindex: <... completed>
+step noop1:
'specs': [
'basic',
'inplace',
+ 'reindex_concurrently_deferred',
'repack',
'repack_temporal',
'repack_temporal_multirange',
--- /dev/null
+# REINDEX CONCURRENTLY with DEFERRED constraints
+#
+# Verify that concurrent writes that temporarily violate a deferred unique
+# constraint do not fail while REINDEX CONCURRENTLY is running.
+#
+# The injection point "reindex-conc-index-built" fires after the phase 2
+# of REINDEX CONCURRENTLY, when the new index has indisready = true (inserts
+# are checked against it) but indisvalid = false.
+
+setup
+{
+ CREATE EXTENSION injection_points;
+ CREATE TABLE reind_deferred (id int, val int,
+ CONSTRAINT uq_val UNIQUE(val) DEFERRABLE INITIALLY DEFERRED);
+ INSERT INTO reind_deferred VALUES (1, 1), (2, 2);
+}
+
+teardown
+{
+ DROP TABLE reind_deferred;
+ DROP EXTENSION injection_points;
+}
+
+session s1
+setup
+{
+ SELECT injection_points_set_local();
+ SELECT injection_points_attach('reindex-conc-index-built', 'wait');
+}
+step reindex { REINDEX TABLE CONCURRENTLY reind_deferred; }
+step noop1 { }
+
+session s2
+step check_catalog {
+ SELECT c.relname, i.indisunique, i.indimmediate, i.indisready, i.indisvalid
+ FROM pg_class c
+ JOIN pg_index i ON i.indexrelid = c.oid
+ WHERE c.relname = 'uq_val_ccnew';
+}
+step begin2 { BEGIN; }
+step write2 { INSERT INTO reind_deferred VALUES (3, 9); }
+step write_dup { INSERT INTO reind_deferred VALUES (4, 9); }
+step resolve_dup { UPDATE reind_deferred SET val = 10 WHERE id = 4; }
+step commit2 { COMMIT; }
+step wakeup {
+ SELECT injection_points_detach('reindex-conc-index-built');
+ SELECT injection_points_wakeup('reindex-conc-index-built');
+}
+
+permutation reindex check_catalog begin2 write2 write_dup resolve_dup commit2 wakeup noop1