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 1tml35-00ANdJ-L9 for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Feb 2025 02:51:56 +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 1tml33-00CQR9-0K for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Feb 2025 02:51:53 +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 1tml32-00CQQz-MX for pgsql-hackers@lists.postgresql.org; Tue, 25 Feb 2025 02:51:52 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tml30-000ebF-0X for pgsql-hackers@postgresql.org; Tue, 25 Feb 2025 02:51:52 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-221050f3f00so115322015ad.2 for ; Mon, 24 Feb 2025 18:51:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740451908; x=1741056708; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=HB06kRMbgdBZCtRlxJnFFZJXNVqHLR4LIxmixoCuqpE=; b=f7WlU41Kw9xEpp/gCVTyKwnw1lkmh0djoHoapnAxpcQnK1xT0MXUinoN9zDJDZKQv/ 7yFY+8rNM3EotLQ0El95TkXNUCLnOmvU2VqCxXP8SMLqj6O7cbzs2Jl7enV1JoxlHj/l D09U4bT77ooi8GTVNgMPAkupLK6jJ2C9IKmrvFOU0GTyMLe3BR4l8mvCgzIvOHQPb48b ezTne4DaWtc9+XnWpevQLsGGn7isSXP3PNSVoy2rmQAK+ZAKngQMh0ItGg6ULMaQhRGg UfaSqWX5kRaDtxXg8JYNko90NCs8AAig/0H5aKG+dH6t+gYAtfNI1RdWN9vk4+P20laE t5Wg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740451908; x=1741056708; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HB06kRMbgdBZCtRlxJnFFZJXNVqHLR4LIxmixoCuqpE=; b=J5OqYmj+2XQD9KQsP0A+S4ColPVzY83jYbdLDsSZunTpaX0D43VURdS1u7hRnNuAHu wFs1JWk3FELYw5EI7UPxUS4cdEw0ELL0Q2mojPaNNDuvY04slSoav8HlC1Oe+fWuDMj1 m90ABPuV6HkEuTrEqKBkcQiKSpn2EkqoNe3WOkgg/farfm09z1jlLqveqRwuJyuDCMzE JYyB5d8p8798doizMblYiRKkWmjdm44P/e1/UqOrCEb+fJaIQiUOhA07x8qNUwx6f/uP LFYFBzfdyaB5tGz31sKYbkWRWxI41wMOaSZst8b99+Q/4n9SKuaotai7zMgI3O4rwbgY wAPQ== X-Forwarded-Encrypted: i=1; AJvYcCVnV93PbEmlzgQAG+zq3YVPi6q2D9NmW8lUzPCKSe0BNjeeW7H//oJH1ucMVNPr07Ga/LE0t6eO/2IGbNWZ@postgresql.org X-Gm-Message-State: AOJu0YyUqBpAY24ejwhSRUv4IdYX97bPlxN+lXbcd6+HuReKaLXXjS+K nS5ckaXv2PKr1Po0UURs9bMS2JgI3jFQau2Az6Gs9MOfzWSV3gBWUJi9XtwDaGU2jbxM9DYLFKH h3wTKqKS2SzNNo6YkOWbamljmhpU= X-Gm-Gg: ASbGncsTtT3JwSOVGU2drioOM6+zev6C/5+VOjwL8n86znf6LrjZv/EiNeYxC4Z3HRK t/iDc6sLvQtLBIOVSWTCyBiti/dwRvRh3m1QxX/4avZstZZ1PY0ZUenl/CjLx/9CiG0qkFo1HsA /gcfUWzwS5 X-Google-Smtp-Source: AGHT+IFX6fY18OOylmhh28jAQss54z+Npjinp1+qsGTk98qQ8IAcKI4GNkExsCCFZtL3t2jt+6pbl0Eh5nrVbg7aqVI= X-Received: by 2002:a17:90b:56d0:b0:2f5:747:cbd with SMTP id 98e67ed59e1d1-2fe68ae292bmr2914025a91.18.1740451908047; Mon, 24 Feb 2025 18:51:48 -0800 (PST) MIME-Version: 1.0 References: <54c35fb9-da3a-4754-ab8c-46ed0b612465@vondra.me> <684c70d7-180e-461d-9377-600c2db581ba@vondra.me> <74839af6-aadc-4f60-ae77-ae65f94bf607@gmail.com> In-Reply-To: From: Amit Langote Date: Tue, 25 Feb 2025 11:51:31 +0900 X-Gm-Features: AWEUYZls-fXCWdmqGwMv4QqZz8EPwjxpMlqdJHn25CjqCk14VzGCEeujDirLKGM Message-ID: Subject: Re: generic plans and "initial" pruning To: Tender Wang Cc: Alexander Lakhin , Tomas Vondra , Robert Haas , Alvaro Herrera , Andres Freund , Daniel Gustafsson , David Rowley , PostgreSQL Hackers , Thom Brown , Tom Lane Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, Feb 23, 2025 at 9:46=E2=80=AFPM Tender Wang wr= ote: > Amit Langote =E4=BA=8E2025=E5=B9=B42=E6=9C=8823= =E6=97=A5=E5=91=A8=E6=97=A5 16:36=E5=86=99=E9=81=93=EF=BC=9A >> On Sun, Feb 23, 2025 at 2:03=E2=80=AFAM Tender Wang = wrote: >> > Alexander Lakhin =E4=BA=8E2025=E5=B9=B42=E6=9C= =8822=E6=97=A5=E5=91=A8=E5=85=AD 23:00=E5=86=99=E9=81=93=EF=BC=9A >> >> Please look at new error, produced by the following script, >> >> starting from 525392d57: >> >> CREATE TABLE t(id int) PARTITION BY RANGE (id); >> >> CREATE INDEX idx on t(id); >> >> CREATE TABLE tp_1 PARTITION OF t FOR VALUES FROM (10) TO (20); >> >> CREATE TABLE tp_2 PARTITION OF t FOR VALUES FROM (20) TO (30) PARTITI= ON BY RANGE(id); >> >> CREATE TABLE tp_2_1 PARTITION OF tp_2 FOR VALUES FROM (21) to (22); >> >> CREATE TABLE tp_2_2 PARTITION OF tp_2 FOR VALUES FROM (22) to (23); >> >> CREATE FUNCTION stable_one() RETURNS INT AS $$ BEGIN RETURN 1; END; $= $ LANGUAGE plpgsql STABLE; >> >> >> >> SELECT min(id) OVER (PARTITION BY id ORDER BY id) FROM t WHERE id >= =3D stable_one(); >> >> >> >> ERROR: XX000: trying to open a pruned relation >> >> LOCATION: ExecGetRangeTableRelation, execUtils.c:830 >> >> >> >> This issue was discovered with SQLsmith. >> >> Thanks for the report. >> >> > The error message was added in commit 525392d57. In this case, the es= tate->es_unpruned_relids only includes 1, which is the offset of table t. >> > In register_partpruneinfo(), we collect glob->prunableRelids; in this = case, it contains 2,3,4,5. Then we will do: >> > result->unprunableRelids =3D bms_difference(glob->allRelids, >> > glob->prunableRelids); >> > so the result->unprunableRelids only contains 1. >> > >> > But tp_2 is also partition table, and its partpruneinfo created by cre= ate_append_plan() is put into the head of global list. >> > So we first process it in ExecDoInitialPruning(). Then error reports = because we only contain 1 in estate->es_unpruned_relids. >> >> Thanks for checking. >> >> The RT index of tp_2 should appear in PlannedStmt.unprunableRelids, >> because it needs to be opened in CreatePartitionPruneState() for >> setting up its PartitionPruneInfo. We use ExecGetRangeTableRelation() >> to open, which expects the relation to be locked, so the error. >> >> To ensure tp_2 appears in PlannedStmt.unprunableRelids, we should >> prevent make_partitionedrel_pruneinfo() from placing the RT index into >> leafpart_rti_map[], as the current condition for inclusion doesn=E2=80= =99t >> account for whether the partition is itself partitioned. >> >> I've come up with the attached. > > LGTM. Pushed after some tweaks to comments and the test case. --=20 Thanks, Amit Langote