Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YImVJ-0000pI-EV for pgsql-bugs@arkaria.postgresql.org; Tue, 03 Feb 2015 23:03:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YImVI-00019v-Ui for pgsql-bugs@arkaria.postgresql.org; Tue, 03 Feb 2015 23:03:32 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YImVH-00019o-UC for pgsql-bugs@postgresql.org; Tue, 03 Feb 2015 23:03:32 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YImVA-00031y-2k for pgsql-bugs@postgresql.org; Tue, 03 Feb 2015 23:03:30 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id t13N32eZ001848; Tue, 3 Feb 2015 18:03:02 -0500 From: Tom Lane To: cyril.ballagny@abbd.fr cc: pgsql-bugs@postgresql.org Subject: Re: BUG #12733: Inconsistent output of query involving array_agg In-reply-to: <20150203142626.2703.93881@wrigleys.postgresql.org> References: <20150203142626.2703.93881@wrigleys.postgresql.org> Comments: In-reply-to cyril.ballagny@abbd.fr message dated "Tue, 03 Feb 2015 14:26:26 +0000" Date: Tue, 03 Feb 2015 18:03:02 -0500 Message-ID: <1847.1423004582@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org cyril.ballagny@abbd.fr writes: > Below is the script to reproduce the database. At the end of this message > there are two queries which should produce the same output but it is > different. I don't understand why. It seems that it is related to the > ORDER_BY on view singledoublons0 and array_agg function on view > singledoublons1. AFAICT the problem is that these views are underdetermined. In particular, the order of the input to the array_agg aggregate is underdetermined, so that it's possible for it to produce different array outputs for the same group depending on how the planner and executor happen to do the grouping step needed for singledoublons1. And that means that the GROUP BY at the next level up may or may not see different array_auto_id values among the set of groups with the same ratio_exaus/groupid combination. I was able to make the instability go away by changing the intermediate view like this: CREATE OR REPLACE VIEW singledoublons1 AS SELECT singledoublons0.groupid, array_agg(singledoublons0.auto_id ORDER BY auto_id) AS array_auto_id, ^^^^^^^^^^^^^^^^ singledoublons0.priority, singledoublons0.ratio_exaus FROM singledoublons0 GROUP BY singledoublons0.priority, singledoublons0.ratio_exaus, singledoublons0.groupid; That's a bit brute-force and you might be able to do better with your real data, but anyway the problem is that you need to ensure that the array_agg result is uniquely determined, which it isn't with the definitions and data as presented. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs