Skip to content

Commit add5cf2

Browse files
committed
Preserve opclass parameters across REINDEX CONCURRENTLY
The opclass parameter Datums from the old index are fetched in the same way as for predicates and expressions, by grabbing them directly from the system catalogs. They are then copied into the new IndexInfo that will be used for the creation of the new copy. This caused the new index to be rebuilt with default parameters rather than the ones pre-defined by a user. The only way to get back a new index with correct opclass parameters would be to recreate a new index from scratch. The issue has been introduced by 911e702. Author: Michael Paquier Reviewed-by: Zhihong Yu Discussion: https://postgr.es/m/YX0CG/QpLXcPr8HJ@paquier.xyz Backpatch-through: 13
1 parent a667b06 commit add5cf2

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/backend/catalog/index.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
#include "storage/procarray.h"
7474
#include "storage/smgr.h"
7575
#include "utils/builtins.h"
76+
#include "utils/datum.h"
7677
#include "utils/fmgroids.h"
7778
#include "utils/guc.h"
7879
#include "utils/inval.h"
@@ -1365,6 +1366,15 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId,
13651366
newInfo->ii_IndexAttrNumbers[i] = oldInfo->ii_IndexAttrNumbers[i];
13661367
}
13671368

1369+
/* Extract opclass parameters for each attribute, if any */
1370+
if (oldInfo->ii_OpclassOptions != NULL)
1371+
{
1372+
newInfo->ii_OpclassOptions = palloc0(sizeof(Datum) *
1373+
newInfo->ii_NumIndexAttrs);
1374+
for (int i = 0; i < newInfo->ii_NumIndexAttrs; i++)
1375+
newInfo->ii_OpclassOptions[i] = get_attoptions(oldIndexId, i + 1);
1376+
}
1377+
13681378
/*
13691379
* Now create the new index.
13701380
*

src/test/regress/expected/create_index.out

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,25 @@ SELECT indexrelid::regclass, indisreplident FROM pg_index
21762176
(1 row)
21772177

21782178
DROP TABLE concur_replident;
2179+
-- Check that opclass parameters are preserved
2180+
CREATE TABLE concur_appclass_tab(i tsvector, j tsvector, k tsvector);
2181+
CREATE INDEX concur_appclass_ind on concur_appclass_tab
2182+
USING gist (i tsvector_ops (siglen='1000'), j tsvector_ops (siglen='500'));
2183+
CREATE INDEX concur_appclass_ind_2 on concur_appclass_tab
2184+
USING gist (k tsvector_ops (siglen='300'), j tsvector_ops);
2185+
REINDEX TABLE CONCURRENTLY concur_appclass_tab;
2186+
\d concur_appclass_tab
2187+
Table "public.concur_appclass_tab"
2188+
Column | Type | Collation | Nullable | Default
2189+
--------+----------+-----------+----------+---------
2190+
i | tsvector | | |
2191+
j | tsvector | | |
2192+
k | tsvector | | |
2193+
Indexes:
2194+
"concur_appclass_ind" gist (i tsvector_ops (siglen='1000'), j tsvector_ops (siglen='500'))
2195+
"concur_appclass_ind_2" gist (k tsvector_ops (siglen='300'), j)
2196+
2197+
DROP TABLE concur_appclass_tab;
21792198
-- Partitions
21802199
-- Create some partitioned tables
21812200
CREATE TABLE concur_reindex_part (c1 int, c2 int) PARTITION BY RANGE (c1);

src/test/regress/sql/create_index.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,15 @@ REINDEX TABLE CONCURRENTLY concur_replident;
888888
SELECT indexrelid::regclass, indisreplident FROM pg_index
889889
WHERE indrelid = 'concur_replident'::regclass;
890890
DROP TABLE concur_replident;
891+
-- Check that opclass parameters are preserved
892+
CREATE TABLE concur_appclass_tab(i tsvector, j tsvector, k tsvector);
893+
CREATE INDEX concur_appclass_ind on concur_appclass_tab
894+
USING gist (i tsvector_ops (siglen='1000'), j tsvector_ops (siglen='500'));
895+
CREATE INDEX concur_appclass_ind_2 on concur_appclass_tab
896+
USING gist (k tsvector_ops (siglen='300'), j tsvector_ops);
897+
REINDEX TABLE CONCURRENTLY concur_appclass_tab;
898+
\d concur_appclass_tab
899+
DROP TABLE concur_appclass_tab;
891900

892901
-- Partitions
893902
-- Create some partitioned tables

0 commit comments

Comments
 (0)