Skip to content

Commit 19956e0

Browse files
committed
Add error location info to ResTarget parse nodes. Allows error cursor to be supplied
for various mistakes involving INSERT and UPDATE target columns.
1 parent a3f0b3d commit 19956e0

File tree

13 files changed

+132
-42
lines changed

13 files changed

+132
-42
lines changed

src/backend/commands/analyze.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.92 2006/03/05 15:58:23 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/analyze.c,v 1.93 2006/03/23 00:19:28 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -216,6 +216,11 @@ analyze_rel(Oid relid, VacuumStmt *vacstmt)
216216
char *col = strVal(lfirst(le));
217217

218218
i = attnameAttNum(onerel, col, false);
219+
if (i == InvalidAttrNumber)
220+
ereport(ERROR,
221+
(errcode(ERRCODE_UNDEFINED_COLUMN),
222+
errmsg("column \"%s\" of relation \"%s\" does not exist",
223+
col, RelationGetRelationName(onerel))));
219224
vacattrstats[tcnt] = examine_attribute(onerel, i);
220225
if (vacattrstats[tcnt] != NULL)
221226
tcnt++;

src/backend/commands/copy.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.260 2006/03/05 15:58:23 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.261 2006/03/23 00:19:29 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -3109,9 +3109,14 @@ CopyGetAttnums(Relation rel, List *attnamelist)
31093109
char *name = strVal(lfirst(l));
31103110
int attnum;
31113111

3112-
/* Lookup column name, ereport on failure */
3112+
/* Lookup column name */
31133113
/* Note we disallow system columns here */
31143114
attnum = attnameAttNum(rel, name, false);
3115+
if (attnum == InvalidAttrNumber)
3116+
ereport(ERROR,
3117+
(errcode(ERRCODE_UNDEFINED_COLUMN),
3118+
errmsg("column \"%s\" of relation \"%s\" does not exist",
3119+
name, RelationGetRelationName(rel))));
31153120
/* Check for duplicates */
31163121
if (list_member_int(attnums, attnum))
31173122
ereport(ERROR,

src/backend/nodes/copyfuncs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.331 2006/03/16 00:31:54 tgl Exp $
18+
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.332 2006/03/23 00:19:29 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -1520,6 +1520,7 @@ _copyResTarget(ResTarget *from)
15201520
COPY_STRING_FIELD(name);
15211521
COPY_NODE_FIELD(indirection);
15221522
COPY_NODE_FIELD(val);
1523+
COPY_SCALAR_FIELD(location);
15231524

15241525
return newnode;
15251526
}

src/backend/nodes/equalfuncs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
2020
* IDENTIFICATION
21-
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.267 2006/03/16 00:31:54 tgl Exp $
21+
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.268 2006/03/23 00:19:29 tgl Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -1608,6 +1608,7 @@ _equalResTarget(ResTarget *a, ResTarget *b)
16081608
COMPARE_STRING_FIELD(name);
16091609
COMPARE_NODE_FIELD(indirection);
16101610
COMPARE_NODE_FIELD(val);
1611+
COMPARE_SCALAR_FIELD(location);
16111612

16121613
return true;
16131614
}

src/backend/nodes/outfuncs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.271 2006/03/16 00:31:54 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/nodes/outfuncs.c,v 1.272 2006/03/23 00:19:29 tgl Exp $
1212
*
1313
* NOTES
1414
* Every node type that can appear in stored rules' parsetrees *must*
@@ -1738,6 +1738,7 @@ _outResTarget(StringInfo str, ResTarget *node)
17381738
WRITE_STRING_FIELD(name);
17391739
WRITE_NODE_FIELD(indirection);
17401740
WRITE_NODE_FIELD(val);
1741+
WRITE_INT_FIELD(location);
17411742
}
17421743

17431744
static void

src/backend/parser/analyze.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.331 2006/03/14 22:48:20 tgl Exp $
9+
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.332 2006/03/23 00:19:29 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -700,7 +700,7 @@ transformInsertStmt(ParseState *pstate, InsertStmt *stmt,
700700

701701
Assert(!tle->resjunk);
702702
updateTargetListEntry(pstate, tle, col->name, lfirst_int(attnos),
703-
col->indirection);
703+
col->indirection, col->location);
704704

705705
icols = lnext(icols);
706706
attnos = lnext(attnos);
@@ -2360,6 +2360,7 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
23602360
{
23612361
TargetEntry *tle = (TargetEntry *) lfirst(tl);
23622362
ResTarget *origTarget;
2363+
int attrno;
23632364

23642365
if (tle->resjunk)
23652366
{
@@ -2378,10 +2379,20 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt)
23782379
origTarget = (ResTarget *) lfirst(origTargetList);
23792380
Assert(IsA(origTarget, ResTarget));
23802381

2382+
attrno = attnameAttNum(pstate->p_target_relation,
2383+
origTarget->name, true);
2384+
if (attrno == InvalidAttrNumber)
2385+
ereport(ERROR,
2386+
(errcode(ERRCODE_UNDEFINED_COLUMN),
2387+
errmsg("column \"%s\" of relation \"%s\" does not exist",
2388+
origTarget->name,
2389+
RelationGetRelationName(pstate->p_target_relation)),
2390+
parser_errposition(pstate, origTarget->location)));
2391+
23812392
updateTargetListEntry(pstate, tle, origTarget->name,
2382-
attnameAttNum(pstate->p_target_relation,
2383-
origTarget->name, true),
2384-
origTarget->indirection);
2393+
attrno,
2394+
origTarget->indirection,
2395+
origTarget->location);
23852396

23862397
origTargetList = lnext(origTargetList);
23872398
}

src/backend/parser/gram.y

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.536 2006/03/14 23:03:20 tgl Exp $
14+
* $PostgreSQL: pgsql/src/backend/parser/gram.y,v 2.537 2006/03/23 00:19:29 tgl Exp $
1515
*
1616
* HISTORY
1717
* AUTHOR DATE MAJOR EVENT
@@ -5187,6 +5187,7 @@ insert_column_item:
51875187
$$->name = $1;
51885188
$$->indirection = $2;
51895189
$$->val = NULL;
5190+
$$->location = @1;
51905191
}
51915192
;
51925193

@@ -7918,13 +7919,15 @@ target_el: a_expr AS ColLabel
79187919
$$->name = $3;
79197920
$$->indirection = NIL;
79207921
$$->val = (Node *)$1;
7922+
$$->location = @1;
79217923
}
79227924
| a_expr
79237925
{
79247926
$$ = makeNode(ResTarget);
79257927
$$->name = NULL;
79267928
$$->indirection = NIL;
79277929
$$->val = (Node *)$1;
7930+
$$->location = @1;
79287931
}
79297932
| '*'
79307933
{
@@ -7936,6 +7939,7 @@ target_el: a_expr AS ColLabel
79367939
$$->name = NULL;
79377940
$$->indirection = NIL;
79387941
$$->val = (Node *)n;
7942+
$$->location = @1;
79397943
}
79407944
;
79417945

@@ -7951,13 +7955,15 @@ update_target_el:
79517955
$$->name = $1;
79527956
$$->indirection = $2;
79537957
$$->val = (Node *) $4;
7958+
$$->location = @1;
79547959
}
79557960
| ColId opt_indirection '=' DEFAULT
79567961
{
79577962
$$ = makeNode(ResTarget);
79587963
$$->name = $1;
79597964
$$->indirection = $2;
79607965
$$->val = (Node *) makeNode(SetToDefault);
7966+
$$->location = @1;
79617967
}
79627968

79637969
;
@@ -7974,13 +7980,15 @@ insert_target_el:
79747980
$$->name = NULL;
79757981
$$->indirection = NIL;
79767982
$$->val = (Node *)$1;
7983+
$$->location = @1;
79777984
}
79787985
| DEFAULT
79797986
{
79807987
$$ = makeNode(ResTarget);
79817988
$$->name = NULL;
79827989
$$->indirection = NIL;
79837990
$$->val = (Node *) makeNode(SetToDefault);
7991+
$$->location = @1;
79847992
}
79857993
;
79867994

src/backend/parser/parse_relation.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/parser/parse_relation.c,v 1.121 2006/03/16 00:31:55 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/parser/parse_relation.c,v 1.122 2006/03/23 00:19:30 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1703,7 +1703,9 @@ get_tle_by_resno(List *tlist, AttrNumber resno)
17031703
}
17041704

17051705
/*
1706-
* given relation and att name, return id of variable
1706+
* given relation and att name, return attnum of variable
1707+
*
1708+
* Returns InvalidAttrNumber if the attr doesn't exist (or is dropped).
17071709
*
17081710
* This should only be used if the relation is already
17091711
* heap_open()'ed. Use the cache version get_attnum()
@@ -1732,11 +1734,7 @@ attnameAttNum(Relation rd, const char *attname, bool sysColOK)
17321734
}
17331735

17341736
/* on failure */
1735-
ereport(ERROR,
1736-
(errcode(ERRCODE_UNDEFINED_COLUMN),
1737-
errmsg("column \"%s\" of relation \"%s\" does not exist",
1738-
attname, RelationGetRelationName(rd))));
1739-
return InvalidAttrNumber; /* keep compiler quiet */
1737+
return InvalidAttrNumber;
17401738
}
17411739

17421740
/* specialAttNum()

0 commit comments

Comments
 (0)