Re: order by, within a plpgsql fx

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: david(dot)sahagian(at)emc(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: order by, within a plpgsql fx
Date: 2011-12-02 15:43:44
Message-ID: 17495.1322840624@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<david(dot)sahagian(at)emc(dot)com> writes:
> Please consider this plpgsql function:
> = = = = = = = = = =
> CREATE Or Replace FUNCTION fx_order_by ( )
> RETURNS table( last_name text, first_name )
> AS $eofx$
> DECLARE
> --
> BEGIN

> Return Query
> select
> lname, fname
> from
> my_table
> order by
> lname ASC
> ;

> END;
> $eofx$ LANGUAGE plpgsql;
> = = = = = = = = = =

> So, is this select statement's result set guaranteed to be ordered as specified by the [order by] coded within the function body ?
> = = = = = = = = = =
> select last_name, first_name from fx_order_by() ;
> = = = = = = = = = =

Well, it's not "guaranteed" exactly, but as long as the outer query
doesn't have any occasion to do anything but seqscan the function
result, you'd get the rows in that order. If you were to throw in
DISTINCT, or GROUP BY, or a JOIN to something else, etc, then maybe
not.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2011-12-02 15:46:13 Re: How to convert HEX to ASCII?
Previous Message david.sahagian 2011-12-02 15:04:37 order by, within a plpgsql fx