Received: from sss2.sss.pgh.pa.us (sss.pgh.pa.us [209.114.166.2]) by hub.org (8.9.3/8.9.3) with ESMTP id AAA07708 for ; Tue, 25 Jan 2000 00:01:48 -0500 (EST) (envelope-from tgl@sss.pgh.pa.us) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss2.sss.pgh.pa.us (8.9.3/8.9.3) with ESMTP id AAA11882; Tue, 25 Jan 2000 00:01:10 -0500 (EST) To: Philip Warner cc: Chris Bitmead , pgsql-hackers@postgreSQL.org Subject: Re: [HACKERS] DISTINCT ON: speak now or forever hold your peace In-reply-to: <3.0.5.32.20000125150557.036121c0@mail.rhyme.com.au> References: <2295.948765015@sss.pgh.pa.us> <3.0.5.32.20000125140349.035768d0@mail.rhyme.com.au> <3.0.5.32.20000125150557.036121c0@mail.rhyme.com.au> Comments: In-reply-to Philip Warner message dated "Tue, 25 Jan 2000 15:05:57 +1100" Date: Tue, 25 Jan 2000 00:01:10 -0500 Message-ID: <11879.948776470@sss.pgh.pa.us> From: Tom Lane Philip Warner writes: > Just a thought, but would I be right in saying that this could be easily > done with the addition of a new aggregate function 'FIRST', which simply > returns the first value sent to it? Since the aggregates operate a row at > a time, you are guaranteed a consistent set of values, I think. No, because the system doesn't guarantee to deliver tuples to the aggregate in any particular order. The physical order of tuples on the disk will depend on the order in which they were last updated (plus random perturbations introduced by VACUUM); and the order in which they are processed by a query will depend on the query plan chosen by the planner (sequential scan vs. indexscan, etc). SQL in general doesn't believe that tuple ordering has any semantic significance --- you can ask for ORDER BY, but that's only honored at the end stage of SELECT result delivery, not necessarily anywhere in the bowels of a query. Also, if you are going to use an aggregate, ISTM you might as well use one like MIN() or MAX() and get a fully predictable result. I suppose a FIRST() aggregate would save a few cycles by not running comparisons, but I bet it'd be tough to notice any difference. regards, tom lane