pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: boris@folgmann.de
To: pgsql-bugs@postgresql.org
Subject: BUG #6774: FOR IN SELECT LOOP ignores ORDER BY
Date: Fri, 27 Jul 2012 14:56:18 +0000
Message-ID: <E1Sulxi-0003NK-5i@wrigleys.postgresql.org> (raw)

The following bug has been logged on the website:

Bug reference:      6774
Logged by:          Boris Folgmann
Email address:      boris@folgmann.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.





view thread (5+ messages)  latest in thread

Message-ID: <E1Sulxi-0003NK-5i@wrigleys.postgresql.org>
Permalink:  ../E1Sulxi-0003NK-5i@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/E1Sulxi-0003NK-5i@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: boris@folgmann.de
  Subject: Re: BUG #6774: FOR IN SELECT LOOP ignores ORDER BY
  In-Reply-To: <E1Sulxi-0003NK-5i@wrigleys.postgresql.org>
  References: <E1Sulxi-0003NK-5i@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox