pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG: pg_class.relchecks overflow, making table undroppable
8+ messages / 5 participants
[nested] [flat]

Commits from this thread:
  2026-09-27 5e59290402 b80d5b17ea 652a2f6fb9 9ca1d5726c 4cbac62871 9dad054b56 3eff6531d1  Enforce pg_class.relchecks limit (32,767 or INT16_MAX)
* BUG: pg_class.relchecks overflow, making table undroppable
@ 2026-09-24 17:03 Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-09-24 20:35 ` Re: BUG: pg_class.relchecks overflow, making table undroppable Sivaprasad <sivaprasad.postgres@gmail.com>
  2026-09-24 22:59 ` Re: BUG: pg_class.relchecks overflow, making table undroppable Michael Paquier <michael@paquier.xyz>
  0 siblings, 2 replies; 8+ messages in thread

From: Matthias van de Meent @ 2026-09-24 17:03 UTC (permalink / raw)
  To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi,

There's probably no user that finds good use for this many check
constraints, but users *can* create > 2^15 CHECK constraints on a
table, and doing so will overflow the int16 (sqltype int2) column in
which the relation's count of those is stored.

This causes various issues, such as possible WARNING spam to users
that need to load that relation into relcache, and an inability to
drop the relation because dropping the table requires the constraints
to be dropped first, and dropping a constraint decrements the counter
that has a check is in place to avoid the counter ever dropping below
zero (with an error if you try to decrement non-positive counter
values).

I think we should forbid creating such large amounts of CHECK
constraints (as attached, backpatch-safe), or drop the "relchecks"
field wholesale/replace it with a 'haschecks' field.

Also attached is an SQL script that shows the issue, and which doesn't
fail in the unsafe manner once PG is patched with the attached patch.

Note: the attached patch does not solve any issues in existing
databases with overflowed relchecks fields.


Kind regards,

Matthias van de Meent
Databricks (https://www.databricks.com)

Attachments:

v1-0001-Enforce-the-pg_class.relchecks-limit.patch (application/octet-stream, 2.4K) download From 8b003be09c64bdc565e2478a2d2c3897efd7cbdc Mon Sep 17 00:00:00 2001 From: Matthias van de Meent <boekewurm+postgres@gmail.com> Date: Thu, 24 Sep 2026 17:02:25 +0200 Subject: [PATCH v1] Enforce the pg_class.relchecks limit Previously, the count could overflow if sufficiently many CHECK constraints were added to the table, which (if new CHECKs kept getting added) could cause the table to become undroppable due to enforcement of non-negative counts when dropping these constraints. --- doc/src/sgml/limits.sgml | 7 +++++++ src/backend/catalog/heap.c | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/doc/src/sgml/limits.sgml b/doc/src/sgml/limits.sgml index f26f4466719..0e9a0b4e94e 100644 --- a/doc/src/sgml/limits.sgml +++ b/doc/src/sgml/limits.sgml @@ -62,6 +62,13 @@ below</entry> </row> + <row> + <entry><literal>CHECK</literal> constraints per table</entry> + <!-- 2^15 - 1 --> + <entry>32,767</entry> + <entry>constrained by the size of pg_class.relchecks</entry> + </row> + <row> <entry>columns in a result set</entry> <entry>1,664</entry> diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index a107193d7ab..7bbf6de4f2d 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -2360,6 +2360,12 @@ StoreConstraints(Relation rel, List *cooked_constraints, bool is_internal) con->is_local, con->inhcount, con->is_no_inherit, is_internal); numchecks++; + + if (numchecks >= PG_INT16_MAX) + ereport(ERROR, + errmsg("too many check constraints on relation \"%s\"", + RelationGetQualifiedRelationName(rel))); + break; default: @@ -2629,6 +2635,11 @@ AddRelationNewConstraints(Relation rel, numchecks++; + if (numchecks >= PG_INT16_MAX) + ereport(ERROR, + errmsg("too many check constraints on relation \"%s\"", + RelationGetQualifiedRelationName(rel))); + cooked = palloc_object(CookedConstraint); cooked->contype = CONSTR_CHECK; cooked->conoid = constrOid; @@ -3198,6 +3209,10 @@ SetRelationNumChecks(Relation rel, int numchecks) if (relStruct->relchecks != numchecks) { + if (numchecks > INT16_MAX || numchecks < 0) + elog(ERROR, "invalid new relchecks %d for relation %u", + numchecks, RelationGetRelid(rel)); + relStruct->relchecks = numchecks; CatalogTupleUpdate(relrel, &reltup->t_self, reltup); -- 2.54.0 (Apple Git-157)
scratch_67.sql (application/octet-stream, 1.4K) download ^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: BUG: pg_class.relchecks overflow, making table undroppable
  2026-09-24 17:03 BUG: pg_class.relchecks overflow, making table undroppable Matthias van de Meent <boekewurm+postgres@gmail.com>
@ 2026-09-24 20:35 ` Sivaprasad <sivaprasad.postgres@gmail.com>
  1 sibling, 0 replies; 8+ messages in thread

From: Sivaprasad @ 2026-09-24 20:35 UTC (permalink / raw)
  To: Matthias van de Meent <boekewurm+postgres@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi, sending v2 to the list as well. This version changes the limit
check from `>=` to `>`, allowing the documented maximum of 32,767
CHECK constraints.

On Thu, Sep 24, 2026 at 10:34 PM Matthias van de Meent
<boekewurm+postgres@gmail.com> wrote:
>
> Hi,
>
> There's probably no user that finds good use for this many check
> constraints, but users *can* create > 2^15 CHECK constraints on a
> table, and doing so will overflow the int16 (sqltype int2) column in
> which the relation's count of those is stored.
>
> This causes various issues, such as possible WARNING spam to users
> that need to load that relation into relcache, and an inability to
> drop the relation because dropping the table requires the constraints
> to be dropped first, and dropping a constraint decrements the counter
> that has a check is in place to avoid the counter ever dropping below
> zero (with an error if you try to decrement non-positive counter
> values).
>
> I think we should forbid creating such large amounts of CHECK
> constraints (as attached, backpatch-safe), or drop the "relchecks"
> field wholesale/replace it with a 'haschecks' field.
>
> Also attached is an SQL script that shows the issue, and which doesn't
> fail in the unsafe manner once PG is patched with the attached patch.
>
> Note: the attached patch does not solve any issues in existing
> databases with overflowed relchecks fields.
>
>
> Kind regards,
>
> Matthias van de Meent
> Databricks (https://www.databricks.com)

Attachments:

v2-0001-Enforce-the-pg_class.relchecks-limit.patch (text/x-patch, 2.4K) download From 8b003be09c64bdc565e2478a2d2c3897efd7cbdc Mon Sep 17 00:00:00 2001 From: Matthias van de Meent <boekewurm+postgres@gmail.com> Date: Thu, 24 Sep 2026 17:02:25 +0200 Subject: [PATCH v1] Enforce the pg_class.relchecks limit Previously, the count could overflow if sufficiently many CHECK constraints were added to the table, which (if new CHECKs kept getting added) could cause the table to become undroppable due to enforcement of non-negative counts when dropping these constraints. --- doc/src/sgml/limits.sgml | 7 +++++++ src/backend/catalog/heap.c | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/doc/src/sgml/limits.sgml b/doc/src/sgml/limits.sgml index f26f4466719..0e9a0b4e94e 100644 --- a/doc/src/sgml/limits.sgml +++ b/doc/src/sgml/limits.sgml @@ -62,6 +62,13 @@ below</entry> </row> + <row> + <entry><literal>CHECK</literal> constraints per table</entry> + <!-- 2^15 - 1 --> + <entry>32,767</entry> + <entry>constrained by the size of pg_class.relchecks</entry> + </row> + <row> <entry>columns in a result set</entry> <entry>1,664</entry> diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index a107193d7ab..7bbf6de4f2d 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -2360,6 +2360,12 @@ StoreConstraints(Relation rel, List *cooked_constraints, bool is_internal) con->is_local, con->inhcount, con->is_no_inherit, is_internal); numchecks++; + + if (numchecks > PG_INT16_MAX) + ereport(ERROR, + errmsg("too many check constraints on relation \"%s\"", + RelationGetQualifiedRelationName(rel))); + break; default: @@ -2629,6 +2635,11 @@ AddRelationNewConstraints(Relation rel, numchecks++; + if (numchecks > PG_INT16_MAX) + ereport(ERROR, + errmsg("too many check constraints on relation \"%s\"", + RelationGetQualifiedRelationName(rel))); + cooked = palloc_object(CookedConstraint); cooked->contype = CONSTR_CHECK; cooked->conoid = constrOid; @@ -3198,6 +3209,10 @@ SetRelationNumChecks(Relation rel, int numchecks) if (relStruct->relchecks != numchecks) { + if (numchecks > INT16_MAX || numchecks < 0) + elog(ERROR, "invalid new relchecks %d for relation %u", + numchecks, RelationGetRelid(rel)); + relStruct->relchecks = numchecks; CatalogTupleUpdate(relrel, &reltup->t_self, reltup); -- 2.54.0 (Apple Git-157)
^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: BUG: pg_class.relchecks overflow, making table undroppable
  2026-09-24 17:03 BUG: pg_class.relchecks overflow, making table undroppable Matthias van de Meent <boekewurm+postgres@gmail.com>
@ 2026-09-24 22:59 ` Michael Paquier <michael@paquier.xyz>
  2026-09-25 08:41   ` Re: BUG: pg_class.relchecks overflow, making table undroppable Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  1 sibling, 1 reply; 8+ messages in thread

From: Michael Paquier @ 2026-09-24 22:59 UTC (permalink / raw)
  To: Matthias van de Meent <boekewurm+postgres@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Thu, Sep 24, 2026 at 07:03:45PM +0200, Matthias van de Meent wrote:
> This causes various issues, such as possible WARNING spam to users
> that need to load that relation into relcache, and an inability to
> drop the relation because dropping the table requires the constraints
> to be dropped first, and dropping a constraint decrements the counter
> that has a check is in place to avoid the counter ever dropping below
> zero (with an error if you try to decrement non-positive counter
> values).
> 
> I think we should forbid creating such large amounts of CHECK
> constraints (as attached, backpatch-safe), or drop the "relchecks"
> field wholesale/replace it with a 'haschecks' field.
> 
> Also attached is an SQL script that shows the issue, and which doesn't
> fail in the unsafe manner once PG is patched with the attached patch.

Ahah, fun one!  That's in the same line as the recent trigger fix in
b99b74144f9f where catalogs could overflow.  Even if it's something
that people would not do in practice, this deserves a backpatch.  Will
look at that later.. 
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG: pg_class.relchecks overflow, making table undroppable
  2026-09-24 17:03 BUG: pg_class.relchecks overflow, making table undroppable Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-09-24 22:59 ` Re: BUG: pg_class.relchecks overflow, making table undroppable Michael Paquier <michael@paquier.xyz>
@ 2026-09-25 08:41   ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  2026-09-25 08:52     ` Re: BUG: pg_class.relchecks overflow, making table undroppable jian he <jian.universality@gmail.com>
  2026-09-25 09:04     ` Re: BUG: pg_class.relchecks overflow, making table undroppable Michael Paquier <michael@paquier.xyz>
  0 siblings, 2 replies; 8+ messages in thread

From: Bertrand Drouvot @ 2026-09-25 08:41 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Matthias van de Meent <boekewurm+postgres@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

Hi,

On Fri, Sep 25, 2026 at 07:59:22AM +0900, Michael Paquier wrote:
> On Thu, Sep 24, 2026 at 07:03:45PM +0200, Matthias van de Meent wrote:
> > This causes various issues, such as possible WARNING spam to users
> > that need to load that relation into relcache, and an inability to
> > drop the relation because dropping the table requires the constraints
> > to be dropped first, and dropping a constraint decrements the counter
> > that has a check is in place to avoid the counter ever dropping below
> > zero (with an error if you try to decrement non-positive counter
> > values).
> > 
> > I think we should forbid creating such large amounts of CHECK
> > constraints (as attached, backpatch-safe), or drop the "relchecks"
> > field wholesale/replace it with a 'haschecks' field.
> > 
> > Also attached is an SQL script that shows the issue, and which doesn't
> > fail in the unsafe manner once PG is patched with the attached patch.
> 
> Ahah, fun one!  That's in the same line as the recent trigger fix in
> b99b74144f9f where catalogs could overflow.  Even if it's something
> that people would not do in practice, this deserves a backpatch.

+1

Just a few comments:

=== 1

It needs a rebase due to 926627bf902

=== 2

+					ereport(ERROR,
+							errmsg("too many check constraints on relation \"%s\"",
+								   RelationGetQualifiedRelationName(rel)));


I think ERRCODE_PROGRAM_LIMIT_EXCEEDED would be appropriate here?

=== 3

 				numchecks++;
+
+				if (numchecks >= PG_INT16_MAX)
+					ereport(ERROR,
+							errmsg("too many check constraints on relation \"%s\"",
+								   RelationGetQualifiedRelationName(rel)));

I wonder if it wouldn't make more sense to check numchecks >= PG_INT16_MAX before
calling StoreRelCheck()? That would avoid inserting the constraint, recording its
dependencies and invoking the post create hook for an object that will be rejected.

Regards,

-- 
Bertrand Drouvot
PostgreSQL Contributors Team
RDS Open Source Databases
Amazon Web Services: https://aws.amazon.com





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG: pg_class.relchecks overflow, making table undroppable
  2026-09-24 17:03 BUG: pg_class.relchecks overflow, making table undroppable Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-09-24 22:59 ` Re: BUG: pg_class.relchecks overflow, making table undroppable Michael Paquier <michael@paquier.xyz>
  2026-09-25 08:41   ` Re: BUG: pg_class.relchecks overflow, making table undroppable Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
@ 2026-09-25 08:52     ` jian he <jian.universality@gmail.com>
  1 sibling, 0 replies; 8+ messages in thread

From: jian he @ 2026-09-25 08:52 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Matthias van de Meent <boekewurm+postgres@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Fri, Sep 25, 2026 at 4:41 PM Bertrand Drouvot
<bertranddrouvot.pg@gmail.com> wrote:
>
>                                 numchecks++;
> +
> +                               if (numchecks >= PG_INT16_MAX)
> +                                       ereport(ERROR,
> +                                                       errmsg("too many check constraints on relation \"%s\"",
> +                                                                  RelationGetQualifiedRelationName(rel)));
>
Hi.

In MergeWithExistingConstraint, we have
```
else if (pg_add_s16_overflow(con->coninhcount, 1,
&con->coninhcount))
ereport(ERROR,
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("too many inheritance parents"));
```

here, we can also use pg_add_s16_overflow?



--
jian
https://www.enterprisedb.com/





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG: pg_class.relchecks overflow, making table undroppable
  2026-09-24 17:03 BUG: pg_class.relchecks overflow, making table undroppable Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-09-24 22:59 ` Re: BUG: pg_class.relchecks overflow, making table undroppable Michael Paquier <michael@paquier.xyz>
  2026-09-25 08:41   ` Re: BUG: pg_class.relchecks overflow, making table undroppable Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
@ 2026-09-25 09:04     ` Michael Paquier <michael@paquier.xyz>
  2026-09-25 12:33       ` Re: BUG: pg_class.relchecks overflow, making table undroppable Matthias van de Meent <boekewurm+postgres@gmail.com>
  1 sibling, 1 reply; 8+ messages in thread

From: Michael Paquier @ 2026-09-25 09:04 UTC (permalink / raw)
  To: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; +Cc: Matthias van de Meent <boekewurm+postgres@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>

On Fri, Sep 25, 2026 at 08:41:38AM +0000, Bertrand Drouvot wrote:
> Just a few comments:
> 
> === 1
> 
> It needs a rebase due to 926627bf902
> 
> === 2
> 
> +                    ereport(ERROR,
> +                            errmsg("too many check constraints on relation \"%s\"",
> +                                   RelationGetQualifiedRelationName(rel)));
> 
> 
> I think ERRCODE_PROGRAM_LIMIT_EXCEEDED would be appropriate here?

An error record would be appropriate, yes.  That's reachable with SQL.

>                  numchecks++;
> +
> +                if (numchecks >= PG_INT16_MAX)
> +                    ereport(ERROR,
> +                            errmsg("too many check constraints on relation \"%s\"",
> +                                   RelationGetQualifiedRelationName(rel)));
> 
> I wonder if it wouldn't make more sense to check numchecks >= PG_INT16_MAX before
> calling StoreRelCheck()? That would avoid inserting the constraint, recording its
> dependencies and invoking the post create hook for an object that will be rejected.

Yeah, let's do that.  That's unlikely but it would just be a waste and
that's just switching the order of things.

If somebody is willing to send an updated patch, feel free, I don't
have the energy for that today..  Jian also mentioned
pg_add_s16_overflow(), which is a good idea for this case.  :D
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: BUG: pg_class.relchecks overflow, making table undroppable
  2026-09-24 17:03 BUG: pg_class.relchecks overflow, making table undroppable Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-09-24 22:59 ` Re: BUG: pg_class.relchecks overflow, making table undroppable Michael Paquier <michael@paquier.xyz>
  2026-09-25 08:41   ` Re: BUG: pg_class.relchecks overflow, making table undroppable Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  2026-09-25 09:04     ` Re: BUG: pg_class.relchecks overflow, making table undroppable Michael Paquier <michael@paquier.xyz>
@ 2026-09-25 12:33       ` Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-09-27 21:56         ` Re: BUG: pg_class.relchecks overflow, making table undroppable Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 8+ messages in thread

From: Matthias van de Meent @ 2026-09-25 12:33 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Sivaprasad <sivaprasad.postgres@gmail.com>

On Fri, 25 Sept 2026 at 11:05, Michael Paquier <michael@paquier.xyz> wrote:
>
> On Fri, Sep 25, 2026 at 08:41:38AM +0000, Bertrand Drouvot wrote:
> > Just a few comments:
> >
> > === 1
> >
> > It needs a rebase due to 926627bf902

Done

> > === 2
> >
> > +                    ereport(ERROR,
> > +                            errmsg("too many check constraints on relation \"%s\"",
> > +                                   RelationGetQualifiedRelationName(rel)));
> >
> >
> > I think ERRCODE_PROGRAM_LIMIT_EXCEEDED would be appropriate here?

Done.

> >                  numchecks++;
> > +
> > +                if (numchecks >= PG_INT16_MAX)
> > +                    ereport(ERROR,
> > +                            errmsg("too many check constraints on relation \"%s\"",
> > +                                   RelationGetQualifiedRelationName(rel)));
> >
> > I wonder if it wouldn't make more sense to check numchecks >= PG_INT16_MAX before
> > calling StoreRelCheck()? That would avoid inserting the constraint, recording its
> > dependencies and invoking the post create hook for an object that will be rejected.
>
> Yeah, let's do that.  That's unlikely but it would just be a waste and
> that's just switching the order of things.

Also done.  Thanks for the fast replies.

Attached v3:

* Add errcode(PROGRAM_LIMIT_EXCEEDED) to the ereports.
* Use pg_add_s16_overflow() to detect the overflows.
    This includes changing the type of local numchecks variables to
int16. SetRelationNumChecks's signature is unchanged.
*  Move the overflow checks to before StoreRelCheck.
    This saves one dirty tuple in catalog tables when that overflow happens.


Kind regards,

Matthias van de Meent
Databricks (https://www.databricks.com)

Attachments:

v3-0001-Enforce-the-pg_class.relchecks-limit.patch (application/octet-stream, 3.6K) download From 021c19c4adc95044743b9fbb26d4bac9b7488019 Mon Sep 17 00:00:00 2001 From: Matthias van de Meent <boekewurm+postgres@gmail.com> Date: Thu, 24 Sep 2026 17:02:25 +0200 Subject: [PATCH v3] Enforce the pg_class.relchecks limit Previously, the count could overflow if sufficiently many CHECK constraints were added to the table, which (if new CHECKs kept getting added) could cause the table to become undroppable due to enforcement of non-negative counts when dropping these constraints. --- doc/src/sgml/limits.sgml | 7 +++++++ src/backend/catalog/heap.c | 28 +++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/doc/src/sgml/limits.sgml b/doc/src/sgml/limits.sgml index 7f7eb51985e..44a14947bf2 100644 --- a/doc/src/sgml/limits.sgml +++ b/doc/src/sgml/limits.sgml @@ -62,6 +62,13 @@ below</entry> </row> + <row> + <entry><literal>CHECK</literal> constraints per table</entry> + <!-- 2^15 - 1 --> + <entry>32,767</entry> + <entry>constrained by the size of pg_class.relchecks</entry> + </row> + <row> <entry>columns in a result set</entry> <entry>1,664</entry> diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 1c188b7a0ff..a3919985503 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -2331,7 +2331,7 @@ StoreRelNotNull(Relation rel, const char *nnname, AttrNumber attnum, static void StoreConstraints(Relation rel, List *cooked_constraints, bool is_internal) { - int numchecks = 0; + int16 numchecks = 0; ListCell *lc; if (cooked_constraints == NIL) @@ -2355,12 +2355,18 @@ StoreConstraints(Relation rel, List *cooked_constraints, bool is_internal) is_internal); break; case CONSTR_CHECK: + if (pg_add_s16_overflow(numchecks, 1, &numchecks)) + ereport(ERROR, + errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("too many check constraints on relation \"%s\"", + RelationGetRelationName(rel))); + con->conoid = StoreRelCheck(rel, con->name, con->expr, con->is_enforced, !con->skip_validation, con->is_local, con->inhcount, con->is_no_inherit, is_internal); - numchecks++; + break; default: @@ -2418,7 +2424,7 @@ AddRelationNewConstraints(Relation rel, int numoldchecks; ParseState *pstate; ParseNamespaceItem *nsitem; - int numchecks; + int16 numchecks; List *checknames; List *nnnames; Node *expr; @@ -2619,6 +2625,16 @@ AddRelationNewConstraints(Relation rel, checknames = lappend(checknames, ccname); } + /* + * pg_class.relchecks stores this count in an int16, so we + * should avoid overflowing that field + */ + if (pg_add_s16_overflow(numchecks, 1, &numchecks)) + ereport(ERROR, + errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("too many check constraints on relation \"%s\"", + RelationGetRelationName(rel))); + /* * OK, store it. */ @@ -2628,8 +2644,6 @@ AddRelationNewConstraints(Relation rel, is_local ? 0 : 1, cdef->is_no_inherit, is_internal); - numchecks++; - cooked = palloc_object(CookedConstraint); cooked->contype = CONSTR_CHECK; cooked->conoid = constrOid; @@ -3199,6 +3213,10 @@ SetRelationNumChecks(Relation rel, int numchecks) if (relStruct->relchecks != numchecks) { + if (numchecks > INT16_MAX || numchecks < 0) + elog(ERROR, "invalid new relchecks %d for relation %u", + numchecks, RelationGetRelid(rel)); + relStruct->relchecks = numchecks; CatalogTupleUpdate(relrel, &reltup->t_self, reltup); -- 2.54.0 (Apple Git-157)
^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: BUG: pg_class.relchecks overflow, making table undroppable
  2026-09-24 17:03 BUG: pg_class.relchecks overflow, making table undroppable Matthias van de Meent <boekewurm+postgres@gmail.com>
  2026-09-24 22:59 ` Re: BUG: pg_class.relchecks overflow, making table undroppable Michael Paquier <michael@paquier.xyz>
  2026-09-25 08:41   ` Re: BUG: pg_class.relchecks overflow, making table undroppable Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
  2026-09-25 09:04     ` Re: BUG: pg_class.relchecks overflow, making table undroppable Michael Paquier <michael@paquier.xyz>
  2026-09-25 12:33       ` Re: BUG: pg_class.relchecks overflow, making table undroppable Matthias van de Meent <boekewurm+postgres@gmail.com>
@ 2026-09-27 21:56         ` Michael Paquier <michael@paquier.xyz>
  0 siblings, 0 replies; 8+ messages in thread

From: Michael Paquier @ 2026-09-27 21:56 UTC (permalink / raw)
  To: Matthias van de Meent <boekewurm+postgres@gmail.com>; +Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Sivaprasad <sivaprasad.postgres@gmail.com>

On Fri, Sep 25, 2026 at 02:33:57PM +0200, Matthias van de Meent wrote:
> Also done.  Thanks for the fast replies.
> 
> Attached v3:
> 
> * Add errcode(PROGRAM_LIMIT_EXCEEDED) to the ereports.
> * Use pg_add_s16_overflow() to detect the overflows.
>     This includes changing the type of local numchecks variables to
> int16. SetRelationNumChecks's signature is unchanged.
> *  Move the overflow checks to before StoreRelCheck.
>     This saves one dirty tuple in catalog tables when that overflow happens.

That looks pretty good here, so applied and backpatched as the
consequences of keeping an overflowed counter can be really annoying.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2026-09-27 21:56 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-09-24 17:03 BUG: pg_class.relchecks overflow, making table undroppable Matthias van de Meent <boekewurm+postgres@gmail.com>
2026-09-24 20:35 ` Sivaprasad <sivaprasad.postgres@gmail.com>
2026-09-24 22:59 ` Michael Paquier <michael@paquier.xyz>
2026-09-25 08:41   ` Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
2026-09-25 08:52     ` jian he <jian.universality@gmail.com>
2026-09-25 09:04     ` Michael Paquier <michael@paquier.xyz>
2026-09-25 12:33       ` Matthias van de Meent <boekewurm+postgres@gmail.com>
2026-09-27 21:56         ` Michael Paquier <michael@paquier.xyz>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox