8000 Rethink definition of pg_attribute.attcompression. · postgrespro/postgres@e6241d8 · GitHub
[go: up one dir, main page]

Skip to content

Commit e6241d8

Browse files
committed
Rethink definition of pg_attribute.attcompression.
Redefine '\0' (InvalidCompressionMethod) as meaning "if we need to compress, use the current setting of default_toast_compression". This allows '\0' to be a suitable default choice regardless of datatype, greatly simplifying code paths that initialize tupledescs and the like. It seems like a more user-friendly approach as well, because now the default compression choice doesn't migrate into table definitions, meaning that changing default_toast_compression is usually sufficient to flip an installation's behavior; one needn't tediously issue per-column ALTER SET COMPRESSION commands. Along the way, fix a few minor bugs and documentation issues with the per-column-compression feature. Adopt more robust APIs for SetIndexStorageProperties and GetAttributeCompression. Bump catversion because typical contents of attcompression will now be different. We could get away without doing that, but it seems better to ensure v14 installations all agree on this. (We already forced initdb for beta2, anyway.) Discussion: https://postgr.es/m/626613.1621787110@sss.pgh.pa.us
1 parent a717e5c commit e6241d8

29 files changed

+257
-380
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,10 +1261,14 @@
12611261
<structfield>attcompression</structfield> <type>char</type>
12621262
</para>
12631263
<para>
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.
1264+
The current compression method of the column. Typically this is
1265+
<literal>'\0'</literal> to specify use of the current default setting
1266+
(see <xref linkend="guc-default-toast-compression"/>). Otherwise,
1267+
<literal>'p'</literal> selects pglz compression, while
1268+
<literal>'l'</literal> selects <productname>LZ4</productname>
1269+
compression. However, this field is ignored
1270+
whenever <structfield>attstorage</structfield> does not allow
1271+
compression.
12681272
</para></entry>
12691273
</row>
12701274

doc/src/sgml/config.sgml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8256,13 +8256,14 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv;
82568256
<para>
82578257
This variable sets the default
82588258
<link linkend="storage-toast">TOAST</link>
8259-
compression method for columns of newly-created tables. The
8260-
<command>CREATE TABLE</command> statement can override this default
8261-
by specifying the <literal>COMPRESSION</literal> column option.
8262-
8263-
The supported compression methods are <literal>pglz</literal> and,
8264-
if <productname>PostgreSQL</productname> was compiled with
8265-
<literal>--with-lz4</literal>, <literal>lz4</literal>.
8259+
compression method for values of compressible columns.
8260+
(This can be overridden for individual columns by setting
8261+
the <literal>COMPRESSION</literal> column option in
8262+
<command>CREATE TABLE</command> or
8263+
<command>ALTER TABLE</command>.)
8264+
The supported compression methods are <literal>pglz</literal> and
8265+
(if <productname>PostgreSQL</productname> was compiled with
8266+
<option>--with-lz4</option>) <literal>lz4</literal>.
82668267
The default is <literal>pglz</literal>.
82678268
</para>
82688269
</listitem>

doc/src/sgml/func.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26253,10 +26253,10 @@ postgres=# SELECT * FROM pg_walfile_name_offset(pg_stop_backup());
2625326253
<primary>pg_column_compression</primary>
2625426254
</indexterm>
2625526255
<function>pg_column_compression</function> ( <type>"any"</type> )
26256-
<returnvalue>integer</returnvalue>
26256+
<returnvalue>text</returnvalue>
2625726257
</para>
2625826258
<para>
26259-
Shows the compression algorithm that was used to compress a
26259+
Shows the compression algorithm that was used to compress
2626026260
an individual variable-length value. Returns <literal>NULL</literal>
2626126261
if the value is not compressed.
2626226262
</para></entry>

doc/src/sgml/ref/alter_table.sgml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
104104
GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( <replaceable>sequence_options</replaceable> ) ] |
105105
UNIQUE <replaceable class="parameter">index_parameters</replaceable> |
106106
PRIMARY KEY <replaceable class="parameter">index_parameters</replaceable> |
107-
COMPRESSION <replaceable class="parameter">compression_method</replaceable> |
108107
REFERENCES <replaceable class="parameter">reftable</replaceable> [ ( <replaceable class="parameter">refcolumn</replaceable> ) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
109108
[ ON DELETE <replaceable class="parameter">referential_action</replaceable> ] [ ON UPDATE <replaceable class="parameter">referential_action</replaceable> ] }
110109
[ DEFERRABLE | NOT DEFERRABLE ] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
@@ -391,24 +390,27 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
391390
</term>
392391
<listitem>
393392
<para>
394-
This sets the compression method to be used for data inserted into a column.
395-
393+
This form sets the compression method for a column, determining how
394+
values inserted in future will be compressed (if the storage mode
395+
permits compression at all).
396396
This does not cause the table to be rewritten, so existing data may still
397397
be compressed with other compression methods. If the table is rewritten with
398398
<command>VACUUM FULL</command> or <command>CLUSTER</command>, or restored
399-
with <application>pg_restore</application>, then all tuples are rewritten
400-
with the configured compression methods.
401-
402-
Also, note that when data is inserted from another relation (for example,
403-
by <command>INSERT ... SELECT</command>), tuples from the source data are
404-
not necessarily detoasted, and any previously compressed data is retained
405-
with its existing compression method, rather than recompressing with the
406-
compression methods of the target columns.
407-
399+
with <application>pg_restore</application>, then all values are rewritten
400+
with the configured compression method.
401+
However, when data is inserted from another relation (for example,
402+
by <command>INSERT ... SELECT</command>), values from the source table are
403+
not necessarily detoasted, so any previously compressed data may retain
404+
its existing compression method, rather than being recompressed with the
405+
compression method of the target column.
408406
The supported compression
409407
methods are <literal>pglz</literal> and <literal>lz4</literal>.
410-
<literal>lz4</literal> is available only if <literal>--with-lz4</literal>
411-
was used when building <productname>PostgreSQL</productname>.
408+
(<literal>lz4</literal> is available only if <option>--with-lz4</option>
409+
was used when building <productname>PostgreSQL</productname>.) In
410+
addition, <replaceable class="parameter">compression_method</replaceable>
411+
can be <literal>default</literal>, which selects the default behavior of
412+
consulting the <xref linkend="guc-default-toast-compression"/> setting
413+
at the time of data insertion to determine the method to use.
412414
</para>
413415
</listitem>
414416
</varlistentry>

doc/src/sgml/ref/create_table.sgml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ PostgreSQL documentation
2222
<refsynopsisdiv>
2323
<synopsis>
2424
CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] <replaceable class="parameter">table_name</replaceable> ( [
25-
{ <replaceable class="parameter">column_name</replaceable> <replaceable class="parameter">data_type</replaceable> [ COLLATE <replaceable>collation</replaceable> ] [ COMPRESSION <replaceable>compression_method</replaceable> ] [ <replaceable class="parameter">column_constraint</replaceable> [ ... ] ]
25+
{ <replaceable class="parameter">column_name</replaceable> <replaceable class="parameter">data_type</replaceable> [ COMPRESSION <replaceable>compression_method</replaceable> ] [ COLLATE <replaceable>collation</replaceable> ] [ <replaceable class="parameter">column_constraint</replaceable> [ ... ] ]
2626
| <replaceable>table_constraint</replaceable>
2727
| LIKE <replaceable>source_table</replaceable> [ <replaceable>like_option</replaceable> ... ] }
2828
[, ... ]
@@ -293,17 +293,22 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
293293
<listitem>
294294
<para>
295295
The <literal>COMPRESSION</literal> clause sets the compression method
296-
for a column. Compression is supported only for variable-width data
297-
types, and is used only for columns whose storage type is main or
298-
extended. (See <xref linkend="sql-altertable"/> for information on
299-
column storage types.) Setting this property for a partitioned table
296+
for the column. Compression is supported only for variable-width data
297+
types, and is used only when the column's storage mode
298+
is <literal>main</literal> or <literal>extended</literal>.
299+
(See <xref linkend="sql-altertable"/> for information on
300+
column storage modes.) Setting this property for a partitioned table
300301
has no direct effect, because such tables have no storage of their own,
301-
but the configured value is inherited by newly-created partitions.
302+
but the configured value will be inherited by newly-created partitions.
302303
The supported compression methods are <literal>pglz</literal> and
303-
<literal>lz4</literal>. <literal>lz4</literal> is available only if
304-
<literal>--with-lz4</literal> was used when building
305-
<productname>PostgreSQL</productname>. The default
306-
is <literal>pglz</literal>.
304+
<literal>lz4</literal>. (<literal>lz4</literal> is available only if
305+
<option>--with-lz4</option> was used when building
306+
<productname>PostgreSQL</productname>.) In addition,
307+
<replaceable class="parameter">compression_method</replaceabl F438 e>
308+
can be <literal>default</literal> to explicitly specify the default
309+
behavior, which is to consult the
310+
<xref linkend="guc-default-toast-compression"/> setting at the time of
311+
data insertion to determine the method to use.
307312
</para>
308313
</listitem>
309314
</varlistentry>

doc/src/sgml/ref/pg_dump.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -975,8 +975,8 @@ PostgreSQL documentation
975975
<para>
976976
Do not output commands to set <acronym>TOAST</acronym> compression
977977
methods.
978-
With this option, all objects will be created using whichever
979-
compression method is the default during restore.
978+
With this option, all columns will be restored with the default
979+
compression setting.
980980
</para>
981981
</listitem>
982982
</varlistentry>

doc/src/sgml/ref/pg_dumpall.sgml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,12 @@ PostgreSQL documentation
464464
<para>
465465
Do not output commands to set <acronym>TOAST</acronym> compression
466466
methods.
467-
With this option, all objects will be created using whichever
468-
compression method is the default during restore.
467+
With this option, all columns will be restored with the default
468+
compression setting.
469469
</para>
470470
</listitem>
471471
</varlistentry>
472-
472+
473473
<varlistentry>
474474
<term><option>--no-unlogged-table-data</option></term>
475475
<listitem>

doc/src/sgml/storage.sgml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,16 @@ but the varlena header does not tell whether it has occurred &mdash;
376376
the content of the <acronym>TOAST</acronym> pointer tells that, instead.
377377
</para>
378378

379+
<para>
380+
The compression technique used for either in-line or out-of-line compressed
381+
data can be selected for each column by setting
382+
the <literal>COMPRESSION</literal> column option in <command>CREATE
383+
TABLE</command> or <command>ALTER TABLE</command>. The default for columns
384+
with no explicit setting is to consult the
385+
<xref linkend="guc-default-toast-compression"/> parameter at the time data is
386+
inserted.
387+
</para>
388+
379389
<para>
380390
As mentioned, there are multiple types of <acronym>TOAST</acronym> pointer datums.
381391
The oldest and most common type is a pointer to out-of-line data stored in
@@ -392,13 +402,6 @@ useful for avoiding copying and redundant processing of large data values.
392402
Further details appear in <xref linkend="storage-toast-inmemory"/>.
393403
</para>
394404

395-
<para>
396-
The compression technique used for either in-line or out-of-line compressed
397-
data can be selected using the <literal>COMPRESSION</literal> option on a per-column
398-
basis when creating a table. The default for columns with no explicit setting
399-
is taken from the value of <xref linkend="guc-default-toast-compression" />.
400-
</para>
401-
402405
<sect2 id="storage-toast-ondisk">
403406
<title>Out-of-Line, On-Disk TOAST Storage</title>
404407

src/backend/access/brin/brin_tuple.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,10 @@ brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple,
232232
* same compression method. Otherwise we have to use the
233233
* default method.
234234
*/
235-
if (att->atttypid == atttype->type_id &&
236-
CompressionMethodIsValid(att->attcompression))
235+
if (att->atttypid == atttype->type_id)
237236
compression = att->attcompression;
238237
else
239-
compression = GetDefaultToastCompression();
238+
compression = InvalidCompressionMethod;
240239

241240
cvalue = toast_compress_datum(value, compression);
242241

src/backend/access/common/indextuple.c

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,9 @@ index_form_tuple(TupleDesc tupleDescriptor,
104104
att->attstorage == TYPSTORAGE_MAIN))
105105
{
106106
Datum cvalue;
107-
char compression = att->attcompression;
108107

109-
/*
110-
* If the compression method is not valid, use the default. We
111-
* don't expect this to happen for regular index columns, which
112-
* inherit the setting from the corresponding table column, but we
113-
* do expect it to happen whenever an expression is indexed.
114-
*/
115-
if (!CompressionMethodIsValid(compression))
116-
compression = GetDefaultToastCompression();
117-
118-
cvalue = toast_compress_datum(untoasted_values[i], compression);
108+
cvalue = toast_compress_datum(untoasted_values[i],
109+
att->attcompression);
119110

120111
if (DatumGetPointer(cvalue) != NULL)
121112
{

0 commit comments

Comments
 (0)
0