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 1tU65f-00E5Xj-NY for pgsql-general@arkaria.postgresql.org; Sat, 04 Jan 2025 15:29:28 +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 1tU65d-00B2KZ-C2 for pgsql-general@arkaria.postgresql.org; Sat, 04 Jan 2025 15:29:25 +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 1tTmGp-004B9F-Si for pgsql-general@lists.postgresql.org; Fri, 03 Jan 2025 18:19:39 +0000 Received: from mail-ej1-x62b.google.com ([2a00:1450:4864:20::62b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tTmGk-0004H5-2Y for pgsql-general@postgresql.org; Fri, 03 Jan 2025 18:19:39 +0000 Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-aa66ead88b3so2295122266b.0 for ; Fri, 03 Jan 2025 10:19:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735928373; x=1736533173; darn=postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=YYqdbhxNkunUP0IZtyqpCxSxlu9Z7XA1YMkdFMUhIUI=; b=XiZnteN7j1Mmwhevxekx/GBwOA4phym2ujtOwrceRre0NKrxJsdRO24oeoKi3pr5jF GIr7X6IBJl7wodLANbFiYLsSnzerKOYU4YBniZGJO8QOCgkEIoSVAMccLsNhd6N8q1l3 VtW4WoUOvTsGMl/prFiz2KjTUkE10dSVn6zC2pG0wS2t/EsGcn4Qy8K9b8IZt3h2yOuy cyZ1grBD317oaSXeGiUaS6eIBtDBvOHwaOWV/Oeyqo5MrdgAkG1gskH6HfW6Xn+A7GqQ AR4OoxTgsczWzVh9fqwWHS/O8YkJVH58bOaEOBqAmdaBf6sLmKyeW8Nu8FOFcWqnt/0S BMLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735928373; x=1736533173; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=YYqdbhxNkunUP0IZtyqpCxSxlu9Z7XA1YMkdFMUhIUI=; b=IDfaLVjvyYrpjyNboJsZCawQn1Q7V9DRg0RcY4fI3MZi8JDKwxAoirp6kPIOK+wyYn XXzFkB7bw7x4N3gk8l8TwY+tt28vhh3O94IzoBC17oWZRT7tyobJ4sUSq+TMvDE0vK00 ru21AB/ce9d/E+9gENT8qeE40/wgc2pbgjt1E3A49xZ7P9ToxreneqN8k8ldkczn5qlI K0822e+s4UhkgIlgXakL+39z7tR8PjcWRw/tME9EMM3K3qESIuJtpXlDBpsR1SIs99xB nNE93aeI5MLTXkvziW3omc/69YZoIVTGRoUlLZHxzKSZ8Rv+yNCUubOFKdobwwvwjOIl 1dkw== X-Gm-Message-State: AOJu0Ywn5z6aLLHENrXzTB4tgs1lbwX9G1Ubs28cK1K/LzzIfifE6yCi DIe2gJZqP3PK9/PGim2adPV02ez3gBzKKH9CCCBc1IBtez51iJ2gK1BKQGhTuGIJDtaceNg4x9R EtZAkmn6HwVhamRF41kCjNWxZkpvwJGMr X-Gm-Gg: ASbGncvOofqLBRTS0hPUWGm21kemJ4n0rZR26MKtCi38ZKQfE/Kk6VBhgWE7xz84btM 5D2esOJN6xFiPMoT/h7xr/5Qs0bBDL42Ua08+1Fw= X-Google-Smtp-Source: AGHT+IHBmZaBbBtrONwLN2VRUlmZsmqAdSFGNvM9o1zgYZjPmFltf1xxuUuqg06/++uvzrLkKIoqcOQY51X1rIJheUg= X-Received: by 2002:a17:907:7f8e:b0:aa6:995d:9ee8 with SMTP id a640c23a62f3a-aac2702ae5cmr4358825266b.5.1735928372510; Fri, 03 Jan 2025 10:19:32 -0800 (PST) MIME-Version: 1.0 From: Andrey Date: Fri, 3 Jan 2025 20:19:21 +0200 Message-ID: Subject: Unexpected results from a query with UNION ALL To: pgsql-general@postgresql.org Content-Type: multipart/alternative; boundary="000000000000ffc650062ad151c3" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000ffc650062ad151c3 Content-Type: text/plain; charset="UTF-8" Hi everyone, Recently I got unexpected results from a query that seems to be legit. The setup is like this: -- setup CREATE TABLE parents ( id uuid not null primary key, revision integer not null ); CREATE TABLE children ( id uuid not null primary key, parent_id uuid not null references parents ); INSERT INTO parents (id, revision) VALUES ('ec422e09-55bb-4465-a990-31f59859959d', 1); INSERT INTO children (id, parent_id) VALUES ('5cb82ceb-c5ef-4c59-a02e-f7b610470f8c', 'ec422e09-55bb-4465-a990-31f59859959d'); INSERT INTO children (id, parent_id) VALUES ('ce5b22b0-c6c4-4c09-826c-7086c53ee9ec', 'ec422e09-55bb-4465-a990-31f59859959d'); The query is: -- query SELECT children_union.id AS child_id FROM parents p JOIN (SELECT id, parent_id FROM children UNION ALL SELECT null::uuid, null::uuid WHERE false) children_union ON children_union.parent_id = p.id WHERE p.id = 'ec422e09-55bb-4465-a990-31f59859959d' FOR UPDATE OF p; It looks weird, but it's just a simplification of a much bigger query. The 'SELECT null::uuid, null::uuid WHERE false' part was actually more meaningful but I substituted it with a query that returns 0 rows after finding out that it's irrelevant. If I just run this query I get something that I would expect to get: -- result 1 child_id -------------------------------------- 5cb82ceb-c5ef-4c59-a02e-f7b610470f8c ce5b22b0-c6c4-4c09-826c-7086c53ee9ec (2 rows) But if I lock the single row in the parents table: -- concurrent query BEGIN; UPDATE parents SET revision = revision + 1 WHERE id = 'ec422e09-55bb-4465-a990-31f59859959d'; and then run my query again in a separate session, then it's waiting for the lock to be released. Once I commit the concurrent query and release the lock, I get this: -- result 2 child_id -------------------------------------- 5cb82ceb-c5ef-4c59-a02e-f7b610470f8c 5cb82ceb-c5ef-4c59-a02e-f7b610470f8c (2 rows) but I would expect to get the same result as previously. Is it a bug or am I doing something wrong here? Thank you, Andrii --000000000000ffc650062ad151c3 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi everyone,

Recently I g= ot unexpected results from a query that seems to be legit.
The setup is = like this:

-- setup
CREATE TABLE parents
(
=C2=A0 =C2=A0 id= =C2=A0 =C2=A0 =C2=A0 uuid =C2=A0 =C2=A0not null primary key,
=C2=A0 =C2= =A0 revision integer not null
);
CREATE TABLE children
(
=C2=A0= =C2=A0 id =C2=A0 =C2=A0 =C2=A0 =C2=A0uuid =C2=A0 =C2=A0not null primary ke= y,
=C2=A0 =C2=A0 parent_id uuid =C2=A0 =C2=A0not null references parents=
);
INSERT INTO parents (id, revision)
VALUES ('ec422e09-55bb-= 4465-a990-31f59859959d', 1);
INSERT INTO children (id, parent_id)VALUES ('5cb82ceb-c5ef-4c59-a02e-f7b610470f8c', 'ec422e09-55bb= -4465-a990-31f59859959d');
INSERT INTO children (id, parent_id)
V= ALUES ('ce5b22b0-c6c4-4c09-826c-7086c53ee9ec', 'ec422e09-55bb-4= 465-a990-31f59859959d');

The query is:

-- query
SELECT= children_union.id AS child_id
= FROM parents p
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0JOIN (SELECT id, parent= _id
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 FROM children
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 UNION ALL
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 SELECT null::uuid, null::uuid
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 WHERE false) childr= en_union ON children_union.parent_id =3D p.idWHERE p.id =3D 'ec422e09-55bb-4465-a990-3= 1f59859959d'
=C2=A0 =C2=A0 FOR UPDATE OF p;

It looks weird, b= ut it's just a simplification of a much bigger query. The 'SELECT n= ull::uuid, null::uuid WHERE false' part was actually more meaningful bu= t I substituted it with a query that returns 0 rows after finding out that = it's irrelevant.

If I just run this query I get something that I= would expect to get:

-- result 1
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0child_id
--------------------------------------<= br>=C2=A05cb82ceb-c5ef-4c59-a02e-f7b610470f8c
=C2=A0ce5b22b0-c6c4-4c09-8= 26c-7086c53ee9ec
(2 rows)

But if I lock the single row in the par= ents table:

-- concurrent query
BEGIN;
UPDATE parents
SET r= evision =3D revision + 1
WHERE id =3D 'ec422e09-55bb-4465-a990-31f59= 859959d';

and then run my query again in a separate session, the= n it's waiting for the lock to be released. Once I commit the concurren= t query and release the lock, I get this:

-- result 2
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0child_id
------------------= --------------------
=C2=A05cb82ceb-c5ef-4c59-a02e-f7b610470f8c
=C2= =A05cb82ceb-c5ef-4c59-a02e-f7b610470f8c
(2 rows)

but I would expe= ct to get the same result as previously. Is it a bug or am I doing somethin= g wrong here?

Thank you,
Andrii
--000000000000ffc650062ad151c3--