]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix an oversight in 3f28b2fcac.
authorAmit Kapila <akapila@postgresql.org>
Wed, 23 Apr 2025 05:22:36 +0000 (10:52 +0530)
committerAmit Kapila <akapila@postgresql.org>
Wed, 23 Apr 2025 05:22:36 +0000 (10:52 +0530)
Commit 3f28b2fcac tried to ensure that the replication origin shouldn't be
advanced in case of an ERROR in the apply worker, so that it can request
the same data again after restart. However, it is possible that an ERROR
was caught and handled by a (say PL/pgSQL) function, and  the apply worker
continues to apply further changes, in which case, we shouldn't reset the
replication origin.

Ensure to reset the origin only when the apply worker exits after an
ERROR.

Commit 3f28b2fcac added new function geterrlevel, which we removed in HEAD
as part of this commit, but kept it in backbranches to avoid breaking any
applications. A separate case can be made to have such a function even for
HEAD.

Reported-by: Shawn McCoy <shawn.the.mccoy@gmail.com>
Author: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Reviewed-by: vignesh C <vignesh21@gmail.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Backpatch-through: 16, where it was introduced
Discussion: https://postgr.es/m/CALsgZNCGARa2mcYNVTSj9uoPcJo-tPuWUGECReKpNgTpo31_Pw@mail.gmail.com

src/backend/replication/logical/worker.c
src/test/subscription/t/100_bugs.pl

index 65e22306c4863c804d4d1a484195aa1770dd7935..db09978697f39b3345ba6e3c4628818b63809a71 100644 (file)
@@ -416,6 +416,8 @@ static inline void reset_apply_error_context_info(void);
 static TransApplyAction get_transaction_apply_action(TransactionId xid,
                                                                                                         ParallelApplyWorkerInfo **winfo);
 
+static void replorigin_reset(int code, Datum arg);
+
 /*
  * Form the origin name for the subscription.
  *
@@ -4441,6 +4443,14 @@ start_apply(XLogRecPtr origin_startpos)
        }
        PG_CATCH();
        {
+               /*
+                * Reset the origin state to prevent the advancement of origin
+                * progress if we fail to apply. Otherwise, this will result in
+                * transaction loss as that transaction won't be sent again by the
+                * server.
+                */
+               replorigin_reset(0, (Datum) 0);
+
                if (MySubscription->disableonerr)
                        DisableSubscriptionAndExit();
                else
@@ -4928,23 +4938,12 @@ void
 apply_error_callback(void *arg)
 {
        ApplyErrorCallbackArg *errarg = &apply_error_callback_arg;
-       int                     elevel;
 
        if (apply_error_callback_arg.command == 0)
                return;
 
        Assert(errarg->origin_name);
 
-       elevel = geterrlevel();
-
-       /*
-        * Reset the origin state to prevent the advancement of origin progress if
-        * we fail to apply. Otherwise, this will result in transaction loss as
-        * that transaction won't be sent again by the server.
-        */
-       if (elevel >= ERROR)
-               replorigin_reset(0, (Datum) 0);
-
        if (errarg->rel == NULL)
        {
                if (!TransactionIdIsValid(errarg->remote_xid))
index cb36ca7b16b62481a21f0be5e0fadfd057a9f8fa..3498a90e7416087d6458be160c773c5592290335 100644 (file)
@@ -490,4 +490,81 @@ is( $result, qq(2|f
 $node_publisher->stop('fast');
 $node_subscriber->stop('fast');
 
+# The bug was that when an ERROR was caught and handled by a (PL/pgSQL)
+# function, the apply worker reset the replication origin but continued
+# processing subsequent changes. So, we fail to update the replication origin
+# during further apply operations. This can lead to the apply worker requesting
+# the changes that have been applied again after restarting.
+
+$node_publisher->rotate_logfile();
+$node_publisher->start();
+
+$node_subscriber->rotate_logfile();
+$node_subscriber->start();
+
+# Set up a publication with a table
+$node_publisher->safe_psql(
+       'postgres', qq(
+       CREATE TABLE t1 (a int);
+       CREATE PUBLICATION regress_pub FOR TABLE t1;
+));
+
+# Set up a subscription which subscribes the publication
+$node_subscriber->safe_psql(
+       'postgres', qq(
+       CREATE TABLE t1 (a int);
+       CREATE SUBSCRIPTION regress_sub CONNECTION '$publisher_connstr' PUBLICATION regress_pub;
+));
+
+$node_subscriber->wait_for_subscription_sync($node_publisher, 'regress_sub');
+
+# Create an AFTER INSERT trigger on the table that raises and subsequently
+# handles an exception. Subsequent insertions will trigger this exception,
+# causing the apply worker to invoke its error callback with an ERROR. However,
+# since the error is caught within the trigger, the apply worker will continue
+# processing changes.
+$node_subscriber->safe_psql(
+       'postgres', q{
+CREATE FUNCTION handle_exception_trigger()
+RETURNS TRIGGER AS $$
+BEGIN
+       BEGIN
+               -- Raise an exception
+               RAISE EXCEPTION 'This is a test exception';
+       EXCEPTION
+               WHEN OTHERS THEN
+                       RETURN NEW;
+       END;
+
+       RETURN NEW;
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE TRIGGER silent_exception_trigger
+AFTER INSERT OR UPDATE ON t1
+FOR EACH ROW
+EXECUTE FUNCTION handle_exception_trigger();
+
+ALTER TABLE t1 ENABLE ALWAYS TRIGGER silent_exception_trigger;
+});
+
+# Obtain current remote_lsn value to check its advancement later
+my $remote_lsn = $node_subscriber->safe_psql('postgres',
+       "SELECT remote_lsn FROM pg_replication_origin_status os, pg_subscription s WHERE os.external_id = 'pg_' || s.oid AND s.subname = 'regress_sub'"
+);
+
+# Insert a tuple to replicate changes
+$node_publisher->safe_psql('postgres', "INSERT INTO t1 VALUES (1);");
+$node_publisher->wait_for_catchup('regress_sub');
+
+# Confirms the origin can be advanced
+$result = $node_subscriber->safe_psql('postgres',
+       "SELECT remote_lsn > '$remote_lsn' FROM pg_replication_origin_status os, pg_subscription s WHERE os.external_id = 'pg_' || s.oid AND s.subname = 'regress_sub'"
+);
+is($result, 't',
+       'remote_lsn has advanced for apply worker raising an exception');
+
+$node_publisher->stop('fast');
+$node_subscriber->stop('fast');
+
 done_testing();