Received: from sss2.sss.pgh.pa.us (sss.pgh.pa.us [209.114.166.2]) by hub.org (8.10.1/8.10.1) with ESMTP id e67GZdW28754; Fri, 7 Jul 2000 12:35:39 -0400 (EDT) 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 MAA10554; Fri, 7 Jul 2000 12:35:15 -0400 (EDT) To: Chris Bitmead cc: pgsql-bugs@postgresql.org, pgsql-hackers@postgresql.org Subject: Re: Re: [HACKERS] Re: [SQL] MAX() of 0 records. In-reply-to: <3965F8A6.3322C001@bitmead.com> References: <396535EF.8132CF80@opentec.com.au> <8477.962940582@sss.pgh.pa.us> <39656BD5.801BDEDC@opentec.com.au> <9124.962953563@sss.pgh.pa.us> <3965F8A6.3322C001@bitmead.com> Comments: In-reply-to Chris Bitmead message dated "Sat, 08 Jul 2000 01:35:02 +1000" Date: Fri, 07 Jul 2000 12:35:14 -0400 Message-ID: <10551.962987714@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200007/50 Chris Bitmead writes: > Another observation is that if the WHERE clause is successful, it seems > to update the first record in the target relation that it finds which is > a pretty random result. Wouldn't surprise me --- leastwise, you will get a random one of the input ctid values emitted into the aggregated SELECT row. Offhand I'd have expected the last-scanned one, not the first-scanned, but the point is that the behavior is dependent on the implementation's choice of scanning order. This is exactly the uncertainty that the check for "attribute must be GROUPed or used in an aggregate function" is designed to protect you from. But ctid is (currently) escaping that check. It seems to me that we have two reasonable ways to proceed: 1. Forbid aggregates at the top level of UPDATE. Then you'd need to do a subselect, perhaps something like UPDATE foo SET bar = (SELECT min(f1) FROM othertab WHERE othertab.keycol = foo.keycol) WHERE condition-determining-which-foo-rows-to-update if you wanted to use an aggregate. This is pretty ugly, especially so if the outer WHERE condition is itself dependent on scanning othertab to see if there are matches to the foo row. 2. Do an implicit GROUP BY ctid as I suggested last night. I still don't see any holes in that idea, but I am still worried that there might be one. regards, tom lane