v2-HEAD-0001-Fix-oversight-3f28b2f.patch
application/octet-stream
Filename: v2-HEAD-0001-Fix-oversight-3f28b2f.patch
Type: application/octet-stream
Part: 0
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch v2-0001
Subject: Fix oversight 3f28b2f
| File | + | − |
|---|---|---|
| src/backend/replication/logical/worker.c | 9 | 11 |
| src/backend/utils/error/elog.c | 0 | 17 |
| src/include/utils/elog.h | 0 | 1 |
From b6305cf030b469e52aa68cfd5c4ea9976647686d Mon Sep 17 00:00:00 2001
From: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Date: Mon, 21 Apr 2025 15:16:31 +0900
Subject: [PATCH v2-HEAD] Fix oversight 3f28b2f
3f28b2f introduced replorigin_reset() in apply_error_callback() in case of
elevel >= ERROR to ensure the replication origin is released from the session
before exiting the process. However, in some cases, apply_error_callback() can
be called with ERROR, but the apply worker can continue applying the logical
replication. If this happens, the worker applies changes without setting the
replication origin, which can lead to data duplication.
This commit moves replorigin_reset() to PG_CATCH() part in start_apply(). The
path can be reached only by the exiting workers.
---
src/backend/replication/logical/worker.c | 20 +++++++++-----------
src/backend/utils/error/elog.c | 17 -----------------
src/include/utils/elog.h | 1 -
3 files changed, 9 insertions(+), 29 deletions(-)
diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index 5ce596f4576..1fd04f05e6a 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -414,6 +414,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.
*
@@ -4516,6 +4518,13 @@ 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
@@ -5004,23 +5013,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))
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index d6299633ab7..47af743990f 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -1590,23 +1590,6 @@ geterrcode(void)
return edata->sqlerrcode;
}
-/*
- * geterrlevel --- return the currently set error level
- *
- * This is only intended for use in error callback subroutines, since there
- * is no other place outside elog.c where the concept is meaningful.
- */
-int
-geterrlevel(void)
-{
- ErrorData *edata = &errordata[errordata_stack_depth];
-
- /* we don't bother incrementing recursion_depth */
- CHECK_STACK_DEPTH();
-
- return edata->elevel;
-}
-
/*
* geterrposition --- return the currently set error position (0 if none)
*
diff --git a/src/include/utils/elog.h b/src/include/utils/elog.h
index a5313c5d2d5..5eac0e16970 100644
--- a/src/include/utils/elog.h
+++ b/src/include/utils/elog.h
@@ -227,7 +227,6 @@ extern int internalerrquery(const char *query);
extern int err_generic_string(int field, const char *str);
extern int geterrcode(void);
-extern int geterrlevel(void);
extern int geterrposition(void);
extern int getinternalerrposition(void);
--
2.43.5