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 1ukryh-008593-BT for pgsql-hackers@arkaria.postgresql.org; Sat, 09 Aug 2025 22:23:51 +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 1ukryc-0052k8-Ql for pgsql-hackers@arkaria.postgresql.org; Sat, 09 Aug 2025 22:23:46 +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 1ukryc-0052k0-7R for pgsql-hackers@lists.postgresql.org; Sat, 09 Aug 2025 22:23:46 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ukryZ-001gef-0c for pgsql-hackers@postgresql.org; Sat, 09 Aug 2025 22:23:45 +0000 Received: by mail-pl1-x62f.google.com with SMTP id d9443c01a7336-2403ceef461so27398245ad.3 for ; Sat, 09 Aug 2025 15:23:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1754778221; x=1755383021; darn=postgresql.org; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=z02xZqjvfF63A4rTPr6BiIBweFLqLvUZWec6ZZi3ODA=; b=N7eub/2gZ3008DJ2gdJuMfUUaSsEIEUTRq5IaFg4Y2fo5hlS2ZXheUrGRxHET932VM tc8qSdZA0FEDVRZlr7C50WhXf66ZU94jbzw+VNZw/Zw02EJMUfepWL0m6SQw8vNquXCS GcE7lNTzSyc4+WCa+gpzViazc42DRr5vRS0CU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1754778221; x=1755383021; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=z02xZqjvfF63A4rTPr6BiIBweFLqLvUZWec6ZZi3ODA=; b=eJcBLL3WGpoWUucdattBU9WOTFVN09tr+w+AnNdZJOps9bgKIDwUkC7Sm0RIjE0AJ3 tnTj++oN0C87be0RHznzEPW5TY0KxOfR/DBMMvaHjw/RdQfrMJJT0Qh9epch0b7KJinM DqeISnm//ng7QLd2GmOJ1mRWPm64CRdHKqdg9N6i7fZ7IAQrqzBDfQsz5bbgvFtWIsRg /hich2iYgyseh4U3cDb43bloGKNKSmBM4iUePTntFGQ/eoyGlyS3bLIEHCkUvseYWuIU LPX3AtQ+6saEYWNVc9HJHIiymxxWn3XzzppEDWqjGw+dD3IPb4uppVhAil1KUrBEGKKs KHHQ== X-Forwarded-Encrypted: i=1; AJvYcCWfzvL7LJkypz9Gb++0s8Q+5LmErBnNl4l6G7d+8qEfCXKyK20TM0+NdTTneiSoviNdGUrB6LhjCSWHw014@postgresql.org X-Gm-Message-State: AOJu0Yys6WoPc79VFMZiOzeC4hAWSJE9hMHqbYu1EbSnbNUcy5Q6aZwZ uh0NKba/pWu9zxSplAVJS75hdCpKl7+K5C5EITb0iVGX50LzFUkcCYUiAuM8qruJ0Q== X-Gm-Gg: ASbGncswfdKz6GuTAvpoSdRBoFf/tdYFnMgVlagDrcidlGi0IINvbooSMgitKrqc3nU tmAapdDmt+qWvkfAiaKxMyG9OX58H/DLDXloTrrAxmqy8vM0It60Y8NHeGaGJZBxr+0Pusd/WxF C3yYlzjpgdPsdaDVeErTjjTcEfGpBSEmAnRFM7VczDaKxoJZbbGkW/xBssSKIHP9H17R4P9IE1E At/2Wa77Ty01aFDpp294Gw98fP++qJYenwuTntK3BBM0JCLhBpb7E2rv/eVDsHO5hAdGNZ3vpOF QDanuMY2KLeR3Jqf8ejcbo68Nclx2zmcRtK+1RJidNXADhiUGn5IiEAKCfI9iC0Bhg12412HJVp NDTEBVAm94fVDvFDAafeJRlRqtBhGCcIG1EAP0oFkuWWiuKUYhcQ= X-Google-Smtp-Source: AGHT+IFaqpFMpTakYNamehaaefVS5x+v73Hwe0TIkwXdwO43r0J3xLA9uK6rSY1NSpSyTGcB4rgJCQ== X-Received: by 2002:a17:903:98f:b0:240:678c:d2b7 with SMTP id d9443c01a7336-242c2007592mr119709745ad.15.1754778220758; Sat, 09 Aug 2025 15:23:40 -0700 (PDT) Received: from google.com (c-73-15-160-255.hsd1.ca.comcast.net. [73.15.160.255]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-241e899b4adsm238072865ad.132.2025.08.09.15.23.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 09 Aug 2025 15:23:40 -0700 (PDT) Date: Sat, 9 Aug 2025 15:23:38 -0700 From: Noah Misch To: Heikki Linnakangas Cc: Andres Freund , Nathan Bossart , "pgsql-hackers@postgresql.org" Subject: Re: A few patches to clarify snapshot management Message-ID: <20250809222338.cc.nmisch@google.com> References: <7c56f180-b9e1-481e-8c1d-efa63de3ecbb@iki.fi> <0cc21d6a-32c4-4d34-9eeb-7a0cf6dca9e8@iki.fi> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <0cc21d6a-32c4-4d34-9eeb-7a0cf6dca9e8@iki.fi> User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Jan 07, 2025 at 11:55:00AM +0200, Heikki Linnakangas wrote: > On 07/01/2025 00:00, Andres Freund wrote: > > On 2024-12-20 19:31:01 +0200, Heikki Linnakangas wrote: > > > While playing around some more with this, I noticed that this code in > > > GetTransactionSnapshot() is never reached, and AFAICS has always been dead > > > code: > > > > > > > Snapshot > > > > GetTransactionSnapshot(void) > > > > { > > > > /* > > > > * Return historic snapshot if doing logical decoding. We'll never need a > > > > * non-historic transaction snapshot in this (sub-)transaction, so there's > > > > * no need to be careful to set one up for later calls to > > > > * GetTransactionSnapshot(). > > > > */ > > > > if (HistoricSnapshotActive()) > > > > { > > > > Assert(!FirstSnapshotSet); > > > > return HistoricSnapshot; > > > > } > > > > > > when you think about it, that's good, because it doesn't really make sense > > > to call GetTransactionSnapshot() during logical decoding. We jump through > > > hoops to make the historic catalog decoding possible with historic > > > snapshots, tracking subtransactions that modify catalogs and WAL-logging > > > command ids, but they're not suitable for general purpose queries. So I > > > think we should turn that into an error, per attached patch. > > > > Hm. I'm not sure it's a good idea to forbid this. Couldn't there be sane C > > code in an output functions calling GetTransactionSnapshot() or such to do > > some internal lookups? > > I haven't seen any. And I don't think that would work correctly while doing > logical decoding anyway, because historical snapshots only track XIDs that > modify catalogs. regclassout and enumout do work because they use the > catalog snapshot rather than GetTransactionSnapshot(). > > (I committed that change in commit 1585ff7387 already, but discussion is > still welcome of course) https://github.com/2ndQuadrant/pglogical does rely on the pre-1585ff7387 code for its row_filter feature. row_filter calls ExecEvalExpr() from the output plugin, e.g. to evaluate expression "id between 2 AND 4" arising from this configuration in the pglogical test suite: SELECT * FROM pglogical.replication_set_add_table('default', 'basic_dml', false, row_filter := $rf$id between 2 AND 4$rf$); One of the GetTransactionSnapshot() calls is in pglogical_output_plugin.c itself. For that, I could work around the change by forcing the old HistoricSnapshot use: - PushActiveSnapshot(GetTransactionSnapshot()); + Assert(HistoricSnapshotActive()); + PushActiveSnapshot(GetCatalogSnapshot(InvalidOid)); That doesn't get far. Calling a plpgsql function in the expression reaches the "cannot take query snapshot during logical decoding" error via this stack trace: GetTransactionSnapshot at snapmgr.c:279:3 exec_eval_simple_expr at pl_exec.c:6214:3 (inlined by) exec_eval_expr at pl_exec.c:5699:6 exec_stmt_raise at pl_exec.c:3820:8 (inlined by) exec_stmts at pl_exec.c:2096:10 exec_stmt_block at pl_exec.c:1955:6 exec_toplevel_block at pl_exec.c:1646:7 plpgsql_exec_function at pl_exec.c:636:5 plpgsql_call_handler at pl_handler.c:278:11 fmgr_security_definer at fmgr.c:755:52 ExecInterpExpr at execExprInterp.c:927:7 pglogical_change_filter at pglogical_output_plugin.c:663:7 (inlined by) pg_decode_change at pglogical_output_plugin.c:691:7 change_cb_wrapper at logical.c:1121:22 ReorderBufferApplyChange at reorderbuffer.c:2078:3 (inlined by) ReorderBufferProcessTXN at reorderbuffer.c:2383:7 DecodeCommit at decode.c:743:3 (inlined by) xact_decode at decode.c:242:5 LogicalDecodingProcessRecord at decode.c:123:1 XLogSendLogical at walsender.c:3442:33 WalSndLoop at walsender.c:2837:7 StartLogicalReplication at walsender.c:1504:2 (inlined by) exec_replication_command at walsender.c:2158:6 PostgresMain at postgres.c:4762:10 BackendMain at backend_startup.c:80:2 postmaster_child_launch at launch_backend.c:291:3 BackendStartup at postmaster.c:3587:8 (inlined by) ServerLoop at postmaster.c:1702:6 PostmasterMain at postmaster.c:1252:6 main at main.c:165:4 > > Hm. I'm not sure it's a good idea to forbid this. Couldn't there be sane C > > code in an output functions calling GetTransactionSnapshot() or such to do > > some internal lookups? I think pglogical_output_plugin.c w/ plpgsql is largely sane when used with a plpgsql function that consults only catalogs and the output tuple. If the pglogical test suite is representative, that's the usual case for a row_filter. A plpgsql function that reads user tables will be fragile with concurrent pruning, but a user might sanely accept that fragility. A plpgsql function that writes tuples is not sane in a row_filter. How do you see it? So far, I know of these options: 1. Make pglogical block the row_filter feature for any v18+ origin. 2. Revert postgresql.git commit 1585ff7387. 3. Make pglogical use HistoricSnapshot where pglogical_output_plugin.c handles snapshots directly. That should keep simple row_filter expressions like "col > 0" functioning. Entering plpgsql or similarly-complex logic will fail with "cannot take query snapshot during logical decoding", and we'll consider that to be working as intended. 4. Fail later and lazily, for just the most-unreasonable cases. For example, fail when HistoricSnapshot applies to a write operation. (Maybe this already fails. I didn't check.) Which of those or other options should we consider? For reference, https://github.com/2ndQuadrant/pglogical/pull/503 is an otherwise-working port of pglogical to v18. Its Makefile currently disables the tests that reach "cannot take query snapshot during logical decoding".