diff --git a/src/backend/replication/logical/relation.c b/src/backend/replication/logical/relation.c index 46e515e..4300429 100644 --- a/src/backend/replication/logical/relation.c +++ b/src/backend/replication/logical/relation.c @@ -33,9 +33,9 @@ static MemoryContext LogicalRepRelMapContext = NULL; static HTAB *LogicalRepRelMap = NULL; -static HTAB *LogicalRepTypMap = NULL; +static HTAB *LogicalRepRemoteTyp = NULL; -static void logicalrep_typmap_invalidate_cb(Datum arg, int cacheid, +static void logicalrep_remotetyp_invalidate_cb(Datum arg, int cacheid, uint32 hashvalue); /* @@ -72,7 +72,7 @@ logicalrep_relmap_invalidate_cb(Datum arg, Oid reloid) /* invalidate all cache entries */ HASH_SEQ_STATUS status; - hash_seq_init(&status, LogicalRepRelMap); + hash_seq_init(&status, LogicalRepRemoteTyp); while ((entry = (LogicalRepRelMapEntry *) hash_seq_search(&status)) != NULL) entry->localreloid = InvalidOid; @@ -109,13 +109,13 @@ logicalrep_relmap_init(void) ctl.hcxt = LogicalRepRelMapContext; /* This will usually be small. */ - LogicalRepTypMap = hash_create("logicalrep type map cache", 2, &ctl, + LogicalRepRemoteTyp = hash_create("logicalrep remote type cache", 2, &ctl, HASH_ELEM | HASH_BLOBS | HASH_CONTEXT); /* Watch for invalidation events. */ CacheRegisterRelcacheCallback(logicalrep_relmap_invalidate_cb, (Datum) 0); - CacheRegisterSyscacheCallback(TYPEOID, logicalrep_typmap_invalidate_cb, + CacheRegisterSyscacheCallback(TYPEOID, logicalrep_remotetyp_invalidate_cb, (Datum) 0); } @@ -376,58 +376,52 @@ logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode) /* - * Type cache invalidation callback for our type map cache. + * Type cache invalidation callback for our remote type cache. */ static void -logicalrep_typmap_invalidate_cb(Datum arg, int cacheid, uint32 hashvalue) +logicalrep_remotetyp_invalidate_cb(Datum arg, int cacheid, uint32 hashvalue) { HASH_SEQ_STATUS status; - LogicalRepTyp *entry; /* Just to be sure. */ - if (LogicalRepTypMap == NULL) + if (LogicalRepRemoteTyp == NULL) return; /* invalidate all cache entries */ - hash_seq_init(&status, LogicalRepTypMap); - - while ((entry = (LogicalRepTyp *) hash_seq_search(&status)) != NULL) - entry->typoid = InvalidOid; + hash_seq_init(&status, LogicalRepRemoteTyp); } /* - * Free the type map cache entry data. + * Free the remote type cache entry data. */ static void -logicalrep_typmap_free_entry(LogicalRepTyp *entry) +logicalrep_remotetyp_free_entry(LogicalRepTyp *entry) { pfree(entry->nspname); pfree(entry->typname); - - entry->typoid = InvalidOid; } /* - * Add new entry or update existing entry in the type map cache. + * Add new entry or update existing entry in the remote type cache. */ void -logicalrep_typmap_update(LogicalRepTyp *remotetyp) +logicalrep_remotetyp_update(LogicalRepTyp *remotetyp) { MemoryContext oldctx; LogicalRepTyp *entry; bool found; - if (LogicalRepTypMap == NULL) + if (LogicalRepRemoteTyp == NULL) logicalrep_relmap_init(); /* * HASH_ENTER returns the existing entry if present or creates a new one. */ - entry = hash_search(LogicalRepTypMap, (void *) &remotetyp->remoteid, + entry = hash_search(LogicalRepRemoteTyp, (void *) &remotetyp->remoteid, HASH_ENTER, &found); if (found) - logicalrep_typmap_free_entry(entry); + logicalrep_remotetyp_free_entry(entry); /* Make cached copy of the data */ entry->remoteid = remotetyp->remoteid; @@ -435,58 +429,39 @@ logicalrep_typmap_update(LogicalRepTyp *remotetyp) entry->nspname = pstrdup(remotetyp->nspname); entry->typname = pstrdup(remotetyp->typname); MemoryContextSwitchTo(oldctx); - entry->typoid = InvalidOid; } /* - * Fetch type info from the cache. + * Fetch type name from the cache by remote type OID. */ -Oid -logicalrep_typmap_getid(Oid remoteid) +char * +logicalrep_get_remotetypname(Oid remoteid) { LogicalRepTyp *entry; bool found; - Oid nspoid; - /* Internal types are mapped directly. */ + /* If internal types, we can get from local. */ if (remoteid < FirstNormalObjectId) { if (!get_typisdefined(remoteid)) ereport(ERROR, (errmsg("built-in type %u not found", remoteid), errhint("This can be caused by having a publisher with a higher PostgreSQL major version than the subscriber."))); - return remoteid; + return format_type_be(remoteid); } - if (LogicalRepTypMap == NULL) + if (LogicalRepRemoteTyp == NULL) logicalrep_relmap_init(); - /* Try finding the mapping. */ - entry = hash_search(LogicalRepTypMap, (void *) &remoteid, + /* Try finding in cache. */ + entry = hash_search(LogicalRepRemoteTyp, (void *) &remoteid, HASH_FIND, &found); if (!found) elog(ERROR, "no type map entry for remote type %u", remoteid); - /* Found and mapped, return the oid. */ - if (OidIsValid(entry->typoid)) - return entry->typoid; - - /* Otherwise, try to map to local type. */ - nspoid = LookupExplicitNamespace(entry->nspname, true); - if (OidIsValid(nspoid)) - entry->typoid = GetSysCacheOid2(TYPENAMENSP, - PointerGetDatum(entry->typname), - ObjectIdGetDatum(nspoid)); - else - entry->typoid = InvalidOid; - - if (!OidIsValid(entry->typoid)) - ereport(ERROR, - (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), - errmsg("data type \"%s.%s\" required for logical replication does not exist", - entry->nspname, entry->typname))); + Assert(OidIsValid(entry->remoteid)); - return entry->typoid; + return entry->typname; } diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c index e46a62e..1380800 100644 --- a/src/backend/replication/logical/worker.c +++ b/src/backend/replication/logical/worker.c @@ -100,8 +100,8 @@ static dlist_head lsn_mapping = DLIST_STATIC_INIT(lsn_mapping); typedef struct SlotErrCallbackArg { - LogicalRepRelation *rel; - int attnum; + LogicalRepRelMapEntry *rel; + int local_attnum; } SlotErrCallbackArg; static MemoryContext ApplyMessageContext = NULL; @@ -281,20 +281,32 @@ slot_fill_defaults(LogicalRepRelMapEntry *rel, EState *estate, static void slot_store_error_callback(void *arg) { - SlotErrCallbackArg *errarg = (SlotErrCallbackArg *) arg; + SlotErrCallbackArg *errarg = (SlotErrCallbackArg *) arg; + LogicalRepRelMapEntry *rel; + char *remotetypname; + int remote_attnum; Oid remotetypoid, localtypoid; - if (errarg->attnum < 0) + rel = errarg->rel; + remote_attnum = rel->attrmap[errarg->local_attnum]; + + if (remote_attnum < 0) return; - remotetypoid = errarg->rel->atttyps[errarg->attnum]; - localtypoid = logicalrep_typmap_getid(remotetypoid); + remotetypoid = rel->remoterel.atttyps[remote_attnum]; + + /* Fetch remote type name from cache */ + remotetypname = logicalrep_get_remotetypname(remotetypoid); + + /* Fetch local type OID from the local sys cache */ + localtypoid = get_atttype(errarg->rel->localreloid, errarg->local_attnum + 1); + errcontext("processing remote data for replication target relation \"%s.%s\" column \"%s\", " - "remote type %s, local type %s", - errarg->rel->nspname, errarg->rel->relname, - errarg->rel->attnames[errarg->attnum], - format_type_be(remotetypoid), + "remote type \"%s\", local type \"%s\"", + rel->remoterel.nspname, rel->remoterel.relname, + rel->remoterel.attnames[remote_attnum], + remotetypname, format_type_be(localtypoid)); } @@ -315,8 +327,8 @@ slot_store_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel, ExecClearTuple(slot); /* Push callback + info on the error context stack */ - errarg.rel = &rel->remoterel; - errarg.attnum = -1; + errarg.rel = rel; + errarg.local_attnum = -1; errcallback.callback = slot_store_error_callback; errcallback.arg = (void *) &errarg; errcallback.previous = error_context_stack; @@ -334,8 +346,7 @@ slot_store_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel, Oid typinput; Oid typioparam; - errarg.attnum = remoteattnum; - + errarg.local_attnum = i; getTypeInputInfo(att->atttypid, &typinput, &typioparam); slot->tts_values[i] = OidInputFunctionCall(typinput, values[remoteattnum], @@ -380,8 +391,8 @@ slot_modify_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel, ExecClearTuple(slot); /* Push callback + info on the error context stack */ - errarg.rel = &rel->remoterel; - errarg.attnum = -1; + errarg.rel = rel; + errarg.local_attnum = -1; errcallback.callback = slot_store_error_callback; errcallback.arg = (void *) &errarg; errcallback.previous = error_context_stack; @@ -404,7 +415,7 @@ slot_modify_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel, Oid typinput; Oid typioparam; - errarg.attnum = remoteattnum; + errarg.local_attnum = i; getTypeInputInfo(att->atttypid, &typinput, &typioparam); slot->tts_values[i] = OidInputFunctionCall(typinput, @@ -535,7 +546,7 @@ apply_handle_type(StringInfo s) LogicalRepTyp typ; logicalrep_read_typ(s, &typ); - logicalrep_typmap_update(&typ); + logicalrep_remotetyp_update(&typ); } /* diff --git a/src/include/replication/logicalproto.h b/src/include/replication/logicalproto.h index a9736e1..b8a7957 100644 --- a/src/include/replication/logicalproto.h +++ b/src/include/replication/logicalproto.h @@ -52,13 +52,12 @@ typedef struct LogicalRepRelation Bitmapset *attkeys; /* Bitmap of key columns */ } LogicalRepRelation; -/* Type mapping info */ +/* Remote type info */ typedef struct LogicalRepTyp { - Oid remoteid; /* unique id of the type */ - char *nspname; /* schema name */ - char *typname; /* name of the type */ - Oid typoid; /* local type Oid */ + Oid remoteid; /* unique id of the remote type */ + char *nspname; /* schema name of remote type */ + char *typname; /* remote type name */ } LogicalRepTyp; /* Transaction info */ diff --git a/src/include/replication/logicalrelation.h b/src/include/replication/logicalrelation.h index 8352705..1016e68 100644 --- a/src/include/replication/logicalrelation.h +++ b/src/include/replication/logicalrelation.h @@ -36,7 +36,7 @@ extern LogicalRepRelMapEntry *logicalrep_rel_open(LogicalRepRelId remoteid, extern void logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode); -extern void logicalrep_typmap_update(LogicalRepTyp *remotetyp); -extern Oid logicalrep_typmap_getid(Oid remoteid); +extern void logicalrep_remotetyp_update(LogicalRepTyp *remotetyp); +extern char *logicalrep_get_remotetypname(Oid remoteid); #endif /* LOGICALRELATION_H */