Skip to content

Commit f5024d8

Browse files
committed
Re-order pg_attribute columns to eliminate some padding space.
Now that attcompression is just a char, there's a lot of wasted padding space after it. Move it into the group of char-wide columns to save a net of 4 bytes per pg_attribute entry. While we're at it, swap the order of attstorage and attalign to make for a more logical grouping of these columns. Also re-order actions in related code to match the new field ordering. This patch also fixes one outright bug: equalTupleDescs() failed to compare attcompression. That could, for example, cause relcache reload to fail to adopt a new value following a change. Michael Paquier and Tom Lane, per a gripe from Andres Freund. Discussion: https://postgr.es/m/20210517204803.iyk5wwvwgtjcmc5w@alap3.anarazel.de
1 parent bc2a389 commit f5024d8

File tree

11 files changed

+79
-73
lines changed

11 files changed

+79
-73
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,15 @@
12361236
</para></entry>
12371237
</row>
12381238

1239+
<row>
1240+
<entry role="catalog_table_entry"><para role="column_definition">
1241+
<structfield>attalign</structfield> <type>char</type>
1242+
</para>
1243+
<para>
1244+
A copy of <literal>pg_type.typalign</literal> of this column's type
1245+
</para></entry>
1246+
</row>
1247+
12391248
<row>
12401249
<entry role="catalog_table_entry"><para role="column_definition">
12411250
<structfield>attstorage</structfield> <type>char</type>
@@ -1249,10 +1258,13 @@
12491258

12501259
<row>
12511260
<entry role="catalog_table_entry"><para role="column_definition">
1252-
<structfield>attalign</structfield> <type>char</type>
1261+
<structfield>attcompression</structfield> <type>char</type>
12531262
</para>
12541263
<para>
1255-
A copy of <literal>pg_type.typalign</literal> of this column's type
1264+
The current compression method of the column. If it is an invalid
1265+
compression method (<literal>'\0'</literal>) then column data will not
1266+
be compressed. Otherwise, <literal>'p'</literal> = pglz compression or
1267+
<literal>'l'</literal> = <productname>LZ4</productname> compression.
12561268
</para></entry>
12571269
</row>
12581270

@@ -1355,18 +1367,6 @@
13551367
</para></entry>
13561368
</row>
13571369

1358-
<row>
1359-
<entry role="catalog_table_entry"><para role="column_definition">
1360-
<structfield>attcompression</structfield> <type>char</type>
1361-
</para>
1362-
<para>
1363-
The current compression method of the column. If it is an invalid
1364-
compression method (<literal>'\0'</literal>) then column data will not
1365-
be compressed. Otherwise, <literal>'p'</literal> = pglz compression or
1366-
<literal>'l'</literal> = <productname>LZ4</productname> compression.
1367-
</para></entry>
1368-
</row>
1369-
13701370
<row>
13711371
<entry role="catalog_table_entry"><para role="column_definition">
13721372
<structfield>attacl</structfield> <type>aclitem[]</type>

src/backend/access/common/tupdesc.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
424424
* general it seems safer to check them always.
425425
*
426426
* attcacheoff must NOT be checked since it's possibly not set in both
427-
* copies.
427+
* copies. We also intentionally ignore atthasmissing, since that's
428+
* not very relevant in tupdescs, which lack the attmissingval field.
428429
*/
429430
if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
430431
return false;
@@ -440,9 +441,11 @@ equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
440441
return false;
441442
if (attr1->attbyval != attr2->attbyval)
442443
return false;
444+
if (attr1->attalign != attr2->attalign)
445+
return false;
443446
if (attr1->attstorage != attr2->attstorage)
444447
return false;
445-
if (attr1->attalign != attr2->attalign)
448+
if (attr1->attcompression != attr2->attcompression)
446449
return false;
447450
if (attr1->attnotnull != attr2->attnotnull)
448451
return false;
@@ -460,7 +463,7 @@ equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
460463
return false;
461464
if (attr1->attcollation != attr2->attcollation)
462465
return false;
463-
/* attacl, attoptions and attfdwoptions are not even present... */
466+
/* variable-length fields are not even present... */
464467
}
465468

466469
if (tupdesc1->constr != NULL)
@@ -639,12 +642,11 @@ TupleDescInitEntry(TupleDesc desc,
639642
att->attbyval = typeForm->typbyval;
640643
att->attalign = typeForm->typalign;
641644
att->attstorage = typeForm->typstorage;
642-
att->attcollation = typeForm->typcollation;
643-
644645
if (IsStorageCompressible(typeForm->typstorage))
645646
att->attcompression = GetDefaultToastCompression();
646647
else
647648
att->attcompression = InvalidCompressionMethod;
649+
att->attcollation = typeForm->typcollation;
648650

649651
ReleaseSysCache(tuple);
650652
}
@@ -709,6 +711,7 @@ TupleDescInitBuiltinEntry(TupleDesc desc,
709711
att->attbyval = false;
710712
att->attalign = TYPALIGN_INT;
711713
att->attstorage = TYPSTORAGE_EXTENDED;
714+
att->attcompression = GetDefaultToastCompression();
712715
att->attcollation = DEFAULT_COLLATION_OID;
713716
break;
714717

@@ -717,6 +720,7 @@ TupleDescInitBuiltinEntry(TupleDesc desc,
717720
att->attbyval = true;
718721
att->attalign = TYPALIGN_CHAR;
719722
att->attstorage = TYPSTORAGE_PLAIN;
723+
att->attcompression = InvalidCompressionMethod;
720724
att->attcollation = InvalidOid;
721725
break;
722726

@@ -725,6 +729,7 @@ TupleDescInitBuiltinEntry(TupleDesc desc,
725729
att->attbyval = true;
726730
att->attalign = TYPALIGN_INT;
727731
att->attstorage = TYPSTORAGE_PLAIN;
732+
att->attcompression = InvalidCompressionMethod;
728733
att->attcollation = InvalidOid;
729734
break;
730735

@@ -733,6 +738,7 @@ TupleDescInitBuiltinEntry(TupleDesc desc,
733738
att->attbyval = FLOAT8PASSBYVAL;
734739
att->attalign = TYPALIGN_DOUBLE;
735740
att->attstorage = TYPSTORAGE_PLAIN;
741+
att->attcompression = InvalidCompressionMethod;
736742
att->attcollation = InvalidOid;
737743
break;
738744

src/backend/access/spgist/spgutils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ fillTypeDesc(SpGistTypeDesc *desc, Oid type)
165165
typtup = (Form_pg_type) GETSTRUCT(tp);
166166
desc->attlen = typtup->typlen;
167167
desc->attbyval = typtup->typbyval;
168-
desc->attstorage = typtup->typstorage;
169168
desc->attalign = typtup->typalign;
169+
desc->attstorage = typtup->typstorage;
170170
ReleaseSysCache(tp);
171171
}
172172

@@ -304,8 +304,8 @@ getSpGistTupleDesc(Relation index, SpGistTypeDesc *keyType)
304304
att->attalign = keyType->attalign;
305305
att->attstorage = keyType->attstorage;
306306
/* We shouldn't need to bother with making these valid: */
307-
att->attcollation = InvalidOid;
308307
att->attcompression = InvalidCompressionMethod;
308+
att->attcollation = InvalidOid;
309309
/* In case we changed typlen, we'd better reset following offsets */
310310
for (int i = spgFirstIncludeColumn; i < outTupDesc->natts; i++)
311311
TupleDescAttr(outTupDesc, i)->attcacheoff = -1;

src/backend/bootstrap/bootstrap.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,8 @@ DefineAttr(char *name, char *type, int attnum, int nullness)
699699
attrtypes[attnum]->atttypid = Ap->am_oid;
700700
attrtypes[attnum]->attlen = Ap->am_typ.typlen;
701701
attrtypes[attnum]->attbyval = Ap->am_typ.typbyval;
702-
attrtypes[attnum]->attstorage = Ap->am_typ.typstorage;
703702
attrtypes[attnum]->attalign = Ap->am_typ.typalign;
703+
attrtypes[attnum]->attstorage = Ap->am_typ.typstorage;
704704
attrtypes[attnum]->attcollation = Ap->am_typ.typcollation;
705705
/* if an array type, assume 1-dimensional attribute */
706706
if (Ap->am_typ.typelem != InvalidOid && Ap->am_typ.typlen < 0)
@@ -713,8 +713,8 @@ DefineAttr(char *name, char *type, int attnum, int nullness)
713713
attrtypes[attnum]->atttypid = TypInfo[typeoid].oid;
714714
attrtypes[attnum]->attlen = TypInfo[typeoid].len;
715715
attrtypes[attnum]->attbyval = TypInfo[typeoid].byval;
716-
attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
717716
attrtypes[attnum]->attalign = TypInfo[typeoid].align;
717+
attrtypes[attnum]->attstorage = TypInfo[typeoid].storage;
718718
attrtypes[attnum]->attcollation = TypInfo[typeoid].collation;
719719
/* if an array type, assume 1-dimensional attribute */
720720
if (TypInfo[typeoid].elem != InvalidOid &&
@@ -724,6 +724,11 @@ DefineAttr(char *name, char *type, int attnum, int nullness)
724724
attrtypes[attnum]->attndims = 0;
725725
}
726726

727+
if (IsStorageCompressible(attrtypes[attnum]->attstorage))
728+
attrtypes[attnum]->attcompression = GetDefaultToastCompression();
729+
else
730+
attrtypes[attnum]->attcompression = InvalidCompressionMethod;
731+
727732
/*
728733
* If a system catalog column is collation-aware, force it to use C
729734
* collation, so that its behavior is independent of the database's
@@ -737,10 +742,6 @@ DefineAttr(char *name, char *type, int attnum, int nullness)
737742
attrtypes[attnum]->attcacheoff = -1;
738743
attrtypes[attnum]->atttypmod = -1;
739744
attrtypes[attnum]->attislocal = true;
740-
if (IsStorageCompressible(attrtypes[attnum]->attstorage))
741-
attrtypes[attnum]->attcompression = GetDefaultToastCompression();
742-
else
743-
attrtypes[attnum]->attcompression = InvalidCompressionMethod;
744745

745746
if (nullness == BOOTCOL_NULL_FORCE_NOT_NULL)
746747
{

src/backend/catalog/genbki.pl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -897,8 +897,11 @@ sub morph_row_for_pgattr
897897
$row->{atttypid} = $type->{oid};
898898
$row->{attlen} = $type->{typlen};
899899
$row->{attbyval} = $type->{typbyval};
900-
$row->{attstorage} = $type->{typstorage};
901900
$row->{attalign} = $type->{typalign};
901+
$row->{attstorage} = $type->{typstorage};
902+
903+
$row->{attcompression} =
904+
$type->{typstorage} ne 'p' && $type->{typstorage} ne 'e' ? 'p' : '\0';
902905

903906
# set attndims if it's an array type
904907
$row->{attndims} = $type->{typcategory} eq 'A' ? '1' : '0';
@@ -907,9 +910,6 @@ sub morph_row_for_pgattr
907910
$row->{attcollation} =
908911
$type->{typcollation} ne '0' ? $C_COLLATION_OID : 0;
909912

910-
$row->{attcompression} =
911-
$type->{typstorage} ne 'p' && $type->{typstorage} ne 'e' ? 'p' : '\0';
912-
913913
if (defined $attr->{forcenotnull})
914914
{
915915
$row->{attnotnull} = 't';

src/backend/catalog/heap.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ static Node *cookConstraint(ParseState *pstate,
146146
/*
147147
* The initializers below do not include trailing variable length fields,
148148
* but that's OK - we're never going to reference anything beyond the
149-
* fixed-size portion of the structure anyway.
149+
* fixed-size portion of the structure anyway. Fields that can default
150+
* to zeroes are also not mentioned.
150151
*/
151152

152153
static const FormData_pg_attribute a1 = {
@@ -157,8 +158,8 @@ static const FormData_pg_attribute a1 = {
157158
.attcacheoff = -1,
158159
.atttypmod = -1,
159160
.attbyval = false,
160-
.attstorage = TYPSTORAGE_PLAIN,
161161
.attalign = TYPALIGN_SHORT,
162+
.attstorage = TYPSTORAGE_PLAIN,
162163
.attnotnull = true,
163164
.attislocal = true,
164165
};
@@ -171,8 +172,8 @@ static const FormData_pg_attribute a2 = {
171172
.attcacheoff = -1,
172173
.atttypmod = -1,
173174
.attbyval = true,
174-
.attstorage = TYPSTORAGE_PLAIN,
175175
.attalign = TYPALIGN_INT,
176+
.attstorage = TYPSTORAGE_PLAIN,
176177
.attnotnull = true,
177178
.attislocal = true,
178179
};
@@ -185,8 +186,8 @@ static const FormData_pg_attribute a3 = {
185186
.attcacheoff = -1,
186187
.atttypmod = -1,
187188
.attbyval = true,
188-
.attstorage = TYPSTORAGE_PLAIN,
189189
.attalign = TYPALIGN_INT,
190+
.attstorage = TYPSTORAGE_PLAIN,
190191
.attnotnull = true,
191192
.attislocal = true,
192193
};
@@ -199,8 +200,8 @@ static const FormData_pg_attribute a4 = {
199200
.attcacheoff = -1,
200201
.atttypmod = -1,
201202
.attbyval = true,
202-
.attstorage = TYPSTORAGE_PLAIN,
203203
.attalign = TYPALIGN_INT,
204+
.attstorage = TYPSTORAGE_PLAIN,
204205
.attnotnull = true,
205206
.attislocal = true,
206207
};
@@ -213,8 +214,8 @@ static const FormData_pg_attribute a5 = {
213214
.attcacheoff = -1,
214215
.atttypmod = -1,
215216
.attbyval = true,
216-
.attstorage = TYPSTORAGE_PLAIN,
217217
.attalign = TYPALIGN_INT,
218+
.attstorage = TYPSTORAGE_PLAIN,
218219
.attnotnull = true,
219220
.attislocal = true,
220221
};
@@ -233,8 +234,8 @@ static const FormData_pg_attribute a6 = {
233234
.attcacheoff = -1,
234235
.atttypmod = -1,
235236
.attbyval = true,
236-
.attstorage = TYPSTORAGE_PLAIN,
237237
.attalign = TYPALIGN_INT,
238+
.attstorage = TYPSTORAGE_PLAIN,
238239
.attnotnull = true,
239240
.attislocal = true,
240241
};
@@ -779,8 +780,9 @@ InsertPgAttributeTuples(Relation pg_attribute_rel,
779780
slot[slotCount]->tts_values[Anum_pg_attribute_attcacheoff - 1] = Int32GetDatum(-1);
780781
slot[slotCount]->tts_values[Anum_pg_attribute_atttypmod - 1] = Int32GetDatum(attrs->atttypmod);
781782
slot[slotCount]->tts_values[Anum_pg_attribute_attbyval - 1] = BoolGetDatum(attrs->attbyval);
782-
slot[slotCount]->tts_values[Anum_pg_attribute_attstorage - 1] = CharGetDatum(attrs->attstorage);
783783
slot[slotCount]->tts_values[Anum_pg_attribute_attalign - 1] = CharGetDatum(attrs->attalign);
784+
slot[slotCount]->tts_values[Anum_pg_attribute_attstorage - 1] = CharGetDatum(attrs->attstorage);
785+
slot[slotCount]->tts_values[Anum_pg_attribute_attcompression - 1] = CharGetDatum(attrs->attcompression);
784786
slot[slotCount]->tts_values[Anum_pg_attribute_attnotnull - 1] = BoolGetDatum(attrs->attnotnull);
785787
slot[slotCount]->tts_values[Anum_pg_attribute_atthasdef - 1] = BoolGetDatum(attrs->atthasdef);
786788
slot[slotCount]->tts_values[Anum_pg_attribute_atthasmissing - 1] = BoolGetDatum(attrs->atthasmissing);
@@ -790,7 +792,6 @@ InsertPgAttributeTuples(Relation pg_attribute_rel,
790792
slot[slotCount]->tts_values[Anum_pg_attribute_attislocal - 1] = BoolGetDatum(attrs->attislocal);
791793
slot[slotCount]->tts_values[Anum_pg_attribute_attinhcount - 1] = Int32GetDatum(attrs->attinhcount);
792794
slot[slotCount]->tts_values[Anum_pg_attribute_attcollation - 1] = ObjectIdGetDatum(attrs->attcollation);
793-
slot[slotCount]->tts_values[Anum_pg_attribute_attcompression - 1] = CharGetDatum(attrs->attcompression);
794795
if (attoptions && attoptions[natts] != (Datum) 0)
795796
slot[slotCount]->tts_values[Anum_pg_attribute_attoptions - 1] = attoptions[natts];
796797
else

src/backend/catalog/index.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,8 @@ ConstructTupleDescriptor(Relation heapRelation,
345345
to->attndims = from->attndims;
346346
to->atttypmod = from->atttypmod;
347347
to->attbyval = from->attbyval;
348-
to->attstorage = from->attstorage;
349348
to->attalign = from->attalign;
349+
to->attstorage = from->attstorage;
350350
to->attcompression = from->attcompression;
351351
}
352352
else
@@ -373,16 +373,16 @@ ConstructTupleDescriptor(Relation heapRelation,
373373
*/
374374
to->atttypid = keyType;
375375
to->attlen = typeTup->typlen;
376+
to->atttypmod = exprTypmod(indexkey);
376377
to->attbyval = typeTup->typbyval;
377-
to->attstorage = typeTup->typstorage;
378378
to->attalign = typeTup->typalign;
379-
to->atttypmod = exprTypmod(indexkey);
379+
to->attstorage = typeTup->typstorage;
380380

381381
/*
382382
* For expression columns, set attcompression invalid, since
383383
* there's no table column from which to copy the value. Whenever
384384
* we actually need to compress a value, we'll use whatever the
385-
* current value of default_compression_method is at that point in
385+
* current value of default_toast_compression is at that point in
386386
* time.
387387
*/
388388
to->attcompression = InvalidCompressionMethod;
@@ -464,6 +464,8 @@ ConstructTupleDescriptor(Relation heapRelation,
464464
to->attbyval = typeTup->typbyval;
465465
to->attalign = typeTup->typalign;
466466
to->attstorage = typeTup->typstorage;
467+
/* As above, use the default compression method in this case */
468+
to->attcompression = InvalidCompressionMethod;
467469

468470
ReleaseSysCache(tuple);
469471
}

src/backend/commands/tablecmds.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2640,8 +2640,8 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
26402640
def->constraints = NIL;
26412641
def->location = -1;
26422642
if (CompressionMethodIsValid(attribute->attcompression))
2643-
def->compression = pstrdup(GetCompressionMethodName(
2644-
attribute->attcompression));
2643+
def->compression =
2644+
pstrdup(GetCompressionMethodName(attribute->attcompression));
26452645
else
26462646
def->compression = NULL;
26472647
inhSchema = lappend(inhSchema, def);
@@ -6596,12 +6596,19 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
65966596
attribute.atttypid = typeOid;
65976597
attribute.attstattarget = (newattnum > 0) ? -1 : 0;
65986598
attribute.attlen = tform->typlen;
6599-
attribute.atttypmod = typmod;
66006599
attribute.attnum = newattnum;
6601-
attribute.attbyval = tform->typbyval;
66026600
attribute.attndims = list_length(colDef->typeName->arrayBounds);
6603-
attribute.attstorage = tform->typstorage;
6601+
attribute.atttypmod = typmod;
6602+
attribute.attbyval = tform->typbyval;
66046603
attribute.attalign = tform->typalign;
6604+
attribute.attstorage = tform->typstorage;
6605+
/* do not set compression in views etc */
6606+
if (rel->rd_rel->relkind == RELKIND_RELATION ||
6607+
rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
6608+
attribute.attcompression = GetAttributeCompression(&attribute,
6609+
colDef->compression);
6610+
else
6611+
attribute.attcompression = InvalidCompressionMethod;
66056612
attribute.attnotnull = colDef->is_not_null;
66066613
attribute.atthasdef = false;
66076614
attribute.atthasmissing = false;
@@ -6612,17 +6619,6 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
66126619
attribute.attinhcount = colDef->inhcount;
66136620
attribute.attcollation = collOid;
66146621

6615-
/*
6616-
* lookup attribute's compression method and store it in the
6617-
* attr->attcompression.
6618-
*/
6619-
if (rel->rd_rel->relkind == RELKIND_RELATION ||
6620-
rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
6621-
attribute.attcompression = GetAttributeCompression(&attribute,
6622-
colDef->compression);
6623-
else
6624-
attribute.attcompression = InvalidCompressionMethod;
6625-
66266622
/* attribute.attacl is handled by InsertPgAttributeTuples() */
66276623

66286624
ReleaseSysCache(typeTuple);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy