Skip to content

Commit

Permalink
[#23163] YSQL: pg_partman: make 'apply_publications' idempotent
Browse files Browse the repository at this point in the history
Summary:
Currently transactional DDL is not supported in YugabyteDB . Due to which if any procedure or function which is performing multiple DDLs in a
transactional context can lead to issue of some DDLs getting executed and committed even if stored procedure failed due to some exception
while running stored procedure or Postgres backend process or tserver process kill.

This diff makes apply_publications.sql function idempotent such that multiple calls to don't have any additional consequences.

 - This function is used to add child tables to publication
 - Changes done
   - Added a check to not invoke ALTER PUBLICATION <publication> ADD table <child_table> if child_table is already part of the publication.
Jira: DB-12101

Test Plan: jenkins: compile only

Reviewers: skumar, jason, hsunder

Reviewed By: jason

Subscribers: yql

Differential Revision: https://phorge.dev.yugabyte.com/D36677
  • Loading branch information
Devansh Saxena committed Jul 23, 2024
1 parent 9e046fb commit 2248dcd
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ DECLARE
v_publications text[];
v_row record;
v_sql text;
yb_v_table_exists boolean;
BEGIN
/*
* Function to ATLER PUBLICATION ... ADD TABLE to support logical replication
Expand All @@ -18,9 +19,23 @@ WHERE c.parent_table = p_parent_table;
FOR v_row IN
SELECT pubname FROM unnest(v_publications) AS pubname
LOOP
v_sql = format('ALTER PUBLICATION %I ADD TABLE %I.%I', v_row.pubname, p_child_schema, p_child_tablename);
RAISE DEBUG '%', v_sql;
EXECUTE v_sql;

SELECT EXISTS (
SELECT 1
FROM pg_publication_rel pr
JOIN pg_class c ON pr.prrelid = c.oid
JOIN pg_namespace n ON c.relnamespace = n.oid
JOIN pg_publication p ON pr.prpubid = p.oid
WHERE c.relname = p_child_tablename
AND n.nspname = p_child_schema
AND p.pubname = v_row.pubname
) INTO yb_v_table_exists;

IF NOT yb_v_table_exists THEN
v_sql = format('ALTER PUBLICATION %I ADD TABLE %I.%I', v_row.pubname, p_child_schema, p_child_tablename);
RAISE DEBUG '%', v_sql;
EXECUTE v_sql;
END IF;
END LOOP;

END;
Expand Down

0 comments on commit 2248dcd

Please sign in to comment.