Skip to content

Commit 2052caf

Browse files
committed
v5.2.3 fix replica indentity inheritance when using indexes
1 parent ec78f2e commit 2052caf

5 files changed

+113
-9
lines changed

CHANGELOG.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
5.2.3
2+
=====
3+
BUG FIXES
4+
---------
5+
- Fixed replica identity inheritance not working when the USING INDEX clause is used to set the replica identity of the parent table. (Github Issue #721)
6+
7+
18
5.2.2
29
=====
310
BUG FIXES
411
---------
5-
- Corrected SQL statements and updated functions missing from the 5.1.0 to 5.2.0 update files. If pg_partman was installed initially with 5.2.0, there are no known issues. If errors about missing functions or columns are encountered during maintenance after updating to 5.2.x, please make sure you are on this latest release. Thank you to all the users helping to test this issues!
12+
- Corrected SQL statements and updated functions missing from the 5.1.0 to 5.2.0 update files. If pg_partman was installed initially with 5.2.0, there are no known issues. If errors about missing functions or columns are encountered during maintenance after updating to 5.2.x, please make sure you are on this latest release. Thank you to all the users helping to test this issues! (Github Issue #718)
613

714

815
5.2.1
916
=====
1017
BUG FIXES
1118
---------
12-
- Corrected a syntax error in the 5.1.0 to 5.2.0 update file. There are no problems with a direct install of 5.2.0 and there is no technical need to update to 5.2.1. The issue was encountered only when updating from any prior version to 5.2.0. New release was tagged to ease rolling out the fix for package managers.
19+
- Corrected a syntax error in the 5.1.0 to 5.2.0 update file. There are no problems with a direct install of 5.2.0 and there is no technical need to update to 5.2.1. The issue was encountered only when updating from any prior version to 5.2.0. New release was tagged to ease rolling out the fix for package managers. (Github Issue #711)
1320

1421

1522
5.2.0

META.json

+3-3
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.2.2",
4+
"version": "5.2.3",
55
"maintainer": [
66
"Keith Fiske <[email protected]>"
77
],
@@ -20,9 +20,9 @@
2020
},
2121
"provides": {
2222
"pg_partman": {
23-
"file": "sql/pg_partman--5.2.2.sql",
23+
"file": "sql/pg_partman--5.2.3.sql",
2424
"docfile": "doc/pg_partman.md",
25-
"version": "5.2.2",
25+
"version": "5.2.3",
2626
"abstract": "Extension to manage partitioned tables by time or ID"
2727
}
2828
},

pg_partman.control

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
default_version = '5.2.2'
1+
default_version = '5.2.3'
22
comment = 'Extension to manage partitioned tables by time or ID'
33
relocatable = false
44
superuser = false

sql/functions/inherit_replica_identity.sql

+23-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ CREATE FUNCTION @[email protected]_replica_identity (p_parent_schemaname text,
33
AS $$
44
DECLARE
55

6+
v_child_partition_index text;
7+
v_child_partition_oid oid;
68
v_parent_oid oid;
79
v_parent_replident char;
810
v_parent_replident_index name;
@@ -12,7 +14,7 @@ v_sql text;
1214
BEGIN
1315

1416
/*
15-
* Set the given child table's replica idenitity to the same as the parent
17+
* Set the given child table's replica identity to the same as the parent
1618
NOTE: Replication identity not automatically inherited as of PG16 (revisit in future versions)
1719
*/
1820

@@ -26,20 +28,38 @@ WHERE n.nspname = p_parent_schemaname
2628
AND c.relname = p_parent_tablename;
2729

2830
IF v_parent_replident = 'i' THEN
31+
2932
SELECT c.relname
3033
INTO v_parent_replident_index
3134
FROM pg_catalog.pg_class c
3235
JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
3336
WHERE i.indrelid = v_parent_oid
3437
AND indisreplident;
38+
39+
SELECT c.oid
40+
INTO v_child_partition_oid
41+
FROM pg_catalog.pg_class c
42+
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
43+
WHERE n.nspname = p_parent_schemaname
44+
AND c.relname = p_child_tablename;
45+
46+
SELECT partition_index.indexrelid::regclass::text
47+
INTO v_child_partition_index
48+
FROM pg_index parent_index -- parent index
49+
INNER JOIN pg_inherits index_inheritance ON (index_inheritance.inhparent=parent_index.indexrelid) -- parent partition index
50+
INNER JOIN pg_index partition_index ON (index_inheritance.inhrelid=partition_index.indexrelid) -- connection between parent and partition indexes
51+
INNER JOIN pg_class partition_table ON (partition_table.oid=partition_index.indrelid) -- connection between child table and child index
52+
WHERE partition_table.oid=v_child_partition_oid -- child partition table
53+
AND parent_index.indexrelid=v_parent_replident_index::regclass; -- parent partition index
54+
3555
END IF;
3656

37-
RAISE DEBUG 'inherit_replica_ident: v_parent_oid: %, v_parent_replident: %, v_parent_replident_index: %', v_parent_oid, v_parent_replident, v_parent_replident_index;
57+
RAISE DEBUG 'inherit_replica_ident: v_parent_oid: %, v_parent_replident: %, v_parent_replident_index: %, v_child_partition_oid: %, v_child_partition_index: %', v_parent_oid, v_parent_replident, v_parent_replident_index, v_child_partition_oid, v_child_partition_index;
3858

3959
IF v_parent_replident != 'd' THEN
4060
CASE v_parent_replident
4161
WHEN 'f' THEN v_replident_string := 'FULL';
42-
WHEN 'i' THEN v_replident_string := format('USING INDEX %I', v_parent_replident_index);
62+
WHEN 'i' THEN v_replident_string := format('USING INDEX %I', v_child_partition_index);
4363
WHEN 'n' THEN v_replident_string := 'NOTHING';
4464
ELSE
4565
RAISE EXCEPTION 'inherit_replica_identity: Unknown replication identity encountered (%). Please report as a bug on pg_partman''s github', v_parent_replident;

updates/pg_partman--5.2.2--5.2.3.sql

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
2+
CREATE OR REPLACE FUNCTION @[email protected]_replica_identity (p_parent_schemaname text, p_parent_tablename text, p_child_tablename text) RETURNS void
3+
LANGUAGE plpgsql
4+
AS $$
5+
DECLARE
6+
7+
v_child_partition_index text;
8+
v_child_partition_oid oid;
9+
v_parent_oid oid;
10+
v_parent_replident char;
11+
v_parent_replident_index name;
12+
v_replident_string text;
13+
v_sql text;
14+
15+
BEGIN
16+
17+
/*
18+
* Set the given child table's replica identity to the same as the parent
19+
NOTE: Replication identity not automatically inherited as of PG16 (revisit in future versions)
20+
*/
21+
22+
SELECT c.oid
23+
, c.relreplident
24+
INTO v_parent_oid
25+
, v_parent_replident
26+
FROM pg_catalog.pg_class c
27+
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
28+
WHERE n.nspname = p_parent_schemaname
29+
AND c.relname = p_parent_tablename;
30+
31+
IF v_parent_replident = 'i' THEN
32+
33+
SELECT c.relname
34+
INTO v_parent_replident_index
35+
FROM pg_catalog.pg_class c
36+
JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
37+
WHERE i.indrelid = v_parent_oid
38+
AND indisreplident;
39+
40+
SELECT c.oid
41+
INTO v_child_partition_oid
42+
FROM pg_catalog.pg_class c
43+
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
44+
WHERE n.nspname = p_parent_schemaname
45+
AND c.relname = p_child_tablename;
46+
47+
SELECT partition_index.indexrelid::regclass::text
48+
INTO v_child_partition_index
49+
FROM pg_index parent_index -- parent index
50+
INNER JOIN pg_inherits index_inheritance ON (index_inheritance.inhparent=parent_index.indexrelid) -- parent partition index
51+
INNER JOIN pg_index partition_index ON (index_inheritance.inhrelid=partition_index.indexrelid) -- connection between parent and partition indexes
52+
INNER JOIN pg_class partition_table ON (partition_table.oid=partition_index.indrelid) -- connection between child table and child index
53+
WHERE partition_table.oid=v_child_partition_oid -- child partition table
54+
AND parent_index.indexrelid=v_parent_replident_index::regclass; -- parent partition index
55+
56+
END IF;
57+
58+
RAISE DEBUG 'inherit_replica_ident: v_parent_oid: %, v_parent_replident: %, v_parent_replident_index: %, v_child_partition_oid: %, v_child_partition_index: %', v_parent_oid, v_parent_replident, v_parent_replident_index, v_child_partition_oid, v_child_partition_index;
59+
60+
IF v_parent_replident != 'd' THEN
61+
CASE v_parent_replident
62+
WHEN 'f' THEN v_replident_string := 'FULL';
63+
WHEN 'i' THEN v_replident_string := format('USING INDEX %I', v_child_partition_index);
64+
WHEN 'n' THEN v_replident_string := 'NOTHING';
65+
ELSE
66+
RAISE EXCEPTION 'inherit_replica_identity: Unknown replication identity encountered (%). Please report as a bug on pg_partman''s github', v_parent_replident;
67+
END CASE;
68+
v_sql := format('ALTER TABLE %I.%I REPLICA IDENTITY %s'
69+
, p_parent_schemaname
70+
, p_child_tablename
71+
, v_replident_string);
72+
RAISE DEBUG 'inherit_replica_identity: replident v_sql: %', v_sql;
73+
EXECUTE v_sql;
74+
END IF;
75+
76+
END
77+
$$;

0 commit comments

Comments
 (0)