From ecc6def69e0288612d4faf6e0a4700e5e6468b27 Mon Sep 17 00:00:00 2001 From: Doruk Date: Thu, 15 Aug 2024 23:34:26 +0300 Subject: [PATCH] pg_replication_origin_session_setup: pid parameter Since the introduction of parallel apply workers (commit 216a784829c), the replorigin_session_setup() was extended to accept an extra parameter: pid. This process ID is used to inform that multiple processes are sharing the same replication origin to apply changes in parallel. The replorigin_session_setup function has a SQL user interface: pg_replication_origin_session_setup. This commit adds an optional parameter that passes the process ID to the internal function replorigin_session_setup. It allows multiple processes to use the same replication origin if you are using the replication functions. --- doc/src/sgml/func.sgml | 8 +++++++- src/backend/catalog/system_functions.sql | 9 ++++++++- src/backend/replication/logical/origin.c | 4 +++- src/include/catalog/pg_proc.dat | 2 +- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 2488e9ba9982..7abe25deea41 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -29786,7 +29786,7 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset pg_replication_origin_session_setup - pg_replication_origin_session_setup ( node_name text ) + pg_replication_origin_session_setup ( node_name text , pid integer DEFAULT 0 ) void @@ -29794,6 +29794,12 @@ postgres=# SELECT '0/0'::pg_lsn + pd.segment_number * ps.setting::int + :offset origin, allowing replay progress to be tracked. Can only be used if no origin is currently selected. Use pg_replication_origin_session_reset to undo. + If multiple processes can safely use the same replication origin (for + example, parallel apply processes), the optional pid + parameter can be used to specify the process ID of the first process. + The first process must provide pid equals to + 0 and the other processes that share the same + replication origin should provide the process ID of the first process. diff --git a/src/backend/catalog/system_functions.sql b/src/backend/catalog/system_functions.sql index 566f308e4439..f60287dbb668 100644 --- a/src/backend/catalog/system_functions.sql +++ b/src/backend/catalog/system_functions.sql @@ -650,6 +650,13 @@ LANGUAGE INTERNAL CALLED ON NULL INPUT VOLATILE PARALLEL SAFE AS 'pg_stat_reset_slru'; +CREATE OR REPLACE FUNCTION + pg_replication_origin_session_setup(node_name text, pid integer DEFAULT 0) +RETURNS void +LANGUAGE INTERNAL +STRICT VOLATILE +AS 'pg_replication_origin_session_setup'; + -- -- The default permissions for functions mean that anyone can execute them. -- A number of functions shouldn't be executable by just anyone, but rather @@ -751,7 +758,7 @@ REVOKE EXECUTE ON FUNCTION pg_replication_origin_session_progress(boolean) FROM REVOKE EXECUTE ON FUNCTION pg_replication_origin_session_reset() FROM public; -REVOKE EXECUTE ON FUNCTION pg_replication_origin_session_setup(text) FROM public; +REVOKE EXECUTE ON FUNCTION pg_replication_origin_session_setup(text, integer) FROM public; REVOKE EXECUTE ON FUNCTION pg_replication_origin_xact_reset() FROM public; diff --git a/src/backend/replication/logical/origin.c b/src/backend/replication/logical/origin.c index 6583dd497da2..e1122ad8df62 100644 --- a/src/backend/replication/logical/origin.c +++ b/src/backend/replication/logical/origin.c @@ -1351,12 +1351,14 @@ pg_replication_origin_session_setup(PG_FUNCTION_ARGS) { char *name; RepOriginId origin; + int pid; replorigin_check_prerequisites(true, false); name = text_to_cstring((text *) DatumGetPointer(PG_GETARG_DATUM(0))); origin = replorigin_by_name(name, false); - replorigin_session_setup(origin, 0); + pid = PG_GETARG_INT32(1); + replorigin_session_setup(origin, pid); replorigin_session_origin = origin; diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 4962d611f316..aafe0bffc4ca 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -12152,7 +12152,7 @@ { oid => '6006', descr => 'configure session to maintain replication progress tracking for the passed in origin', proname => 'pg_replication_origin_session_setup', provolatile => 'v', - proparallel => 'u', prorettype => 'void', proargtypes => 'text', + proparallel => 'u', prorettype => 'void', proargtypes => 'text int4', prosrc => 'pg_replication_origin_session_setup' }, { oid => '6007', descr => 'teardown configured replication progress tracking',