Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lQmJD-0003Qh-U9 for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Mar 2021 07:27:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lQmJB-0003F7-AE for pgsql-hackers@arkaria.postgresql.org; Mon, 29 Mar 2021 07:27:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lQmJA-0003Ez-Vb for pgsql-hackers@lists.postgresql.org; Mon, 29 Mar 2021 07:27:33 +0000 Received: from mail-wr1-x430.google.com ([2a00:1450:4864:20::430]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lQmJ8-0006nA-1U for pgsql-hackers@postgresql.org; Mon, 29 Mar 2021 07:27:31 +0000 Received: by mail-wr1-x430.google.com with SMTP id k8so11739755wrc.3 for ; Mon, 29 Mar 2021 00:27:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=DBi07dUuxkMrItVIRJ1kG4PZMiVSGZv7ERqO72TFzn8=; b=1tJdDAaKmNt8KM3Sk3SP6KnnRuqJg1K7YZ/oLYPjA/0E5Xkqb5sGom7GKU5hrOeSLo QTM+7dTaErX1QCw5WzM2AgMECKlOpubxEcDAwaCeH9AFZdRgW8CsV3zzvsZa1xQas79S upkNvPEs0glDGlGXSeY4FuQ44v2l0W2LmEF+eiOVvBISnF2E2Nf+k803MES/tLWCiU1P 0BJHEqQcNSNzPvxr2dFoFpGp9hEt79Zp9EfCiFEnkFFp4bdpwCml0CdXPY0ScO13KvCl FEmD9MraQ7rhEFitFYT96MVF9E17DFwh9cJGXvY8phGFuXCGhUGXCALa4t1G2280aL32 RCaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=DBi07dUuxkMrItVIRJ1kG4PZMiVSGZv7ERqO72TFzn8=; b=JvwZrE6QFsB1uO4MhMy/BQ42xfDK/EFbpvA0tyvMQAZCMnLuqefng3eTQd23gtPbGr eCYRl72gk6XliKoA5rLsvLNXkENktiFxtdPDzNt8TDONSQgWcmEbaPaeJiB4MpA8WeZk Szb7UwcibvelhtOvY45lh8HAqVVJUzp1bs9qk6292xdqUM+jmIG/mZXHr/bKPsPNu4CU kOVrvAJH5mKQ91ObfVelCja02tGpQVOlsN7AzcvVPnmmfjHh5SjInn2/j+RuhZfBav5E NQZwQciaIXlCAnIGc4J7qdRRJEYdpkHSyPARod0KaGxqNC24bHDeDnxX+S+vFLmLsq40 VeFg== X-Gm-Message-State: AOAM531oIpTEZyIoXv2o+Es8QQYGB/rkB6nvJNV+RE6gZCCs0QiiRQXu 5Cv8PLv7HVUsIlKbKL9hfV0zB+S2p3mgIlrBIucdJcJ/jLOGREVyomGntZhodKQ8rOmIO4ABsbd eaUczTeeKv99CXWN/8UVDlsmOV5GeGpGED78m1KW5fuomJdVl/3B2JyDaU1ujP6u380wClb0ovc IWEYqHzU02I51HY/zKyeICe7cEYMBcJJN3uBdl X-Google-Smtp-Source: ABdhPJx4SVl1BWmV/YvMCJ0VmKstNWPIfQLq67I236TnRr5cyog5jVtGhLxfDnrLdjIR9INNusrlEQ== X-Received: by 2002:a5d:6307:: with SMTP id i7mr27321923wru.305.1617002848409; Mon, 29 Mar 2021 00:27:28 -0700 (PDT) Received: from [10.80.18.124] (pool1-225.cgn.as8758.net. [212.25.6.225]) by smtp.gmail.com with ESMTPSA id d13sm29230090wro.23.2021.03.29.00.27.26 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 29 Mar 2021 00:27:27 -0700 (PDT) Subject: Re: [PATCH] Provide more information to filter_prepare To: Amit Kapila Cc: "pgsql-hackers@postgresql.org" , Ajin Cherian References: <6f322b21-cbb8-5f9a-b2aa-30323a6ab604@enterprisedb.com> <722e7841-f4fa-dddd-4466-990fcada5d78@enterprisedb.com> From: Markus Wanner Message-ID: <590e0d87-8a7d-9310-ff6d-b484f15522d0@enterprisedb.com> Date: Mon, 29 Mar 2021 09:27:25 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.9.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------5D4258EC4291437A17E92DAF" Content-Language: en-US X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------5D4258EC4291437A17E92DAF Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 29.03.21 08:23, Amit Kapila wrote: > On Mon, Mar 29, 2021 at 11:42 AM Amit Kapila wrote: >> Why do you think that this callback can be invoked several times per >> transaction? I think it could be called at most two times, once at >> prepare time, then at commit or rollback time. So, I think using >> 'multiple' instead of 'several' times is better. Thank you for reviewing. That's fine with me, I just wanted to provide an explanation for why the callback needs to be stable. (I would not want to limit us in the docs to guarantee it is called only twice. 'multiple' sounds generic enough, I changed it to that word.) > What exactly is the node identifier here? Is it a publisher or > subscriber node id? We might want to be a bit more explicit here? Good point. I clarified this to speak of the origin node (given this is not necessarily the direct provider when using chained replication). An updated patch is attached. Regards Markus --------------5D4258EC4291437A17E92DAF Content-Type: text/x-patch; charset=UTF-8; name="0001-add-xid-arg-to-filter_prepare_v4.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="0001-add-xid-arg-to-filter_prepare_v4.patch" From: Markus Wanner Date: Tue, 2 Mar 2021 11:33:54 +0100 Subject: [PATCH] Add an xid argument to the filter_prepare callback for output plugins --- contrib/test_decoding/test_decoding.c | 4 ++- doc/src/sgml/logicaldecoding.sgml | 34 +++++++++++++++-------- src/backend/replication/logical/decode.c | 17 ++++++++---- src/backend/replication/logical/logical.c | 5 ++-- src/include/replication/logical.h | 3 +- src/include/replication/output_plugin.h | 1 + 6 files changed, 43 insertions(+), 21 deletions(-) diff --git a/contrib/test_decoding/test_decoding.c b/contrib/test_decoding/test_decoding.c index ae5f397f351..de1b6926581 100644 --- a/contrib/test_decoding/test_decoding.c +++ b/contrib/test_decoding/test_decoding.c @@ -77,6 +77,7 @@ static void pg_decode_message(LogicalDecodingContext *ctx, bool transactional, const char *prefix, Size sz, const char *message); static bool pg_decode_filter_prepare(LogicalDecodingContext *ctx, + TransactionId xid, const char *gid); static void pg_decode_begin_prepare_txn(LogicalDecodingContext *ctx, ReorderBufferTXN *txn); @@ -440,7 +441,8 @@ pg_decode_rollback_prepared_txn(LogicalDecodingContext *ctx, * substring, then we filter it out. */ static bool -pg_decode_filter_prepare(LogicalDecodingContext *ctx, const char *gid) +pg_decode_filter_prepare(LogicalDecodingContext *ctx, TransactionId xid, + const char *gid) { if (strstr(gid, "_nodecode") != NULL) return true; diff --git a/doc/src/sgml/logicaldecoding.sgml b/doc/src/sgml/logicaldecoding.sgml index 80eb96d609a..f3ac84aa85a 100644 --- a/doc/src/sgml/logicaldecoding.sgml +++ b/doc/src/sgml/logicaldecoding.sgml @@ -794,20 +794,30 @@ typedef void (*LogicalDecodeMessageCB) (struct LogicalDecodingContext *ctx, COMMIT PREPARED time. To signal that decoding should be skipped, return true; false otherwise. When the callback is not - defined, false is assumed (i.e. nothing is - filtered). + defined, false is assumed (i.e. no filtering, all + transactions using two-phase commit are decoded in two phases as well). typedef bool (*LogicalDecodeFilterPrepareCB) (struct LogicalDecodingContext *ctx, + TransactionId xid, const char *gid); - The ctx parameter has the same contents as for the - other callbacks. The gid is the identifier that later - identifies this transaction for COMMIT PREPARED or - ROLLBACK PREPARED. + The ctx parameter has the same contents as for + the other callbacks. The parameters xid + and gid provide two different ways to identify + the transaction. For some systems, the gid may + be sufficient. However, reuse of the same gid + for example by a downstream node using multiple subscriptions may lead + to it not being a unique identifier. Therefore, other systems combine + the xid with a node identifier to form a + globally unique transaction identifier. The later COMMIT + PREPARED or ROLLBACK PREPARED carries both + identifiers, providing an output plugin the choice of what to use. - The callback has to provide the same static answer for a given - gid every time it is called. + The callback may be invoked several times per transaction to decode and + must provide the same static answer for a given pair of + xid and gid every time + it is called. @@ -1219,9 +1229,11 @@ stream_commit_cb(...); <-- commit of the streamed transaction - Optionally the output plugin can specify a name pattern in the - filter_prepare_cb and transactions with gid containing - that name pattern will not be decoded as a two-phase commit transaction. + Optionally the output plugin can define filtering rules via + filter_prepare_cb to decode only specific transaction + in two phases. This can be achieved by pattern matching on the + gid or via lookups using the + xid. diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c index 5f596135b15..97be4b0f23f 100644 --- a/src/backend/replication/logical/decode.c +++ b/src/backend/replication/logical/decode.c @@ -80,7 +80,8 @@ static void DecodePrepare(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, static void DecodeXLogTuple(char *data, Size len, ReorderBufferTupleBuf *tup); /* helper functions for decoding transactions */ -static inline bool FilterPrepare(LogicalDecodingContext *ctx, const char *gid); +static inline bool FilterPrepare(LogicalDecodingContext *ctx, + TransactionId xid, const char *gid); static bool DecodeTXNNeedSkip(LogicalDecodingContext *ctx, XLogRecordBuffer *buf, Oid dbId, RepOriginId origin_id); @@ -271,7 +272,8 @@ DecodeXactOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) * doesn't filter the transaction at prepare time. */ if (info == XLOG_XACT_COMMIT_PREPARED) - two_phase = !(FilterPrepare(ctx, parsed.twophase_gid)); + two_phase = !(FilterPrepare(ctx, xid, + parsed.twophase_gid)); DecodeCommit(ctx, buf, &parsed, xid, two_phase); break; @@ -298,7 +300,8 @@ DecodeXactOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) * doesn't filter the transaction at prepare time. */ if (info == XLOG_XACT_ABORT_PREPARED) - two_phase = !(FilterPrepare(ctx, parsed.twophase_gid)); + two_phase = !(FilterPrepare(ctx, xid, + parsed.twophase_gid)); DecodeAbort(ctx, buf, &parsed, xid, two_phase); break; @@ -355,7 +358,8 @@ DecodeXactOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) * manner iff output plugin supports two-phase commits and * doesn't filter the transaction at prepare time. */ - if (FilterPrepare(ctx, parsed.twophase_gid)) + if (FilterPrepare(ctx, parsed.twophase_xid, + parsed.twophase_gid)) { ReorderBufferProcessXid(reorder, parsed.twophase_xid, buf->origptr); @@ -581,7 +585,8 @@ DecodeHeapOp(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) * this transaction as a regular commit later. */ static inline bool -FilterPrepare(LogicalDecodingContext *ctx, const char *gid) +FilterPrepare(LogicalDecodingContext *ctx, TransactionId xid, + const char *gid) { /* * Skip if decoding of two-phase transactions at PREPARE time is not @@ -599,7 +604,7 @@ FilterPrepare(LogicalDecodingContext *ctx, const char *gid) if (ctx->callbacks.filter_prepare_cb == NULL) return false; - return filter_prepare_cb_wrapper(ctx, gid); + return filter_prepare_cb_wrapper(ctx, xid, gid); } static inline bool diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c index 37b75deb728..2f6803637bf 100644 --- a/src/backend/replication/logical/logical.c +++ b/src/backend/replication/logical/logical.c @@ -1083,7 +1083,8 @@ truncate_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, } bool -filter_prepare_cb_wrapper(LogicalDecodingContext *ctx, const char *gid) +filter_prepare_cb_wrapper(LogicalDecodingContext *ctx, TransactionId xid, + const char *gid) { LogicalErrorCallbackState state; ErrorContextCallback errcallback; @@ -1104,7 +1105,7 @@ filter_prepare_cb_wrapper(LogicalDecodingContext *ctx, const char *gid) ctx->accept_writes = false; /* do the actual work: call callback */ - ret = ctx->callbacks.filter_prepare_cb(ctx, gid); + ret = ctx->callbacks.filter_prepare_cb(ctx, xid, gid); /* Pop the error context stack */ error_context_stack = errcallback.previous; diff --git a/src/include/replication/logical.h b/src/include/replication/logical.h index c2534033723..af551d6f4ee 100644 --- a/src/include/replication/logical.h +++ b/src/include/replication/logical.h @@ -125,7 +125,8 @@ extern void LogicalIncreaseRestartDecodingForSlot(XLogRecPtr current_lsn, XLogRecPtr restart_lsn); extern void LogicalConfirmReceivedLocation(XLogRecPtr lsn); -extern bool filter_prepare_cb_wrapper(LogicalDecodingContext *ctx, const char *gid); +extern bool filter_prepare_cb_wrapper(LogicalDecodingContext *ctx, + TransactionId xid, const char *gid); extern bool filter_by_origin_cb_wrapper(LogicalDecodingContext *ctx, RepOriginId origin_id); extern void ResetLogicalStreamingState(void); extern void UpdateDecodingStats(LogicalDecodingContext *ctx); diff --git a/src/include/replication/output_plugin.h b/src/include/replication/output_plugin.h index 2c2c964c55f..810495ed0e4 100644 --- a/src/include/replication/output_plugin.h +++ b/src/include/replication/output_plugin.h @@ -106,6 +106,7 @@ typedef void (*LogicalDecodeShutdownCB) (struct LogicalDecodingContext *ctx); * and sent as usual transaction. */ typedef bool (*LogicalDecodeFilterPrepareCB) (struct LogicalDecodingContext *ctx, + TransactionId xid, const char *gid); /* -- 2.30.2 --------------5D4258EC4291437A17E92DAF--