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

Commit feff615

Browse files
committed
Don't try to print data type names in slot_store_error_callback().
The existing code tried to do syscache lookups in an already-failed transaction, which is problematic to say the least. After some consideration of alternatives, the best fix seems to be to just drop type names from the error message altogether. The table and column names seem like sufficient localization. If the user is unsure what types are involved, she can check the local and remote table definitions. Having done that, we can also discard the LogicalRepTypMap hash table, which had no other use. Arguably, LOGICAL_REP_MSG_TYPE replication messages are now obsolete as well; but we should probably keep them in case some other use emerges. (The complexity of removing something from the replication protocol would likely outweigh any savings anyhow.) Masahiko Sawada and Bharath Rupireddy, per complaint from Andres Freund. Back-patch to v10 where this code originated. Discussion: https://postgr.es/m/20210106020229.ne5xnuu6wlondjpe@alap3.anarazel.de
1 parent 383c29d commit feff615

File tree

3 files changed

+6
-135
lines changed

3 files changed

+6
-135
lines changed

src/backend/replication/logical/relation.c

Lines changed: 1 addition & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,19 @@
1717
#include "postgres.h"
1818

1919
#include "access/relation.h"
20-
#include "access/sysattr.h"
2120
#include "access/table.h"
2221
#include "catalog/namespace.h"
2322
#include "catalog/pg_subscription_rel.h"
2423
#include "executor/executor.h"
2524
#include "nodes/makefuncs.h"
2625
#include "replication/logicalrelation.h"
2726
#include "replication/worker_internal.h"
28-
#include "utils/builtins.h"
2927
#include "utils/inval.h"
30-
#include "utils/lsyscache.h"
31-
#include "utils/memutils.h"
32-
#include "utils/syscache.h"
28+
3329

3430
static MemoryContext LogicalRepRelMapContext = NULL;
3531

3632
static HTAB *LogicalRepRelMap = NULL;
37-
static HTAB *LogicalRepTypMap = NULL;
3833

3934

4035
/*
@@ -101,16 +96,6 @@ logicalrep_relmap_init(void)
10196
LogicalRepRelMap = hash_create("logicalrep relation map cache", 128, &ctl,
10297
HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
10398

104-
/* Initialize the type hash table. */
105-
MemSet(&ctl, 0, sizeof(ctl));
106-
ctl.keysize = sizeof(Oid);
107-
ctl.entrysize = sizeof(LogicalRepTyp);
108-
ctl.hcxt = LogicalRepRelMapContext;
109-
110-
/* This will usually be small. */
111-
LogicalRepTypMap = hash_create("logicalrep type map cache", 2, &ctl,
112-
HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
113-
11499
/* Watch for invalidation events. */
115100
CacheRegisterRelcacheCallback(logicalrep_relmap_invalidate_cb,
116101
(Datum) 0);
@@ -399,92 +384,3 @@ logicalrep_rel_close(LogicalRepRelMapEntry *rel, LOCKMODE lockmode)
399384
table_close(rel->localrel, lockmode);
400385
rel->localrel = NULL;
401386
}
402-
403-
/*
404-
* Free the type map cache entry data.
405-
*/
406-
static void
407-
logicalrep_typmap_free_entry(LogicalRepTyp *entry)
408-
{
409-
pfree(entry->nspname);
410-
pfree(entry->typname);
411-
}
412-
413-
/*
414-
* Add new entry or update existing entry in the type map cache.
415-
*/
416-
void
417-
logicalrep_typmap_update(LogicalRepTyp *remotetyp)
418-
{
419-
MemoryContext oldctx;
420-
LogicalRepTyp *entry;
421-
bool found;
422-
423-
if (LogicalRepTypMap == NULL)
424-
logicalrep_relmap_init();
425-
426-
/*
427-
* HASH_ENTER returns the existing entry if present or creates a new one.
428-
*/
429-
entry = hash_search(LogicalRepTypMap, (void *) &remotetyp->remoteid,
430-
HASH_ENTER, &found);
431-
432-
if (found)
433-
logicalrep_typmap_free_entry(entry);
434-
435-
/* Make cached copy of the data */
436-
entry->remoteid = remotetyp->remoteid;
437-
oldctx = MemoryContextSwitchTo(LogicalRepRelMapContext);
438-
entry->nspname = pstrdup(remotetyp->nspname);
439-
entry->typname = pstrdup(remotetyp->typname);
440-
MemoryContextSwitchTo(oldctx);
441-
}
442-
443-
/*
444-
* Fetch type name from the cache by remote type OID.
445-
*
446-
* Return a substitute value if we cannot find the data type; no message is
447-
* sent to the log in that case, because this is used by error callback
448-
* already.
449-
*/
450-
char *
451-
logicalrep_typmap_gettypname(Oid remoteid)
452-
{
453-
LogicalRepTyp *entry;
454-
bool found;
455-
456-
/* Internal types are mapped directly. */
457-
if (remoteid < FirstGenbkiObjectId)
458-
{
459-
if (!get_typisdefined(remoteid))
460-
{
461-
/*
462-
* This can be caused by having a publisher with a higher
463-
* PostgreSQL major version than the subscriber.
464-
*/
465-
return psprintf("unrecognized %u", remoteid);
466-
}
467-
468-
return format_type_be(remoteid);
469-
}
470-
471-
if (LogicalRepTypMap == NULL)
472-
{
473-
/*
474-
* If the typemap is not initialized yet, we cannot possibly attempt
475-
* to search the hash table; but there's no way we know the type
476-
* locally yet, since we haven't received a message about this type,
477-
* so this is the best we can do.
478-
*/
479-
return psprintf("unrecognized %u", remoteid);
480-
}
481-
482-
/* search the mapping */
483-
entry = hash_search(LogicalRepTypMap, (void *) &remoteid,
484-
HASH_FIND, &found);
485-
if (!found)
486-
return psprintf("unrecognized %u", remoteid);
487-
488-
Assert(OidIsValid(entry->remoteid));
489-
return psprintf("%s.%s", entry->nspname, entry->typname);
490-
}

src/backend/replication/logical/worker.c

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ static dlist_head lsn_mapping = DLIST_STATIC_INIT(lsn_mapping);
8989
typedef struct SlotErrCallbackArg
9090
{
9191
LogicalRepRelMapEntry *rel;
92-
int local_attnum;
9392
int remote_attnum;
9493
} SlotErrCallbackArg;
9594

@@ -288,36 +287,23 @@ slot_fill_defaults(LogicalRepRelMapEntry *rel, EState *estate,
288287
}
289288

290289
/*
291-
* Error callback to give more context info about type conversion failure.
290+
* Error callback to give more context info about data conversion failures
291+
* while reading data from the remote server.
292292
*/
293293
static void
294294
slot_store_error_callback(void *arg)
295295
{
296296
SlotErrCallbackArg *errarg = (SlotErrCallbackArg *) arg;
297297
LogicalRepRelMapEntry *rel;
298-
char *remotetypname;
299-
Oid remotetypoid,
300-
localtypoid;
301298

302299
/* Nothing to do if remote attribute number is not set */
303300
if (errarg->remote_attnum < 0)
304301
return;
305302

306303
rel = errarg->rel;
307-
remotetypoid = rel->remoterel.atttyps[errarg->remote_attnum];
308-
309-
/* Fetch remote type name from the LogicalRepTypMap cache */
310-
remotetypname = logicalrep_typmap_gettypname(remotetypoid);
311-
312-
/* Fetch local type OID from the local sys cache */
313-
localtypoid = get_atttype(rel->localreloid, errarg->local_attnum + 1);
314-
315-
errcontext("processing remote data for replication target relation \"%s.%s\" column \"%s\", "
316-
"remote type %s, local type %s",
304+
errcontext("processing remote data for replication target relation \"%s.%s\" column \"%s\"",
317305
rel->remoterel.nspname, rel->remoterel.relname,
318-
rel->remoterel.attnames[errarg->remote_attnum],
319-
remotetypname,
320-
format_type_be(localtypoid));
306+
rel->remoterel.attnames[errarg->remote_attnum]);
321307
}
322308

323309
/*
@@ -338,7 +324,6 @@ slot_store_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel,
338324

339325
/* Push callback + info on the error context stack */
340326
errarg.rel = rel;
341-
errarg.local_attnum = -1;
342327
errarg.remote_attnum = -1;
343328
errcallback.callback = slot_store_error_callback;
344329
errcallback.arg = (void *) &errarg;
@@ -357,7 +342,6 @@ slot_store_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel,
357342
Oid typinput;
358343
Oid typioparam;
359344

360-
errarg.local_attnum = i;
361345
errarg.remote_attnum = remoteattnum;
362346

363347
getTypeInputInfo(att->atttypid, &typinput, &typioparam);
@@ -366,7 +350,6 @@ slot_store_cstrings(TupleTableSlot *slot, LogicalRepRelMapEntry *rel,
366350
typioparam, att->atttypmod);
367351
slot->tts_isnull[i] = false;
368352

369-
errarg.local_attnum = -1;
370353
errarg.remote_attnum = -1;
371354
}
372355
else
@@ -422,7 +405,6 @@ slot_modify_cstrings(TupleTableSlot *slot, TupleTableSlot *srcslot,
422405

423406
/* For error reporting, push callback + info on the error context stack */
424407
errarg.rel = rel;
425-
errarg.local_attnum = -1;
426408
errarg.remote_attnum = -1;
427409
errcallback.callback = slot_store_error_callback;
428410
errcallback.arg = (void *) &errarg;
@@ -446,7 +428,6 @@ slot_modify_cstrings(TupleTableSlot *slot, TupleTableSlot *srcslot,
446428
Oid typinput;
447429
Oid typioparam;
448430

449-
errarg.local_attnum = i;
450431
errarg.remote_attnum = remoteattnum;
451432

452433
getTypeInputInfo(att->atttypid, &typinput, &typioparam);
@@ -455,7 +436,6 @@ slot_modify_cstrings(TupleTableSlot *slot, TupleTableSlot *srcslot,
455436
typioparam, att->atttypmod);
456437
slot->tts_isnull[i] = false;
457438

458-
errarg.local_attnum = -1;
459439
errarg.remote_attnum = -1;
460440
}
461441
else
@@ -579,16 +559,14 @@ apply_handle_relation(StringInfo s)
579559
/*
580560
* Handle TYPE message.
581561
*
582-
* Note we don't do local mapping here, that's done when the type is
583-
* actually used.
562+
* This is now vestigial; we read the info and discard it.
584563
*/
585564
static void
586565
apply_handle_type(StringInfo s)
587566
{
588567
LogicalRepTyp typ;
589568

590569
logicalrep_read_typ(s, &typ);
591-
logicalrep_typmap_update(&typ);
592570
}
593571

594572
/*

src/include/replication/logicalrelation.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,4 @@ extern LogicalRepRelMapEntry *logicalrep_rel_open(LogicalRepRelId remoteid,
3838
extern void logicalrep_rel_close(LogicalRepRelMapEntry *rel,
3939
LOCKMODE lockmode);
4040

41-
extern void logicalrep_typmap_update(LogicalRepTyp *remotetyp);
42-
extern char *logicalrep_typmap_gettypname(Oid remoteid);
43-
4441
#endif /* LOGICALRELATION_H */

0 commit comments

Comments
 (0)