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 1vucwV-00GTLL-2A for pgsql-performance@arkaria.postgresql.org; Mon, 23 Feb 2026 20:54:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vucwT-00FL4Y-1H for pgsql-performance@arkaria.postgresql.org; Mon, 23 Feb 2026 20:54:09 +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.96) (envelope-from ) id 1vucwT-00FL4P-09 for pgsql-performance@lists.postgresql.org; Mon, 23 Feb 2026 20:54:09 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vucwP-00000000vRf-2xpe for pgsql-performance@postgresql.org; Mon, 23 Feb 2026 20:54:08 +0000 Received: by mail-wr1-x443.google.com with SMTP id ffacd0b85a97d-4377174e1ebso3684628f8f.3 for ; Mon, 23 Feb 2026 12:54:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1771880045; x=1772484845; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=9MoEtmu2VcXOr5HMyPDSPM+utaXrY16n0BrkI6F7hyI=; b=i0w3gbm9VUmpkL16epgKmx3oiCq5hAomwlAEtgpausoA52mg2g4agVB8OmAoi78+nz L7VWiYAZ2R1qPVQla3N9eMP8nFHV536OfWcE/+SPxi17N1rI2kET1QdoJrTIemnB6ZWb YIbpe3EDOaznkxoDFxmdxtYYR+cP2ZBIr5NCY5Rk9wuu0mQsqyY6jVYnW3InI2FWQTdE nhMoKOFa/ZD5oiZWoppRItEPjGTdyRYTU76L5r87oGZUhUkE0PrNGe24kZZzQgJ1T6O2 /oe2+llStVJZn+iYqPDf4BfJ3+ROHWhPCo8K0H0VgajqpplEVd2bKdnGgAHXHKCmuW84 g/Gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771880045; x=1772484845; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9MoEtmu2VcXOr5HMyPDSPM+utaXrY16n0BrkI6F7hyI=; b=J1eVFx20nJadebGskujqJCBHXIIT4CdI0Y6NJaGbV4mJu7qkcMkz9cTGVWQx97nMBR m+PF4AB/e7nVQDkLo4C+AWIulyQUKMEUUuTR0lAOq68Vg7FU3BxIXHw/m65jw0yxz72t PooGJPEaEwz60aOgt8S+ZZZLl2zQ/gucL/wQ75wgK3KgYwLus80QAbxBQCfQmeCsHmLr sYbAnTRDKXCEYCWHVSOfD3y+SaUHSLwEfIW+kY/yMdVzjCpmpUJgC7zhfqmbezEG3fNs EmFyHYNYnb5v9nm6C8turn8pzB4YG2oZtfzC7hZxA+m8jAOFU3JP5vIrbRWFCZ/K4b3u Cm/w== X-Gm-Message-State: AOJu0YyIOHUQxCbGfidbjd7JU8C53q4LcFNGlUyuWHUuZfhkKoQHQiGZ zaNC6c5q8euYc9lxG0r/irbjJka8eiorQRsfS1vDchDhpPmVMwDOnsiSgxyQHLaAG9A= X-Gm-Gg: ATEYQzwnb/tX4dZ0lnP+CnwAn4bmW4zGfC9Tjr4Upsb3hNbQJ3fIQk9Q0RExNM0cAon 0uc0tzLD0CDvd0t66pfXEs7Nm1tpWwQHJP/8s13AtkC8O55AfSG9LZjUdC8aqQXQ9I5OIKTw6E/ iLItI+7hyNv2ulcSInwY62cwORT/pa5y/fYLJiH5bL9ryM/cmcle/s6fSqvWFra1thvzkd3eyhX hMyUbt2xqqWv9ri/fd0gi7J7FQv5i1N5CIsTxEd7lBizLN+fxNHizZQdicyHB9MCuwOR1EdEr8p 5AJooC5zxkCP0q39OLQpZqnf54Az6+nXdXs9698sweI7D1znCXojlzTZeK1FbsPv5EJEWY1VZTz cMl5rEFoM+BKJH2lamUHRc8EklY5gq/Y9BtXIYoJpqzC4tMkcz+0xNqbYUkWcYemsGoXlxMuGfj YMv5C6jICZ35VC76VHp974EhiVwm2C+aPMiSSvm9+iKTY55yxF0vZPRWJYtrzJQy5qAdHDj2uZJ YDp/MxWWJh9fviuoj7H6s81f42ENWZh6lhsGUOsxnzQKf+r7LVQca0= X-Received: by 2002:a05:6000:2403:b0:436:1590:f9e7 with SMTP id ffacd0b85a97d-4396f15cd3amr16952558f8f.12.1771880045258; Mon, 23 Feb 2026 12:54:05 -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-43970d4c96csm23722479f8f.30.2026.02.23.12.54.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Feb 2026 12:54:04 -0800 (PST) Message-ID: Subject: Re: unstable query plan on pg 16,17,18 From: Laurenz Albe To: Attila Soki Cc: pgsql-performance@postgresql.org Date: Mon, 23 Feb 2026 21:54:04 +0100 In-Reply-To: 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 21:42 +0100, Attila Soki wrote: > > > plan-ok: > > > https://explain.depesz.com/s/hQvM > > >=20 > > > plan-wrong: > > > https://explain.depesz.com/s/uLvl > >=20 > > Thanks. > >=20 > > The difference in the plans is under the "Subquery Scan on odg", starti= ng with > > plan node 50 (everything under the "Sort"). =C2=A0I suspect that the mi= s-estimate > > that is at the root of the problem is here: > >=20 > > -> =C2=A0Index Scan using table_k_late_spec_dp_end_dat_key on schema1.t= able_k kal =C2=A0(... rows=3D196053 ...) (... rows=3D471.00 ...) > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Index Cond: (kal.dp_end_dat < ('now'::cst= ring)::date) > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Index Searches: 1 > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0Buffers: shared hit=3D230 read=3D49 > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0I/O Timings: shared read=3D0.142 > >=20 > > 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 righ= t thing. > >=20 > > Perhaps a simple ANALYZE on the table can do the trick. >=20 >=20 > In the examples I used table_k to flip the plan with > vacuumed -Upostgres -vZ -t schema1.tbl_used_in_query db1 > in the explain output schema1.tbl_used_in_query is table_k I cannot understand that. > > 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= weird > > condition, but perhaps if you write "current_date" instead, you'd get b= etter results. >=20 > I didn't realize that made a difference. I will replace all occurrences. = It also looks more clean with current_date. It *didn't* make a difference when I played with that... > > I'd play just with a query like > >=20 > > =C2=A0EXPLAIN (ANALYZE) > > =C2=A0SELECT * FROM schema1.table_k AS kal > > =C2=A0WHERE dp_end_dat < current_date; > >=20 > > until I get a good estimate. >=20 > I will try to set custom statistics for dp_end_dat and the fields used by= the=C2=A0table_k_late_spec_dp_end_dat_key index. > Let=E2=80=99s see if that helps. For a simple condition like that, extended statistics won't help. That's why I suggested a plain ANALYZE. I am not sure why that is estimated so badly. Yours, Laurenz Albe