]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Improve handling of dropped partitioned indexes for REINDEX INDEX
authorMichael Paquier <michael@paquier.xyz>
Thu, 18 Jan 2024 07:31:46 +0000 (16:31 +0900)
committerMichael Paquier <michael@paquier.xyz>
Thu, 18 Jan 2024 07:31:46 +0000 (16:31 +0900)
A REINDEX INDEX done on a partitioned index builds a list of the indexes
to work on before processing its partitions in individual transactions.
When combined with a DROP of the partitioned index, there was a window
where it was possible to see some unexpected "could not open relation
with OID", synonym of relation lookup error.  The code was robust enough
to handle the case where the parent relation is missing, but not the
case where an index would be gone missing.

This is similar to 1d65416661bb.

Support for REINDEX on partitioned relations has been introduced in
a6642b3ae060, so backpatch down to 14.

Author: Fei Changhong
Discussion: https://postgr.es/m/tencent_6A52106095ACDE55333E3AD33F304C0C3909@qq.com
Backpatch-through: 14

src/backend/catalog/index.c

index db697a9fc38f35d9af44048317d8b27c8fb7c8a6..bab02e16b528ff7b48b82cc8a37dbe3a82c14071 100644 (file)
@@ -3621,7 +3621,24 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence,
         * Open the target index relation and get an exclusive lock on it, to
         * ensure that no one else is touching this particular index.
         */
-       iRel = index_open(indexId, AccessExclusiveLock);
+       if ((params->options & REINDEXOPT_MISSING_OK) != 0)
+               iRel = try_index_open(indexId, AccessExclusiveLock);
+       else
+               iRel = index_open(indexId, AccessExclusiveLock);
+
+       /* if index relation is gone, leave */
+       if (!iRel)
+       {
+               /* Roll back any GUC changes */
+               AtEOXact_GUC(false, save_nestlevel);
+
+               /* Restore userid and security context */
+               SetUserIdAndSecContext(save_userid, save_sec_context);
+
+               /* Close parent heap relation, but keep locks */
+               table_close(heapRelation, NoLock);
+               return;
+       }
 
        if (progress)
                pgstat_progress_update_param(PROGRESS_CREATEIDX_ACCESS_METHOD_OID,