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 1v6PcA-004Bgk-S7 for pgsql-docs@arkaria.postgresql.org; Wed, 08 Oct 2025 08:33:38 +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 1v6Pc8-008c8A-Fg for pgsql-docs@arkaria.postgresql.org; Wed, 08 Oct 2025 08:33:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v6Pc8-008c82-6B for pgsql-docs@lists.postgresql.org; Wed, 08 Oct 2025 08:33:37 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1v6Pc6-0010Lo-1E for pgsql-docs@lists.postgresql.org; Wed, 08 Oct 2025 08:33:36 +0000 Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-b50206773adso130841966b.0 for ; Wed, 08 Oct 2025 01:33:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1759912413; x=1760517213; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=TMF/8Rqg5dIZgXdpDoNoknxPayOxJ+v82JwVXmRI3F0=; b=nvpdGFkHXdl54W6zgkfsMDdbUTnWjv3sOvwgxeScHzzjb31+bXO7SQLOFr/4dD+ArQ Xk3q6tvnXET9w2Y2WYh7Had6Uq+TyC57q6uS/8iEqri22vW42cuWQFsN6VELcGkaROuA tGN/3BX9GsLYMTFBQRORcEoAVwe87dP7RT03BUVk97pZ3jjHIKUDYPSc9loNjMAe7DuU L77SfhjWfqCuLAvh2+lZYVEWazBDUx2Q04KeENq9PH22krdswStlPsLvZHroAcZypYow O3BYgLR6np9LwGhOy9Dp/J2ZCAQtPqzp2PfmrHDXfuNYpGT8KdRSW+GhVDnqSFF94ZCc /5pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759912413; x=1760517213; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=TMF/8Rqg5dIZgXdpDoNoknxPayOxJ+v82JwVXmRI3F0=; b=RDKXDbOn4zRtMzDOosluKDcLb794PnhDpHBwpO9c8mk7BFDb9aoXi0PtOv0wI8wCWr okjCCTnMopAvTOEalw4/iBoMq6sVP6NbfVA2KR31iT2TLyyAg2MMeMs3QAB+b87E9mzV Z+M1Koe0nFJMp/1UtPi9Wt852mRzhVJKurOR7gODZ/t0mJ5FZ+WlwPD4oj+CsnwmBXfC jtPSBXVQFryYT1ZJnIbO9z/AtDvc4MsL8gXsh7UwMbHv13ySIRuHfuSd7UZdMCfJxV1I JIXQtBluhJ7RU1fohX1kz1CLzb6pjcr1sGzDhAZrWFQgu81Dkh51LKfvfqSbkSpnmX5A J5Hg== X-Forwarded-Encrypted: i=1; AJvYcCUHu40RT39hWsBXuevuaZgWy+af44sYb1oyJE59hddE8K08cp72l20kzohRbfCwhUvlTXvRIh2h373N@lists.postgresql.org X-Gm-Message-State: AOJu0Yw1sJRmCrKoBzVkHzjfuMYFGB96vGKwQBjrMASz0KvXNaVMNvis VAXSWODTlfWN6FPl8DnqR2mrM/hqTiUQDS/YxQwf/1CZrMulFw5zBaaHzLtM2k3fpzI= X-Gm-Gg: ASbGnctqJpN/rUZ3CIWNFp2iqrInakRrHWuQJ4R3q4iVn1yBeHs0QGeR3IyYoFcGdfv seidR6xKOykzVqKOn9Ftt3tBwIl7vJamYTTPJ2B43DuluqQqUCVi8qjymGXuE69GriwFu4pvSWV 1ju5DEwBW4IyrKkZoTu3GhVlmT57nXfvPpnCe7+94uJ6Qnf3cGZCAV5Kisd94wk6iCxAbv2Tpsd FCn8NdF7i/lNNfSg9P4aSCQSlWV7t5VXrEB3AkUs8WMmI3yO7fQMWpkN20Va0Oxx+H9JvKRR4rP PbT8hVKR5TIUYOsEKrurpbipGjNRt2FRCqIEotpchq7UUk+0zEaa7G1HfQ3na/B8Po+QdsaIIo3 f32Hpt/QRnjqUblTU8Hx/zExJiE0qH1efoIHWJ56NHnqNHWur4PwfXeyMaCOLXlX207q+ew11Xd 2S X-Google-Smtp-Source: AGHT+IGxgPn55OGUzRDVfJ1nK777VwyhwQa5xc9vDHtDuOluerNpglza8l7rK0fpowTr+6LHMhkJpg== X-Received: by 2002:a17:906:6a16:b0:b23:20e7:b480 with SMTP id a640c23a62f3a-b50bedbf4f1mr319611466b.18.1759912413056; Wed, 08 Oct 2025 01:33:33 -0700 (PDT) Received: from laurenz.albe-K4N0CV00F97414D ([88.116.133.170]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b486970b36fsm1585941166b.62.2025.10.08.01.33.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Oct 2025 01:33:32 -0700 (PDT) Message-ID: <4e024d5468aef77d168d46d86554fdabf802b031.camel@cybertec.at> Subject: Re: UNION does not append From: Laurenz Albe To: trescher40@gmx.de, pgsql-docs@lists.postgresql.org Date: Wed, 08 Oct 2025 10:33:31 +0200 In-Reply-To: <175987603436.768.10549246436481476867@wrigleys.postgresql.org> References: <175987603436.768.10549246436481476867@wrigleys.postgresql.org> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2 (3.56.2-2.fc42) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 2025-10-07 at 22:27 +0000, PG Doc comments form wrote: > Page: https://www.postgresql.org/docs/18/queries-union.html >=20 > I'm very confused about this statement in the documentation of UNION: "UN= ION > effectively appends the result of query2 to the result of query1". > Because, if I append one item to a list, it is still the same list. But i= n > the case of UNION, it's ""copied"" into a new anonymous derived-table. Al= ias > don't work anymore, which I would expect if I just append some rows. Or i= s > it like "effectively" means that this statement is not to be taken > literally? In that case I would really appreciate a more detailed > description, that after the UNION with tables a new anonymous derived-tab= le > is returned and the original first table can't be accessed anymore. Thank > you for taking your time to read this even if you disagree. I can understand your confusion. Yes, "effectively" means that you shouldn= 't take it literally, but UNION does behave *as if* the second result is appen= ded to the first one. Look at this: SELECT * FROM (VALUES (1), (5), (3)) AS t1(x) UNION ALL SELECT * FROM (VALUES (2), (4), (3)) AS t2(y); x=20 =E2=95=90=E2=95=90=E2=95=90 1 5 3 2 4 3 (6 rows) The column alias is "x", which is the alias from the first result. And look at the execution plan: EXPLAIN SELECT * FROM (VALUES (1), (5), (3)) AS t1(x) UNION ALL SELECT * FROM (VALUES (2), (4), (3)) AS t2(y); 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=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=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 Append (cost=3D0.00..0.11 rows=3D6 width=3D4) -> Values Scan on "*VALUES*" (cost=3D0.00..0.04 rows=3D3 width=3D4) -> Values Scan on "*VALUES*_1" (cost=3D0.00..0.04 rows=3D3 width=3D4= ) (3 rows) So I'd say that the documentation is alright. Yours, Laurenz Albe