Fix waits of REINDEX CONCURRENTLY for indexes with predicates or expressions
authorMichael Paquier <michael@paquier.xyz>
Mon, 9 Sep 2024 04:49:36 +0000 (13:49 +0900)
committerMichael Paquier <michael@paquier.xyz>
Mon, 9 Sep 2024 04:49:36 +0000 (13:49 +0900)
As introduced by f9900df5f94, a REINDEX CONCURRENTLY job done for an
index with predicates or expressions would set PROC_IN_SAFE_IC in its
MyProc->statusFlags, causing it to be ignored by other concurrent
operations.

Such concurrent index rebuilds should never be ignored, as a predicate
or an expression could call a user-defined function that accesses a
different table than the table where the index is rebuilt.

A test that uses injection points is added, backpatched down to 17.
Michail has proposed a different test, but I have added something
simpler with more coverage.

Oversight in f9900df5f949.

Author: Michail Nikolaev
Discussion: https://postgr.es/m/CANtu0oj9A3kZVduFTG0vrmGnKB+DCHgEpzOp0qAyOgmks84j0w@mail.gmail.com
Backpatch-through: 14

src/backend/commands/indexcmds.c
src/test/modules/injection_points/Makefile
src/test/modules/injection_points/expected/reindex_conc.out [new file with mode: 0644]
src/test/modules/injection_points/meson.build
src/test/modules/injection_points/sql/reindex_conc.sql [new file with mode: 0644]

index c5a56c75f699521a85be73511ac4931c0e13e9b1..b987e023849e9aa7e65750b58453f6f17568624b 100644 (file)
@@ -61,6 +61,7 @@
 #include "utils/builtins.h"
 #include "utils/fmgroids.h"
 #include "utils/guc.h"
+#include "utils/injection_point.h"
 #include "utils/inval.h"
 #include "utils/lsyscache.h"
 #include "utils/memutils.h"
@@ -3782,8 +3783,16 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
        RestrictSearchPath();
 
        /* determine safety of this index for set_indexsafe_procflags */
-       idx->safe = (indexRel->rd_indexprs == NIL &&
-                    indexRel->rd_indpred == NIL);
+       idx->safe = (RelationGetIndexExpressions(indexRel) == NIL &&
+                    RelationGetIndexPredicate(indexRel) == NIL);
+
+#ifdef USE_INJECTION_POINTS
+       if (idx->safe)
+           INJECTION_POINT("reindex-conc-index-safe");
+       else
+           INJECTION_POINT("reindex-conc-index-not-safe");
+#endif
+
        idx->tableId = RelationGetRelid(heapRel);
        idx->amId = indexRel->rd_rel->relam;
 
index 1c1c2d0b130743e56fc143ae99117ef2a428fd67..8cb8c498e23e2e8c998e63035000778a6b27e294 100644 (file)
@@ -10,7 +10,7 @@ EXTENSION = injection_points
 DATA = injection_points--1.0.sql
 PGFILEDESC = "injection_points - facility for injection points"
 
-REGRESS = injection_points
+REGRESS = injection_points reindex_conc
 REGRESS_OPTS = --dlpath=$(top_builddir)/src/test/regress
 
 ISOLATION = inplace
diff --git a/src/test/modules/injection_points/expected/reindex_conc.out b/src/test/modules/injection_points/expected/reindex_conc.out
new file mode 100644 (file)
index 0000000..db8de4b
--- /dev/null
@@ -0,0 +1,51 @@
+-- Tests for REINDEX CONCURRENTLY
+CREATE EXTENSION injection_points;
+-- Check safety of indexes with predicates and expressions.
+SELECT injection_points_set_local();
+ injection_points_set_local 
+----------------------------
+(1 row)
+
+SELECT injection_points_attach('reindex-conc-index-safe', 'notice');
+ injection_points_attach 
+-------------------------
+(1 row)
+
+SELECT injection_points_attach('reindex-conc-index-not-safe', 'notice');
+ injection_points_attach 
+-------------------------
+(1 row)
+
+CREATE SCHEMA reindex_inj;
+CREATE TABLE reindex_inj.tbl(i int primary key, updated_at timestamp);
+CREATE UNIQUE INDEX ind_simple ON reindex_inj.tbl(i);
+CREATE UNIQUE INDEX ind_expr ON reindex_inj.tbl(ABS(i));
+CREATE UNIQUE INDEX ind_pred ON reindex_inj.tbl(i) WHERE mod(i, 2) = 0;
+CREATE UNIQUE INDEX ind_expr_pred ON reindex_inj.tbl(abs(i)) WHERE mod(i, 2) = 0;
+REINDEX INDEX CONCURRENTLY reindex_inj.ind_simple;
+NOTICE:  notice triggered for injection point reindex-conc-index-safe
+REINDEX INDEX CONCURRENTLY reindex_inj.ind_expr;
+NOTICE:  notice triggered for injection point reindex-conc-index-not-safe
+REINDEX INDEX CONCURRENTLY reindex_inj.ind_pred;
+NOTICE:  notice triggered for injection point reindex-conc-index-not-safe
+REINDEX INDEX CONCURRENTLY reindex_inj.ind_expr_pred;
+NOTICE:  notice triggered for injection point reindex-conc-index-not-safe
+-- Cleanup
+SELECT injection_points_detach('reindex-conc-index-safe');
+ injection_points_detach 
+-------------------------
+(1 row)
+
+SELECT injection_points_detach('reindex-conc-index-not-safe');
+ injection_points_detach 
+-------------------------
+(1 row)
+
+DROP TABLE reindex_inj.tbl;
+DROP SCHEMA reindex_inj;
+DROP EXTENSION injection_points;
index c9e357f644116ddb98d321b3f1db3a83ad372bae..fdb5a25d7b32778a540bb5766bfc767663cc1d98 100644 (file)
@@ -34,6 +34,7 @@ tests += {
   'regress': {
     'sql': [
       'injection_points',
+      'reindex_conc',
     ],
     'regress_args': ['--dlpath', meson.build_root() / 'src/test/regress'],
     # The injection points are cluster-wide, so disable installcheck
diff --git a/src/test/modules/injection_points/sql/reindex_conc.sql b/src/test/modules/injection_points/sql/reindex_conc.sql
new file mode 100644 (file)
index 0000000..6cf211e
--- /dev/null
@@ -0,0 +1,28 @@
+-- Tests for REINDEX CONCURRENTLY
+CREATE EXTENSION injection_points;
+
+-- Check safety of indexes with predicates and expressions.
+SELECT injection_points_set_local();
+SELECT injection_points_attach('reindex-conc-index-safe', 'notice');
+SELECT injection_points_attach('reindex-conc-index-not-safe', 'notice');
+
+CREATE SCHEMA reindex_inj;
+CREATE TABLE reindex_inj.tbl(i int primary key, updated_at timestamp);
+
+CREATE UNIQUE INDEX ind_simple ON reindex_inj.tbl(i);
+CREATE UNIQUE INDEX ind_expr ON reindex_inj.tbl(ABS(i));
+CREATE UNIQUE INDEX ind_pred ON reindex_inj.tbl(i) WHERE mod(i, 2) = 0;
+CREATE UNIQUE INDEX ind_expr_pred ON reindex_inj.tbl(abs(i)) WHERE mod(i, 2) = 0;
+
+REINDEX INDEX CONCURRENTLY reindex_inj.ind_simple;
+REINDEX INDEX CONCURRENTLY reindex_inj.ind_expr;
+REINDEX INDEX CONCURRENTLY reindex_inj.ind_pred;
+REINDEX INDEX CONCURRENTLY reindex_inj.ind_expr_pred;
+
+-- Cleanup
+SELECT injection_points_detach('reindex-conc-index-safe');
+SELECT injection_points_detach('reindex-conc-index-not-safe');
+DROP TABLE reindex_inj.tbl;
+DROP SCHEMA reindex_inj;
+
+DROP EXTENSION injection_points;