Interesting CREATE TABLE AS misbehavior

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)postgreSQL(dot)org
Subject: Interesting CREATE TABLE AS misbehavior
Date: 2006-09-18 02:10:52
Message-ID: 11824.1158545452@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

regression=# select * from int8_tbl union all select * from int8_tbl order by q2;
q1 | q2
------------------+-------------------
4567890123456789 | -4567890123456789
4567890123456789 | -4567890123456789
4567890123456789 | 123
4567890123456789 | 123
123 | 456
123 | 456
4567890123456789 | 4567890123456789
123 | 4567890123456789
4567890123456789 | 4567890123456789
123 | 4567890123456789
(10 rows)

regression=# create table fooey(z1,z2) as select * from int8_tbl union all select * from int8_tbl order by q2;
ERROR: column "q2" does not exist
LINE 1: ... from int8_tbl union all select * from int8_tbl order by q2;
^

The reason for this behavior is that analyze.c attaches the CREATE TABLE
AS aliases to the targetlist (via applyColumnNames) before it processes
the ORDER BY clause. (So, "order by z2" works instead.) This seems
like a bug: one would expect that the aliases do not change the
semantics of the SELECT part of the command.

Interestingly, 7.2 seems to get it right, the misbehavior appears in
7.3 and later.

I'm inclined to fix this in HEAD but not back-patch it, on the grounds
that there might be apps out there expecting the existing behavior,
and it's not a big enough deal to change behavior in a minor release.
Thoughts?

regards, tom lane

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2006-09-18 02:23:19 Re: 8.2 beta blockers
Previous Message Jim Nasby 2006-09-18 02:07:48 Re: Mid cycle release?