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 1s1zMo-00GYXs-ML for pgsql-hackers@arkaria.postgresql.org; Wed, 01 May 2024 02:06:42 +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 1s1zLm-00A1Hm-Fa for pgsql-hackers@arkaria.postgresql.org; Wed, 01 May 2024 02:05:39 +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 1s1zLm-00A1FI-5c for pgsql-hackers@lists.postgresql.org; Wed, 01 May 2024 02:05:39 +0000 Received: from mail-il1-x135.google.com ([2607:f8b0:4864:20::135]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s1zLj-000wGR-M2 for pgsql-hackers@postgresql.org; Wed, 01 May 2024 02:05:38 +0000 Received: by mail-il1-x135.google.com with SMTP id e9e14a558f8ab-36c546b7d04so9420955ab.0 for ; Tue, 30 Apr 2024 19:05:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714529134; x=1715133934; darn=postgresql.org; h=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=8MAGyzg12s5PKpDSi7Mj8iksQBCr7eyL5MB0A3IQYiw=; b=CxkWRzPNZ5amm6H1rXT+G/b2W5KVHEgSn2dGqtEwNFhB10YwjhadUlP7MCbzQWtMq7 wgMv4pmXDvVa4rjprnDGgQHUsooCa4gXVxpRYii8kWdxFhFknIHn2ku/ExncKYj53tIk XJ+b8PvN7jeT0cjXHGM89//lQ+kxJMnuwHiItgcV6N6sIQZV5LAGAJIfPy3mhrq8kecV bi2W6TaF6grwiLoBnyH2yOvvmR4u5dn9rq9qg4Fk0YklbvYNJz0dl1MXhKd1Tb0dUhJE MdPrP5Gk4O7/U4WYjltmKvao4Hoc2K0ABF8qMztpLifpIz2E0m6VkQ5D96oFCxdDOLUi GFDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714529134; x=1715133934; h=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=8MAGyzg12s5PKpDSi7Mj8iksQBCr7eyL5MB0A3IQYiw=; b=fsbVklDiY4aRUCW1Czm8+vMMmEB8E4oJ2nAHZ/BxxtymnUa98E4KnRs+1gB2afYSjE cBluIVJN/oCEFajh1JjbhTqVqlvM3660FSGJgPmLazakZwqSfmt76eNkf1ewY+10yNFm +7fsGVd8K+GhfiipzgoF53FONBnFQ+hDSx9HQCWwggmH7TCeO0d9LvUVzFIcaHZXp/u+ Xs7o5oDvb126ZyR154AA1CeVB1cyoeQYcEIhebX82PhdMST70w/H9qMMLOrVJQN16ARp uVd/NaZaVtGpu/WcpD5VLE3tfzO62UTiGAbmeApBnhG2XC85dxHcHX6IgaurSOZYZYjL +2EA== X-Gm-Message-State: AOJu0YxcYexiwynQ8tjaWR5tx0iibbElV4dkro6XxccR+uuj75Nz9WHv qMF5b+BjcYiQy3bei4mMA3tXzrWNeyDSi3HbIoqvDi/+Wk9hYMot X-Google-Smtp-Source: AGHT+IGQDhi2/oSE7+PYAEPGMpHreu+uESYGCKcP4VXjk3Q6RcTJdpL9biq2aSVbRE1fJLs8bD/Zfg== X-Received: by 2002:a05:6e02:1c2a:b0:36a:ff03:d2c5 with SMTP id m10-20020a056e021c2a00b0036aff03d2c5mr1645077ilh.3.1714529133646; Tue, 30 Apr 2024 19:05:33 -0700 (PDT) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id e8-20020a056e020b2800b0036c5186c271sm1045454ilu.64.2024.04.30.19.05.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 19:05:32 -0700 (PDT) Date: Tue, 30 Apr 2024 21:05:31 -0500 From: Nathan Bossart To: Tom Lane Cc: pgsql-hackers@postgresql.org Subject: Re: pg_sequence_last_value() for unlogged sequences on standbys Message-ID: <20240501020531.GA721953@nathanxps13> References: <20240501005730.GA594666@nathanxps13> <1846474.1714525564@sss.pgh.pa.us> <20240501011317.GC594666@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="7AUc2qLy4jB3hD7Z" Content-Disposition: inline In-Reply-To: <20240501011317.GC594666@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --7AUc2qLy4jB3hD7Z Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Apr 30, 2024 at 08:13:17PM -0500, Nathan Bossart wrote: > Good point. I'll work on a patch along these lines, then. This ended up being easier than I expected. While unlogged sequences are only supported on v15 and above, temporary sequences have been around since v7.2, so this will probably need to be back-patched to all supported versions. The added test case won't work for v12-v14 since it uses an unlogged sequence. I'm not sure it's worth constructing a test case for temporary sequences. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --7AUc2qLy4jB3hD7Z Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-Fix-pg_sequence_last_value-for-non-permanent-sequ.patch" From 71008f13da88f41a205e0643129162df9d2ebc81 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 30 Apr 2024 20:54:51 -0500 Subject: [PATCH v1 1/1] Fix pg_sequence_last_value() for non-permanent sequences on standbys. --- src/backend/commands/sequence.c | 18 +++++++++++++----- src/test/recovery/t/001_stream_rep.pl | 8 ++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index 46103561c3..659d2ad4fc 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -1780,7 +1780,7 @@ pg_sequence_last_value(PG_FUNCTION_ARGS) Buffer buf; HeapTupleData seqtuple; Form_pg_sequence_data seq; - bool is_called; + bool is_called = false; int64 result; /* open and lock sequence */ @@ -1792,12 +1792,20 @@ pg_sequence_last_value(PG_FUNCTION_ARGS) errmsg("permission denied for sequence %s", RelationGetRelationName(seqrel)))); - seq = read_seq_tuple(seqrel, &buf, &seqtuple); + /* + * For the benefit of the pg_sequences system view, we return NULL for + * temporary and unlogged sequences on standbys instead of throwing an + * error. + */ + if (RelationIsPermanent(seqrel) || !RecoveryInProgress()) + { + seq = read_seq_tuple(seqrel, &buf, &seqtuple); - is_called = seq->is_called; - result = seq->last_value; + is_called = seq->is_called; + result = seq->last_value; - UnlockReleaseBuffer(buf); + UnlockReleaseBuffer(buf); + } sequence_close(seqrel, NoLock); if (is_called) diff --git a/src/test/recovery/t/001_stream_rep.pl b/src/test/recovery/t/001_stream_rep.pl index 5311ade509..4c698b5ce1 100644 --- a/src/test/recovery/t/001_stream_rep.pl +++ b/src/test/recovery/t/001_stream_rep.pl @@ -95,6 +95,14 @@ $result = $node_standby_2->safe_psql('postgres', "SELECT * FROM seq1"); print "standby 2: $result\n"; is($result, qq(33|0|t), 'check streamed sequence content on standby 2'); +# Check pg_sequence_last_value() returns NULL for unlogged sequence on standby +$node_primary->safe_psql('postgres', + "CREATE UNLOGGED SEQUENCE ulseq; SELECT nextval('ulseq')"); +$node_primary->wait_for_replay_catchup($node_standby_1); +is($node_standby_1->safe_psql('postgres', + "SELECT pg_sequence_last_value('ulseq'::regclass) IS NULL"), + 't', 'pg_sequence_last_value() on unlogged sequence on standby 1'); + # Check that only READ-only queries can run on standbys is($node_standby_1->psql('postgres', 'INSERT INTO tab_int VALUES (1)'), 3, 'read-only queries on standby 1'); -- 2.25.1 --7AUc2qLy4jB3hD7Z--