Skip to content

Commit a12b23e

Browse files
authored
fix: allow toast relopt inheritance from template (#850)
* fix: allow toast relopt inheritance from template * update source sql file * fix wrong variable name. add unit test for relopt inheritance * update test count for new tests * adjust relopt test per suggestion
1 parent eded220 commit a12b23e

6 files changed

Lines changed: 765 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
5.4.3
2+
=====
3+
NEW FEATURES
4+
------------
5+
- Inherit the toast table relation options from the template table
6+
7+
BUGFIXES
8+
--------
9+
- Version 5.4.2 did not include the proper pg_partman version in the extension control file. If you already had version 5.4.1 installed, then it would not update properly to version 5.4.2. The changes for version 5.4.2 have also been included in the update file for 5.4.3 to ensure they are properly applied in this case. If you installed version 5.4.2 from scratch, PostgreSQL would report that version 5.4.1 was installed even though the extension code for 5.4.2 was properly installed. Installing version 5.4.3 should resolve all issues around this bug.
10+
11+
112
5.4.2
213
=====
314
BUGFIXES

META.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "pg_partman",
33
"abstract": "Extension to manage partitioned tables by time or ID",
4-
"version": "5.4.2",
4+
"version": "5.4.3",
55
"maintainer": [
66
"Keith Fiske <keith@keithf4.com>"
77
],
@@ -20,9 +20,9 @@
2020
},
2121
"provides": {
2222
"pg_partman": {
23-
"file": "sql/pg_partman--5.4.2.sql",
23+
"file": "sql/pg_partman--5.4.3.sql",
2424
"docfile": "doc/pg_partman.md",
25-
"version": "5.4.2",
25+
"version": "5.4.3",
2626
"abstract": "Extension to manage partitioned tables by time or ID"
2727
}
2828
},

pg_partman.control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
default_version = '5.4.1'
1+
default_version = '5.4.3'
22
comment = 'Extension to manage partitioned tables by time or ID'
33
relocatable = false
44
superuser = false

sql/functions/inherit_template_properties.sql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ v_template_schemaname text;
2525
v_template_table text;
2626
v_template_tablename name;
2727
v_template_unlogged char;
28+
v_toast_table_oid oid;
2829

2930
BEGIN
3031
/*
@@ -212,6 +213,26 @@ LOOP
212213
RAISE DEBUG 'inherit_template_properties: Set relopts: %', v_sql;
213214
EXECUTE v_sql;
214215
END LOOP;
216+
217+
-- Get toast table options
218+
SELECT reltoastrelid INTO v_toast_table_oid
219+
FROM pg_catalog.pg_class c
220+
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
221+
WHERE c.oid = v_template_oid;
222+
223+
FOR v_relopt IN
224+
SELECT unnest(reloptions) as value
225+
FROM pg_catalog.pg_class
226+
WHERE oid = v_toast_table_oid
227+
LOOP
228+
v_sql := format('ALTER TABLE %I.%I SET (toast.%s)'
229+
, v_child_schema
230+
, v_child_tablename
231+
, v_relopt.value);
232+
RAISE DEBUG 'inherit_template_properties: Set toast relopts: %', v_sql;
233+
EXECUTE v_sql;
234+
END LOOP;
235+
215236
RETURN true;
216237

217238
END

test/test-id-gap-fill.sql

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
-- backfill gap in child tables, start with higher number
44
-- pre-created template table with indexes
55
-- old create_parent() alias
6+
-- Test relopt inheritance (regular table and toast)
67

78
\set ON_ERROR_ROLLBACK 1
89
\set ON_ERROR_STOP true
910

1011
BEGIN;
1112
SELECT set_config('search_path','partman, public',false);
1213

13-
SELECT plan(41);
14+
SELECT plan(47);
1415
CREATE SCHEMA partman_test;
1516

1617
CREATE TABLE partman_test.id_taptest_table (
@@ -24,6 +25,9 @@ CREATE TABLE partman_test.template_id_taptest_table (LIKE partman_test.id_taptes
2425
ALTER TABLE partman_test.id_taptest_table ADD PRIMARY KEY (col1);
2526
CREATE INDEX ON partman_test.id_taptest_table (col3);
2627

28+
ALTER TABLE partman_test.template_id_taptest_table SET (fillfactor = 75);
29+
ALTER TABLE partman_test.template_id_taptest_table SET (toast.autovacuum_vacuum_threshold = 120);
30+
2731
-- Always create the index on the template also so that we can test excluding duplicates.
2832
CREATE INDEX ON partman_test.template_id_taptest_table (col3);
2933

@@ -54,6 +58,61 @@ SELECT is_empty('SELECT * FROM ONLY partman_test.id_taptest_table', 'Check that
5458
SELECT results_eq('SELECT count(*)::int FROM partman_test.id_taptest_table', ARRAY[9], 'Check count from parent table');
5559
SELECT results_eq('SELECT count(*)::int FROM partman_test.id_taptest_table_p3000000000', ARRAY[9], 'Check count from id_taptest_table_p3000000000');
5660

61+
-- Check reloptions set for child table
62+
SELECT results_eq('WITH relopt AS (
63+
SELECT unnest(reloptions) AS relopt FROM pg_class c
64+
JOIN pg_namespace n ON c.relnamespace = n.oid
65+
WHERE c.relname = ''id_taptest_table_p3000000000''
66+
AND n.nspname = ''partman_test''
67+
)
68+
SELECT count(*)::int from relopt WHERE relopt = ''fillfactor=75'''
69+
70+
, ARRAY[1]
71+
, 'Check reloptions for id_taptest_table_p3000000000 child table'
72+
);
73+
SELECT results_eq('WITH relopt AS (
74+
SELECT unnest(reloptions) AS relopt FROM pg_class c
75+
JOIN pg_namespace n ON c.relnamespace = n.oid
76+
WHERE c.relname = ''id_taptest_table_p3000000030''
77+
AND n.nspname = ''partman_test''
78+
)
79+
SELECT count(*)::int from relopt WHERE relopt = ''fillfactor=75'''
80+
81+
, ARRAY[1]
82+
, 'Check reloptions for id_taptest_table_p3000000030 child table'
83+
);
84+
85+
-- Check reloptions set for toast
86+
87+
SELECT results_eq('WITH child_toast AS (
88+
SELECT reltoastrelid
89+
FROM pg_class c1
90+
JOIN pg_namespace n ON c1.relnamespace = n.oid
91+
WHERE c1.relname = ''id_taptest_table_p3000000000''
92+
AND n.nspname = ''partman_test''
93+
), toast_relopt AS (
94+
SELECT unnest(reloptions) AS relopt FROM pg_class c
95+
JOIN child_toast ct ON c.oid = ct.reltoastrelid
96+
)
97+
SELECT count(*)::int from toast_relopt WHERE relopt = ''autovacuum_vacuum_threshold=120'''
98+
, ARRAY[1]
99+
, 'Check reloptions for id_taptest_table_p3000000000 toast table'
100+
);
101+
SELECT results_eq('WITH child_toast AS (
102+
SELECT reltoastrelid
103+
FROM pg_class c1
104+
JOIN pg_namespace n ON c1.relnamespace = n.oid
105+
WHERE c1.relname = ''id_taptest_table_p3000000040''
106+
AND n.nspname = ''partman_test''
107+
), toast_relopt AS (
108+
SELECT unnest(reloptions) AS relopt FROM pg_class c
109+
JOIN child_toast ct ON c.oid = ct.reltoastrelid
110+
)
111+
SELECT count(*)::int from toast_relopt WHERE relopt = ''autovacuum_vacuum_threshold=120'''
112+
, ARRAY[1]
113+
, 'Check reloptions for id_taptest_table_p3000000040 toast table'
114+
);
115+
57116
SELECT run_maintenance();
58117
INSERT INTO partman_test.id_taptest_table (col1, col4) VALUES (generate_series(3000000010,3000000025), 'stuff'||generate_series(3000000010,3000000025));
59118
-- Run again to make new partition based on latest data
@@ -81,6 +140,32 @@ SELECT hasnt_table('partman_test', 'id_taptest_table_p3000000080', 'Check id_tap
81140
SELECT col_is_pk('partman_test', 'id_taptest_table_p3000000060', ARRAY['col1'], 'Check for primary key in id_taptest_table_p3000000060');
82141
SELECT col_is_pk('partman_test', 'id_taptest_table_p3000000070', ARRAY['col1'], 'Check for primary key in id_taptest_table_p3000000070');
83142

143+
SELECT results_eq('WITH relopt AS (
144+
SELECT unnest(reloptions) AS relopt FROM pg_class c
145+
JOIN pg_namespace n ON c.relnamespace = n.oid
146+
WHERE c.relname = ''id_taptest_table_p3000000070''
147+
AND n.nspname = ''partman_test''
148+
)
149+
SELECT count(*)::int from relopt WHERE relopt = ''fillfactor=75'''
150+
151+
, ARRAY[1]
152+
, 'Check reloptions for id_taptest_table_p3000000070 child table'
153+
);
154+
SELECT results_eq('WITH child_toast AS (
155+
SELECT reltoastrelid
156+
FROM pg_class c1
157+
JOIN pg_namespace n ON c1.relnamespace = n.oid
158+
WHERE c1.relname = ''id_taptest_table_p3000000070''
159+
AND n.nspname = ''partman_test''
160+
), toast_relopt AS (
161+
SELECT unnest(reloptions) AS relopt FROM pg_class c
162+
JOIN child_toast ct ON c.oid = ct.reltoastrelid
163+
)
164+
SELECT count(*)::int from toast_relopt WHERE relopt = ''autovacuum_vacuum_threshold=120'''
165+
, ARRAY[1]
166+
, 'Check reloptions for id_taptest_table_p3000000070 toast table'
167+
);
168+
84169
DROP TABLE partman_test.id_taptest_table_p3000000020;
85170
DROP TABLE partman_test.id_taptest_table_p3000000040;
86171
DROP TABLE partman_test.id_taptest_table_p3000000050;

0 commit comments

Comments
 (0)