pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: cyril.ballagny@abbd.fr
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #12733: Inconsistent output of query involving array_agg
Date: Tue, 03 Feb 2015 18:03:02 -0500
Message-ID: <1847.1423004582@sss.pgh.pa.us> (raw)
In-Reply-To: <20150203142626.2703.93881@wrigleys.postgresql.org>
References: <20150203142626.2703.93881@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

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



view thread (2+ messages)

Message-ID: <1847.1423004582@sss.pgh.pa.us>
Permalink:  ../1847.1423004582@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1847.1423004582@sss.pgh.pa.us

 · 

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: tgl@sss.pgh.pa.us, cyril.ballagny@abbd.fr
  Subject: Re: BUG #12733: Inconsistent output of query involving array_agg
  In-Reply-To: <1847.1423004582@sss.pgh.pa.us>
  References: <20150203142626.2703.93881@wrigleys.postgresql.org> <1847.1423004582@sss.pgh.pa.us>

* 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