aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/commands/tablecmds.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index cef975afb97..93f41ed1ffa 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -9954,6 +9954,11 @@ ATExecValidateConstraint(List **wqueue, Relation rel, char *constrName,
* transformColumnNameList - transform list of column names
*
* Lookup each name and return its attnum and type OID
+ *
+ * Note: the name of this function suggests that it's general-purpose,
+ * but actually it's only used to look up names appearing in foreign-key
+ * clauses. The error messages would need work to use it in other cases,
+ * and perhaps the validity checks as well.
*/
static int
transformColumnNameList(Oid relId, List *colList,
@@ -9967,6 +9972,7 @@ transformColumnNameList(Oid relId, List *colList,
{
char *attname = strVal(lfirst(l));
HeapTuple atttuple;
+ Form_pg_attribute attform;
atttuple = SearchSysCacheAttName(relId, attname);
if (!HeapTupleIsValid(atttuple))
@@ -9974,13 +9980,18 @@ transformColumnNameList(Oid relId, List *colList,
(errcode(ERRCODE_UNDEFINED_COLUMN),
errmsg("column \"%s\" referenced in foreign key constraint does not exist",
attname)));
+ attform = (Form_pg_attribute) GETSTRUCT(atttuple);
+ if (attform->attnum < 0)
+ ereport(ERROR,
+ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+ errmsg("system columns cannot be used in foreign keys")));
if (attnum >= INDEX_MAX_KEYS)
ereport(ERROR,
(errcode(ERRCODE_TOO_MANY_COLUMNS),
errmsg("cannot have more than %d keys in a foreign key",
INDEX_MAX_KEYS)));
- attnums[attnum] = ((Form_pg_attribute) GETSTRUCT(atttuple))->attnum;
- atttypids[attnum] = ((Form_pg_attribute) GETSTRUCT(atttuple))->atttypid;
+ attnums[attnum] = attform->attnum;
+ atttypids[attnum] = attform->atttypid;
ReleaseSysCache(atttuple);
attnum++;
}