[Bug] SELECT INSTEAD with sub-query

Lists: pgsql-hackers
From: Kohei KaiGai <kaigai(at)kaigai(dot)gr(dot)jp>
To: PgHacker <pgsql-hackers(at)postgresql(dot)org>
Subject: [Bug] SELECT INSTEAD with sub-query
Date: 2012-10-22 21:05:25
Message-ID: CADyhKSUWa8wqWXTH59_NRj3a=nSooAnHJhmB_e+yS+RSC+=x1A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

I could find out a case to cause Assert() failure during investigation of RLS...

postgres=# CREATE TABLE t1 (x int, y text);
CREATE TABLE

postgres=# CREATE RULE "_RETURN" AS ON SELECT TO t1 DO INSTEAD SELECT
1 AS x, 'aaa'::text AS y;
CREATE RULE

postgres=# SELECT * FROM t1;
x | y
---+-----
1 | aaa
(1 row)

postgres=# SELECT tableoid, * FROM t1;
TRAP: FailedAssertion("!(attno >= rel->min_attr && attno <=
rel->max_attr)", File: "initsplan.c", Line: 180)
The connection to the server was lost. Attempting reset: LOG: server
process (PID 27345) was terminated by signal 6: Aborted
DETAIL: Failed process was running: SELECT tableoid, * FROM t1;

The failure scenario is obvious. Reference to "t1" was replaced by a sub-query
that does not have any system columns, however, the given query tries to the
column with negative attribute number. Then, it was caught on the
FailedAssertion.
How do we fix the problem? Please give us some comments.

Thanks,
--
KaiGai Kohei <kaigai(at)kaigai(dot)gr(dot)jp>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Kohei KaiGai <kaigai(at)kaigai(dot)gr(dot)jp>
Cc: PgHacker <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [Bug] SELECT INSTEAD with sub-query
Date: 2012-10-23 02:07:04
Message-ID: 29751.1350958024@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Kohei KaiGai <kaigai(at)kaigai(dot)gr(dot)jp> writes:
> postgres=# CREATE TABLE t1 (x int, y text);
> CREATE TABLE

> postgres=# CREATE RULE "_RETURN" AS ON SELECT TO t1 DO INSTEAD SELECT
> 1 AS x, 'aaa'::text AS y;
> CREATE RULE

> postgres=# SELECT * FROM t1;
> x | y
> ---+-----
> 1 | aaa
> (1 row)

> postgres=# SELECT tableoid, * FROM t1;
> TRAP: FailedAssertion("!(attno >= rel->min_attr && attno <=
> rel->max_attr)", File: "initsplan.c", Line: 180)

Huh. I'm amazed nobody noticed this before. It's really a bug in the
"convert table to view" logic: a view has no system columns so we ought
to remove the pg_attribute entries for the system columns, but we don't.

Given that we can't retroactively fix the pg_attribute rows for existing
views, I guess that we'll also have to put in a defense in the parser to
not believe that views have any system columns, even if pg_attribute
claims they do.

regards, tom lane