From ca5ca11d4dba6c29d34c46861f206b943983b18b Mon Sep 17 00:00:00 2001 From: Masahiko Sawada Date: Mon, 3 Apr 2017 20:30:00 +0900 Subject: [PATCH] Specify the storage option for TOAST table when create table. The storage option such as AUTOVACUUM_VACUUM_SCALE_FACTOR can be set to both heap table and TOAST table. But the storage parameter for TOAST table had gone after repacked. This change create new function get_storage_param which returns all storage paramters including for TOAST table and OID setting. Issue #10. --- lib/pg_repack.sql.in | 37 ++++++++++++++++++++++++++++++++++++- regress/expected/repack.out | 21 +++++++++++++++++++++ regress/sql/repack.sql | 14 ++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/lib/pg_repack.sql.in b/lib/pg_repack.sql.in index d294548..ef7d160 100644 --- a/lib/pg_repack.sql.in +++ b/lib/pg_repack.sql.in @@ -152,6 +152,41 @@ WHERE $$ LANGUAGE sql STABLE STRICT; +-- Get a comma-separated storage paramter for the table including +-- paramters for the corresponding TOAST table. +-- Note that since oid setting is always not NULL, this function +-- never returns NULL +CREATE FUNCTION repack.get_storage_param(oid) + RETURNS TEXT AS +$$ +SELECT array_to_string(array_agg(param), ', ') +FROM ( + -- table storage parameter + SELECT unnest(reloptions) as param + FROM pg_class + WHERE oid = $1 + UNION ALL + -- TOAST table storage parameter + SELECT ('toast.' || unnest(reloptions)) as param + FROM ( + SELECT reltoastrelid from pg_class where oid = $1 + ) as t, + pg_class as c + WHERE c.oid = t.reltoastrelid + UNION ALL + -- table oid + SELECT 'oids = ' || + CASE WHEN relhasoids + THEN 'true' + ELSE 'false' + END + FROM pg_class + WHERE oid = $1 + + ) as t +$$ +LANGUAGE sql STABLE STRICT; + -- includes not only PRIMARY KEYS but also UNIQUE NOT NULL keys CREATE VIEW repack.primary_keys AS SELECT indrelid, (repack.array_accum(indexrelid))[1] AS indexrelid @@ -183,7 +218,7 @@ CREATE VIEW repack.tables AS 'CREATE TABLE repack.log_' || R.oid || ' (id bigserial PRIMARY KEY, pk repack.pk_' || R.oid || ', row ' || repack.oid2text(R.oid) || ')' AS create_log, repack.get_create_trigger(R.oid, PK.indexrelid) AS create_trigger, repack.get_enable_trigger(R.oid) as enable_trigger, - 'CREATE TABLE repack.table_' || R.oid || ' WITH (' || array_to_string(array_append(R.reloptions, 'oids=' || CASE WHEN R.relhasoids THEN 'true' ELSE 'false' END), ',') || ') TABLESPACE ' AS create_table_1, + 'CREATE TABLE repack.table_' || R.oid || ' WITH (' || repack.get_storage_param(R.oid) || ') TABLESPACE ' AS create_table_1, coalesce(quote_ident(S.spcname), 'pg_default') as tablespace_orig, ' AS SELECT ' || repack.get_columns_for_create_as(R.oid) || ' FROM ONLY ' || repack.oid2text(R.oid) AS create_table_2, repack.get_drop_columns(R.oid, 'repack.table_' || R.oid) AS drop_columns, diff --git a/regress/expected/repack.out b/regress/expected/repack.out index ef0ed4a..f5d4ac2 100644 --- a/regress/expected/repack.out +++ b/regress/expected/repack.out @@ -58,6 +58,12 @@ CREATE INDEX idxopts_t ON tbl_idxopts (t DESC NULLS LAST) WHERE (t != 'aaa'); -- Use this table to play with attribute options too ALTER TABLE tbl_idxopts ALTER i SET STATISTICS 1; ALTER TABLE tbl_idxopts ALTER t SET (n_distinct = -0.5); +CREATE TABLE tbl_with_toast ( + i integer PRIMARY KEY, + c text +); +ALTER TABLE tbl_with_toast SET (AUTOVACUUM_VACUUM_SCALE_FACTOR = 30, AUTOVACUUM_VACUUM_THRESHOLD = 300); +ALTER TABLE tbl_with_toast SET (TOAST.AUTOVACUUM_VACUUM_SCALE_FACTOR = 40, TOAST.AUTOVACUUM_VACUUM_THRESHOLD = 400); -- -- insert data -- @@ -131,6 +137,7 @@ INFO: repacking table "tbl_with_dropped_toast" INFO: repacking table "tbl_badindex" WARNING: skipping invalid index: CREATE UNIQUE INDEX idx_badindex_n ON tbl_badindex USING btree (n) INFO: repacking table "tbl_idxopts" +INFO: repacking table "tbl_with_toast" -- -- after -- @@ -285,6 +292,20 @@ SELECT * FROM tbl_with_dropped_toast; RESET enable_seqscan; RESET enable_indexscan; +-- check if storage option for both table and TOAST table didn't go away. +SELECT CASE relkind + WHEN 'r' THEN relname + WHEN 't' THEN 'toast_table' + END as table, + reloptions +FROM pg_class +WHERE relname = 'tbl_with_toast' OR relname = 'pg_toast_' || 'tbl_with_toast'::regclass::oid; + table | reloptions +----------------+--------------------------------------------------------------------- + toast_table | {autovacuum_vacuum_scale_factor=40,autovacuum_vacuum_threshold=400} + tbl_with_toast | {autovacuum_vacuum_scale_factor=30,autovacuum_vacuum_threshold=300} +(2 rows) + -- -- check broken links or orphan toast relations -- diff --git a/regress/sql/repack.sql b/regress/sql/repack.sql index 58b358f..fa2ef8a 100644 --- a/regress/sql/repack.sql +++ b/regress/sql/repack.sql @@ -69,6 +69,12 @@ CREATE INDEX idxopts_t ON tbl_idxopts (t DESC NULLS LAST) WHERE (t != 'aaa'); -- Use this table to play with attribute options too ALTER TABLE tbl_idxopts ALTER i SET STATISTICS 1; ALTER TABLE tbl_idxopts ALTER t SET (n_distinct = -0.5); +CREATE TABLE tbl_with_toast ( + i integer PRIMARY KEY, + c text +); +ALTER TABLE tbl_with_toast SET (AUTOVACUUM_VACUUM_SCALE_FACTOR = 30, AUTOVACUUM_VACUUM_THRESHOLD = 300); +ALTER TABLE tbl_with_toast SET (TOAST.AUTOVACUUM_VACUUM_SCALE_FACTOR = 40, TOAST.AUTOVACUUM_VACUUM_THRESHOLD = 400); -- -- insert data @@ -157,6 +163,14 @@ SELECT * FROM view_for_dropped_column; SELECT * FROM tbl_with_dropped_toast; RESET enable_seqscan; RESET enable_indexscan; +-- check if storage option for both table and TOAST table didn't go away. +SELECT CASE relkind + WHEN 'r' THEN relname + WHEN 't' THEN 'toast_table' + END as table, + reloptions +FROM pg_class +WHERE relname = 'tbl_with_toast' OR relname = 'pg_toast_' || 'tbl_with_toast'::regclass::oid; -- -- check broken links or orphan toast relations