diff options
-rw-r--r-- | src/backend/parser/parse_relation.c | 11 | ||||
-rw-r--r-- | src/test/regress/expected/rules.out | 22 | ||||
-rw-r--r-- | src/test/regress/sql/rules.sql | 15 |
3 files changed, 46 insertions, 2 deletions
diff --git a/src/backend/parser/parse_relation.c b/src/backend/parser/parse_relation.c index 5359e691dd1..a97db6bfa8f 100644 --- a/src/backend/parser/parse_relation.c +++ b/src/backend/parser/parse_relation.c @@ -501,10 +501,17 @@ scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, char *colname, attnum = specialAttNum(colname); if (attnum != InvalidAttrNumber) { - /* now check to see if column actually is defined */ + /* + * Now check to see if column actually is defined. Because of + * an ancient oversight in DefineQueryRewrite, it's possible that + * pg_attribute contains entries for system columns for a view, + * even though views should not have such --- so we also check + * the relkind. This kluge will not be needed in 9.3 and later. + */ if (SearchSysCacheExists2(ATTNUM, ObjectIdGetDatum(rte->relid), - Int16GetDatum(attnum))) + Int16GetDatum(attnum)) && + get_rel_relkind(rte->relid) != RELKIND_VIEW) { var = make_var(pstate, rte, attnum, location); /* Require read access to the column */ diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out index 20cdc39752d..53a4b2cfb93 100644 --- a/src/test/regress/expected/rules.out +++ b/src/test/regress/expected/rules.out @@ -1459,6 +1459,28 @@ ERROR: cannot drop rule _RETURN on view fooview because view fooview requires i HINT: You can drop view fooview instead. drop view fooview; -- +-- test conversion of table to view (needed to load some pg_dump files) +-- +create table fooview (x int, y text); +select xmin, * from fooview; + xmin | x | y +------+---+--- +(0 rows) + +create rule "_RETURN" as on select to fooview do instead + select 1 as x, 'aaa'::text as y; +select * from fooview; + x | y +---+----- + 1 | aaa +(1 row) + +select xmin, * from fooview; -- fail, views don't have such a column +ERROR: column "xmin" does not exist +LINE 1: select xmin, * from fooview; + ^ +drop view fooview; +-- -- check for planner problems with complex inherited UPDATES -- create table id (id serial primary key, name text); diff --git a/src/test/regress/sql/rules.sql b/src/test/regress/sql/rules.sql index 16dc106ab07..5ec8bbb63fa 100644 --- a/src/test/regress/sql/rules.sql +++ b/src/test/regress/sql/rules.sql @@ -860,6 +860,21 @@ drop rule "_RETURN" on fooview; drop view fooview; -- +-- test conversion of table to view (needed to load some pg_dump files) +-- + +create table fooview (x int, y text); +select xmin, * from fooview; + +create rule "_RETURN" as on select to fooview do instead + select 1 as x, 'aaa'::text as y; + +select * from fooview; +select xmin, * from fooview; -- fail, views don't have such a column + +drop view fooview; + +-- -- check for planner problems with complex inherited UPDATES -- |