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 1s4PkM-003f7d-BE for pgsql-hackers@arkaria.postgresql.org; Tue, 07 May 2024 18:41:02 +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 1s4PkI-004aeV-NP for pgsql-hackers@arkaria.postgresql.org; Tue, 07 May 2024 18:40:59 +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 1s4PkI-004aeN-AW for pgsql-hackers@lists.postgresql.org; Tue, 07 May 2024 18:40:59 +0000 Received: from mail-oa1-x35.google.com ([2001:4860:4864:20::35]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1s4PkG-0022hH-7r for pgsql-hackers@postgresql.org; Tue, 07 May 2024 18:40:58 +0000 Received: by mail-oa1-x35.google.com with SMTP id 586e51a60fabf-23d3d092a1bso1503992fac.1 for ; Tue, 07 May 2024 11:40:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715107254; x=1715712054; 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=JEWG7sLNOQQzb0mmVtW9OphYUX1PQeZ4Q4oP/ElV6Dk=; b=EMYxI88jdDa40wWUPonOJfIh5GolGDAfbfQ5/iI2D2oEc5blL034hOuRdpdS9y4M2u 4zKyBI9Rawp5An+auPkm6d3pXzqFzup5tRXwa3P+7GpjxwOq9or+qZxXKDhK9MmxFDgU 3oUNiU7WBOi0A+xC3zAmI5SfxzmX6V3vRRe3Oq0jEuLrs8X9LWGcDDUzDp7UKp9aPPZB viHPJcomm8N5zoJL6HOhLPXLAMSXfjtqxj7OoBJjbNgddnoqfqIjFZMmcAMRyp7MueVT wl42RrH84wei2HLt6h12k96FnzASzK2t9zc+azWOy90AQqaWJ2I9rlRqLg0sknq8qEa+ g96Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715107254; x=1715712054; 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=JEWG7sLNOQQzb0mmVtW9OphYUX1PQeZ4Q4oP/ElV6Dk=; b=Qu9UBWC2vSBb5BcmXOQxc+55z9ZNrRqWD6Bcns/MR/CuE+gCEpE7GEVVdmayWto6RE +uCnP419Bj11SmHXXLmmbh/rrcSbfN1cfW7mv2/zY/rb13ettqqNCr+i+aC1LXi2uGRQ XBMLU9+4NqbXfgZag0qmXVGj7P3rD0P65ELBpf7D/9cq31yMlIU0FhVCFagFscGvrBl/ kUbiR5HMoljQTQ+BRSKbolYAy0ZFKZsSx7Yuh/XUHyZuqPA3sBld4BAIsTUP8CP4g/Pl zor5kyDyoorOwA2eP41CmU05FkI+QxpuQlBTYLqHLYnsc/806GzD8XtgmRQqknhvSOJV SGlw== X-Forwarded-Encrypted: i=1; AJvYcCU7N75SrmFM4UHS7YmL9MATIZYvSOG/kD7TCHLOqVt8mgmrtnfoHzoDVdZN16QFuIm3jemeWfuVzEC+HmJAnkgxe3erH3cOfPmBlzjb X-Gm-Message-State: AOJu0YyUj7NgaFa9V5ayhgZZfpAN0cIAUUIwJIPUUazlSUu6OMSJv7r/ +g7KWW7OFCKyFHk3xpA6HMzZfcWnvvsZaHaKCFwdIUGib71kCRIi X-Google-Smtp-Source: AGHT+IFQ68n8u0h1lRV4m+a7D03RbK0O0DV+WWenDRX4TQT2aEM36Gt1goCD8DknyOldjHZBqZAOMQ== X-Received: by 2002:a05:6870:8552:b0:23c:bbe9:858 with SMTP id 586e51a60fabf-2409809af32mr528692fac.16.1715107254184; Tue, 07 May 2024 11:40:54 -0700 (PDT) Received: from nathanxps13 (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id fi3-20020a056638630300b00488d0b9fa40sm557506jab.100.2024.05.07.11.40.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 11:40:53 -0700 (PDT) Date: Tue, 7 May 2024 13:40:51 -0500 From: Nathan Bossart To: Tom Lane Cc: Michael Paquier , pgsql-hackers@postgresql.org Subject: Re: pg_sequence_last_value() for unlogged sequences on standbys Message-ID: <20240507184051.GA2600328@nathanxps13> References: <20240501005730.GA594666@nathanxps13> <1846474.1714525564@sss.pgh.pa.us> <20240501011317.GC594666@nathanxps13> <20240501020531.GA721953@nathanxps13> <20240503204908.GA1737291@nathanxps13> <2746846.1714771326@sss.pgh.pa.us> <20240507171033.GA2524814@nathanxps13> <4179588.1715103856@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="y0ulUmNC+osPPQO6" Content-Disposition: inline In-Reply-To: <4179588.1715103856@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --y0ulUmNC+osPPQO6 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, May 07, 2024 at 01:44:16PM -0400, Tom Lane wrote: > Nathan Bossart writes: >> char relpersist = seqrel->rd_rel->relpersistence; > >> if (relpersist == RELPERSISTENCE_PERMANENT || >> (relpersist == RELPERSISTENCE_UNLOGGED && !RecoveryInProgress()) || >> !RELATION_IS_OTHER_TEMP(seqrel)) >> { >> ... >> } > > Should be AND'ing not OR'ing the !TEMP condition, no? Also I liked > your other formulation of the persistence check better. Yes, that's a silly mistake on my part. I changed it to if ((RelationIsPermanent(seqrel) || !RecoveryInProgress()) && !RELATION_IS_OTHER_TEMP(seqrel)) { ... } in the attached v2. >> I personally think that would be fine to back-patch since pg_sequences >> already filters it out anyway. > > +1 to include that, as it offers a defense if someone invokes this > function directly. In HEAD we could then rip out the test in the > view. I apologize for belaboring this point, but I don't see how we would be comfortable removing that check unless we are okay with other sessions' temporary sequences appearing in the view, albeit with a NULL last_value. This check lives in the WHERE clause today, so if we remove it, we'd no longer exclude those sequences. Michael and you seem united on this, so I have a sinking feeling that I'm missing something terribly obvious. > BTW, I think you also need something like > > - int64 result; > + int64 result = 0; > > Your compiler may not complain about result being possibly > uninitialized, but IME others will. Good call. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --y0ulUmNC+osPPQO6 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v2-0001-Fix-pg_sequence_last_value-for-non-permanent-sequ.patch" From 974f56896add92983b664c11fd25010ef29ac42c Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Tue, 30 Apr 2024 20:54:51 -0500 Subject: [PATCH v2 1/1] Fix pg_sequence_last_value() for non-permanent sequences on standbys. --- src/backend/commands/sequence.c | 22 ++++++++++++++++------ src/test/recovery/t/001_stream_rep.pl | 8 ++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index 46103561c3..9d7468d7bb 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -1780,8 +1780,8 @@ pg_sequence_last_value(PG_FUNCTION_ARGS) Buffer buf; HeapTupleData seqtuple; Form_pg_sequence_data seq; - bool is_called; - int64 result; + bool is_called = false; + int64 result = 0; /* open and lock sequence */ init_sequence(relid, &elm, &seqrel); @@ -1792,12 +1792,22 @@ 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. We also always return NULL for other sessions' temporary + * sequences. + */ + if ((RelationIsPermanent(seqrel) || !RecoveryInProgress()) && + !RELATION_IS_OTHER_TEMP(seqrel)) + { + 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 --y0ulUmNC+osPPQO6--