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 1vuc5t-00FsgL-0v for pgsql-performance@arkaria.postgresql.org; Mon, 23 Feb 2026 19:59:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vuc5r-00EuCo-0u for pgsql-performance@arkaria.postgresql.org; Mon, 23 Feb 2026 19:59:47 +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 1vuc5q-00EuCg-31 for pgsql-performance@lists.postgresql.org; Mon, 23 Feb 2026 19:59:46 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vuc5n-00000000qat-18wX for pgsql-performance@postgresql.org; Mon, 23 Feb 2026 19:59:45 +0000 Received: by mail-wm1-x341.google.com with SMTP id 5b1f17b1804b1-4834826e555so46223025e9.2 for ; Mon, 23 Feb 2026 11:59:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1771876783; x=1772481583; darn=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=9mjtfzvF/2dV2OCazwwGlA7FY6P57JDh7IPczGuYJ3Q=; b=j4V1ZAXurLdfj3igmukDpeOGlsVsuNxdYQKPqgoHPB4TCtuzolQiz8kuygewIiAr+j pAGfFCj78spXUkp4KheZMG12RKCwKhxWK9HPRjHtciN4wyfmKc/G0O1/vhdumxAW1Psn gqn832JiSh1gffdUSrK3ppqMkK3tHFuGlsyY1wDlOivWhmFKtvx/OIZ6+RApREP38jak N3krZ5Szb0Hg395uRQ5sef70FDX+H7dlZ0owBWOcAeY4XsP6MA4rA8FlcZNZo9M7fawy KCn5ml2aDZNcjR2w9Ym+0u2rxsO0nsSsKFLqy+ZaIjLXnfmkvScfJSfLQubbJrY+kS2a 0FsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771876783; x=1772481583; 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=9mjtfzvF/2dV2OCazwwGlA7FY6P57JDh7IPczGuYJ3Q=; b=icKad06+OvKSBDXaIhpDuRfhSFQ1P9jNjmzlSxlvixP6h7iLXKZa8syRoMmLyc2zD5 Tcs2Wosep2b4VF31CXv8okqFPnvOE9Ga6UjAeE5nrScdKxTTDwYWkdwy2O29qcAoMqSX TS7CWVYvtdsAxzPBCD6wjFLH5/Woz8Glbtu6WvoRWbF4IVsPj9xY+dgdQmTy3qTThN2l KY1x3tmiORQUkie7d+sSGjGLQEKAXDnyxceu2cE3LD5LubyZ7HEc5n/QMVvwQ3EFU+Ux sH1x5Ypl1hohjVWvk5dsQNp1zxaoTF8zkEwNf8BAJp9e5K1uiDX3VrpxCwaL1MOg9Mrg PrNQ== X-Forwarded-Encrypted: i=1; AJvYcCWR1PHuXKB5VXNVqusf8KiOUMteBUdjcOfWfx/4RtIWT8pzwtzktstdG8kY1dlztXlv5jmIaJjjLW3JOLnW2cKThw==@postgresql.org X-Gm-Message-State: AOJu0YxWS2uWvqjU3vhuL3D3sXPvcbEjH08liyPAsuHkyMh0zzTP9SB9 p460yYEvaB3DqkoCpMOPu991LJkLdwWfN+DbZpsV1Ebb9ApTQLvsVbU8xBz3tJeIYzg= X-Gm-Gg: ATEYQzx3SVmpkMz+mIc8QvQcE+eFwcfjoI1fu27Eja4YwLAHCcz2sSiMKUAxxI7D7hp PqWG+NrPFoRwMp3pkd8msmItSN9c1HZLPaujmTxaIjAFV6KUDkRVrKEXnwUogX2YGIHOlS4VNjV G7WNrwsBBMlEEBEHPyoAUUhkXnGLtvstiEeAcIIQX9eAa4QcjTCLtleSSG9elRc3DhcpvWn6TJH +KFjteewURXx+kWXTEKOmNvAoEDf2u7xfx498aeThkttHdbndUDNeAuBVNUfkl4mF3jaLSFAeNk S+t3niLFd/ctOpPFez6u4BOu1No8GggtlLAKgpd9ClcF9H3aO0cDjHT7599tmvQSXWfGqcDUhDu RGj7sRYGRgE39zIAgWZyy9c96i8N33dP/TUka2smtEqdxzZap8DXFwvxx6Bb5zoKH1FQO/D06Cp f266injmqTqaghZBMC3CAf2ioN5GA65bapG9aVwbyexOWEmpBhtafUuhkvKlL2v9Tqm93fYYzJC ohPJntOqi6NCe1Tqx/aeWKOpktLOsA5h3boDUEzqPfo X-Received: by 2002:a05:6000:26c1:b0:435:add0:3d77 with SMTP id ffacd0b85a97d-4396f1851bdmr19666819f8f.55.1771876782563; Mon, 23 Feb 2026 11:59:42 -0800 (PST) Received: from laurenz.albe-K4N0CV00F97414D (210.144.128.164.static.wline.lns.ent.cust.swisscom.ch. [164.128.144.210]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-43970c00d95sm21066922f8f.13.2026.02.23.11.59.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Feb 2026 11:59:42 -0800 (PST) Message-ID: Subject: Re: unstable query plan on pg 16,17,18 From: Laurenz Albe To: Attila Soki , pgsql-performance@postgresql.org Date: Mon, 23 Feb 2026 20:59:41 +0100 In-Reply-To: <1695A676-062B-47C5-B302-91E2357DC874@gmx.net> References: <1695A676-062B-47C5-B302-91E2357DC874@gmx.net> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.58.3 (3.58.3-1.fc43) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2026-02-23 at 16:10 +0100, Attila Soki wrote: > > On 23 Feb 2026, at 10:41, Laurenz Albe wrote= : > >=20 > > On Mon, 2026-02-23 at 10:37 +0100, Attila Soki wrote: > > > When upgrading from PostgreSQL 14.4, I noticed that one of my somewha= t complex > > > analytical queries sometimes gets an inefficient plan under PostgreSQ= L 16, 17, and 18. > > > Under 14.4, the query runs with a stable plan and completes in 19 to = 22 seconds. > > > In newer versions, the plan seems to be unstable, sometimes the query= completes > > > in 17 to 20 seconds, sometimes it runs for 5 to 18 minutes with the i= nefficient plan. > > > This also happens even if the data is not significantly changed. > >=20 > > This is very likely owing to a bad estimate. > >=20 > > Could you turn on "track_io_timing" and send us the EXPLAIN (ANALYZE, B= UFFERS) output > > for both the good and the bad plan? >=20 > Thank you for your reply. Here are the two explains. > In order to be able to publish the plans here, I have obfuscated the tabl= e and field names, but this is reversible, so I can provide more info if ne= eded. >=20 > plan-ok: > https://explain.depesz.com/s/hQvM >=20 > plan-wrong: > https://explain.depesz.com/s/uLvl Thanks. The difference in the plans is under the "Subquery Scan on odg", starting w= ith plan node 50 (everything under the "Sort"). I suspect that the mis-estimat= e that is at the root of the problem is here: -> Index Scan using table_k_late_spec_dp_end_dat_key on schema1.table_k ka= l (... rows=3D196053 ...) (... rows=3D471.00 ...) Index Cond: (kal.dp_end_dat < ('now'::cstring)::date) Index Searches: 1 Buffers: shared hit=3D230 read=3D49 I/O Timings: shared read=3D0.142 PostgreSQL overestimates the row count by a factor of over 400. Try to fix that estimate and see if that gets PostgreSQL to do the right th= ing. Perhaps a simple ANALYZE on the table can do the trick. The right side of the comparison looks awkward, as if you wrote 'now'::text= ::date My experiments show that PostgreSQL v18 estimates well even with such a wei= rd condition, but perhaps if you write "current_date" instead, you'd get bette= r results. I'd play just with a query like EXPLAIN (ANALYZE) SELECT * FROM schema1.table_k AS kal WHERE dp_end_dat < current_date; until I get a good estimate. Yours, Laurenz Albe