pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Weirdness (bug?) with aggregates and subqueries
3+ messages / 2 participants
[nested] [flat]

* Weirdness (bug?) with aggregates and subqueries
@ 2023-11-08 21:12 Laurenz Albe <laurenz.albe@cybertec.at>
  2023-11-08 21:36 ` Re: Weirdness (bug?) with aggregates and subqueries Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Laurenz Albe @ 2023-11-08 21:12 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

I found this in a blog (https://buttondown.email/jaffray/archive/sql-scoping-is-surprisingly-subtle-and-semantic/):

  CREATE TABLE aa (a INT);
  INSERT INTO aa VALUES (1), (2), (3);
  CREATE TABLE xx (x INT);
  INSERT INTO xx VALUES (10), (20), (30);

  SELECT (SELECT sum(a) FROM xx LIMIT 1) FROM aa;

   sum 
  ═════
     6
  (1 row)

Huh?  Shouldn't that return three rows, just like

  SELECT (SELECT sum(14) FROM xx LIMIT 1) FROM aa;

   sum 
  ═════
    42
    42
    42
  (3 rows)

Looking at the plan of the weird query, the aggregate seems to be in the wrong place:

  EXPLAIN (COSTS OFF) SELECT (SELECT sum(a) FROM xx LIMIT 1) FROM aa;

            QUERY PLAN          
  ══════════════════════════════
   Aggregate
     ->  Seq Scan on aa
     SubPlan 1
       ->  Limit
             ->  Seq Scan on xx
  (5 rows)


And this gives an error:

  SELECT a, (SELECT sum(a) FROM xx LIMIT 1) FROM aa;
  ERROR:  column "aa.a" must appear in the GROUP BY clause or be used in an aggregate function
  LINE 1: SELECT a, (SELECT sum(a) FROM xx LIMIT 1) FROM aa;
                 ^

I think that the optimizer is going astray here...

But perhaps I am missing something obvious.

Yours,
Laurenz Albe





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Weirdness (bug?) with aggregates and subqueries
  2023-11-08 21:12 Weirdness (bug?) with aggregates and subqueries Laurenz Albe <laurenz.albe@cybertec.at>
@ 2023-11-08 21:36 ` Tom Lane <tgl@sss.pgh.pa.us>
  2023-11-09 07:26   ` Re: Weirdness (bug?) with aggregates and subqueries Laurenz Albe <laurenz.albe@cybertec.at>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2023-11-08 21:36 UTC (permalink / raw)
  To: Laurenz Albe <laurenz.albe@cybertec.at>; +Cc: pgsql-general@lists.postgresql.org

Laurenz Albe <laurenz.albe@cybertec.at> writes:
> I found this in a blog (https://buttondown.email/jaffray/archive/sql-scoping-is-surprisingly-subtle-and-semantic/):
>   CREATE TABLE aa (a INT);
>   INSERT INTO aa VALUES (1), (2), (3);
>   CREATE TABLE xx (x INT);
>   INSERT INTO xx VALUES (10), (20), (30);

>   SELECT (SELECT sum(a) FROM xx LIMIT 1) FROM aa;

>    sum 
>   ═════
>      6
>   (1 row)

> Huh?  Shouldn't that return three rows, just like

No.  The aggregate function is semantically of the closest query level
that contributes a Var to its argument, so it's evaluated at the "FROM
aa" level, causing that level to become an aggregated query that
returns just one row.  Then it acts like an outer reference as far
as the sub-select is concerned.  This is documented at the end of
Section 4.2.7 in our manual,

https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-AGGREGATES

Thank the SQL spec for that weirdness.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Weirdness (bug?) with aggregates and subqueries
  2023-11-08 21:12 Weirdness (bug?) with aggregates and subqueries Laurenz Albe <laurenz.albe@cybertec.at>
  2023-11-08 21:36 ` Re: Weirdness (bug?) with aggregates and subqueries Tom Lane <tgl@sss.pgh.pa.us>
@ 2023-11-09 07:26   ` Laurenz Albe <laurenz.albe@cybertec.at>
  0 siblings, 0 replies; 3+ messages in thread

From: Laurenz Albe @ 2023-11-09 07:26 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general@lists.postgresql.org

On Wed, 2023-11-08 at 16:36 -0500, Tom Lane wrote:
> Laurenz Albe <laurenz.albe@cybertec.at> writes:
> > I found this in a blog (https://buttondown.email/jaffray/archive/sql-scoping-is-surprisingly-subtle-and-semantic/):
> >   CREATE TABLE aa (a INT);
> >   INSERT INTO aa VALUES (1), (2), (3);
> >   CREATE TABLE xx (x INT);
> >   INSERT INTO xx VALUES (10), (20), (30);
> 
> >   SELECT (SELECT sum(a) FROM xx LIMIT 1) FROM aa;
> 
> >    sum 
> >   ═════
> >      6
> >   (1 row)
> 
> > Huh?  Shouldn't that return three rows, just like
> 
> No.  The aggregate function is semantically of the closest query level
> that contributes a Var to its argument, so it's evaluated at the "FROM
> aa" level, causing that level to become an aggregated query that
> returns just one row.  Then it acts like an outer reference as far
> as the sub-select is concerned.  This is documented at the end of
> Section 4.2.7 in our manual,
> 
> https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-AGGREGATES
> 
> Thank the SQL spec for that weirdness.

Thanks for the explanation.  Seems like another instance of the standard
committee smoking the wrong stuff.

Yours,
Laurenz Albe





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2023-11-09 07:26 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-11-08 21:12 Weirdness (bug?) with aggregates and subqueries Laurenz Albe <laurenz.albe@cybertec.at>
2023-11-08 21:36 ` Tom Lane <tgl@sss.pgh.pa.us>
2023-11-09 07:26   ` Laurenz Albe <laurenz.albe@cybertec.at>

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