]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Don't error out when dropping constraint if relchecks is already zero
authorÁlvaro Herrera <alvherre@kurilemu.de>
Tue, 28 Oct 2025 18:13:32 +0000 (19:13 +0100)
committerÁlvaro Herrera <alvherre@kurilemu.de>
Tue, 28 Oct 2025 18:13:32 +0000 (19:13 +0100)
I have never seen this be a problem in practice, but it came up when
purposely corrupting catalog contents to study the fix for a nearby bug:
we'd try to decrement relchecks, but since it's zero we error out and
fail to drop the constraint.  The fix is to downgrade the error to
warning, skip decrementing the counter, and otherwise proceed normally.

Given lack of field complaints, no backpatch.

Author: Álvaro Herrera <alvherre@kurilemu.de>
Discussion: https://postgr.es/m/202508291058.q2zscdcs64fj@alvherre.pgsql

src/backend/catalog/pg_constraint.c

index 6002fd0002fc9fba45c44343c538ee7b8c948552..9944e4bd2d10e4638f6985095f541b43416cb6ef 100644 (file)
@@ -937,10 +937,12 @@ RemoveConstraintById(Oid conId)
                                         con->conrelid);
                        classForm = (Form_pg_class) GETSTRUCT(relTup);
 
-                       if (classForm->relchecks == 0)  /* should not happen */
-                               elog(ERROR, "relation \"%s\" has relchecks = 0",
-                                        RelationGetRelationName(rel));
-                       classForm->relchecks--;
+                       if (classForm->relchecks > 0)
+                               classForm->relchecks--;
+                       else
+                               /* should not happen */
+                               elog(WARNING, "relation \"%s\" has relchecks = %d",
+                                        RelationGetRelationName(rel), classForm->relchecks);
 
                        CatalogTupleUpdate(pgrel, &relTup->t_self, relTup);