public inbox for [email protected]
help / color / mirror / Atom feedFrom: Alvaro Herrera <[email protected]>
Subject: [PATCH] Don't lose attnotnull if a deferred PK supports it
Date: Tue, 5 Mar 2024 13:32:50 +0100
When dropping a NOT NULL constraint on a column that has a deferred
primary key, we would reset attnotnull, but that's bogus.
XXX this patch is nowhere near final.
Reported-by: Amul Sul <[email protected]>
Discussion: https://postgr.es/m/CAAJ_b94QonkgsbDXofakHDnORQNgafd1y3Oa5QXfpQNJyXyQ7A@mail.gmail.com
---
src/backend/commands/tablecmds.c | 71 ++++++++++++++++++++++++++++++--
1 file changed, 68 insertions(+), 3 deletions(-)
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index c61f9305c2..84c7871dda 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -12704,6 +12704,73 @@ ATExecDropConstraint(Relation rel, const char *constrName,
table_close(conrel, RowExclusiveLock);
}
+/*
+ * dropconstraint_getpkcols -- subroutine for dropconstraint_internal
+ *
+ * This is a workaround to the fact that RelationGetIndexAttrBitmap does
+ * not consider a DEFERRABLE PRIMARY KEY to be a real primary key. Maybe
+ * this code should be elsewhere.
+ */
+static Bitmapset *
+dropconstraint_getpkcols(Relation rel)
+{
+ Relation indrel;
+ SysScanDesc indscan;
+ ScanKeyData skey;
+ HeapTuple htup;
+ Bitmapset *b = NULL;
+
+ /*
+ * We try first to obtain a list of PK columns from the cache; if there
+ * is one, we're done.
+ */
+ b = RelationGetIndexAttrBitmap(rel, INDEX_ATTR_BITMAP_PRIMARY_KEY);
+ if (b != NULL)
+ return b;
+
+ /* Prepare to scan pg_index for entries having indrelid = this rel. */
+ ScanKeyInit(&skey,
+ Anum_pg_index_indrelid,
+ BTEqualStrategyNumber, F_OIDEQ,
+ ObjectIdGetDatum(RelationGetRelid(rel)));
+
+ indrel = table_open(IndexRelationId, AccessShareLock);
+ indscan = systable_beginscan(indrel, IndexIndrelidIndexId, true,
+ NULL, 1, &skey);
+
+ while (HeapTupleIsValid(htup = systable_getnext(indscan)))
+ {
+ Form_pg_index index = (Form_pg_index) GETSTRUCT(htup);
+
+ /*
+ * Ignore any indexes that are currently being dropped and those that
+ * aren't a primary key.
+ */
+ if (!index->indislive)
+ continue;
+ if (!index->indisprimary)
+ continue;
+
+ /*
+ * If this primary key was IMMEDIATE, it would have been returned
+ * by RelationGetIndexAttrBitmap.
+ */
+ Assert(!index->indimmediate);
+
+ for (int i = 0; i < index->indnatts; i++)
+ {
+ int attrnum = index->indkey.values[i];
+
+ b = bms_add_member(b, attrnum - FirstLowInvalidHeapAttributeNumber);
+ }
+ }
+
+ systable_endscan(indscan);
+ table_close(indrel, AccessShareLock);
+
+ return b;
+}
+
/*
* Remove a constraint, using its pg_constraint tuple
*
@@ -12837,9 +12904,7 @@ dropconstraint_internal(Relation rel, HeapTuple constraintTup, DropBehavior beha
* We want to test columns for their presence in the primary key, but
* only if we're not dropping it.
*/
- pkcols = dropping_pk ? NULL :
- RelationGetIndexAttrBitmap(rel,
- INDEX_ATTR_BITMAP_PRIMARY_KEY);
+ pkcols = dropping_pk ? NULL : dropconstraint_getpkcols(rel);
ircols = RelationGetIndexAttrBitmap(rel, INDEX_ATTR_BITMAP_IDENTITY_KEY);
foreach(lc, unconstrained_cols)
--
2.39.2
--qek6jpjdi3ls6ksm--
view thread (5+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected]
Subject: Re: [PATCH] Don't lose attnotnull if a deferred PK supports it
In-Reply-To: <no-message-id-1858104@localhost>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox