Skip to content

Commit

Permalink
v5.2.3 fix replica indentity inheritance when using indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
keithf4 committed Dec 19, 2024
1 parent ec78f2e commit 2052caf
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 9 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
5.2.3
=====
BUG FIXES
---------
- 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)


5.2.2
=====
BUG FIXES
---------
- 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!
- 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)


5.2.1
=====
BUG FIXES
---------
- 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.
- 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)


5.2.0
Expand Down
6 changes: 3 additions & 3 deletions META.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pg_partman",
"abstract": "Extension to manage partitioned tables by time or ID",
"version": "5.2.2",
"version": "5.2.3",
"maintainer": [
"Keith Fiske <[email protected]>"
],
Expand All @@ -20,9 +20,9 @@
},
"provides": {
"pg_partman": {
"file": "sql/pg_partman--5.2.2.sql",
"file": "sql/pg_partman--5.2.3.sql",
"docfile": "doc/pg_partman.md",
"version": "5.2.2",
"version": "5.2.3",
"abstract": "Extension to manage partitioned tables by time or ID"
}
},
Expand Down
2 changes: 1 addition & 1 deletion pg_partman.control
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
default_version = '5.2.2'
default_version = '5.2.3'
comment = 'Extension to manage partitioned tables by time or ID'
relocatable = false
superuser = false
26 changes: 23 additions & 3 deletions sql/functions/inherit_replica_identity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ CREATE FUNCTION @[email protected]_replica_identity (p_parent_schemaname text,
AS $$
DECLARE

v_child_partition_index text;
v_child_partition_oid oid;
v_parent_oid oid;
v_parent_replident char;
v_parent_replident_index name;
Expand All @@ -12,7 +14,7 @@ v_sql text;
BEGIN

/*
* Set the given child table's replica idenitity to the same as the parent
* Set the given child table's replica identity to the same as the parent
NOTE: Replication identity not automatically inherited as of PG16 (revisit in future versions)
*/

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

IF v_parent_replident = 'i' THEN

SELECT c.relname
INTO v_parent_replident_index
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
WHERE i.indrelid = v_parent_oid
AND indisreplident;

SELECT c.oid
INTO v_child_partition_oid
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE n.nspname = p_parent_schemaname
AND c.relname = p_child_tablename;

SELECT partition_index.indexrelid::regclass::text
INTO v_child_partition_index
FROM pg_index parent_index -- parent index
INNER JOIN pg_inherits index_inheritance ON (index_inheritance.inhparent=parent_index.indexrelid) -- parent partition index
INNER JOIN pg_index partition_index ON (index_inheritance.inhrelid=partition_index.indexrelid) -- connection between parent and partition indexes
INNER JOIN pg_class partition_table ON (partition_table.oid=partition_index.indrelid) -- connection between child table and child index
WHERE partition_table.oid=v_child_partition_oid -- child partition table
AND parent_index.indexrelid=v_parent_replident_index::regclass; -- parent partition index

END IF;

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;
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;

IF v_parent_replident != 'd' THEN
CASE v_parent_replident
WHEN 'f' THEN v_replident_string := 'FULL';
WHEN 'i' THEN v_replident_string := format('USING INDEX %I', v_parent_replident_index);
WHEN 'i' THEN v_replident_string := format('USING INDEX %I', v_child_partition_index);
WHEN 'n' THEN v_replident_string := 'NOTHING';
ELSE
RAISE EXCEPTION 'inherit_replica_identity: Unknown replication identity encountered (%). Please report as a bug on pg_partman''s github', v_parent_replident;
Expand Down
77 changes: 77 additions & 0 deletions updates/pg_partman--5.2.2--5.2.3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

CREATE OR REPLACE FUNCTION @[email protected]_replica_identity (p_parent_schemaname text, p_parent_tablename text, p_child_tablename text) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE

v_child_partition_index text;
v_child_partition_oid oid;
v_parent_oid oid;
v_parent_replident char;
v_parent_replident_index name;
v_replident_string text;
v_sql text;

BEGIN

/*
* Set the given child table's replica identity to the same as the parent
NOTE: Replication identity not automatically inherited as of PG16 (revisit in future versions)
*/

SELECT c.oid
, c.relreplident
INTO v_parent_oid
, v_parent_replident
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE n.nspname = p_parent_schemaname
AND c.relname = p_parent_tablename;

IF v_parent_replident = 'i' THEN

SELECT c.relname
INTO v_parent_replident_index
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
WHERE i.indrelid = v_parent_oid
AND indisreplident;

SELECT c.oid
INTO v_child_partition_oid
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid
WHERE n.nspname = p_parent_schemaname
AND c.relname = p_child_tablename;

SELECT partition_index.indexrelid::regclass::text
INTO v_child_partition_index
FROM pg_index parent_index -- parent index
INNER JOIN pg_inherits index_inheritance ON (index_inheritance.inhparent=parent_index.indexrelid) -- parent partition index
INNER JOIN pg_index partition_index ON (index_inheritance.inhrelid=partition_index.indexrelid) -- connection between parent and partition indexes
INNER JOIN pg_class partition_table ON (partition_table.oid=partition_index.indrelid) -- connection between child table and child index
WHERE partition_table.oid=v_child_partition_oid -- child partition table
AND parent_index.indexrelid=v_parent_replident_index::regclass; -- parent partition index

END IF;

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;

IF v_parent_replident != 'd' THEN
CASE v_parent_replident
WHEN 'f' THEN v_replident_string := 'FULL';
WHEN 'i' THEN v_replident_string := format('USING INDEX %I', v_child_partition_index);
WHEN 'n' THEN v_replident_string := 'NOTHING';
ELSE
RAISE EXCEPTION 'inherit_replica_identity: Unknown replication identity encountered (%). Please report as a bug on pg_partman''s github', v_parent_replident;
END CASE;
v_sql := format('ALTER TABLE %I.%I REPLICA IDENTITY %s'
, p_parent_schemaname
, p_child_tablename
, v_replident_string);
RAISE DEBUG 'inherit_replica_identity: replident v_sql: %', v_sql;
EXECUTE v_sql;
END IF;

END
$$;

0 comments on commit 2052caf

Please sign in to comment.