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

Commit cec3be0

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". Back-patch to 9.4, but not into older branches where the set of translatable error strings is supposed to be stable.
1 parent 5520006 commit cec3be0

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/backend/rewrite/rewriteDefine.c

+28-5
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect,
633633
foreach(tllist, targetList)
634634
{
635635
TargetEntry *tle = (TargetEntry *) lfirst(tllist);
636+
Oid tletypid;
636637
int32 tletypmod;
637638
Form_pg_attribute attr;
638639
char *attname;
@@ -664,19 +665,32 @@ checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect,
664665
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
665666
errmsg("cannot convert relation containing dropped columns to view")));
666667

668+
/* Check name match if required; no need for two error texts here */
667669
if (requireColumnNameMatch && strcmp(tle->resname, attname) != 0)
668670
ereport(ERROR,
669671
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
670-
errmsg("SELECT rule's target entry %d has different column name from \"%s\"", i, attname)));
671-
672-
if (attr->atttypid != exprType((Node *) tle->expr))
672+
errmsg("SELECT rule's target entry %d has different column name from column \"%s\"",
673+
i, attname),
674+
errdetail("SELECT target entry is named \"%s\".",
675+
tle->resname)));
676+
677+
/* Check type match. */
678+
tletypid = exprType((Node *) tle->expr);
679+
if (attr->atttypid != tletypid)
673680
ereport(ERROR,
674681
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
675682
isSelect ?
676683
errmsg("SELECT rule's target entry %d has different type from column \"%s\"",
677684
i, attname) :
678685
errmsg("RETURNING list's entry %d has different type from column \"%s\"",
679-
i, attname)));
686+
i, attname),
687+
isSelect ?
688+
errdetail("SELECT target entry has type %s, but column has type %s.",
689+
format_type_be(tletypid),
690+
format_type_be(attr->atttypid)) :
691+
errdetail("RETURNING list entry has type %s, but column has type %s.",
692+
format_type_be(tletypid),
693+
format_type_be(attr->atttypid))));
680694

681695
/*
682696
* Allow typmods to be different only if one of them is -1, ie,
@@ -693,7 +707,16 @@ checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect,
693707
errmsg("SELECT rule's target entry %d has different size from column \"%s\"",
694708
i, attname) :
695709
errmsg("RETURNING list's entry %d has different size from column \"%s\"",
696-
i, attname)));
710+
i, attname),
711+
isSelect ?
712+
errdetail("SELECT target entry has type %s, but column has type %s.",
713+
format_type_with_typemod(tletypid, tletypmod),
714+
format_type_with_typemod(attr->atttypid,
715+
attr->atttypmod)) :
716+
errdetail("RETURNING list entry has type %s, but column has type %s.",
717+
format_type_with_typemod(tletypid, tletypmod),
718+
format_type_with_typemod(attr->atttypid,
719+
attr->atttypmod))));
697720
}
698721

699722
if (i != resultDesc->natts)

0 commit comments

Comments
 (0)