Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 90189ee

Browse files
committed
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
1 parent 637dce2 commit 90189ee

File tree

9 files changed

+100
-53
lines changed

9 files changed

+100
-53
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,23 +1164,6 @@
11641164
</para></entry>
11651165
</row>
11661166

1167-
<row>
1168-
<entry role="catalog_table_entry"><para role="column_definition">
1169-
<structfield>attstattarget</structfield> <type>int4</type>
1170-
</para>
1171-
<para>
1172-
<structfield>attstattarget</structfield> controls the level of detail
1173-
of statistics accumulated for this column by
1174-
<link linkend="sql-analyze"><command>ANALYZE</command></link>.
1175-
A zero value indicates that no statistics should be collected.
1176-
A negative value says to use the system default statistics target.
1177-
The exact meaning of positive values is data type-dependent.
1178-
For scalar data types, <structfield>attstattarget</structfield>
1179-
is both the target number of <quote>most common values</quote>
1180-
to collect, and the target number of histogram bins to create.
1181-
</para></entry>
1182-
</row>
1183-
11841167
<row>
11851168
<entry role="catalog_table_entry"><para role="column_definition">
11861169
<structfield>attlen</structfield> <type>int2</type>
@@ -1202,17 +1185,6 @@
12021185
</para></entry>
12031186
</row>
12041187

1205-
<row>
1206-
<entry role="catalog_table_entry"><para role="column_definition">
1207-
<structfield>attndims</structfield> <type>int4</type>
1208-
</para>
1209-
<para>
1210-
Number of dimensions, if the column is an array type; otherwise 0.
1211-
(Presently, the number of dimensions of an array is not enforced,
1212-
so any nonzero value effectively means <quote>it's an array</quote>.)
1213-
</para></entry>
1214-
</row>
1215-
12161188
<row>
12171189
<entry role="catalog_table_entry"><para role="column_definition">
12181190
<structfield>attcacheoff</structfield> <type>int4</type>
@@ -1237,6 +1209,17 @@
12371209
</para></entry>
12381210
</row>
12391211

1212+
<row>
1213+
<entry role="catalog_table_entry"><para role="column_definition">
1214+
<structfield>attndims</structfield> <type>int2</type>
1215+
</para>
1216+
<para>
1217+
Number of dimensions, if the column is an array type; otherwise 0.
1218+
(Presently, the number of dimensions of an array is not enforced,
1219+
so any nonzero value effectively means <quote>it's an array</quote>.)
1220+
</para></entry>
1221+
</row>
1222+
12401223
<row>
12411224
<entry role="catalog_table_entry"><para role="column_definition">
12421225
<structfield>attbyval</structfield> <type>bool</type>
@@ -1362,14 +1345,31 @@
13621345

13631346
<row>
13641347
<entry role="catalog_table_entry"><para role="column_definition">
1365-
<structfield>attinhcount</structfield> <type>int4</type>
1348+
<structfield>attinhcount</structfield> <type>int2</type>
13661349
</para>
13671350
<para>
13681351
The number of direct ancestors this column has. A column with a
13691352
nonzero number of ancestors cannot be dropped nor renamed.
13701353
</para></entry>
13711354
</row>
13721355

1356+
<row>
1357+
<entry role="catalog_table_entry"><para role="column_definition">
1358+
<structfield>attstattarget</structfield> <type>int2</type>
1359+
</para>
1360+
<para>
1361+
<structfield>attstattarget</structfield> controls the level of detail
1362+
of statistics accumulated for this column by
1363+
<link linkend="sql-analyze"><command>ANALYZE</command></link>.
1364+
A zero value indicates that no statistics should be collected.
1365+
A negative value says to use the system default statistics target.
1366+
The exact meaning of positive values is data type-dependent.
1367+
For scalar data types, <structfield>attstattarget</structfield>
1368+
is both the target number of <quote>most common values</quote>
1369+
to collect, and the target number of histogram bins to create.
1370+
</para></entry>
1371+
</row>
1372+
13731373
<row>
13741374
<entry role="catalog_table_entry"><para role="column_definition">
13751375
<structfield>attcollation</structfield> <type>oid</type>
@@ -2691,7 +2691,7 @@ SCRAM-SHA-256$<replaceable>&lt;iteration count&gt;</replaceable>:<replaceable>&l
26912691

26922692
<row>
26932693
<entry role="catalog_table_entry"><para role="column_definition">
2694-
<structfield>coninhcount</structfield> <type>int4</type>
2694+
<structfield>coninhcount</structfield> <type>int2</type>
26952695
</para>
26962696
<para>
26972697
The number of direct inheritance ancestors this constraint has.

src/backend/access/common/tupdesc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,8 @@ TupleDescInitEntry(TupleDesc desc,
597597
Assert(PointerIsValid(desc));
598598
Assert(attributeNumber >= 1);
599599
Assert(attributeNumber <= desc->natts);
600+
Assert(attdim >= 0);
601+
Assert(attdim <= PG_INT16_MAX);
600602

601603
/*
602604
* initialize the attribute fields
@@ -667,6 +669,8 @@ TupleDescInitBuiltinEntry(TupleDesc desc,
667669
Assert(PointerIsValid(desc));
668670
Assert(attributeNumber >= 1);
669671
Assert(attributeNumber <= desc->natts);
672+
Assert(attdim >= 0);
673+
Assert(attdim <= PG_INT16_MAX);
670674

671675
/* initialize the attribute fields */
672676
att = TupleDescAttr(desc, attributeNumber - 1);
@@ -827,6 +831,10 @@ BuildDescForRelation(List *schema)
827831

828832
attcollation = GetColumnDefCollation(NULL, entry, atttypid);
829833
attdim = list_length(entry->typeName->arrayBounds);
834+
if (attdim > PG_INT16_MAX)
835+
ereport(ERROR,
836+
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
837+
errmsg("too many array dimensions"));
830838

831839
if (entry->typeName->setof)
832840
ereport(ERROR,

src/backend/catalog/heap.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -732,12 +732,11 @@ InsertPgAttributeTuples(Relation pg_attribute_rel,
732732

733733
slot[slotCount]->tts_values[Anum_pg_attribute_attname - 1] = NameGetDatum(&attrs->attname);
734734
slot[slotCount]->tts_values[Anum_pg_attribute_atttypid - 1] = ObjectIdGetDatum(attrs->atttypid);
735-
slot[slotCount]->tts_values[Anum_pg_attribute_attstattarget - 1] = Int32GetDatum(attrs->attstattarget);
736735
slot[slotCount]->tts_values[Anum_pg_attribute_attlen - 1] = Int16GetDatum(attrs->attlen);
737736
slot[slotCount]->tts_values[Anum_pg_attribute_attnum - 1] = Int16GetDatum(attrs->attnum);
738-
slot[slotCount]->tts_values[Anum_pg_attribute_attndims - 1] = Int32GetDatum(attrs->attndims);
739737
slot[slotCount]->tts_values[Anum_pg_attribute_attcacheoff - 1] = Int32GetDatum(-1);
740738
slot[slotCount]->tts_values[Anum_pg_attribute_atttypmod - 1] = Int32GetDatum(attrs->atttypmod);
739+
slot[slotCount]->tts_values[Anum_pg_attribute_attndims - 1] = Int16GetDatum(attrs->attndims);
741740
slot[slotCount]->tts_values[Anum_pg_attribute_attbyval - 1] = BoolGetDatum(attrs->attbyval);
742741
slot[slotCount]->tts_values[Anum_pg_attribute_attalign - 1] = CharGetDatum(attrs->attalign);
743742
slot[slotCount]->tts_values[Anum_pg_attribute_attstorage - 1] = CharGetDatum(attrs->attstorage);
@@ -749,7 +748,8 @@ InsertPgAttributeTuples(Relation pg_attribute_rel,
749748
slot[slotCount]->tts_values[Anum_pg_attribute_attgenerated - 1] = CharGetDatum(attrs->attgenerated);
750749
slot[slotCount]->tts_values[Anum_pg_attribute_attisdropped - 1] = BoolGetDatum(attrs->attisdropped);
751750
slot[slotCount]->tts_values[Anum_pg_attribute_attislocal - 1] = BoolGetDatum(attrs->attislocal);
752-
slot[slotCount]->tts_values[Anum_pg_attribute_attinhcount - 1] = Int32GetDatum(attrs->attinhcount);
751+
slot[slotCount]->tts_values[Anum_pg_attribute_attinhcount - 1] = Int16GetDatum(attrs->attinhcount);
752+
slot[slotCount]->tts_values[Anum_pg_attribute_attstattarget - 1] = Int16GetDatum(attrs->attstattarget);
753753
slot[slotCount]->tts_values[Anum_pg_attribute_attcollation - 1] = ObjectIdGetDatum(attrs->attcollation);
754754
if (attoptions && attoptions[natts] != (Datum) 0)
755755
slot[slotCount]->tts_values[Anum_pg_attribute_attoptions - 1] = attoptions[natts];
@@ -2615,6 +2615,11 @@ MergeWithExistingConstraint(Relation rel, const char *ccname, Node *expr,
26152615
con->conislocal = true;
26162616
else
26172617
con->coninhcount++;
2618+
2619+
if (con->coninhcount < 0)
2620+
ereport(ERROR,
2621+
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2622+
errmsg("too many inheritance parents"));
26182623
}
26192624

26202625
if (is_no_inherit)

src/backend/catalog/index.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1809,7 +1809,7 @@ index_concurrently_swap(Oid newIndexId, Oid oldIndexId, const char *oldName)
18091809
memset(repl_repl, false, sizeof(repl_repl));
18101810

18111811
repl_repl[Anum_pg_attribute_attstattarget - 1] = true;
1812-
repl_val[Anum_pg_attribute_attstattarget - 1] = Int32GetDatum(attstattarget);
1812+
repl_val[Anum_pg_attribute_attstattarget - 1] = Int16GetDatum(attstattarget);
18131813

18141814
newTuple = heap_modify_tuple(attrTuple,
18151815
RelationGetDescr(pg_attribute),

src/backend/catalog/pg_constraint.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ CreateConstraintEntry(const char *constraintName,
190190
values[Anum_pg_constraint_confdeltype - 1] = CharGetDatum(foreignDeleteType);
191191
values[Anum_pg_constraint_confmatchtype - 1] = CharGetDatum(foreignMatchType);
192192
values[Anum_pg_constraint_conislocal - 1] = BoolGetDatum(conIsLocal);
193-
values[Anum_pg_constraint_coninhcount - 1] = Int32GetDatum(conInhCount);
193+
values[Anum_pg_constraint_coninhcount - 1] = Int16GetDatum(conInhCount);
194194
values[Anum_pg_constraint_connoinherit - 1] = BoolGetDatum(conNoInherit);
195195

196196
if (conkeyArray)
@@ -805,6 +805,10 @@ ConstraintSetParentConstraint(Oid childConstrId,
805805

806806
constrForm->conislocal = false;
807807
constrForm->coninhcount++;
808+
if (constrForm->coninhcount < 0)
809+
ereport(ERROR,
810+
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
811+
errmsg("too many inheritance parents"));
808812
constrForm->conparentid = parentConstrId;
809813

810814
CatalogTupleUpdate(constrRel, &tuple->t_self, newtup);

src/backend/commands/tablecmds.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,6 +2650,10 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
26502650
*/
26512651

26522652
def->inhcount++;
2653+
if (def->inhcount < 0)
2654+
ereport(ERROR,
2655+
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2656+
errmsg("too many inheritance parents"));
26532657

26542658
newattmap->attnums[parent_attno - 1] = exist_attno;
26552659
}
@@ -3173,6 +3177,10 @@ MergeCheckConstraint(List *constraints, char *name, Node *expr)
31733177
{
31743178
/* OK to merge */
31753179
ccon->inhcount++;
3180+
if (ccon->inhcount < 0)
3181+
ereport(ERROR,
3182+
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3183+
errmsg("too many inheritance parents"));
31763184
return true;
31773185
}
31783186

@@ -6828,6 +6836,10 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
68286836

68296837
/* Bump the existing child att's inhcount */
68306838
childatt->attinhcount++;
6839+
if (childatt->attinhcount < 0)
6840+
ereport(ERROR,
6841+
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
6842+
errmsg("too many inheritance parents"));
68316843
CatalogTupleUpdate(attrdesc, &tuple->t_self, tuple);
68326844

68336845
heap_freetuple(tuple);
@@ -6919,6 +6931,10 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
69196931
attribute.attstattarget = (newattnum > 0) ? -1 : 0;
69206932
attribute.attlen = tform->typlen;
69216933
attribute.attnum = newattnum;
6934+
if (list_length(colDef->typeName->arrayBounds) > PG_INT16_MAX)
6935+
ereport(ERROR,
6936+
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
6937+
errmsg("too many array dimensions"));
69226938
attribute.attndims = list_length(colDef->typeName->arrayBounds);
69236939
attribute.atttypmod = typmod;
69246940
attribute.attbyval = tform->typbyval;
@@ -12924,6 +12940,10 @@ ATExecAlterColumnType(AlteredTableInfo *tab, Relation rel,
1292412940
attTup->atttypid = targettype;
1292512941
attTup->atttypmod = targettypmod;
1292612942
attTup->attcollation = targetcollid;
12943+
if (list_length(typeName->arrayBounds) > PG_INT16_MAX)
12944+
ereport(ERROR,
12945+
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
12946+
errmsg("too many array dimensions"));
1292712947
attTup->attndims = list_length(typeName->arrayBounds);
1292812948
attTup->attlen = tform->typlen;
1292912949
attTup->attbyval = tform->typbyval;
@@ -15155,6 +15175,10 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
1515515175
* later on, this change will just roll back.)
1515615176
*/
1515715177
childatt->attinhcount++;
15178+
if (childatt->attinhcount < 0)
15179+
ereport(ERROR,
15180+
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
15181+
errmsg("too many inheritance parents"));
1515815182

1515915183
/*
1516015184
* In case of partitions, we must enforce that value of attislocal
@@ -15292,6 +15316,10 @@ MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel)
1529215316
child_copy = heap_copytuple(child_tuple);
1529315317
child_con = (Form_pg_constraint) GETSTRUCT(child_copy);
1529415318
child_con->coninhcount++;
15319+
if (child_con->coninhcount < 0)
15320+
ereport(ERROR,
15321+
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
15322+
errmsg("too many inheritance parents"));
1529515323

1529615324
/*
1529715325
* In case of partitions, an inherited constraint must be

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202303231
60+
#define CATALOG_VERSION_NO 202303281
6161

6262
#endif

src/include/catalog/pg_attribute.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,6 @@ CATALOG(pg_attribute,1249,AttributeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(75,
5252
*/
5353
Oid atttypid BKI_LOOKUP_OPT(pg_type);
5454

55-
/*
56-
* attstattarget is the target number of statistics datapoints to collect
57-
* during VACUUM ANALYZE of this column. A zero here indicates that we do
58-
* not wish to collect any stats about this column. A "-1" here indicates
59-
* that no value has been explicitly set for this column, so ANALYZE
60-
* should use the default setting.
61-
*/
62-
int32 attstattarget BKI_DEFAULT(-1);
63-
6455
/*
6556
* attlen is a copy of the typlen field from pg_type for this attribute.
6657
* See atttypid comments above.
@@ -82,12 +73,6 @@ CATALOG(pg_attribute,1249,AttributeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(75,
8273
*/
8374
int16 attnum;
8475

85-
/*
86-
* attndims is the declared number of dimensions, if an array type,
87-
* otherwise zero.
88-
*/
89-
int32 attndims;
90-
9176
/*
9277
* fastgetattr() uses attcacheoff to cache byte offsets of attributes in
9378
* heap tuples. The value actually stored in pg_attribute (-1) indicates
@@ -105,6 +90,12 @@ CATALOG(pg_attribute,1249,AttributeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(75,
10590
*/
10691
int32 atttypmod BKI_DEFAULT(-1);
10792

93+
/*
94+
* attndims is the declared number of dimensions, if an array type,
95+
* otherwise zero.
96+
*/
97+
int16 attndims;
98+
10899
/*
109100
* attbyval is a copy of the typbyval field from pg_type for this
110101
* attribute. See atttypid comments above.
@@ -165,7 +156,18 @@ CATALOG(pg_attribute,1249,AttributeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(75,
165156
bool attislocal BKI_DEFAULT(t);
166157

167158
/* Number of times inherited from direct parent relation(s) */
168-
int32 attinhcount BKI_DEFAULT(0);
159+
int16 attinhcount BKI_DEFAULT(0);
160+
161+
/*
162+
* attstattarget is the target number of statistics datapoints to collect
163+
* during VACUUM ANALYZE of this column. A zero here indicates that we do
164+
* not wish to collect any stats about this column. A "-1" here indicates
165+
* that no value has been explicitly set for this column, so ANALYZE
166+
* should use the default setting.
167+
*
168+
* int16 is sufficient because the max value is currently 10000.
169+
*/
170+
int16 attstattarget BKI_DEFAULT(-1);
169171

170172
/* attribute's collation, if any */
171173
Oid attcollation BKI_LOOKUP_OPT(pg_collation);

src/include/catalog/pg_constraint.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ CATALOG(pg_constraint,2606,ConstraintRelationId)
102102
bool conislocal;
103103

104104
/* Number of times inherited from direct parent relation(s) */
105-
int32 coninhcount;
105+
int16 coninhcount;
106106

107107
/* Has a local definition and cannot be inherited */
108108
bool connoinherit;

0 commit comments

Comments
 (0)