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 1l0dM5-0001DA-JE for pgsql-hackers@arkaria.postgresql.org; Sat, 16 Jan 2021 04:38:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l0dM4-0001C0-IJ for pgsql-hackers@arkaria.postgresql.org; Sat, 16 Jan 2021 04:38:28 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l0dM3-0001Bt-VI for pgsql-hackers@lists.postgresql.org; Sat, 16 Jan 2021 04:38:28 +0000 Received: from mail-pf1-x435.google.com ([2607:f8b0:4864:20::435]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l0dLx-0006QV-Cy for pgsql-hackers@postgresql.org; Sat, 16 Jan 2021 04:38:26 +0000 Received: by mail-pf1-x435.google.com with SMTP id w2so6773576pfc.13 for ; Fri, 15 Jan 2021 20:38:21 -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=DWSmv/ljCC0j/aWdGUoVqP1gbwZikeRGOD4wYBvsHlY=; b=SRrct+n6PL8cy20JWKCuRT0mVHdGHnHGfJtZ8CoauleKiX/xFrihNCRUYcndyJsLuA xCHKjBgjrAXXLjBDKsCsERYwm9PDeMtzIIC1IuZtXDx8xd7wGOt2DRvvmP0pvAIG7iuq kAS8zniuajZyVZaxl7O+TGJxPkCVcqlFuaYQg= 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=DWSmv/ljCC0j/aWdGUoVqP1gbwZikeRGOD4wYBvsHlY=; b=ItCjdyJrxcbMON1DpmI3oa0FQlfbygi5RIDhPgChhhMDYOC4RpQjzlg7Y8y23EUwZz krV1c0QSjZRahIyE3QRrPFWvWhdYsRP11UtvxZC8fCw6XJridZCfFEptM/F4jspFNpW/ Hl21rkcry5WI2BEvnC2REdh23r2skDtaZ+mV0zw29MpNMykD1BvTqyEGQu/XhwfxEdWd yAdm70e0eHLr10e20IuWl3ALHgf1Ge/Z39vpl68gF7aQMtNwxJeDa3kCvL5zilv33FWy N69QTxRQFGJdPTiI9C90k8ZQLa8N2gjjxuNeiFVbwFb905VUOpbtH+Z+47WzLrbIzXJY HDRw== X-Gm-Message-State: AOAM533m5TmnlQnlYxkw6gcOGF3YPLDB0ooQpMx/BoeiegMydS7bW2CL nF990a6hjaaOGU+cmTVHR0E8CA== X-Google-Smtp-Source: ABdhPJyYESyLgPHFWQ7lju4vIFoD5Wd9PktnYLWZQZ3pm083PvB6d8SiZA6DkGFKGinc5gTiFfA1KQ== X-Received: by 2002:a63:1315:: with SMTP id i21mr15945727pgl.370.1610771899651; Fri, 15 Jan 2021 20:38:19 -0800 (PST) Received: from rfd.leadboat.com ([2600:1702:a20:5750::46]) by smtp.gmail.com with ESMTPSA id t6sm358165pfc.64.2021.01.15.20.38.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Jan 2021 20:38:18 -0800 (PST) Date: Fri, 15 Jan 2021 20:38:16 -0800 From: Noah Misch To: Kyotaro Horiguchi Cc: pgsql-hackers@postgresql.org, andres@anarazel.de Subject: Re: Wrong usage of RelationNeedsWAL Message-ID: <20210116043816.GA1644261@rfd.leadboat.com> References: <20210113.160705.2225256954956139776.horikyota.ntt@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210113.160705.2225256954956139776.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 Wed, Jan 13, 2021 at 04:07:05PM +0900, Kyotaro Horiguchi wrote: > The definition of the macro RelationNeedsWAL has been changed by > c6b92041d3 to include conditions related to the WAL-skip optimization > but some uses of the macro are not relevant to the optimization. That > misuses are harmless for now as they are only executed while wal_level > >= replica or WAL-skipping is inactive. However, this should be > corrected to prevent future hazard. I see user-visible consequences: > --- a/src/backend/catalog/pg_publication.c > +++ b/src/backend/catalog/pg_publication.c > @@ -67,7 +67,7 @@ check_publication_add_relation(Relation targetrel) > errdetail("System tables cannot be added to publications."))); > > /* UNLOGGED and TEMP relations cannot be part of publication. */ > - if (!RelationNeedsWAL(targetrel)) > + if (!RelationIsWalLogged(targetrel)) Today, the following fails needlessly under wal_level=minimal: BEGIN; SET client_min_messages = 'ERROR'; CREATE TABLE skip_wal (); CREATE PUBLICATION p FOR TABLE skip_wal; ROLLBACK; Could you add that test to one of the regress scripts? > ereport(ERROR, > (errcode(ERRCODE_INVALID_PARAMETER_VALUE), > errmsg("table \"%s\" cannot be replicated", > diff --git a/src/backend/optimizer/util/plancat.c b/src/backend/optimizer/util/plancat.c > index da322b453e..0500efcdb9 100644 > --- a/src/backend/optimizer/util/plancat.c > +++ b/src/backend/optimizer/util/plancat.c > @@ -126,7 +126,7 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent, > relation = table_open(relationObjectId, NoLock); > > /* Temporary and unlogged relations are inaccessible during recovery. */ > - if (!RelationNeedsWAL(relation) && RecoveryInProgress()) > + if (!RelationIsWalLogged(relation) && RecoveryInProgress()) This has no user-visible consequences, but I agree you've clarified it. > ereport(ERROR, > (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), > errmsg("cannot access temporary or unlogged relations during recovery"))); > diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h > index 10b63982c0..810806a542 100644 > --- a/src/include/utils/rel.h > +++ b/src/include/utils/rel.h > @@ -552,16 +552,23 @@ typedef struct ViewOptions > (relation)->rd_smgr->smgr_targblock = (targblock); \ > } while (0) > > +/* > + * RelationIsPermanent > + * True if relation is WAL-logged. > + */ > +#define RelationIsWalLogged(relation) \ > + ((relation)->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT) To me, the names RelationIsWalLogged() and RelationNeedsWAL() don't convey their behavior difference. How about one of the following spellings? - Name the new macro RelationEverNeedsWAL(). - Just test "relpersistence == RELPERSISTENCE_PERMANENT", without a macro. > + > /* > * RelationNeedsWAL > - * True if relation needs WAL. > + * True if relation needs WAL at the time. > * > * Returns false if wal_level = minimal and this relation is created or > * truncated in the current transaction. See "Skipping WAL for New > * RelFileNode" in src/backend/access/transam/README. > */ > #define RelationNeedsWAL(relation) \ > - ((relation)->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT && \ > + (RelationIsWalLogged(relation) && \ > (XLogIsNeeded() || \ > (relation->rd_createSubid == InvalidSubTransactionId && \ > relation->rd_firstRelfilenodeSubid == InvalidSubTransactionId))) > @@ -619,7 +626,7 @@ typedef struct ViewOptions > */ > #define RelationIsAccessibleInLogicalDecoding(relation) \ > (XLogLogicalInfoActive() && \ > - RelationNeedsWAL(relation) && \ > + RelationIsWalLogged(relation) && \ Today's condition expands to: wal_level >= WAL_LEVEL_LOGICAL && relation->rd_rel->relpersistence == RELPERSISTENCE_PERMANENT && (wal_level >= WAL_LEVEL_REPLICA || ...) The proposed change doesn't affect semantics, and it likely doesn't change optimized code. I slightly prefer to leave this line unchanged. > (IsCatalogRelation(relation) || RelationIsUsedAsCatalogTable(relation))) > > /* > @@ -635,7 +642,7 @@ typedef struct ViewOptions > */ > #define RelationIsLogicallyLogged(relation) \ > (XLogLogicalInfoActive() && \ > - RelationNeedsWAL(relation) && \ > + RelationIsWalLogged(relation) && \ Likewise. > !IsCatalogRelation(relation)) > > /* routines in utils/cache/relcache.c */ > diff --git a/src/include/utils/snapmgr.h b/src/include/utils/snapmgr.h > index 579be352c5..7be922a9f1 100644 > --- 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?