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 1kIIWq-0001c7-0S for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Sep 2020 21:30:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kIIWo-0000bU-SR for pgsql-hackers@arkaria.postgresql.org; Tue, 15 Sep 2020 21:30:18 +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 1kIIWo-0000bN-HY for pgsql-hackers@lists.postgresql.org; Tue, 15 Sep 2020 21:30:18 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kIIWm-0005vM-8c for pgsql-hackers@lists.postgresql.org; Tue, 15 Sep 2020 21:30:17 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 08FLUFiv1373252 for ; Tue, 15 Sep 2020 17:30:15 -0400 From: Tom Lane To: pgsql-hackers@lists.postgresql.org Subject: Re: Subscription test 013_partition.pl fails under CLOBBER_CACHE_ALWAYS In-reply-to: <1353598.1600199178@sss.pgh.pa.us> References: <1032727.1600096803@sss.pgh.pa.us> <1353598.1600199178@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Tue, 15 Sep 2020 15:46:18 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <1372325.1600205358.0@sss.pgh.pa.us> Date: Tue, 15 Sep 2020 17:30:15 -0400 Message-ID: <1373250.1600205415@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1372325.1600205358.1@sss.pgh.pa.us> I wrote: > It's not really clear to me why setting localreloid to zero is a sane > way to represent "this entry needs to be revalidated". I think a > separate flag would be more appropriate. Once we have lock on the > target relation, it seems to me that no interesting changes should > be possible as long as we have lock; so there's no very good reason > to destroy useful state to remind ourselves that we should recheck > it next time. Here's a patch that changes that, and also cleans up some sloppy thinking about how to re-acquire lock on the replication target relation. (Just because the OID was valid last you heard does not mean that table_open is guaranteed to succeed.) With this, we get through 013_partition.pl under CCA. I plan to try to run all of subscription/ and recovery/ before concluding there's nothing else to fix, though. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-logical-rep-cache-flush.patch"; charset="us-ascii" Content-ID: <1372325.1600205358.2@sss.pgh.pa.us> Content-Description: fix-logical-rep-cache-flush.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/replication/logical/relation.c b/src/backend/repl= ication/logical/relation.c index 3d2d56295b..9ee70a2563 100644 --- a/src/backend/replication/logical/relation.c +++ b/src/backend/replication/logical/relation.c @@ -77,7 +77,7 @@ logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid) { if (entry->localreloid =3D=3D reloid) { - entry->localreloid =3D InvalidOid; + entry->localrelvalid =3D false; hash_seq_term(&status); break; } @@ -91,7 +91,7 @@ logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid) hash_seq_init(&status, LogicalRepRelMap); = while ((entry =3D (LogicalRepRelMapEntry *) hash_seq_search(&status)) != =3D NULL) - entry->localreloid =3D InvalidOid; + entry->localrelvalid =3D false; } } = @@ -230,15 +230,13 @@ logicalrep_rel_att_by_name(LogicalRepRelation *remot= erel, const char *attname) /* * Open the local relation associated with the remote one. * - * Optionally rebuilds the Relcache mapping if it was invalidated - * by local DDL. + * Rebuilds the Relcache mapping if it was invalidated by local DDL. */ LogicalRepRelMapEntry * logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode) { LogicalRepRelMapEntry *entry; bool found; - Oid relid =3D InvalidOid; LogicalRepRelation *remoterel; = if (LogicalRepRelMap =3D=3D NULL) @@ -254,14 +252,45 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMO= DE lockmode) = remoterel =3D &entry->remoterel; = + /* Ensure we don't leak a relcache refcount. */ + if (entry->localrel) + elog(ERROR, "remote relation ID %u is already open", remoteid); + /* * When opening and locking a relation, pending invalidation messages ar= e - * processed which can invalidate the relation. We need to update the - * local cache both when we are first time accessing the relation and wh= en - * the relation is invalidated (aka entry->localreloid is set InvalidOid= ). + * processed which can invalidate the relation. Hence, if the entry is + * currently considered valid, try to open the local relation by OID and + * see if invalidation ensues. + */ + if (entry->localrelvalid) + { + entry->localrel =3D try_table_open(entry->localreloid, lockmode); + if (!entry->localrel) + { + /* Table was renamed or dropped. */ + entry->localrelvalid =3D false; + } + else if (!entry->localrelvalid) + { + /* Note we release the no-longer-useful lock here. */ + table_close(entry->localrel, lockmode); + entry->localrel =3D NULL; + } + } + + /* + * If the entry has been marked invalid since we last had lock on it, + * re-open the local relation by name and rebuild all derived data. */ - if (!OidIsValid(entry->localreloid)) + if (!entry->localrelvalid) { + Oid relid; + int found; + Bitmapset *idkey; + TupleDesc desc; + MemoryContext oldctx; + int i; + /* Try to find and lock the relation by name. */ relid =3D RangeVarGetRelid(makeRangeVar(remoterel->nspname, remoterel->relname, -1), @@ -272,21 +301,7 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMOD= E lockmode) errmsg("logical replication target relation \"%s.%s\" does not exis= t", remoterel->nspname, remoterel->relname))); entry->localrel =3D table_open(relid, NoLock); - - } - else - { - relid =3D entry->localreloid; - entry->localrel =3D table_open(entry->localreloid, lockmode); - } - - if (!OidIsValid(entry->localreloid)) - { - int found; - Bitmapset *idkey; - TupleDesc desc; - MemoryContext oldctx; - int i; + entry->localreloid =3D relid; = /* Check for supported relkind. */ CheckSubscriptionRelkind(entry->localrel->rd_rel->relkind, @@ -380,7 +395,7 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE= lockmode) } } = - entry->localreloid =3D relid; + entry->localrelvalid =3D true; } = if (entry->state !=3D SUBREL_STATE_READY) @@ -523,7 +538,7 @@ logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid= ) { if (entry->localreloid =3D=3D reloid) { - entry->localreloid =3D InvalidOid; + entry->localrelvalid =3D false; hash_seq_term(&status); break; } @@ -537,7 +552,7 @@ logicalrep_partmap_invalidate_cb(Datum arg, Oid reloid= ) hash_seq_init(&status, LogicalRepPartMap); = while ((entry =3D (LogicalRepRelMapEntry *) hash_seq_search(&status)) != =3D NULL) - entry->localreloid =3D InvalidOid; + entry->localrelvalid =3D false; } } = @@ -656,6 +671,8 @@ logicalrep_partition_open(LogicalRepRelMapEntry *root, = entry->updatable =3D root->updatable; = + entry->localrelvalid =3D true; + /* state and statelsn are left set to 0. */ MemoryContextSwitchTo(oldctx); = diff --git a/src/include/replication/logicalrelation.h b/src/include/repli= cation/logicalrelation.h index a6b44b12bd..62ddd3c7a2 100644 --- a/src/include/replication/logicalrelation.h +++ b/src/include/replication/logicalrelation.h @@ -19,9 +19,16 @@ typedef struct LogicalRepRelMapEntry { LogicalRepRelation remoterel; /* key is remoterel.remoteid */ = - /* Mapping to local relation, filled as needed. */ + /* + * Validity flag -- when false, revalidate all derived info at next + * logicalrep_rel_open. (While the localrel is open, we assume our lock + * on that rel ensures the info remains good.) + */ + bool localrelvalid; + + /* Mapping to local relation. */ Oid localreloid; /* local relation id */ - Relation localrel; /* relcache entry */ + Relation localrel; /* relcache entry (NULL when closed) */ AttrMap *attrmap; /* map of local attributes to remote ones */ bool updatable; /* Can apply updates/deletes? */ = ------- =_aaaaaaaaaa0--