Patch
Don't adopt a partition index with different ordering or opclass options
- Branch
- REL_16_STABLE
- Risk
- –
- Base commit
- be913fabe79d73a37af55eafc3d3352db4c3d1ca
To apply it to a checkout of REL_16_STABLE at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a1cc7f-dc1f-7a13-8a68-80af5436c6b3 | jq -r .body | git am
From a181c644906bd1a1aa2eb95cf930b8b7ced58c6e Mon Sep 17 00:00:00 2001
From: Grok <grok@x.ai>
Date: Wed, 11 Nov 2026 12:06:20 -0800
Subject: [PATCH] Don't adopt a partition index with different ordering or
opclass options
CompareIndexInfo() treated indexes as equivalent without comparing
indoption (ASC/DESC, NULLS FIRST/LAST) or per-column operator class
options. CREATE INDEX on a partitioned table, ALTER TABLE ATTACH
PARTITION, and ALTER INDEX ATTACH PARTITION could therefore attach a
partition index whose definition is not the one that was requested.
Compare those properties. A partition index that does not match is
left in place and a new child is built; ATTACH PARTITION of a
mismatched index fails. Indexes that are already attached are not
detached.
---
src/backend/catalog/index.c | 67 +++++++++++++++++-
src/backend/commands/indexcmds.c | 6 +-
src/backend/commands/tablecmds.c | 12 +++-
src/include/catalog/index.h | 4 +-
src/test/regress/expected/indexing.out | 95 ++++++++++++++++++++++++++
src/test/regress/sql/indexing.sql | 57 ++++++++++++++++
6 files changed, 236 insertions(+), 5 deletions(-)
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index d170d7596e9..6e0f5025164 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -2544,6 +2544,52 @@ BuildDummyIndexInfo(Relation index)
return ii;
}
+/*
+ * Compare per-column operator class options, which are text[] datums stored
+ * in pg_attribute.attoptions. A null datum means the column has no options.
+ * Non-null values are compared for binary equality under the C collation,
+ * the same rule CheckIndexCompatible() uses: option semantics are private
+ * to the operator class.
+ */
+static bool
+IndexOpclassOptionsMatch(Oid relid1, Oid relid2, int natts)
+{
+ FmgrInfo fm;
+ bool fm_inited = false;
+ int i;
+
+ for (i = 0; i < natts; i++)
+ {
+ Datum opt1 = get_attoptions(relid1, i + 1);
+ Datum opt2 = get_attoptions(relid2, i + 1);
+ bool match;
+
+ if (opt1 == (Datum) 0 || opt2 == (Datum) 0)
+ match = (opt1 == opt2);
+ else
+ {
+ if (!fm_inited)
+ {
+ fmgr_info(F_ARRAY_EQ, &fm);
+ fm_inited = true;
+ }
+
+ match = DatumGetBool(FunctionCall2Coll(&fm, C_COLLATION_OID,
+ opt1, opt2));
+ }
+
+ if (DatumGetPointer(opt1) != NULL)
+ pfree(DatumGetPointer(opt1));
+ if (DatumGetPointer(opt2) != NULL)
+ pfree(DatumGetPointer(opt2));
+
+ if (!match)
+ return false;
+ }
+
+ return true;
+}
+
/*
* CompareIndexInfo
* Return whether the properties of two indexes (in different tables)
@@ -2552,13 +2598,20 @@ BuildDummyIndexInfo(Relation index)
* Note: passing collations and opfamilies separately is a kludge. Adding
* them to IndexInfo may result in better coding here and elsewhere.
*
+ * indoptions1/indoptions2 are the pg_index.indoption arrays (one int16 per
+ * key column): ASC/DESC and NULLS FIRST/LAST. indexRelid1/indexRelid2
+ * identify the indexes whose per-column operator class options are compared.
+ * Neither value is stored in IndexInfo.
+ *
* Use build_attrmap_by_name(index2, index1) to build the attmap.
*/
bool
CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
Oid *collations1, Oid *collations2,
Oid *opfamilies1, Oid *opfamilies2,
- AttrMap *attmap)
+ AttrMap *attmap,
+ const int16 *indoptions1, const int16 *indoptions2,
+ Oid indexRelid1, Oid indexRelid2)
{
int i;
@@ -2670,6 +2723,18 @@ CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
if (info1->ii_ExclusionOps != NULL || info2->ii_ExclusionOps != NULL)
return false;
+ /*
+ * Sort direction and NULL ordering are part of the definition. Key
+ * columns are already known to be the same number.
+ */
+ if (memcmp(indoptions1, indoptions2,
+ info1->ii_NumIndexKeyAttrs * sizeof(int16)) != 0)
+ return false;
+
+ if (!IndexOpclassOptionsMatch(indexRelid1, indexRelid2,
+ info1->ii_NumIndexKeyAttrs))
+ return false;
+
return true;
}
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index ace6f539dc8..d35440d991b 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -1374,7 +1374,11 @@ DefineIndex(Oid relationId,
parentIndex->rd_indcollation,
cldidx->rd_opfamily,
parentIndex->rd_opfamily,
- attmap))
+ attmap,
+ cldidx->rd_indoption,
+ parentIndex->rd_indoption,
+ RelationGetRelid(cldidx),
+ RelationGetRelid(parentIndex)))
{
Oid cldConstrOid = InvalidOid;
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index baeab1a8e8a..fc06beb9f9c 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -19162,7 +19162,11 @@ AttachPartitionEnsureIndexes(Relation rel, Relation attachrel)
idxRel->rd_indcollation,
attachrelIdxRels[i]->rd_opfamily,
idxRel->rd_opfamily,
- attmap))
+ attmap,
+ attachrelIdxRels[i]->rd_indoption,
+ idxRel->rd_indoption,
+ RelationGetRelid(attachrelIdxRels[i]),
+ RelationGetRelid(idxRel)))
{
/*
* If this index is being created in the parent because of a
@@ -20192,7 +20196,11 @@ ATExecAttachPartitionIdx(List **wqueue, Relation parentIdx, RangeVar *name)
parentIdx->rd_indcollation,
partIdx->rd_opfamily,
parentIdx->rd_opfamily,
- attmap))
+ attmap,
+ partIdx->rd_indoption,
+ parentIdx->rd_indoption,
+ RelationGetRelid(partIdx),
+ RelationGetRelid(parentIdx)))
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("cannot attach index \"%s\" as a partition of index \"%s\"",
diff --git a/src/include/catalog/index.h b/src/include/catalog/index.h
index aa7bab2113f..cf00a108c49 100644
--- a/src/include/catalog/index.h
+++ b/src/include/catalog/index.h
@@ -127,7 +127,9 @@ extern IndexInfo *BuildDummyIndexInfo(Relation index);
extern bool CompareIndexInfo(IndexInfo *info1, IndexInfo *info2,
Oid *collations1, Oid *collations2,
Oid *opfamilies1, Oid *opfamilies2,
- AttrMap *attmap);
+ AttrMap *attmap,
+ const int16 *indoptions1, const int16 *indoptions2,
+ Oid indexRelid1, Oid indexRelid2);
extern void BuildSpeculativeIndexInfo(Relation index, IndexInfo *ii);
diff --git a/src/test/regress/expected/indexing.out b/src/test/regress/expected/indexing.out
index 77d667ed9b4..86725e238f6 100644
--- a/src/test/regress/expected/indexing.out
+++ b/src/test/regress/expected/indexing.out
@@ -1693,3 +1693,98 @@ select indexrelid::regclass, indisvalid, indisreplident,
(3 rows)
drop table parted_replica_tab;
+-- Sort options and operator class options are part of the match.
+-- A partition index with a different ordering or different opclass
+-- options is not attached; one that agrees is.
+create table idxpart_ord (a int, b int) partition by range (a);
+create table idxpart_ord1 (a int, b int);
+create index idxpart_ord1_desc on idxpart_ord1 (a desc nulls last);
+create index idxpart_ord1_nf on idxpart_ord1 (a nulls first);
+create index idxpart_ord1_bd on idxpart_ord1 (a, b desc);
+alter table idxpart_ord attach partition idxpart_ord1 for values from (0) to (10);
+create index idxpart_ord_a on idxpart_ord (a);
+create index idxpart_ord_ab on idxpart_ord (a, b);
+select indexrelid::regclass, indoption, inhparent::regclass
+ from pg_index idx left join pg_inherits inh on (idx.indexrelid = inh.inhrelid)
+ where indrelid::regclass::text like 'idxpart_ord%'
+ order by indexrelid::regclass::text collate "C";
+ indexrelid | indoption | inhparent
+----------------------+-----------+----------------
+ idxpart_ord1_a_b_idx | 0 0 | idxpart_ord_ab
+ idxpart_ord1_a_idx | 0 | idxpart_ord_a
+ idxpart_ord1_bd | 0 3 |
+ idxpart_ord1_desc | 1 |
+ idxpart_ord1_nf | 2 |
+ idxpart_ord_a | 0 |
+ idxpart_ord_ab | 0 0 |
+(7 rows)
+
+-- same ordering is adopted
+create table idxpart_ord2 (a int, b int);
+create index idxpart_ord2_a on idxpart_ord2 (a);
+alter table idxpart_ord attach partition idxpart_ord2 for values from (10) to (20);
+select indexrelid::regclass, indoption, inhparent::regclass
+ from pg_index idx left join pg_inherits inh on (idx.indexrelid = inh.inhrelid)
+ where indrelid = 'idxpart_ord2'::regclass
+ order by indexrelid::regclass::text collate "C";
+ indexrelid | indoption | inhparent
+----------------------+-----------+----------------
+ idxpart_ord2_a | 0 | idxpart_ord_a
+ idxpart_ord2_a_b_idx | 0 0 | idxpart_ord_ab
+(2 rows)
+
+-- ALTER INDEX ATTACH rejects a different ordering and accepts a match.
+-- Use indexes that are not already attached to some other parent.
+create index idxpart_ord_nf on only idxpart_ord (a nulls first);
+alter index idxpart_ord_nf attach partition idxpart_ord1_desc;
+ERROR: cannot attach index "idxpart_ord1_desc" as a partition of index "idxpart_ord_nf"
+DETAIL: The index definitions do not match.
+create index idxpart_ord_desc on only idxpart_ord (a desc nulls last);
+alter index idxpart_ord_desc attach partition idxpart_ord1_desc;
+select indexrelid::regclass, indisvalid, inhparent::regclass
+ from pg_index idx left join pg_inherits inh on (idx.indexrelid = inh.inhrelid)
+ where indexrelid::regclass::text in ('idxpart_ord_desc', 'idxpart_ord1_desc',
+ 'idxpart_ord_nf')
+ order by indexrelid::regclass::text collate "C";
+ indexrelid | indisvalid | inhparent
+-------------------+------------+------------------
+ idxpart_ord1_desc | t | idxpart_ord_desc
+ idxpart_ord_desc | f |
+ idxpart_ord_nf | f |
+(3 rows)
+
+-- opclass options: CREATE INDEX does not adopt a different siglen
+create table idxpart_opc (id int, t tsvector) partition by range (id);
+create table idxpart_opc1 (id int, t tsvector);
+create index idxpart_opc1_t8 on idxpart_opc1 using gist (t tsvector_ops (siglen = 8));
+alter table idxpart_opc attach partition idxpart_opc1 for values from (0) to (10);
+create index idxpart_opc_t on idxpart_opc using gist (t tsvector_ops (siglen = 200));
+select pg_get_indexdef(indexrelid), inhparent::regclass
+ from pg_index idx left join pg_inherits inh on (idx.indexrelid = inh.inhrelid)
+ where indrelid::regclass::text like 'idxpart_opc1%'
+ order by pg_get_indexdef(indexrelid) collate "C";
+ pg_get_indexdef | inhparent
+---------------------------------------------------------------------------------------------------+---------------
+ CREATE INDEX idxpart_opc1_t8 ON public.idxpart_opc1 USING gist (t tsvector_ops (siglen='8')) |
+ CREATE INDEX idxpart_opc1_t_idx ON public.idxpart_opc1 USING gist (t tsvector_ops (siglen='200')) | idxpart_opc_t
+(2 rows)
+
+-- direct ATTACH, before this parent has a child on the partition
+create table idxpart_opc2 (id int, t tsvector);
+create index idxpart_opc2_t8 on idxpart_opc2 using gist (t tsvector_ops (siglen = 8));
+alter table idxpart_opc attach partition idxpart_opc2 for values from (10) to (20);
+create index idxpart_opc_x on only idxpart_opc using gist (t tsvector_ops (siglen = 16));
+alter index idxpart_opc_x attach partition idxpart_opc2_t8;
+ERROR: cannot attach index "idxpart_opc2_t8" as a partition of index "idxpart_opc_x"
+DETAIL: The index definitions do not match.
+create index idxpart_opc_t8 on only idxpart_opc using gist (t tsvector_ops (siglen = 8));
+alter index idxpart_opc_t8 attach partition idxpart_opc2_t8;
+select indexrelid::regclass, inhparent::regclass
+ from pg_index idx join pg_inherits inh on (idx.indexrelid = inh.inhrelid)
+ where indexrelid = 'idxpart_opc2_t8'::regclass;
+ indexrelid | inhparent
+-----------------+----------------
+ idxpart_opc2_t8 | idxpart_opc_t8
+(1 row)
+
+drop table idxpart_ord, idxpart_opc;
diff --git a/src/test/regress/sql/indexing.sql b/src/test/regress/sql/indexing.sql
index f0b0dcc4783..511d1334051 100644
--- a/src/test/regress/sql/indexing.sql
+++ b/src/test/regress/sql/indexing.sql
@@ -930,3 +930,60 @@ select indexrelid::regclass, indisvalid, indisreplident,
where indexrelid::regclass::text like 'parted_replica%'
order by indexrelid::regclass::text collate "C";
drop table parted_replica_tab;
+
+-- Sort options and operator class options are part of the match.
+-- A partition index with a different ordering or different opclass
+-- options is not attached; one that agrees is.
+create table idxpart_ord (a int, b int) partition by range (a);
+create table idxpart_ord1 (a int, b int);
+create index idxpart_ord1_desc on idxpart_ord1 (a desc nulls last);
+create index idxpart_ord1_nf on idxpart_ord1 (a nulls first);
+create index idxpart_ord1_bd on idxpart_ord1 (a, b desc);
+alter table idxpart_ord attach partition idxpart_ord1 for values from (0) to (10);
+create index idxpart_ord_a on idxpart_ord (a);
+create index idxpart_ord_ab on idxpart_ord (a, b);
+select indexrelid::regclass, indoption, inhparent::regclass
+ from pg_index idx left join pg_inherits inh on (idx.indexrelid = inh.inhrelid)
+ where indrelid::regclass::text like 'idxpart_ord%'
+ order by indexrelid::regclass::text collate "C";
+-- same ordering is adopted
+create table idxpart_ord2 (a int, b int);
+create index idxpart_ord2_a on idxpart_ord2 (a);
+alter table idxpart_ord attach partition idxpart_ord2 for values from (10) to (20);
+select indexrelid::regclass, indoption, inhparent::regclass
+ from pg_index idx left join pg_inherits inh on (idx.indexrelid = inh.inhrelid)
+ where indrelid = 'idxpart_ord2'::regclass
+ order by indexrelid::regclass::text collate "C";
+-- ALTER INDEX ATTACH rejects a different ordering and accepts a match.
+-- Use indexes that are not already attached to some other parent.
+create index idxpart_ord_nf on only idxpart_ord (a nulls first);
+alter index idxpart_ord_nf attach partition idxpart_ord1_desc;
+create index idxpart_ord_desc on only idxpart_ord (a desc nulls last);
+alter index idxpart_ord_desc attach partition idxpart_ord1_desc;
+select indexrelid::regclass, indisvalid, inhparent::regclass
+ from pg_index idx left join pg_inherits inh on (idx.indexrelid = inh.inhrelid)
+ where indexrelid::regclass::text in ('idxpart_ord_desc', 'idxpart_ord1_desc',
+ 'idxpart_ord_nf')
+ order by indexrelid::regclass::text collate "C";
+-- opclass options: CREATE INDEX does not adopt a different siglen
+create table idxpart_opc (id int, t tsvector) partition by range (id);
+create table idxpart_opc1 (id int, t tsvector);
+create index idxpart_opc1_t8 on idxpart_opc1 using gist (t tsvector_ops (siglen = 8));
+alter table idxpart_opc attach partition idxpart_opc1 for values from (0) to (10);
+create index idxpart_opc_t on idxpart_opc using gist (t tsvector_ops (siglen = 200));
+select pg_get_indexdef(indexrelid), inhparent::regclass
+ from pg_index idx left join pg_inherits inh on (idx.indexrelid = inh.inhrelid)
+ where indrelid::regclass::text like 'idxpart_opc1%'
+ order by pg_get_indexdef(indexrelid) collate "C";
+-- direct ATTACH, before this parent has a child on the partition
+create table idxpart_opc2 (id int, t tsvector);
+create index idxpart_opc2_t8 on idxpart_opc2 using gist (t tsvector_ops (siglen = 8));
+alter table idxpart_opc attach partition idxpart_opc2 for values from (10) to (20);
+create index idxpart_opc_x on only idxpart_opc using gist (t tsvector_ops (siglen = 16));
+alter index idxpart_opc_x attach partition idxpart_opc2_t8;
+create index idxpart_opc_t8 on only idxpart_opc using gist (t tsvector_ops (siglen = 8));
+alter index idxpart_opc_t8 attach partition idxpart_opc2_t8;
+select indexrelid::regclass, inhparent::regclass
+ from pg_index idx join pg_inherits inh on (idx.indexrelid = inh.inhrelid)
+ where indexrelid = 'idxpart_opc2_t8'::regclass;
+drop table idxpart_ord, idxpart_opc;
base-commit: be913fabe79d73a37af55eafc3d3352db4c3d1ca
--
2.53.0