From 90189eefc1e11822794e3386d9bafafd3ba3a6e8 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 28 Mar 2023 09:58:14 +0200 Subject: Save a few bytes in pg_attribute Change the columns attndims, attstattarget, and attinhcount from int32 to int16, and reorder a bit. This saves some space (currently 4 bytes) in pg_attribute and tuple descriptors, which translates into small performance benefits and/or room for new columns in pg_attribute needed by future features. attndims and attinhcount are never realistically used with values larger than int16. Just to be sure, add some overflow checks. attstattarget is currently limited explicitly to 10000. For consistency, pg_constraint.coninhcount is also changed like attinhcount. Discussion: https://www.postgresql.org/message-id/flat/d07ffc2b-e0e8-77f7-38fb-be921dff71af%40enterprisedb.com --- src/backend/commands/tablecmds.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'src/backend/commands/tablecmds.c') diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index c510a01fd8d..3147dddf286 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -2650,6 +2650,10 @@ MergeAttributes(List *schema, List *supers, char relpersistence, */ def->inhcount++; + if (def->inhcount < 0) + ereport(ERROR, + errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("too many inheritance parents")); newattmap->attnums[parent_attno - 1] = exist_attno; } @@ -3173,6 +3177,10 @@ MergeCheckConstraint(List *constraints, char *name, Node *expr) { /* OK to merge */ ccon->inhcount++; + if (ccon->inhcount < 0) + ereport(ERROR, + errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("too many inheritance parents")); return true; } @@ -6828,6 +6836,10 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel, /* Bump the existing child att's inhcount */ childatt->attinhcount++; + if (childatt->attinhcount < 0) + ereport(ERROR, + errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("too many inheritance parents")); CatalogTupleUpdate(attrdesc, &tuple->t_self, tuple); heap_freetuple(tuple); @@ -6919,6 +6931,10 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel, attribute.attstattarget = (newattnum > 0) ? -1 : 0; attribute.attlen = tform->typlen; attribute.attnum = newattnum; + if (list_length(colDef->typeName->arrayBounds) > PG_INT16_MAX) + ereport(ERROR, + errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("too many array dimensions")); attribute.attndims = list_length(colDef->typeName->arrayBounds); attribute.atttypmod = typmod; attribute.attbyval = tform->typbyval; @@ -12924,6 +12940,10 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel, attTup->atttypid = targettype; attTup->atttypmod = targettypmod; attTup->attcollation = targetcollid; + if (list_length(typeName->arrayBounds) > PG_INT16_MAX) + ereport(ERROR, + errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("too many array dimensions")); attTup->attndims = list_length(typeName->arrayBounds); attTup->attlen = tform->typlen; attTup->attbyval = tform->typbyval; @@ -15155,6 +15175,10 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel) * later on, this change will just roll back.) */ childatt->attinhcount++; + if (childatt->attinhcount < 0) + ereport(ERROR, + errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("too many inheritance parents")); /* * In case of partitions, we must enforce that value of attislocal @@ -15292,6 +15316,10 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel) child_copy = heap_copytuple(child_tuple); child_con = (Form_pg_constraint) GETSTRUCT(child_copy); child_con->coninhcount++; + if (child_con->coninhcount < 0) + ereport(ERROR, + errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("too many inheritance parents")); /* * In case of partitions, an inherited constraint must be -- cgit v1.2.3