Skip to content

Commit 981518e

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 c66256b commit 981518e

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
@@ -539,6 +539,7 @@ checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect)
539539
foreach(tllist, targetList)
540540
{
541541
TargetEntry *tle = (TargetEntry *) lfirst(tllist);
542+
Oid tletypid;
542543
int32 tletypmod;
543544
Form_pg_attribute attr;
544545
char *attname;
@@ -570,19 +571,32 @@ checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect)
570571
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
571572
errmsg("cannot convert relation containing dropped columns to view")));
572573

574+
/* Check name match if required; no need for two error texts here */
573575
if (isSelect && strcmp(tle->resname, attname) != 0)
574576
ereport(ERROR,
575577
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
576-
errmsg("SELECT rule's target entry %d has different column name from \"%s\"", i, attname)));
577-
578-
if (attr->atttypid != exprType((Node *) tle->expr))
578+
errmsg("SELECT rule's target entry %d has different column name from \"%s\"",
579+
i, attname),
580+
errdetail("SELECT target entry is named \"%s\".",
581+
tle->resname)));
582+
583+
/* Check type match. */
584+
tletypid = exprType((Node *) tle->expr);
585+
if (attr->atttypid != tletypid)
579586
ereport(ERROR,
580587
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
581588
isSelect ?
582589
errmsg("SELECT rule's target entry %d has different type from column \"%s\"",
583590
i, attname) :
584591
errmsg("RETURNING list's entry %d has different type from column \"%s\"",
585-
i, attname)));
592+
i, attname),
593+
isSelect ?
594+
errdetail("SELECT target entry has type %s, but column has type %s.",
595+
format_type_be(tletypid),
596+
format_type_be(attr->atttypid)) :
597+
errdetail("RETURNING list entry has type %s, but column has type %s.",
598+
format_type_be(tletypid),
599+
format_type_be(attr->atttypid))));
586600

587601
/*
588602
* Allow typmods to be different only if one of them is -1, ie,
@@ -599,7 +613,16 @@ checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect)
599613
errmsg("SELECT rule's target entry %d has different size from column \"%s\"",
600614
i, attname) :
601615
errmsg("RETURNING list's entry %d has different size from column \"%s\"",
602-
i, attname)));
616+
i, attname),
617+
isSelect ?
618+
errdetail("SELECT target entry has type %s, but column has type %s.",
619+
format_type_with_typemod(tletypid, tletypmod),
620+
format_type_with_typemod(attr->atttypid,
621+
attr->atttypmod)) :
622+
errdetail("RETURNING list entry has type %s, but column has type %s.",
623+
format_type_with_typemod(tletypid, tletypmod),
624+
format_type_with_typemod(attr->atttypid,
625+
attr->atttypmod))));
603626
}
604627

605628
if (i != resultDesc->natts)

0 commit comments

Comments
 (0)