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 1l1OYW-0004CF-Rg for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Jan 2021 07:02:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l1OYU-0001rb-B8 for pgsql-hackers@arkaria.postgresql.org; Mon, 18 Jan 2021 07:02:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l1OYU-0001rO-43 for pgsql-hackers@lists.postgresql.org; Mon, 18 Jan 2021 07:02:26 +0000 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l1OYS-0007Ea-0L for pgsql-hackers@postgresql.org; Mon, 18 Jan 2021 07:02:25 +0000 Received: by mail-pj1-x102b.google.com with SMTP id l23so9158642pjg.1 for ; Sun, 17 Jan 2021 23:02:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=5u4WtppMBegAx97a4kIP8uxhMR/rTH85cYKG4/FDYYE=; b=VupCUAWDW1xkabmWhJUiLd3Bx3B1nh9ITMmNEgrUjnOW7vW8T3VS+wWffw6x9Gperb MfHjmuhucRpnN+EIfAjdKRn3yZMsPsk3qCiyBumj7nVh+H5VL6QtxB75oL4m/RV8CVk9 l2BcgXF6JOQgyvuQueRSzZt61ogU1XoZ1Qjl0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=5u4WtppMBegAx97a4kIP8uxhMR/rTH85cYKG4/FDYYE=; b=g7iFOXWTzV/T2m3nu76b1xbbgnXGe1mkXl5qpoWjd1uwwH82IZWRQ+IMITZ60KkLyM l9HOr/ens2WB7hlAswVmnFFFoZIjNswor9c3hUu6EpKXvdGdOBPd32fcPMIHaZRuZ3mj gD166BuGKu2aIgCVau0pgr4irOnb/8NkN8YDHBVpQLnjsDYyvYdJ35MKGIsEM4fkxLM9 MsrnERYIbApeiFDhz7DKmyUaQ60khp+YNrJGInRXkjP3BfdMXxYjW4CUlgf+MJ8M/7rZ 9MJ3GV8d3kB4qxePahu9aCxrenQ7UITx1kVshRviR91+csV08Z30WL6V6afL/OQ4n0gC jiWg== X-Gm-Message-State: AOAM532yNGNuYEAlQ2PMLWr5uCOmCiQfgnMMXoEO2snEmSj7i3OyKYYo xKS2A0vgQbkEbQZM9FzjHr5JPw== X-Google-Smtp-Source: ABdhPJywHQgRyFl05JD14m44BMcwx7239L4XFcreA7tcwGtOQflP6+/44VT9rKDgMWyRC61o8x2uQA== X-Received: by 2002:a17:90a:7d08:: with SMTP id g8mr24997076pjl.180.1610953341242; Sun, 17 Jan 2021 23:02:21 -0800 (PST) Received: from rfd.leadboat.com ([2600:1702:a20:5750::46]) by smtp.gmail.com with ESMTPSA id w192sm2628979pff.181.2021.01.17.23.02.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 17 Jan 2021 23:02:20 -0800 (PST) Date: Sun, 17 Jan 2021 23:02:18 -0800 From: Noah Misch To: Kyotaro Horiguchi Cc: pgsql-hackers@postgresql.org, andres@anarazel.de Subject: Re: Wrong usage of RelationNeedsWAL Message-ID: <20210118070218.GA1804576@rfd.leadboat.com> References: <20210113.160705.2225256954956139776.horikyota.ntt@gmail.com> <20210116043816.GA1644261@rfd.leadboat.com> <20210118.150838.864061275414573742.horikyota.ntt@gmail.com> <20210118063631.GB1775469@rfd.leadboat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210118063631.GB1775469@rfd.leadboat.com> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Sun, Jan 17, 2021 at 10:36:31PM -0800, Noah Misch wrote: > On Mon, Jan 18, 2021 at 03:08:38PM +0900, Kyotaro Horiguchi wrote: > > At Fri, 15 Jan 2021 20:38:16 -0800, Noah Misch wrote in > > > On Wed, Jan 13, 2021 at 04:07:05PM +0900, Kyotaro Horiguchi wrote: > > > > --- a/src/include/utils/snapmgr.h > > > > +++ b/src/include/utils/snapmgr.h > > > > @@ -37,7 +37,7 @@ > > > > */ > > > > #define RelationAllowsEarlyPruning(rel) \ > > > > ( \ > > > > - RelationNeedsWAL(rel) \ > > > > + RelationIsWalLogged(rel) \ > > > > > > I suspect this is user-visible for a scenario like: > > > > > > CREATE TABLE t AS SELECT ...; DELETE FROM t; > > > -- ... time passes, rows of t are now eligible for early pruning ... > > > BEGIN; > > > ALTER TABLE t SET TABLESPACE something; -- start skipping WAL > > > SELECT count(*) FROM t; > > > > > > After this patch, the SELECT would do early pruning, as it does in the absence > > > of the ALTER TABLE. When pruning doesn't update the page LSN, > > > TestForOldSnapshot() will be unable to detect that early pruning changed the > > > query results. Hence, RelationAllowsEarlyPruning() must not change this way. > > > Does that sound right? > > > > Mmm, maybe no. The pruning works on XID (or timestamp), not on LSN, so > > it seems to work well even if pruning happened at the SELECT. > > Conversely that should work after old_snapshot_threshold elapsed. > > > > Am I missing something? > > I wrote the above based on the "PageGetLSN(page) > (snapshot)->lsn" check in > TestForOldSnapshot(). If the LSN isn't important, what else explains > RelationAllowsEarlyPruning() checking RelationNeedsWAL()? Thinking about it more, some RelationAllowsEarlyPruning() callers need different treatment. Above, I was writing about the case of deciding whether to do early pruning. The other RelationAllowsEarlyPruning() call sites are deciding whether the relation might be lacking old data. For that case, we should check RELPERSISTENCE_PERMANENT, not RelationNeedsWAL(). Otherwise, we could fail to report an old-snapshot error in a case like this: setup: CREATE TABLE t AS SELECT ...; xact1: BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1; -- take snapshot xact2: DELETE FROM t; (plenty of time passes) xact3: SELECT count(*) FROM t; -- early pruning xact1: SAVEPOINT q; SELECT count(*) FROM t; ROLLBACK TO q; -- "snapshot too old" xact1: ALTER TABLE t SET TABLESPACE something; -- start skipping WAL xact1: SELECT count(*) FROM t; -- no error, wanted "snapshot too old" Is that plausible?