8000 Add some errdetail to checkRuleResultList(). · hackingwu/postgres@e78791e · GitHub
[go: up one dir, main page]

Skip to content

Commit e78791e

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 9fc8661 commit e78791e

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
@@ -525,6 +525,7 @@ checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect)
525525
foreach(tllist, targetList)
526526
{
527527
TargetEntry *tle = (TargetEntry *) lfirst(tllist);
528+
Oid tletypid;
528529
int32 tletypmod;
529530
Form_pg_attribute attr;
530531
char *attname;
@@ -556,19 +557,32 @@ checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect)
556557
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
557558
errmsg("cannot convert relation containing dropped columns to view")));
558559

560+
/* Check name match if required; no need for two error texts here */
559561
if (isSelect && strcmp(tle->resname, attname) != 0)
560562
ereport(ERROR,
561563
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
562-
errmsg("SELECT rule's target entry %d has different column name from \"%s\"", i, attname)));
563-
564-
if (attr->atttypid != exprType((Node *) tle->expr))
564+
errmsg("SELECT rule's target entry %d has different column name from \"%s\"",
565+
i, attname),
566+
errdetail("SELECT target entry is named \"%s\".",
567+
tle->resname)));
568+
569+
/* Check type match. */
570+
tletypid = exprType((Node *) tle->expr);
571+
if (attr->atttypid != tletypid)
565572
ereport(ERROR,
566573
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
567574
isSelect ?
568575
errmsg("SELECT rule's target entry %d has different type from column \"%s\"",
569576
i, attname) :
570577
errmsg("RETURNING list's entry %d has different type from column \"%s\"",
571-
i, attname)));
578+
i, attname),
579+
isSelect ?
580+
errdetail("SELECT target entry has type %s, but column has type %s.",
581+
format_type_be(tletypid),
582+
format_type_be(attr->atttypid)) :
583+
errdetail("RETURNING list entry has type %s, but column has type %s.",
584+
format_type_be(tletypid),
585+
format_type_be(attr->atttypid))));
572586

573587
/*
574588
* Allow typmods to be different only if one of them is -1, ie,
@@ -585,7 +599,16 @@ checkRuleResultList(List *targetList, TupleDesc resultDesc, bool isSelect)
585599
errmsg("SELECT rule's target entry %d has different size from column \"%s\"",
586600
i, attname) :
587601
errmsg("RETURNING list's entry %d has different size from column \"%s\"",
588-
i, attname)));
602+
i, attname),
603+
isSelect ?
604+
errdetail("SELECT target entry has type %s, but column has type %s.",
605+
format_type_with_typemod(tletypid, tletypmod),
606+
format_type_with_typemod(attr->atttypid,
607+
attr->atttypmod)) :
608+
errdetail("RETURNING list entry has type %s, but column has type %s.",
609+
format_type_with_typemod(tletypid, tletypmod),
610+
format_type_with_typemod(attr->atttypid,
611+
attr->atttypmod))));
589612
}
590613

591614
if (i != resultDesc->natts)

0 commit comments

Comments
 (0)
0