aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2020-09-16 12:07:31 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2020-09-16 12:07:31 -0400
commit8580631ff77c5bb29d0b9fed8a38ee461872e86a (patch)
treee0ea3a59910dfbc3c7c7263e203b86d0b3d91a6d /src
parentaf2d09fa1c28216286649d519f4aca3ebbd5399a (diff)
downloadpostgresql-8580631ff77c5bb29d0b9fed8a38ee461872e86a.tar.gz
postgresql-8580631ff77c5bb29d0b9fed8a38ee461872e86a.zip
Fix bogus cache-invalidation logic in logical replication worker.
The code recorded cache invalidation events by zeroing the "localreloid" field of affected cache entries. However, it's possible for an inval event to occur even while we have the entry open and locked. So an ill-timed inval could result in "cache lookup failed for relation 0" errors, if the worker's code tried to use the cleared field. We can fix that by creating a separate bool field to record whether the entry needs to be revalidated. (In the back branches, cram the bool into what had been padding space, to avoid an ABI break in the somewhat unlikely event that any extension is looking at this struct.) Also, rearrange the logic in logicalrep_rel_open so that it does the right thing in cases where table_open would fail. We should retry the lookup by name in that case, but we didn't. The real-world impact of this is probably small. In the first place, the error conditions are very low probability, and in the second place, the worker would just exit and get restarted. We only noticed because in a CLOBBER_CACHE_ALWAYS build, the failure can occur repeatedly, preventing the worker from making progress. Nonetheless, it's clearly a bug, and it impedes a useful type of testing; so back-patch to v10 where this code was introduced. Discussion: https://postgr.es/m/1032727.1600096803@sss.pgh.pa.us
Diffstat (limited to 'src')
-rw-r--r--src/backend/replication/logical/relation.c66
-rw-r--r--src/include/replication/logicalrelation.h6
2 files changed, 45 insertions, 27 deletions
diff --git a/src/backend/replication/logical/relation.c b/src/backend/replication/logical/relation.c
index c73399b334c..2c1313ba52c 100644
--- a/src/backend/replication/logical/relation.c
+++ b/src/backend/replication/logical/relation.c
@@ -16,6 +16,7 @@
#include "postgres.h"
+#include "access/relation.h"
#include "access/sysattr.h"
#include "access/table.h"
#include "catalog/namespace.h"
@@ -59,7 +60,7 @@ logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
{
if (entry->localreloid == reloid)
{
- entry->localreloid = InvalidOid;
+ entry->localrelvalid = false;
hash_seq_term(&status);
break;
}
@@ -73,7 +74,7 @@ logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid)
hash_seq_init(&status, LogicalRepRelMap);
while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL)
- entry->localreloid = InvalidOid;
+ entry->localrelvalid = false;
}
}
@@ -212,15 +213,13 @@ logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, 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 = InvalidOid;
LogicalRepRelation *remoterel;
if (LogicalRepRelMap == NULL)
@@ -236,14 +235,45 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
remoterel = &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 are
- * processed which can invalidate the relation. We need to update the
- * local cache both when we are first time accessing the relation and when
- * 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 (!OidIsValid(entry->localreloid))
+ if (entry->localrelvalid)
{
+ entry->localrel = try_relation_open(entry->localreloid, lockmode);
+ if (!entry->localrel)
+ {
+ /* Table was renamed or dropped. */
+ entry->localrelvalid = false;
+ }
+ else if (!entry->localrelvalid)
+ {
+ /* Note we release the no-longer-useful lock here. */
+ table_close(entry->localrel, lockmode);
+ entry->localrel = 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 (!entry->localrelvalid)
+ {
+ Oid relid;
+ int found;
+ Bitmapset *idkey;
+ TupleDesc desc;
+ MemoryContext oldctx;
+ int i;
+
/* Try to find and lock the relation by name. */
relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
remoterel->relname, -1),
@@ -254,21 +284,7 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
errmsg("logical replication target relation \"%s.%s\" does not exist",
remoterel->nspname, remoterel->relname)));
entry->localrel = table_open(relid, NoLock);
-
- }
- else
- {
- relid = entry->localreloid;
- entry->localrel = table_open(entry->localreloid, lockmode);
- }
-
- if (!OidIsValid(entry->localreloid))
- {
- int found;
- Bitmapset *idkey;
- TupleDesc desc;
- MemoryContext oldctx;
- int i;
+ entry->localreloid = relid;
/* Check for supported relkind. */
CheckSubscriptionRelkind(entry->localrel->rd_rel->relkind,
@@ -362,7 +378,7 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
}
}
- entry->localreloid = relid;
+ entry->localrelvalid = true;
}
if (entry->state != SUBREL_STATE_READY)
diff --git a/src/include/replication/logicalrelation.h b/src/include/replication/logicalrelation.h
index 2642a3f94ec..dfb1fea48b7 100644
--- a/src/include/replication/logicalrelation.h
+++ b/src/include/replication/logicalrelation.h
@@ -18,14 +18,16 @@ typedef struct LogicalRepRelMapEntry
{
LogicalRepRelation remoterel; /* key is remoterel.remoteid */
- /* Mapping to local relation, filled as needed. */
+ /* Mapping to local relation. */
Oid localreloid; /* local relation id */
- Relation localrel; /* relcache entry */
+ Relation localrel; /* relcache entry (NULL when closed) */
AttrNumber *attrmap; /* map of local attributes to remote ones */
bool updatable; /* Can apply updates/deletes? */
/* Sync state. */
char state;
+ /* Validity flag ... inserted here to avoid ABI break in back branches. */
+ bool localrelvalid;
XLogRecPtr statelsn;
} LogicalRepRelMapEntry;