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 1l1nNN-0003xE-5u for pgsql-hackers@arkaria.postgresql.org; Tue, 19 Jan 2021 09:32:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l1nMo-0002PE-Jx for pgsql-hackers@arkaria.postgresql.org; Tue, 19 Jan 2021 09:32:02 +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 1l1nMo-0002P7-5h for pgsql-hackers@lists.postgresql.org; Tue, 19 Jan 2021 09:32:02 +0000 Received: from mail-pl1-x62f.google.com ([2607:f8b0:4864:20::62f]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l1nMk-0003uE-Ms for pgsql-hackers@postgresql.org; Tue, 19 Jan 2021 09:32:00 +0000 Received: by mail-pl1-x62f.google.com with SMTP id t6so10177681plq.1 for ; Tue, 19 Jan 2021 01:31:58 -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=a8ehrLe0R+i7pvMFPy74xEBpTpbwkyjUobeUYCwVtoc=; b=HXwNC8WCeGG9JTsCLzZAM8juAiGfIZE+jP2yyYT2TqJSL9qE5secQ+RLc4+O/lCS44 x4+gKNTqpqUY7Ia+Rjo6/ZWT/LqfkPrGWPHBH32vTA4CDdSYQhV7Tsw2j7YveB1/MENG jzhuKAz0v7zGAjlt8aI9F+DsRQYB/3plDGsuk= 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=a8ehrLe0R+i7pvMFPy74xEBpTpbwkyjUobeUYCwVtoc=; b=epvjk8iS9O4MugcYTnC0NYip1Obs3L9JwgmbYhtvp8D1EJOhCvQSU0FIvq4MMuFHrY JCfYzRKACaZPok4YezljHtrF/f27Cw+eTVjflF+CrisLA8T2WcLranX5wwtQsb1k4Ip2 Dt1ZrhkmXtBfKw0I7Gb5xady674Ei2JQgLGmALe0b6EpUSf+eYzM6hitOETIJru7w7UU /BNUWPoTjGmDCr94aycmIQQ8K/Aj6tEsPuUtLkfeMzY3Je44448U3jx+HuSlBF5uN8Re RV6tN6QsE37EWn2Oxg86AORJmZ84tr+p7H8Ba0aMj7/JfOqxtXJ1l6htSFdQ1nvP9zyH ITeA== X-Gm-Message-State: AOAM532F2hIPf9I2CL5+0ijNkKvABzSwhbP8LxyDGHGFi2t0uKhQlLOz U4EXUf7k3hl/v9CcCObeAKByaw== X-Google-Smtp-Source: ABdhPJyoW6HkcoNLQqIgTssPVRuaRFNRsMA16AWeYWmwzS/30DKneljGQzQiLZnsM6IyWKjfGojq6A== X-Received: by 2002:a17:902:9302:b029:da:f6b0:643a with SMTP id bc2-20020a1709029302b02900daf6b0643amr3853524plb.33.1611048716179; Tue, 19 Jan 2021 01:31:56 -0800 (PST) Received: from rfd.leadboat.com ([2600:1702:a20:5750::46]) by smtp.gmail.com with ESMTPSA id gf23sm2245876pjb.42.2021.01.19.01.31.54 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Jan 2021 01:31:55 -0800 (PST) Date: Tue, 19 Jan 2021 01:31:52 -0800 From: Noah Misch To: Kyotaro Horiguchi Cc: pgsql-hackers@postgresql.org, andres@anarazel.de Subject: Re: Wrong usage of RelationNeedsWAL Message-ID: <20210119093152.GA1831573@rfd.leadboat.com> References: <20210118063631.GB1775469@rfd.leadboat.com> <20210118070218.GA1804576@rfd.leadboat.com> <20210118.173022.368003982900161878.horikyota.ntt@gmail.com> <20210119.134831.1956292555969369177.horikyota.ntt@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210119.134831.1956292555969369177.horikyota.ntt@gmail.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 Tue, Jan 19, 2021 at 01:48:31PM +0900, Kyotaro Horiguchi wrote: > At Mon, 18 Jan 2021 17:30:22 +0900 (JST), Kyotaro Horiguchi wrote in > > At Sun, 17 Jan 2021 23:02:18 -0800, Noah Misch wrote in > > > On Sun, Jan 17, 2021 at 10:36:31PM -0800, Noah Misch wrote: > > > > 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: > > > > A> > setup: CREATE TABLE t AS SELECT ...; > B> > xact1: BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1; -- take snapshot > C> > xact2: DELETE FROM t; > D> > (plenty of time passes) > E> > xact3: SELECT count(*) FROM t; -- early pruning > F> > xact1: SAVEPOINT q; SELECT count(*) FROM t; ROLLBACK TO q; -- "snapshot too old" > G> > xact1: ALTER TABLE t SET TABLESPACE something; -- start skipping WAL > H> > xact1: SELECT count(*) FROM t; -- no error, wanted "snapshot too old" > > > > > > Is that plausible? > > > > Thank you for the consideration and yes. But I get "snapshot too old" > > from the last query with the patched version. maybe I'm missing > > something. I'm going to investigate the case. > > Ah. I took that in reverse way. (caught by the discussion on page > LSN.) Ok, the "current" code works that way. So we need to perform the > check the new way in RelationAllowsEarlyPruning. So in a short answer > for the last question in regard to the reference side is yes. Right. I expect the above procedure shows a bug in git master, but your patch versions suffice to fix that bug. > I understand that you are suggesting that at least > TransactionIdLimitedForOldSnapshots should follow not only relation > persistence but RelationNeedsWAL, based on the discussion on the > check on LSN of TestForOldSnapshot(). > > I still don't think that LSN in the WAL-skipping-created relfilenode > harm. ALTER TABLE SET TABLESPACE always makes a dead copy of every > block (except checksum) including page LSN regardless of wal_level. In > the scenario above, the last select at H correctly reads page LSN set > by E then copied by G, which is larger than the snapshot LSN at B. So > doesn't go the fast-return path before actual check performed by > RelationAllowsEarlyPruning. I agree the above procedure doesn't have a problem under your patch versions. See https://postgr.es/m/20210116043816.GA1644261@rfd.leadboat.com for a different test case. In more detail: setup: CREATE TABLE t AS SELECT ...; xact1: BEGIN; DELETE FROM t; xact2: BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1; -- take snapshot xact1: COMMIT; (plenty of time passes, rows of t are now eligible for early pruning) xact3: BEGIN; xact3: ALTER TABLE t SET TABLESPACE something; -- start skipping WAL xact3: SELECT count(*) FROM t; -- early pruning w/o WAL, w/o LSN changes xact3: COMMIT; xact2: SELECT count(*) FROM t; -- no error, wanted "snapshot too old" I expect that shows no bug for git master, but I expect it does show a bug with your patch versions. Could you try implementing both test procedures in src/test/modules/snapshot_too_old? There's no need to make the test use wal_level=minimal explicitly; it's sufficient to catch these bugs when wal_level=minimal is in the TEMP_CONFIG file.