BUG #6774: FOR IN SELECT LOOP ignores ORDER BY

From: boris(at)folgmann(dot)de
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #6774: FOR IN SELECT LOOP ignores ORDER BY
Date: 2012-07-27 14:56:18
Message-ID: E1Sulxi-0003NK-5i@wrigleys.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 6774
Logged by: Boris Folgmann
Email address: boris(at)folgmann(dot)de
PostgreSQL version: 8.4.12
Operating system: CentOS 6.3
Description:

This is an really interesting one!
I've trimmed down the problem so you can simply reproduce it by copy &
paste:

CREATE OR REPLACE FUNCTION ignores_order_by()
RETURNS TABLE(datname VARCHAR) AS $$
DECLARE
r RECORD;
BEGIN
FOR r IN SELECT * FROM pg_database WHERE datallowconn ORDER BY
datname
LOOP
datname := r.datname;
RETURN NEXT;
END LOOP;
END;
$$ LANGUAGE 'plpgsql';

CREATE OR REPLACE FUNCTION respects_order_by()
RETURNS TABLE(dn VARCHAR) AS $$
DECLARE
r RECORD;
BEGIN
FOR r IN SELECT * FROM pg_database WHERE datallowconn ORDER BY
datname
LOOP
dn := r.datname;
RETURN NEXT;
END LOOP;
END;
$$ LANGUAGE 'plpgsql';

SELECT * from ignores_order_by();
SELECT * from respects_order_by();

Now compare the different output!
The only difference of the two functions is that the first one uses a
variable with the same name of a column.
This might be a feature and not a bug, but browsing through the
documentation I could not find any documented restrictions on variable names
in this context.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message hubert depesz lubaczewski 2012-07-27 16:03:24 Re: BUG #6774: FOR IN SELECT LOOP ignores ORDER BY
Previous Message Craig Ringer 2012-07-27 13:56:07 Re: BUG #6768: Failure in OBDC