Support writing "CREATE/ALTER TABLE ... SET STORAGE DEFAULT".
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 10 Nov 2022 23:20:49 +0000 (18:20 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 10 Nov 2022 23:20:49 +0000 (18:20 -0500)
We already allow explicitly writing DEFAULT for SET COMPRESSION,
so it seems a bit inflexible and non-orthogonal to not have it
for STORAGE.

Aleksander Alekseev

Discussion: https://postgr.es/m/CAJ7c6TMX9ui+6y3TQFaXJYVpZyBukvqhQbVDJ8OUokeLRhtnpA@mail.gmail.com

doc/src/sgml/ref/alter_foreign_table.sgml
doc/src/sgml/ref/alter_materialized_view.sgml
doc/src/sgml/ref/alter_table.sgml
doc/src/sgml/ref/create_table.sgml
src/backend/commands/tablecmds.c
src/backend/parser/gram.y
src/bin/psql/tab-complete.c
src/test/regress/expected/alter_table.out
src/test/regress/sql/alter_table.sql

index 7ca03f3ac9f11a9d9b5b6e33820615537045f0d3..d056dc1bb13d7e7e795db55849b57325aa18319c 100644 (file)
@@ -41,7 +41,7 @@ ALTER FOREIGN TABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceab
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET STATISTICS <replaceable class="parameter">integer</replaceable>
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET ( <replaceable class="parameter">attribute_option</replaceable> = <replaceable class="parameter">value</replaceable> [, ... ] )
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> RESET ( <replaceable class="parameter">attribute_option</replaceable> [, ... ] )
-    ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
+    ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> OPTIONS ( [ ADD | SET | DROP ] <replaceable class="parameter">option</replaceable> ['<replaceable class="parameter">value</replaceable>'] [, ... ])
     ADD <replaceable class="parameter">table_constraint</replaceable> [ NOT VALID ]
     VALIDATE CONSTRAINT <replaceable class="parameter">constraint_name</replaceable>
index cae135c27a0ec7734678f3e66ebca6847926590b..040ae53f982ad8704b7604d8eca615b13d3bc4c9 100644 (file)
@@ -39,7 +39,7 @@ ALTER MATERIALIZED VIEW ALL IN TABLESPACE <replaceable class="parameter">name</r
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET STATISTICS <replaceable class="parameter">integer</replaceable>
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET ( <replaceable class="parameter">attribute_option</replaceable> = <replaceable class="parameter">value</replaceable> [, ... ] )
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> RESET ( <replaceable class="parameter">attribute_option</replaceable> [, ... ] )
-    ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
+    ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET COMPRESSION <replaceable class="parameter">compression_method</replaceable>
     CLUSTER ON <replaceable class="parameter">index_name</replaceable>
     SET WITHOUT CLUSTER
index f0f912a56c5fa6327427b77380d169b1246736d7..43d782fea90ca4ea5fa60766369d27b542d81398 100644 (file)
@@ -53,7 +53,7 @@ ALTER TABLE [ IF EXISTS ] <replaceable class="parameter">name</replaceable>
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET STATISTICS <replaceable class="parameter">integer</replaceable>
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET ( <replaceable class="parameter">attribute_option</replaceable> = <replaceable class="parameter">value</replaceable> [, ... ] )
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> RESET ( <replaceable class="parameter">attribute_option</replaceable> [, ... ] )
-    ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
+    ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }
     ALTER [ COLUMN ] <replaceable class="parameter">column_name</replaceable> SET COMPRESSION <replaceable class="parameter">compression_method</replaceable>
     ADD <replaceable class="parameter">table_constraint</replaceable> [ NOT VALID ]
     ADD <replaceable class="parameter">table_constraint_using_index</replaceable>
@@ -367,7 +367,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
 
    <varlistentry>
     <term>
-     <literal>SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }</literal>
+     <literal>SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }</literal>
      <indexterm>
       <primary>TOAST</primary>
       <secondary>per-column storage settings</secondary>
@@ -376,20 +376,24 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
     <listitem>
      <para>
       This form sets the storage mode for a column. This controls whether this
-      column is held inline or in a secondary <acronym>TOAST</acronym> table, and
-      whether the data
+      column is held inline or in a secondary <acronym>TOAST</acronym> table,
+      and whether the data
       should be compressed or not. <literal>PLAIN</literal> must be used
       for fixed-length values such as <type>integer</type> and is
       inline, uncompressed. <literal>MAIN</literal> is for inline,
       compressible data. <literal>EXTERNAL</literal> is for external,
       uncompressed data, and <literal>EXTENDED</literal> is for external,
-      compressed data.  <literal>EXTENDED</literal> is the default for most
-      data types that support non-<literal>PLAIN</literal> storage.
+      compressed data.
+      Writing <literal>DEFAULT</literal> sets the storage mode to the default
+      mode for the column's data type.  <literal>EXTENDED</literal> is the
+      default for most data types that support non-<literal>PLAIN</literal>
+      storage.
       Use of <literal>EXTERNAL</literal> will make substring operations on
       very large <type>text</type> and <type>bytea</type> values run faster,
-      at the penalty of increased storage space.  Note that
-      <literal>SET STORAGE</literal> doesn't itself change anything in the table,
-      it just sets the strategy to be pursued during future table updates.
+      at the penalty of increased storage space.
+      Note that <literal>ALTER TABLE ... SET STORAGE</literal> doesn't itself
+      change anything in the table; it just sets the strategy to be pursued
+      during future table updates.
       See <xref linkend="storage-toast"/> for more information.
      </para>
     </listitem>
index c14b2010d81d59afcd3824a94cc97e1856845c84..c98223b2a51c631f13a3faa39f1c1e4a2a265736 100644 (file)
@@ -22,7 +22,7 @@ PostgreSQL documentation
  <refsynopsisdiv>
 <synopsis>
 CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } | UNLOGGED ] TABLE [ IF NOT EXISTS ] <replaceable class="parameter">table_name</replaceable> ( [
-  { <replaceable class="parameter">column_name</replaceable> <replaceable class="parameter">data_type</replaceable> [ STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN } ] [ COMPRESSION <replaceable>compression_method</replaceable> ] [ COLLATE <replaceable>collation</replaceable> ] [ <replaceable class="parameter">column_constraint</replaceable> [ ... ] ]
+  { <replaceable class="parameter">column_name</replaceable> <replaceable class="parameter">data_type</replaceable> [ STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT } ] [ COMPRESSION <replaceable>compression_method</replaceable> ] [ COLLATE <replaceable>collation</replaceable> ] [ <replaceable class="parameter">column_constraint</replaceable> [ ... ] ]
     | <replaceable>table_constraint</replaceable>
     | LIKE <replaceable>source_table</replaceable> [ <replaceable>like_option</replaceable> ... ] }
     [, ... ]
@@ -299,7 +299,7 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
 
    <varlistentry>
     <term>
-     <literal>STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }</literal>
+     <literal>STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }</literal>
      <indexterm>
       <primary>TOAST</primary>
       <secondary>per-column storage settings</secondary>
@@ -314,12 +314,14 @@ WITH ( MODULUS <replaceable class="parameter">numeric_literal</replaceable>, REM
       inline, uncompressed. <literal>MAIN</literal> is for inline, compressible
       data. <literal>EXTERNAL</literal> is for external, uncompressed data, and
       <literal>EXTENDED</literal> is for external, compressed data.
-      <literal>EXTENDED</literal> is the default for most data types that
-      support non-<literal>PLAIN</literal> storage. Use of
-      <literal>EXTERNAL</literal> will make substring operations on very large
-      <type>text</type> and <type>bytea</type> values run faster, at the penalty
-      of increased storage space. See <xref linkend="storage-toast"/> for more
-      information.
+      Writing <literal>DEFAULT</literal> sets the storage mode to the default
+      mode for the column's data type.  <literal>EXTENDED</literal> is the
+      default for most data types that support non-<literal>PLAIN</literal>
+      storage.
+      Use of <literal>EXTERNAL</literal> will make substring operations on
+      very large <type>text</type> and <type>bytea</type> values run faster,
+      at the penalty of increased storage space.
+      See <xref linkend="storage-toast"/> for more information.
      </para>
     </listitem>
    </varlistentry>
index 6007e10730a119d37f3a2a1f7492d0d1a45a1913..fc4bd0de91a3c09ffec6f31a5bfdf31541eefa99 100644 (file)
@@ -19311,6 +19311,8 @@ GetAttributeStorage(Oid atttypid, const char *storagemode)
                cstorage = TYPSTORAGE_EXTENDED;
        else if (pg_strcasecmp(storagemode, "main") == 0)
                cstorage = TYPSTORAGE_MAIN;
+       else if (pg_strcasecmp(storagemode, "default") == 0)
+               cstorage = get_typstorage(atttypid);
        else
                ereport(ERROR,
                                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
index 2dddd8f302ca0d484ef7b3114a20e5aeecb1531d..2a910ded15e5c6f3b7b9235dae5802f92e1d4b81 100644 (file)
@@ -3758,6 +3758,7 @@ opt_column_compression:
 
 column_storage:
                        STORAGE ColId                                                   { $$ = $2; }
+                       | STORAGE DEFAULT                                               { $$ = pstrdup("default"); }
                ;
 
 opt_column_storage:
index 4c45e4747a970267c54d0e864773552601807bed..7b73886ce19c46f1b5f505457bf4bb9194794616 100644 (file)
@@ -2420,7 +2420,7 @@ psql_completion(const char *text, int start, int end)
        /* ALTER TABLE ALTER [COLUMN] <foo> SET STORAGE */
        else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STORAGE") ||
                         Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STORAGE"))
-               COMPLETE_WITH("PLAIN", "EXTERNAL", "EXTENDED", "MAIN");
+               COMPLETE_WITH("DEFAULT", "PLAIN", "EXTERNAL", "EXTENDED", "MAIN");
        /* ALTER TABLE ALTER [COLUMN] <foo> SET STATISTICS */
        else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STATISTICS") ||
                         Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STATISTICS"))
index ca13f0c13acefdcaa5861c01d5065f7fec426c82..600e603bdfa2cabac737df977a03c36522e965c8 100644 (file)
@@ -2262,7 +2262,7 @@ select reltoastrelid <> 0 as has_toast_table
  f
 (1 row)
 
-alter table test_storage alter a set storage extended; -- re-add TOAST table
+alter table test_storage alter a set storage default; -- re-add TOAST table
 select reltoastrelid <> 0 as has_toast_table
   from pg_class where oid = 'test_storage'::regclass;
  has_toast_table 
index b85b6c739896c7cba9eddd515b7c4077ec7d1f07..f58b2f75d5b4c201b7e23fddcc770e651ec2af55 100644 (file)
@@ -1535,7 +1535,7 @@ alter table test_storage alter a set storage plain;
 alter table test_storage add b int default random()::int;
 select reltoastrelid <> 0 as has_toast_table
   from pg_class where oid = 'test_storage'::regclass;
-alter table test_storage alter a set storage extended; -- re-add TOAST table
+alter table test_storage alter a set storage default; -- re-add TOAST table
 select reltoastrelid <> 0 as has_toast_table
   from pg_class where oid = 'test_storage'::regclass;