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

Commit f077070

Browse files
author
Amit Kapila
committed
Display the names of missing columns in error during logical replication.
In logical replication when a subscriber is missing some columns, it currently emits an error message that says "some" columns are missing, but it doesn't specify the missing column names. Change that to display missing column names which makes an error to be more informative to the user. We have decided not to backpatch this commit as this is a minor usability improvement and no user has reported this. Reported-by: Bharath Rupireddy Author: Bharath Rupireddy Reviewed-by: Kyotaro Horiguchi and Amit Kapila Discussion: https://postgr.es/m/CALj2ACVkW-EXH_4pmBK8tNeHRz5ksUC4WddGactuCjPiBch-cg@mail.gmail.com
1 parent 0691797 commit f077070

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

src/backend/replication/logical/relation.c

+45-10
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,43 @@ logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
228228
return -1;
229229
}
230230

231+
/*
232+
* Report error with names of the missing local relation column(s), if any.
233+
*/
234+
static void
235+
logicalrep_report_missing_attrs(LogicalRepRelation *remoterel,
236+
Bitmapset *missingatts)
237+
{
238+
if (!bms_is_empty(missingatts))
239+
{
240+
StringInfoData missingattsbuf;
241+
int missingattcnt = 0;
242+
int i;
243+
244+
initStringInfo(&missingattsbuf);
245+
246+
while ((i = bms_first_member(missingatts)) >= 0)
247+
{
248+
missingattcnt++;
249+
if (missingattcnt == 1)
250+
appendStringInfo(&missingattsbuf, _("\"%s\""),
251+
remoterel->attnames[i]);
252+
else
253+
appendStringInfo(&missingattsbuf, _(", \"%s\""),
254+
remoterel->attnames[i]);
255+
}
256+
257+
ereport(ERROR,
258+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
259+
errmsg_plural("logical replication target relation \"%s.%s\" is missing replicated column: %s",
260+
"logical replication target relation \"%s.%s\" is missing replicated columns: %s",
261+
missingattcnt,
262+
remoterel->nspname,
263+
remoterel->relname,
264+
missingattsbuf.data)));
265+
}
266+
}
267+
231268
/*
232269
* Open the local relation associated with the remote one.
233270
*
@@ -286,11 +323,11 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
286323
if (!entry->localrelvalid)
287324
{
288325
Oid relid;
289-
int found;
290326
Bitmapset *idkey;
291327
TupleDesc desc;
292328
MemoryContext oldctx;
293329
int i;
330+
Bitmapset *missingatts;
294331

295332
/* Try to find and lock the relation by name. */
296333
relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
@@ -318,7 +355,8 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
318355
entry->attrmap = make_attrmap(desc->natts);
319356
MemoryContextSwitchTo(oldctx);
320357

321-
found = 0;
358+
/* check and report missing attrs, if any */
359+
missingatts = bms_add_range(NULL, 0, remoterel->natts - 1);
322360
for (i = 0; i < desc->natts; i++)
323361
{
324362
int attnum;
@@ -335,16 +373,13 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
335373

336374
entry->attrmap->attnums[i] = attnum;
337375
if (attnum >= 0)
338-
found++;
376+
missingatts = bms_del_member(missingatts, attnum);
339377
}
340378

341-
/* TODO, detail message with names of missing columns */
342-
if (found < remoterel->natts)
343-
ereport(ERROR,
344-
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
345-
errmsg("logical replication target relation \"%s.%s\" is missing "
346-
"some replicated columns",
347-
remoterel->nspname, remoterel->relname)));
379+
logicalrep_report_missing_attrs(remoterel, missingatts);
380+
381+
/* be tidy */
382+
bms_free(missingatts);
348383

349384
/*
350385
* Check that replica identity matches. We allow for stricter replica

0 commit comments

Comments
 (0)