Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r0prI-0084iE-JW for pgsql-general@arkaria.postgresql.org; Wed, 08 Nov 2023 21:13:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1r0prG-007pJN-8a for pgsql-general@arkaria.postgresql.org; Wed, 08 Nov 2023 21:13:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1r0prF-007pJF-Tn for pgsql-general@lists.postgresql.org; Wed, 08 Nov 2023 21:13:05 +0000 Received: from mail-lf1-x135.google.com ([2a00:1450:4864:20::135]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1r0prD-004dGP-2w for pgsql-general@lists.postgresql.org; Wed, 08 Nov 2023 21:13:04 +0000 Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-5079f6efd64so144963e87.2 for ; Wed, 08 Nov 2023 13:13:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1699477981; x=1700082781; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:date:to:from :subject:message-id:from:to:cc:subject:date:message-id:reply-to; bh=wRYbdlKLBpsDHl5/hLbDSkM5ToC6LNejWFL0Q23kmfY=; b=ijOhX1Y3gvoHmOAHrOGcIQ2XiNiylKtwI/I1uVrteUBLEefXCR7KFQds6C7afXCft8 b33snxXP/HO0wgbDae3mkvZpsO2jxmx0ANoinFiWTCUDy7PjCmF8ukYhS9JeLcFOWjjT NpeGe+JTHggSdLZGiXhH7ojqzey+sEhkPQJq51xLfrFigm9z2f3LicjkEux4PHhlPq8u S4UrQJIgwmbnXb+hiw4lATmI2JgPvqbpFfFSABHjuEo7rLWNBqOg+xvbAQiPxuBVP+5Q NWA7GsDZSarwKofguhS1GpHJlzq1Bji2c5aogu+25VUpwZrqt2piGFKiRxXgMdVT5dT0 SKHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699477981; x=1700082781; h=mime-version:user-agent:content-transfer-encoding:date:to:from :subject:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=wRYbdlKLBpsDHl5/hLbDSkM5ToC6LNejWFL0Q23kmfY=; b=sOO3rGSGGS+IDJsr62vHGv6SIciTCOHK7yKOsa7t82dZHHA1Ghlccf0R9UIi+P661Y 4uzgvT3+zhLq25pEiZrXuW/UzQvES80AxuhEA5+a9dre813aW0aktxF9IopGTtS5g42A trlyIqqsmPrCBcy5Fe7ueEkiFXJcUnIdxje1EpXdM2e0yalOYZTj/m2StOLEWC0kJby3 8e1SfaClu2a3i8yha5cAmSiLqQxvNgYiAogLbj4uh0kQjn8gvrf8Fk+JR5UTqzMqc9Op TPbdLCN9vINU8Y3LW8jzn0CSBwBpPDMlttBB6/R6Bz3BXsGCTAlL1x6bMZqwyy0bVvVj iyMw== X-Gm-Message-State: AOJu0YzxwQo8kJb8WzqaeKetvR2M0VM+w3xiVNbTh5Y9koD7Z7lZ83L0 KuQc46Q/zXLBUACLruoFwpeDxpMOdeWBOXcqGaZ+tA== X-Google-Smtp-Source: AGHT+IElW/1bBaPb8ucqqfWnqJJ9flnxK3KHtzT3EYeG6sbf6Hf2n665VSC2xDuN8YNLvT8CdYHc5g== X-Received: by 2002:ac2:4834:0:b0:508:1889:bfc0 with SMTP id 20-20020ac24834000000b005081889bfc0mr2168460lft.5.1699477980699; Wed, 08 Nov 2023 13:13:00 -0800 (PST) Received: from localhost.localdomain ([2001:871:260:ea1f:cf3b:8147:9c04:e8f1]) by smtp.gmail.com with ESMTPSA id bh5-20020a05600c3d0500b004094e565e71sm21148648wmb.23.2023.11.08.13.13.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 13:13:00 -0800 (PST) Message-ID: <895f34d19556dda5ff3319b584198df20085e596.camel@cybertec.at> Subject: Weirdness (bug?) with aggregates and subqueries From: Laurenz Albe To: pgsql-general@lists.postgresql.org Date: Wed, 08 Nov 2023 22:12:59 +0100 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.48.4 (3.48.4-1.fc38) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I found this in a blog (https://buttondown.email/jaffray/archive/sql-scopin= g-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=20 =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90 6 (1 row) Huh? Shouldn't that return three rows, just like SELECT (SELECT sum(14) FROM xx LIMIT 1) FROM aa; sum=20 =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90 42 42 42 (3 rows) Looking at the plan of the weird query, the aggregate seems to be in the wr= ong place: EXPLAIN (COSTS OFF) SELECT (SELECT sum(a) FROM xx LIMIT 1) FROM aa; QUERY PLAN =20 =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2= =95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95= =90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90 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