Skip to content

Commit 110d293

Browse files
committed
Add some errdetail to checkRuleResultList().
This function wasn't originally thought to be really user-facing, because converting a table to a view isn't something we expect people to do manually. So not all that much effort was spent on the error messages; in particular, while the code will complain that you got the column types wrong it won't say exactly what they are. But since we repurposed the code to also check compatibility of rule RETURNING lists, it's definitely user-facing. It now seems worthwhile to add errdetail messages showing exactly what the conflict is when there's a mismatch of column names or types. This is prompted by bug #10836 from Matthias Raffelsieper, which might have been forestalled if the error message had reported the wrong column type as being "record". Per Alvaro's advice, back-patch to branches before 9.4, but resist the temptation to rephrase any existing strings there. Adding new strings is not really a translation degradation; anyway having the info presented in English is better than not having it at all.
1 parent 174421b commit 110d293

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/backend/rewrite/rewriteDefine.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect,
636636
foreach(tllist, targetList)
637637
{
638638
TargetEntry *tle = (TargetEntry *) lfirst(tllist);
639+
Oid tletypid;
639640
int32 tletypmod;
640641
Form_pg_attribute attr;
641642
char *attname;
@@ -667,19 +668,32 @@ checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect,
667668
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
668669
errmsg("cannot convert relation containing dropped columns to view")));
669670

671+
/* Check name match if required; no need for two error texts here */
670672
if (requireColumnNameMatch && strcmp(tle->resname, attname) != 0)
671673
ereport(ERROR,
672674
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
673-
errmsg("SELECT rule's target entry %d has different column name from \"%s\"", i, attname)));
674-
675-
if (attr->atttypid != exprType((Node *) tle->expr))
675+
errmsg("SELECT rule's target entry %d has different column name from \"%s\"",
676+
i, attname),
677+
errdetail("SELECT target entry is named \"%s\".",
678+
tle->resname)));
679+
680+
/* Check type match. */
681+
tletypid = exprType((Node *) tle->expr);
682+
if (attr->atttypid != tletypid)
676683
ereport(ERROR,
677684
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
678685
isSelect ?
679686
errmsg("SELECT rule's target entry %d has different type from column \"%s\"",
680687
i, attname) :
681688
errmsg("RETURNING list's entry %d has different type from column \"%s\"",
682-
i, attname)));
689+
i, attname),
690+
isSelect ?
691+
errdetail("SELECT target entry has type %s, but column has type %s.",
692+
format_type_be(tletypid),
693+
format_type_be(attr->atttypid)) :
694+
errdetail("RETURNING list entry has type %s, but column has type %s.",
695+
format_type_be(tletypid),
696+
format_type_be(attr->atttypid))));
683697

684698
/*
685699
* Allow typmods to be different only if one of them is -1, ie,
@@ -696,7 +710,16 @@ checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect,
696710
errmsg("SELECT rule's target entry %d has different size from column \"%s\"",
697711
i, attname) :
698712
errmsg("RETURNING list's entry %d has different size from column \"%s\"",
699-
i, attname)));
713+
i, attname),
714+
isSelect ?
715+
errdetail("SELECT target entry has type %s, but column has type %s.",
716+
format_type_with_typemod(tletypid, tletypmod),
717+
format_type_with_typemod(attr->atttypid,
718+
attr->atttypmod)) :
719+
errdetail("RETURNING list entry has type %s, but column has type %s.",
720+
format_type_with_typemod(tletypid, tletypmod),
721+
format_type_with_typemod(attr->atttypid,
722+
attr->atttypmod))));
700723
}
701724

702725
if (i != resultDesc->natts)

0 commit comments

Comments
 (0)