Commit f0770709 authored by Amit Kapila's avatar Amit Kapila

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
parent 06917976
...@@ -228,6 +228,43 @@ logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname) ...@@ -228,6 +228,43 @@ logicalrep_rel_att_by_name(LogicalRepRelation *remoterel, const char *attname)
return -1; return -1;
} }
/*
* Report error with names of the missing local relation column(s), if any.
*/
static void
logicalrep_report_missing_attrs(LogicalRepRelation *remoterel,
Bitmapset *missingatts)
{
if (!bms_is_empty(missingatts))
{
StringInfoData missingattsbuf;
int missingattcnt = 0;
int i;
initStringInfo(&missingattsbuf);
while ((i = bms_first_member(missingatts)) >= 0)
{
missingattcnt++;
if (missingattcnt == 1)
appendStringInfo(&missingattsbuf, _("\"%s\""),
remoterel->attnames[i]);
else
appendStringInfo(&missingattsbuf, _(", \"%s\""),
remoterel->attnames[i]);
}
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg_plural("logical replication target relation \"%s.%s\" is missing replicated column: %s",
"logical replication target relation \"%s.%s\" is missing replicated columns: %s",
missingattcnt,
remoterel->nspname,
remoterel->relname,
missingattsbuf.data)));
}
}
/* /*
* Open the local relation associated with the remote one. * Open the local relation associated with the remote one.
* *
...@@ -286,11 +323,11 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode) ...@@ -286,11 +323,11 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
if (!entry->localrelvalid) if (!entry->localrelvalid)
{ {
Oid relid; Oid relid;
int found;
Bitmapset *idkey; Bitmapset *idkey;
TupleDesc desc; TupleDesc desc;
MemoryContext oldctx; MemoryContext oldctx;
int i; int i;
Bitmapset *missingatts;
/* Try to find and lock the relation by name. */ /* Try to find and lock the relation by name. */
relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname, relid = RangeVarGetRelid(makeRangeVar(remoterel->nspname,
...@@ -318,7 +355,8 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode) ...@@ -318,7 +355,8 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
entry->attrmap = make_attrmap(desc->natts); entry->attrmap = make_attrmap(desc->natts);
MemoryContextSwitchTo(oldctx); MemoryContextSwitchTo(oldctx);
found = 0; /* check and report missing attrs, if any */
missingatts = bms_add_range(NULL, 0, remoterel->natts - 1);
for (i = 0; i < desc->natts; i++) for (i = 0; i < desc->natts; i++)
{ {
int attnum; int attnum;
...@@ -335,16 +373,13 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode) ...@@ -335,16 +373,13 @@ logicalrep_rel_open(LogicalRepRelId remoteid, LOCKMODE lockmode)
entry->attrmap->attnums[i] = attnum; entry->attrmap->attnums[i] = attnum;
if (attnum >= 0) if (attnum >= 0)
found++; missingatts = bms_del_member(missingatts, attnum);
} }
/* TODO, detail message with names of missing columns */ logicalrep_report_missing_attrs(remoterel, missingatts);
if (found < remoterel->natts)
ereport(ERROR, /* be tidy */
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), bms_free(missingatts);
errmsg("logical replication target relation \"%s.%s\" is missing "
"some replicated columns",
remoterel->nspname, remoterel->relname)));
/* /*
* Check that replica identity matches. We allow for stricter replica * Check that replica identity matches. We allow for stricter replica
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment