Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3c3450e

Browse files
committed
logical replication: fix OID type mapping mechanism
The logical replication type map seems to have been misused by its only caller -- it would try to use the remote OID as input for local type routines, which unsurprisingly could result in bogus "cache lookup failed for type XYZ" errors, or random other type names being picked up if they happened to use the right OID. Fix that, changing Oid logicalrep_typmap_getid(Oid remoteid) to char *logicalrep_typmap_gettypname(Oid remoteid) which is more useful. If the remote type is not part of the typmap, this simply prints "unrecognized type" instead of choking trying to figure out -- a pointless exercise (because the only input for that comes from replication messages, which are not under the local node's control) and dangerous to boot, when called from within an error context callback. Once that is done, it comes to light that the local OID in the typmap entry was not being used for anything; the type/schema names are what we need, so remove local type OID from that struct. Once you do that, it becomes pointless to attach a callback to regular syscache invalidation. So remove that also. Reported-by: Dang Minh Huong Author: Masahiko Sawada Reviewed-by: Álvaro Herrera, Petr Jelínek, Dang Minh Huong, Atsushi Torikoshi Discussion: https://postgr.es/m/75DB81BEEA95B445AE6D576A0A5C9E936A6BE964@BPXM05GP.gisp.nec.co.jp Discussion: https://postgr.es/m/75DB81BEEA95B445AE6D576A0A5C9E936A6C4B0A@BPXM05GP.gisp.nec.co.jp
1 parent eadcb7a commit 3c3450e

File tree

4 files changed

+74
-88
lines changed

4 files changed

+74
-88
lines changed

src/backend/replication/logical/relation.c

Lines changed: 29 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ static MemoryContext LogicalRepRelMapContext = NULL;
3535
static HTAB *LogicalRepRelMap = NULL;
3636
static HTAB *LogicalRepTypMap = NULL;
3737

38-
static void logicalrep_typmap_invalidate_cb(Datum arg, int cacheid,
39-
uint32 hashvalue);
4038

4139
/*
4240
* Relcache invalidation callback for our relation map cache.
@@ -115,8 +113,6 @@ logicalrep_relmap_init(void)
115113
/* Watch for invalidation events. */
116114
CacheRegisterRelcacheCallback(logicalrep_relmap_invalidate_cb,
117115
(Datum) 0);
118-
CacheRegisterSyscacheCallback(TYPEOID, logicalrep_typmap_invalidate_cb,
119-
(Datum) 0);
120116
}
121117

122118
/*
@@ -374,27 +370,6 @@ logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
374370
rel->localrel = NULL;
375371
}
376372

377-
378-
/*
379-
* Type cache invalidation callback for our type map cache.
380-
*/
381-
static void
382-
logicalrep_typmap_invalidate_cb(Datum arg, int cacheid, uint32 hashvalue)
383-
{
384-
HASH_SEQ_STATUS status;
385-
LogicalRepTyp *entry;
386-
387-
/* Just to be sure. */
388-
if (LogicalRepTypMap == NULL)
389-
return;
390-
391-
/* invalidate all cache entries */
392-
hash_seq_init(&status, LogicalRepTypMap);
393-
394-
while ((entry = (LogicalRepTyp *) hash_seq_search(&status)) != NULL)
395-
entry->typoid = InvalidOid;
396-
}
397-
398373
/*
399374
* Free the type map cache entry data.
400375
*/
@@ -403,8 +378,6 @@ logicalrep_typmap_free_entry(LogicalRepTyp *entry)
403378
{
404379
pfree(entry->nspname);
405380
pfree(entry->typname);
406-
407-
entry->typoid = InvalidOid;
408381
}
409382

410383
/*
@@ -435,58 +408,53 @@ logicalrep_typmap_update(LogicalRepTyp *remotetyp)
435408
entry->nspname = pstrdup(remotetyp->nspname);
436409
entry->typname = pstrdup(remotetyp->typname);
437410
MemoryContextSwitchTo(oldctx);
438-
entry->typoid = InvalidOid;
439411
}
440412

441413
/*
442-
* Fetch type info from the cache.
414+
* Fetch type name from the cache by remote type OID.
415+
*
416+
* Return a substitute value if we cannot find the data type; no message is
417+
* sent to the log in that case, because this is used by error callback
418+
* already.
443419
*/
444-
Oid
445-
logicalrep_typmap_getid(Oid remoteid)
420+
char *
421+
logicalrep_typmap_gettypname(Oid remoteid)
446422
{
447423
LogicalRepTyp *entry;
448424
bool found;
449-
Oid nspoid;
450425

451426
/* Internal types are mapped directly. */
452427
if (remoteid < FirstNormalObjectId)
453428
{
454429
if (!get_typisdefined(remoteid))
455-
ereport(ERROR,
456-
(errmsg("built-in type %u not found", remoteid),
457-
errhint("This can be caused by having a publisher with a higher PostgreSQL major version than the subscriber.")));
458-
return remoteid;
430+
{
431+
/*
432+
* This can be caused by having a publisher with a higher
433+
* PostgreSQL major version than the subscriber.
434+
*/
435+
return psprintf("unrecognized %u", remoteid);
436+
}
437+
438+
return format_type_be(remoteid);
459439
}
460440

461441
if (LogicalRepTypMap == NULL)
462-
logicalrep_relmap_init();
442+
{
443+
/*
444+
* If the typemap is not initialized yet, we cannot possibly attempt
445+
* to search the hash table; but there's no way we know the type
446+
* locally yet, since we haven't received a message about this type,
447+
* so this is the best we can do.
448+
*/
449+
return psprintf("unrecognized %u", remoteid);
450+
}
463451

464-
/* Try finding the mapping. */
452+
/* search the mapping */
465453
entry = hash_search(LogicalRepTypMap, (void *) &remoteid,
466454
HASH_FIND, &found);
467-
468455
if (!found)
469-
elog(ERROR, "no type map entry for remote type %u",
470-
remoteid);
471-
472-
/* Found and mapped, return the oid. */
473-
if (OidIsValid(entry->typoid))
474-
return entry->typoid;
475-
476-
/* Otherwise, try to map to local type. */
477-
nspoid = LookupExplicitNamespace(entry->nspname, true);
478-
if (OidIsValid(nspoid))
479-
entry->typoid = GetSysCacheOid2(TYPENAMENSP,
480-
PointerGetDatum(entry->typname),
481-
ObjectIdGetDatum(nspoid));
482-
else
483-
entry->typoid = InvalidOid;
484-
485-
if (!OidIsValid(entry->typoid))
486-
ereport(ERROR,
487-
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
488-
errmsg("data type \"%s.%s\" required for logical replication does not exist",
489-
entry->nspname, entry->typname)));
456+
return psprintf("unrecognized %u", remoteid);
490457

491-
return entry->typoid;
458+
Assert(OidIsValid(entry->remoteid));
459+
return psprintf("%s.%s", entry->nspname, entry->typname);
492460
}

src/backend/replication/logical/worker.c

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ static dlist_head lsn_mapping = DLIST_STATIC_INIT(lsn_mapping);
100100

101101
typedef struct SlotErrCallbackArg
102102
{
103-
LogicalRepRelation *rel;
104-
int attnum;
103+
LogicalRepRelMapEntry *rel;
104+
int local_attnum;
105+
int remote_attnum;
105106
} SlotErrCallbackArg;
106107

107108
static MemoryContext ApplyMessageContext = NULL;
@@ -282,19 +283,29 @@ static void
282283
slot_store_error_callback(void *arg)
283284
{
284285
SlotErrCallbackArg *errarg = (SlotErrCallbackArg *) arg;
286+
LogicalRepRelMapEntry *rel;
287+
char *remotetypname;
285288
Oid remotetypoid,
286289
localtypoid;
287290

288-
if (errarg->attnum < 0)
291+
/* Nothing to do if remote attribute number is not set */
292+
if (errarg->remote_attnum < 0)
289293
return;
290294

291-
remotetypoid = errarg->rel->atttyps[errarg->attnum];
292-
localtypoid = logicalrep_typmap_getid(remotetypoid);
295+
rel = errarg->rel;
296+
remotetypoid = rel->remoterel.atttyps[errarg->remote_attnum];
297+
298+
/* Fetch remote type name from the LogicalRepTypMap cache */
299+
remotetypname = logicalrep_typmap_gettypname(remotetypoid);
300+
301+
/* Fetch local type OID from the local sys cache */
302+
localtypoid = get_atttype(rel->localreloid, errarg->local_attnum + 1);
303+
293304
errcontext("processing remote data for replication target relation \"%s.%s\" column \"%s\", "
294305
"remote type %s, local type %s",
295-
errarg->rel->nspname, errarg->rel->relname,
296-
errarg->rel->attnames[errarg->attnum],
297-
format_type_be(remotetypoid),
306+
rel->remoterel.nspname, rel->remoterel.relname,
307+
rel->remoterel.attnames[errarg->remote_attnum],
308+
remotetypname,
298309
format_type_be(localtypoid));
299310
}
300311

@@ -315,8 +326,9 @@ slot_store_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel,
315326
ExecClearTuple(slot);
316327

317328
/* Push callback + info on the error context stack */
318-
errarg.rel = &rel->remoterel;
319-
errarg.attnum = -1;
329+
errarg.rel = rel;
330+
errarg.local_attnum = -1;
331+
errarg.remote_attnum = -1;
320332
errcallback.callback = slot_store_error_callback;
321333
errcallback.arg = (void *) &errarg;
322334
errcallback.previous = error_context_stack;
@@ -334,14 +346,17 @@ slot_store_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel,
334346
Oid typinput;
335347
Oid typioparam;
336348

337-
errarg.attnum = remoteattnum;
349+
errarg.local_attnum = i;
350+
errarg.remote_attnum = remoteattnum;
338351

339352
getTypeInputInfo(att->atttypid, &typinput, &typioparam);
340-
slot->tts_values[i] = OidInputFunctionCall(typinput,
341-
values[remoteattnum],
342-
typioparam,
343-
att->atttypmod);
353+
slot->tts_values[i] =
354+
OidInputFunctionCall(typinput, values[remoteattnum],
355+
typioparam, att->atttypmod);
344356
slot->tts_isnull[i] = false;
357+
358+
errarg.local_attnum = -1;
359+
errarg.remote_attnum = -1;
345360
}
346361
else
347362
{
@@ -380,8 +395,9 @@ slot_modify_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel,
380395
ExecClearTuple(slot);
381396

382397
/* Push callback + info on the error context stack */
383-
errarg.rel = &rel->remoterel;
384-
errarg.attnum = -1;
398+
errarg.rel = rel;
399+
errarg.local_attnum = -1;
400+
errarg.remote_attnum = -1;
385401
errcallback.callback = slot_store_error_callback;
386402
errcallback.arg = (void *) &errarg;
387403
errcallback.previous = error_context_stack;
@@ -404,14 +420,17 @@ slot_modify_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel,
404420
Oid typinput;
405421
Oid typioparam;
406422

407-
errarg.attnum = remoteattnum;
423+
errarg.local_attnum = i;
424+
errarg.remote_attnum = remoteattnum;
408425

409426
getTypeInputInfo(att->atttypid, &typinput, &typioparam);
410-
slot->tts_values[i] = OidInputFunctionCall(typinput,
411-
values[remoteattnum],
412-
typioparam,
413-
att->atttypmod);
427+
slot->tts_values[i] =
428+
OidInputFunctionCall(typinput, values[remoteattnum],
429+
typioparam, att->atttypmod);
414430
slot->tts_isnull[i] = false;
431+
432+
errarg.local_attnum = -1;
433+
errarg.remote_attnum = -1;
415434
}
416435
else
417436
{

src/include/replication/logicalproto.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ typedef struct LogicalRepRelation
5555
/* Type mapping info */
5656
typedef struct LogicalRepTyp
5757
{
58-
Oid remoteid; /* unique id of the type */
59-
char *nspname; /* schema name */
60-
char *typname; /* name of the type */
61-
Oid typoid; /* local type Oid */
58+
Oid remoteid; /* unique id of the remote type */
59+
char *nspname; /* schema name of remote type */
60+
char *typname; /* name of the remote type */
6261
} LogicalRepTyp;
6362

6463
/* Transaction info */

src/include/replication/logicalrelation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ extern void logicalrep_rel_close(LogicalRepRelMapEntry *rel,
3737
LOCKMODE lockmode);
3838

3939
extern void logicalrep_typmap_update(LogicalRepTyp *remotetyp);
40-
extern Oid logicalrep_typmap_getid(Oid remoteid);
40+
extern char *logicalrep_typmap_gettypname(Oid remoteid);
4141

4242
#endif /* LOGICALRELATION_H */

0 commit comments

Comments
 (0)