Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1utk7Y-00DUen-N7 for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Sep 2025 09:49:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1utk7V-008Yro-7q for pgsql-hackers@arkaria.postgresql.org; Wed, 03 Sep 2025 09:49:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1utk7U-008Yrf-Tq for pgsql-hackers@lists.postgresql.org; Wed, 03 Sep 2025 09:49:37 +0000 Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1utk7R-000KOa-2J for pgsql-hackers@lists.postgresql.org; Wed, 03 Sep 2025 09:49:36 +0000 Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-45b79ec2fbeso42861615e9.3 for ; Wed, 03 Sep 2025 02:49:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1756892973; x=1757497773; darn=lists.postgresql.org; h=message-id:date:mime-version:subject:to:from:from:to:cc:subject :date:message-id:reply-to; bh=n990BZFoFulmEVB8YsJBUGAAeCilmdWJnoM8jemXbLo=; b=h93xgRLBvIKrZe51KQ7gIfL5V4dTdQRUyBv7Lo9FCmGJEL40bfl8coqNU9xedWXczs U5U+Amdk0NjvAIuPaAQNqbjMay/g8iUmxeylV/pMwgku65t04zumK/KAs0EbTRlLWjN2 Hea2zbKA4UGvRFswYqpHNiyxm8/N0kRwFHIEDinJQ9kGGnF9NUpEIB/6Gc5KhFkTv9NV cMjD5T2SocCvtPjNE+mkIBeNPeNvc9WG4UFgb5A9yJOlzpWUkjp6GhG2txwBrNZajFq3 F/srVKz/48NqJPt5sObLPc/eLVBDNgr0D4f2AM+afQAdawxLo6Lk7pyeR80d2ue8LL8n wZGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756892973; x=1757497773; h=message-id:date:mime-version:subject:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=n990BZFoFulmEVB8YsJBUGAAeCilmdWJnoM8jemXbLo=; b=Ag8J61RFVdlavP/iOspJgbLz4a5JE/iIOI/9K5GdbLtnFc6nsKXuCiC46EYHKaBf86 xsHcDIWe9v9KZa2OrXYYtvhPlb/K+CjmsMahMR6Il/iifcWntc0hrSV8aPT9C8FCwFmV xvVkswlphYORUOwfzAmsCqVBO+5jalM7BuUOgseKYEPAkjeYXjsX7F1apPGdNnc3LvSQ ycps8Nqn/5u3YUybvpGWlgjTgBzBB3DpG5BokXqYuS9cidGpRZcJdCQrK6bDiJ9VjXDL w53he5qmexx3Z3+0rqIu2fqJ+EYBmogV6b0T7ULrUbIqj6Q38sCNI/6XrAbXoYLs62rB W7ig== X-Gm-Message-State: AOJu0Yw4sB3nFranDB3b+b7qST9fOIZWicUXB4h/MkIB6pMrbVTffIla 26KTpx2fKmk/zrR1ZJnUPkPPj1FY44NoXUwrS1LhdYPzptTgaZnK05rPbW4JiWStzKjDslRKt+2 1puU/ X-Gm-Gg: ASbGncsp4uTHl33MdgigDZhil/aa2DUN48/47Bc0y60luIAUhsskQCnw5KrdDSz9eWq MmZ96n4lwTKapB9xLe3R7b/hNB/rEwKqcdd7yN1RznWpYy3i7ZXhc3JOHYkzxczxG7Xx1Xmi9f/ THeQLOwktneIRy71biilNDZud6CPcGZPqYAkBAoS3dIxtyZVVTTPX3lAWMKKOhSrumRTYVI/GG6 DjgOR9BlxdEWG+nBWCRXtVnlU4h1HdtK/HDY/efdyut8xdeMaqp3HDPD+yCKM144XKgB1OauGfi 2VgztKwO/E9nPCIMeatmv3BH1dWAxj9ep9NSdd3q9GV4H2UeeNg1Ubx89sQ8fglcv1g/5o0yoQQ FphZ0L3K4la7ar/neMotAVaYMwrtkMMMkrsCe X-Google-Smtp-Source: AGHT+IFd38wM9PaXSdgKDqK43j4Nmn8X6gxBFDjhNCtQhd8veV10+t0liLl34+mPyHfihZPFoTqvtQ== X-Received: by 2002:a05:600c:3b8a:b0:45b:8935:16bc with SMTP id 5b1f17b1804b1-45b8ee1788emr82245545e9.37.1756892973398; Wed, 03 Sep 2025 02:49:33 -0700 (PDT) Received: from localhost (109-81-168-144.rct.o2.cz. [109.81.168.144]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-45b7e7d23b7sm232180845e9.1.2025.09.03.02.49.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 02:49:33 -0700 (PDT) From: Antonin Houska To: pgsql-hackers@lists.postgresql.org Subject: Unexpected changes of CurrentResourceOwner and CurrentMemoryContext X-Mailer: MH-E 8.6+git; nmh 1.8; GNU Emacs 28.3 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Date: Wed, 03 Sep 2025 11:49:32 +0200 Message-ID: <119497.1756892972@localhost> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-=-= Content-Type: text/plain When working on the REPACK command, we see an ERROR caused by unexpected change of CurrentResourceOwner [1]. I think the problem is that reorderbuffer.c does not restore the original value after calling RollbackAndReleaseCurrentSubTransaction(). The attached patch tries to handle the call like other callers throughout the tree do. [1] https://www.postgresql.org/message-id/CADzfLwUgPMLiFkXRnk97ugPqkDfsNJ3TRdw9gjJM%3D8WB4_nXwQ%40mail.gmail.com -- Antonin Houska Web: https://www.cybertec-postgresql.com --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Avoid-unexpected-changes-of-CurrentResourceOwner-and.patch From 025322cd23c05fa92bb04c8e1ce76ef40003d4cc Mon Sep 17 00:00:00 2001 From: Antonin Houska Date: Wed, 3 Sep 2025 11:33:45 +0200 Subject: [PATCH] Avoid unexpected changes of CurrentResourceOwner and CurrentMemoryContext. Users of logical decoding can encounter unexpected change of CurrentResourceOwner and CurrentMemoryContext. The problem is that in reorderbuffer.c, unlike other call sites, we call RollbackAndReleaseCurrentSubTransaction() without restoring the original values of these global variables. This patch saves the values prior to the call and restores them eventually. --- src/backend/replication/logical/reorderbuffer.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c index 34cf05668ae..4736f993c37 100644 --- a/src/backend/replication/logical/reorderbuffer.c +++ b/src/backend/replication/logical/reorderbuffer.c @@ -2215,6 +2215,7 @@ ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn, { bool using_subtxn; MemoryContext ccxt = CurrentMemoryContext; + ResourceOwner cowner = CurrentResourceOwner; ReorderBufferIterTXNState *volatile iterstate = NULL; volatile XLogRecPtr prev_lsn = InvalidXLogRecPtr; ReorderBufferChange *volatile specinsert = NULL; @@ -2692,7 +2693,11 @@ ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn, } if (using_subtxn) + { RollbackAndReleaseCurrentSubTransaction(); + MemoryContextSwitchTo(ccxt); + CurrentResourceOwner = cowner; + } /* * We are here due to one of the four reasons: 1. Decoding an @@ -2751,7 +2756,11 @@ ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn, } if (using_subtxn) + { RollbackAndReleaseCurrentSubTransaction(); + MemoryContextSwitchTo(ccxt); + CurrentResourceOwner = cowner; + } /* * The error code ERRCODE_TRANSACTION_ROLLBACK indicates a concurrent @@ -3244,6 +3253,8 @@ ReorderBufferImmediateInvalidation(ReorderBuffer *rb, uint32 ninvalidations, SharedInvalidationMessage *invalidations) { bool use_subtxn = IsTransactionOrTransactionBlock(); + MemoryContext ccxt = CurrentMemoryContext; + ResourceOwner cowner = CurrentResourceOwner; int i; if (use_subtxn) @@ -3262,7 +3273,11 @@ ReorderBufferImmediateInvalidation(ReorderBuffer *rb, uint32 ninvalidations, LocalExecuteInvalidationMessage(&invalidations[i]); if (use_subtxn) + { RollbackAndReleaseCurrentSubTransaction(); + MemoryContextSwitchTo(ccxt); + CurrentResourceOwner = cowner; + } } /* -- 2.47.1 --=-=-=--