From a32e4d5bb18aeb5ed132dfb37d4c8d8ca4fc1353 Mon Sep 17 00:00:00 2001 From: Aleksander Alekseev Date: Mon, 22 Aug 2022 15:10:57 +0300 Subject: [PATCH v1] ALTER TABLE ... SET STORAGE default Add support of 'ALTER TABLE .. SET STORAGE default' syntax similarly to 'SET COMPRESSION default', for consistency. Also add corresponding tab completion to psql. Author: Aleksander Alekseev Reviewed-by: TODO FIXME Discussion: TODO FIXME --- doc/src/sgml/ref/alter_foreign_table.sgml | 2 +- doc/src/sgml/ref/alter_materialized_view.sgml | 2 +- doc/src/sgml/ref/alter_table.sgml | 13 ++++++++----- src/backend/commands/tablecmds.c | 4 +++- src/backend/parser/gram.y | 1 + src/bin/psql/tab-complete.c | 2 +- src/test/regress/expected/alter_table.out | 2 +- src/test/regress/sql/alter_table.sql | 2 +- 8 files changed, 17 insertions(+), 11 deletions(-) diff --git a/doc/src/sgml/ref/alter_foreign_table.sgml b/doc/src/sgml/ref/alter_foreign_table.sgml index 7ca03f3ac9..84c298e7a5 100644 --- a/doc/src/sgml/ref/alter_foreign_table.sgml +++ b/doc/src/sgml/ref/alter_foreign_table.sgml @@ -41,7 +41,7 @@ ALTER FOREIGN TABLE [ IF EXISTS ] namecolumn_name SET STATISTICS integer ALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] ) ALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] ) - ALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN } + ALTER [ COLUMN ] column_name SET STORAGE { DEFAULT | PLAIN | EXTERNAL | EXTENDED | MAIN } ALTER [ COLUMN ] column_name OPTIONS ( [ ADD | SET | DROP ] option ['value'] [, ... ]) ADD table_constraint [ NOT VALID ] VALIDATE CONSTRAINT constraint_name diff --git a/doc/src/sgml/ref/alter_materialized_view.sgml b/doc/src/sgml/ref/alter_materialized_view.sgml index cae135c27a..ecb8f68db5 100644 --- a/doc/src/sgml/ref/alter_materialized_view.sgml +++ b/doc/src/sgml/ref/alter_materialized_view.sgml @@ -39,7 +39,7 @@ ALTER MATERIALIZED VIEW ALL IN TABLESPACE namecolumn_name SET STATISTICS integer ALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] ) ALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] ) - ALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN } + ALTER [ COLUMN ] column_name SET STORAGE { DEFAULT | PLAIN | EXTERNAL | EXTENDED | MAIN } ALTER [ COLUMN ] column_name SET COMPRESSION compression_method CLUSTER ON index_name SET WITHOUT CLUSTER diff --git a/doc/src/sgml/ref/alter_table.sgml b/doc/src/sgml/ref/alter_table.sgml index f0f912a56c..99a23f40e3 100644 --- a/doc/src/sgml/ref/alter_table.sgml +++ b/doc/src/sgml/ref/alter_table.sgml @@ -53,7 +53,7 @@ ALTER TABLE [ IF EXISTS ] name ALTER [ COLUMN ] column_name SET STATISTICS integer ALTER [ COLUMN ] column_name SET ( attribute_option = value [, ... ] ) ALTER [ COLUMN ] column_name RESET ( attribute_option [, ... ] ) - ALTER [ COLUMN ] column_name SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN } + ALTER [ COLUMN ] column_name SET STORAGE { DEFAULT | PLAIN | EXTERNAL | EXTENDED | MAIN } ALTER [ COLUMN ] column_name SET COMPRESSION compression_method ADD table_constraint [ NOT VALID ] ADD table_constraint_using_index @@ -367,7 +367,7 @@ WITH ( MODULUS numeric_literal, REM - SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN } + SET STORAGE { DEFAULT | PLAIN | EXTERNAL | EXTENDED | MAIN } TOAST per-column storage settings @@ -387,9 +387,12 @@ WITH ( MODULUS numeric_literal, REM data types that support non-PLAIN storage. Use of EXTERNAL will make substring operations on very large text and bytea values run faster, - at the penalty of increased storage space. Note that - SET STORAGE 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. + Choosing DEFAULT resets the storage mode to the default + one for a given type. + Note that SET STORAGE doesn't itself change anything + in the table, it just sets the strategy to be pursued during future table + updates. See for more information. diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 9be04c8a1e..ccbe5cef12 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -19289,7 +19289,9 @@ GetAttributeStorage(Oid atttypid, const char *storagemode) { char cstorage = 0; - if (pg_strcasecmp(storagemode, "plain") == 0) + if (pg_strcasecmp(storagemode, "default") == 0) + cstorage = get_typstorage(atttypid); + else if (pg_strcasecmp(storagemode, "plain") == 0) cstorage = TYPSTORAGE_PLAIN; else if (pg_strcasecmp(storagemode, "external") == 0) cstorage = TYPSTORAGE_EXTERNAL; diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index f9037761f9..f1cdf9e1ae 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -3872,6 +3872,7 @@ opt_column_compression: column_storage: STORAGE ColId { $$ = $2; } + | STORAGE DEFAULT { $$ = pstrdup("default"); } ; opt_column_storage: diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index 62a39779b9..4c15348d87 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -2388,7 +2388,7 @@ psql_completion(const char *text, int start, int end) /* ALTER TABLE ALTER [COLUMN] 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] SET STATISTICS */ else if (Matches("ALTER", "TABLE", MatchAny, "ALTER", "COLUMN", MatchAny, "SET", "STATISTICS") || Matches("ALTER", "TABLE", MatchAny, "ALTER", MatchAny, "SET", "STATISTICS")) diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out index d63f4f1cba..b9dd835df8 100644 --- a/src/test/regress/expected/alter_table.out +++ b/src/test/regress/expected/alter_table.out @@ -2247,7 +2247,7 @@ ERROR: composite type recur1 cannot be made a member of itself create table test_storage (a text, c text storage plain); alter table test_storage alter a set storage plain; alter table test_storage add b int default 0; -- rewrite table to remove its TOAST table -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; diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql index e7013f5e15..c29718327e 100644 --- a/src/test/regress/sql/alter_table.sql +++ b/src/test/regress/sql/alter_table.sql @@ -1530,7 +1530,7 @@ alter table recur1 alter column f2 type recur2; -- fails create table test_storage (a text, c text storage plain); alter table test_storage alter a set storage plain; alter table test_storage add b int default 0; -- rewrite table to remove its TOAST table -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 -- 2.37.2