On Tue, Dec 1, 2026 at 5:26 PM Nisha Moond <[email protected]> wrote:
>
> It depends on how the handler is written, so it may be rare, but I
> could reproduce the loop with an ALWAYS trigger on the subscriber that
> calls upper() unqualified, and a public.upper() that raises error
> (steps attached).
>
Sorry, I forgot to attach the test steps file. Here it is:
Repro: GUC change made by the message handler causes the apply worker to fail
in a loop
Setup:
- Publisher: wal_level = logical.
- Subscriber: shared_preload_libraries = 'test_logicalmsg_hooks'
(src/test/modules/test_logicalmsg_hooks, installed with "make install").
Its handler INSERTs each received message into public.test_logicalmsg_log.
- The publisher below is on port 7833. Change the connection string as needed.
The handler doing a SET is simulated by an ALWAYS trigger on
public.test_logicalmsg_log. The trigger runs inside the handler as the
subscription owner, the same as a handler running SET.
1. Table, publication and subscription
[PUB]
CREATE TABLE tab (a int PRIMARY KEY, b int);
CREATE PUBLICATION pubtab FOR TABLE tab;
[SUB]
CREATE TABLE tab (a int PRIMARY KEY, b int);
CREATE SUBSCRIPTION subtab
CONNECTION 'dbname=postgres host=localhost port=7833'
PUBLICATION pubtab WITH (messages = true);
-- wait until the table is synced ('r')
SELECT srrelid::regclass, srsubstate FROM pg_subscription_rel;
2. Let the handler create its log table
[PUB]
SELECT pg_logical_emit_message(true, 'init', 'x');
[SUB]
SELECT count(*) FROM public.test_logicalmsg_log; -- table exists
3. A public.upper() that raises an error, and an ALWAYS trigger on tab that
calls upper() unqualified
[SUB]
CREATE FUNCTION public.upper(text) RETURNS text LANGUAGE plpgsql AS $$
BEGIN
RAISE EXCEPTION 'public.upper() called, search_path = %',
pg_catalog.current_setting('search_path');
END $$;
CREATE FUNCTION public.tab_check() RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
PERFORM upper('x'); -- resolves to pg_catalog.upper() when search_path
= ''
RETURN NEW;
END $$;
CREATE TRIGGER tab_check BEFORE INSERT ON public.tab
FOR EACH ROW EXECUTE FUNCTION public.tab_check();
ALTER TABLE public.tab ENABLE ALWAYS TRIGGER tab_check;
4. Make the "handler" run SET search_path for messages with prefix 'set_path'
[SUB]
CREATE FUNCTION public.handler_sets_path() RETURNS trigger LANGUAGE plpgsql AS
$$
BEGIN
PERFORM pg_catalog.set_config('search_path', 'public, pg_catalog', false);
RETURN NEW;
END $$;
CREATE TRIGGER msg_set_path BEFORE INSERT ON public.test_logicalmsg_log
FOR EACH ROW WHEN (NEW.prefix = 'set_path')
EXECUTE FUNCTION public.handler_sets_path();
ALTER TABLE public.test_logicalmsg_log ENABLE ALWAYS TRIGGER msg_set_path;
5. A normal insert works
[PUB]
INSERT INTO tab VALUES (200, 200);
[SUB]
SELECT * FROM tab WHERE a = 200; -- 1 row
6. The message and an insert in the same transaction
[PUB]
BEGIN;
SELECT pg_logical_emit_message(true, 'set_path', 'SET search_path = public,
pg_catalog');
INSERT INTO tab VALUES (201, 201);
COMMIT;
[SUB]
SELECT * FROM tab WHERE a = 201; -- 0 rows
SELECT subname, apply_error_count FROM pg_stat_subscription_stats; -- keeps
growing
The subscriber log shows this on every retry:
ERROR: public.upper() called, search_path = public, pg_catalog
CONTEXT: PL/pgSQL function upper(text) line 3 at RAISE
SQL statement "SELECT upper('x')"
PL/pgSQL function tab_check() line 3 at PERFORM
processing remote data for replication origin "pg_16390" during message
type "INSERT" for replication target relation "public.tab" in transaction 668,
finished at 0/017CF420
7. Cleanup (dropping the handler trigger lets the next retry apply row 201)
[SUB]
DROP TRIGGER msg_set_path ON public.test_logicalmsg_log;
SELECT * FROM tab WHERE a = 201; -- applied
DROP TRIGGER tab_check ON public.tab;
DROP FUNCTION public.upper(text), public.tab_check(),
public.handler_sets_path();