aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/indexcmds.c
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2023-06-30 13:54:48 +0900
committerMichael Paquier <michael@paquier.xyz>2023-06-30 13:54:48 +0900
commitcfc43aeb3810ebaa8dbda4807046a4c953d9e992 (patch)
tree8493a4f565369145a963d8d5baf4004f85b62a8a /src/backend/commands/indexcmds.c
parentc951e9042dd12db80cac4e9a50f198a1eead2036 (diff)
downloadpostgresql-cfc43aeb3810ebaa8dbda4807046a4c953d9e992.tar.gz
postgresql-cfc43aeb3810ebaa8dbda4807046a4c953d9e992.zip
Fix marking of indisvalid for partitioned indexes at creation
The logic that introduced partitioned indexes missed a few things when invalidating a partitioned index when these are created, still the code is written to handle recursions: 1) If created from scratch because a mapping index could not be found, the new index created could be itself invalid, if for example it was a partitioned index with one of its leaves invalid. 2) A CCI was missing when indisvalid is set for a parent index, leading to inconsistent trees when recursing across more than one level for a partitioned index creation if an invalidation of the parent was required. This could lead to the creation of a partition index tree where some of the partitioned indexes are marked as invalid, but some of the parents are marked valid, which is not something that should happen (as validatePartitionedIndex() defines, indisvalid is switched to true for a partitioned index iff all its partitions are themselves valid). This patch makes sure that indisvalid is set to false on a partitioned index if at least one of its partition is invalid. The flag is set to true if *all* its partitions are valid. The regression test added in this commit abuses of a failed concurrent index creation, marked as invalid, that maps with an index created on its partitioned table afterwards. Reported-by: Alexander Lakhin Reviewed-by: Alexander Lakhin Discussion: https://postgr.es/m/14987634-43c0-0cb3-e075-94d423607e08@gmail.com Backpatch-through: 11
Diffstat (limited to 'src/backend/commands/indexcmds.c')
-rw-r--r--src/backend/commands/indexcmds.c31
1 files changed, 24 insertions, 7 deletions
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index 9bc97e1fc21..403f5fc143f 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -1416,6 +1416,7 @@ DefineIndex(Oid relationId,
IndexStmt *childStmt = copyObject(stmt);
bool found_whole_row;
ListCell *lc;
+ ObjectAddress childAddr;
/*
* We can't use the same index name for the child index,
@@ -1469,15 +1470,25 @@ DefineIndex(Oid relationId,
Assert(GetUserId() == child_save_userid);
SetUserIdAndSecContext(root_save_userid,
root_save_sec_context);
- DefineIndex(childRelid, childStmt,
- InvalidOid, /* no predefined OID */
- indexRelationId, /* this is our child */
- createdConstraintId,
- -1,
- is_alter_table, check_rights, check_not_in_use,
- skip_build, quiet);
+ childAddr =
+ DefineIndex(childRelid, childStmt,
+ InvalidOid, /* no predefined OID */
+ indexRelationId, /* this is our child */
+ createdConstraintId,
+ -1,
+ is_alter_table, check_rights,
+ check_not_in_use,
+ skip_build, quiet);
SetUserIdAndSecContext(child_save_userid,
child_save_sec_context);
+
+ /*
+ * Check if the index just created is valid or not, as it
+ * could be possible that it has been switched as invalid
+ * when recursing across multiple partition levels.
+ */
+ if (!get_index_isvalid(childAddr.objectId))
+ invalidate_parent = true;
}
free_attrmap(attmap);
@@ -1507,6 +1518,12 @@ DefineIndex(Oid relationId,
ReleaseSysCache(tup);
table_close(pg_index, RowExclusiveLock);
heap_freetuple(newtup);
+
+ /*
+ * CCI here to make this update visible, in case this recurses
+ * across multiple partition levels.
+ */
+ CommandCounterIncrement();
}
}