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.96) (envelope-from ) id 1vdlMY-004QhN-24 for pgsql-general@arkaria.postgresql.org; Thu, 08 Jan 2026 08:27:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vdlMW-0014TX-0w for pgsql-general@arkaria.postgresql.org; Thu, 08 Jan 2026 08:27:21 +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.96) (envelope-from ) id 1vdlMV-0014TP-2q for pgsql-general@lists.postgresql.org; Thu, 08 Jan 2026 08:27:20 +0000 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vdlMV-004r6t-0T for pgsql-general@lists.postgresql.org; Thu, 08 Jan 2026 08:27:19 +0000 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-4779adb38d3so20044845e9.2 for ; Thu, 08 Jan 2026 00:27:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1767860837; x=1768465637; 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=VQ4hy6i7VjFeYW72jEclL2lKDz7aypvJJZPHnsPQ4us=; b=e4sDhgvU3pcLWurukwwl62huxsRqQTBhF92Wk3bHsuwo/2zsoppjhHGvVBG8Iq1AuA 5G58OQFvW+hn4T3bdklLxXLddh+FiW1LHnH9dtN/ER/ToeVowKgyUo3RtP4tqw4xiFQL KAq2Rw6aopjQs8IUwD/u0ZWS7/RAHZmG93yBnSmGgSGYjwg+dQVenjrHmpan61H4AzgF 369iSxehnBH21S/v8OD0/SiZltfjP+ZqzUAy3ooy+keWpxFQ0aSUzS/55210XAMA0WOY evp1a14WV/f3HcrdLYByS77HIXHlbhcui6TOFRH4Me4oLYEKIhfYUzk3p9MujRywbm7F ShCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767860837; x=1768465637; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VQ4hy6i7VjFeYW72jEclL2lKDz7aypvJJZPHnsPQ4us=; b=hPIjU1k4j+udWRx3i3Q2si9THWI4gnAx+EVWi41nxu0vEZ8eZ6MpM75fG22lC51+r1 qfO5H8hPhva21+yT7ts7hcmD1r61NdVXfluGEpEVi2Z+hpUOaE+c4sGxDSOz+eVJSniL V9+71gb4PsZSwXTyUJ/vy/x1LcKF/xuFj48aSI2EhQZHRuGpJ1xIpDPV6nABSrqzidnv 4CwlwzugubGHoRKSshE6PN8GrOVA+N6XvcH2JdqrwnimHU9d/sdW+0NRDJTWUhDrRbv7 TUyJfs11lW6WyYA27N+XBCiFZXbSqsYddimSSyJrpKfS05DRlwBpIJpWIXYhvOyjf0H0 wcNw== X-Forwarded-Encrypted: i=1; AJvYcCUn/1AS2unydgD1DET4pkP2E/SgzJDVqJq3CYLsZu6WGwFo8broT800UJUY3xWt7C6qqdqJECdgoI9z/de5@lists.postgresql.org X-Gm-Message-State: AOJu0Yxoupy3sn0rUl4SzI3yXoF/cw/xXUpaM+qO1c6KVJjtcdtFSJZs paqSIV8D/ibifRxfZUKNOe+Bdb9zkKvib5LipooMMP1qhr2v4zOePTChX/Do8fHQkjQ= X-Gm-Gg: AY/fxX5+fTs/xwsyQRsEcfbqCAb2qQ6Zcu2Alz4EX3u7Rg4avcpm+2lIzQs3J3Tdi9C Od3o/zj1HIGD3g3QOXj6f5t6wvWNik1S076RqCegfEuIMbzBBjiRU18MjkHxehfwPS4NBE4fdLO D9VQTtSp6tcGToHqFG3K9NnRcy4JPG9QHh1zv0N2KvHmfqFCCnDEXUFn8QrLa+RolWvv2CSJwJf VHjRtEKp4e45POird8qbdBYpodoMxnf0gglmusxRykPmzcwxWyqfY/P4e6K7gn6fXk7GoljAah+ t63k2oZqlzlpEd6mrzPFSHMPsXntysdG0p8xOLas+YWuYmD0Nz3TeK8Ls50OULltABeNwRbkyZr 4EHtUfSPO0rZ/QiqYJs8F2AklX7CPiKJLXXrEqBu1SOLifXGUOvlpYMoF4XJ7yUUBsVIifRxzB+ OUy+7dEZkL5hEuKgvjqTFFw2FAb0cw4GPsod7/KvhyMoL13KzB3SXZ X-Google-Smtp-Source: AGHT+IGtGDym3ZIwDv8LabHb2ZjrBaA0eG8jnxUo+Wt7xjzyAoIu4FHMnFkVcq/RfHOhu1ug1Ovq/g== X-Received: by 2002:a05:600c:3b19:b0:45d:d97c:236c with SMTP id 5b1f17b1804b1-47d84b3b645mr65140825e9.21.1767860837256; Thu, 08 Jan 2026 00:27:17 -0800 (PST) Received: from laurenz.albe-K4N0CV00F97414D ([2001:871:260:e83b:cde4:8177:a48f:f3db]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-432bd0e175csm14924370f8f.14.2026.01.08.00.27.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Jan 2026 00:27:16 -0800 (PST) Message-ID: Subject: Re: EXPLAIN(GENERIC_PLAN) failing for some queries From: Laurenz Albe To: Yuri Kutsko , pgsql-general@lists.postgresql.org Date: Thu, 08 Jan 2026 09:27:16 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.58.2 (3.58.2-1.fc43) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 2026-01-07 at 14:57 -0800, Yuri Kutsko wrote: > I want to identify every query in my company=E2=80=99s database that pote= ntially uses full table scans. > To do this, I wrote a simple PL/pgSQL function that generates an executio= n plan for each query > from pg_stat_statements and then searches the plan for the 'Seq Scan' pat= tern. > Most queries contain parameters, so I use EXPLAIN (GENERIC_PLAN). This wo= rks for approximately > 80% of queries, but it fails in the following scenarios: >=20 > 1. Planner cannot infer parameter types without explicit casts > The following fails: EXPLAIN (GENERIC_PLAN) SELECT * FROM tbl WHERE id = =3D $1 + $2; > The following works: EXPLAIN (GENERIC_PLAN) SELECT * FROM tbl WHERE id = =3D $1::integer + $2::integer; >=20 > 2. Parameter is used in EXTRACT > The following fails: EXPLAIN (GENERIC_PLAN) SELECT * FROM tbl WHERE EXTRA= CT($1 FROM date_col) =3D 1; > The following works: EXPLAIN (GENERIC_PLAN) SELECT * FROM tbl WHERE date_= part($1, date_col) =3D 1; >=20 > 3. Parameter is used in type 'string' notation > The following fails: EXPLAIN (GENERIC_PLAN) SELECT int $1; > The following works: EXPLAIN (GENERIC_PLAN) SELECT $1::int; >=20 > I modified my procedure to account for these scenarios, but I am not sure= whether there are other > issues with EXPLAIN (GENERIC_PLAN) that I am not aware of. > Problem #1 is mentioned in the EXPLAIN documentation > (https://www.postgresql.org/docs/18/sql-explain.html) at the bottom of th= e page, but the other cases are not. > My questions are: > Are all of the issues described above expected behavior for EXPLAIN (GENE= RIC_PLAN)? Yes. Note that your cases #2 and #3 are illegal SQL, since you cannot use a para= meter in these places, only string literals. I'd say that your problem is that you are using stri= ngs from pg_stat_statements, which ignores the value of constants. Replacing literals with placeholders= can result in incorrect SQL statements. > Are there other known scenarios where EXPLAIN (GENERIC_PLAN) will fail? It is expected to fail for all statements with syntax errors... > Should the EXPLAIN documentation be updated to list scenarios in which EX= PLAIN (GENERIC_PLAN) can fail? I don't think that #2 and #3 deserve documentation, and I'd say it doesn't = need to be documented that EXPLAIN will fail for syntactically incorrect SQL. Yours, Laurenz Albe