Subject: Re: Slow timestamp query after upgrading from Pg13 to Pg16

From: Laurenz Albe <[email-masked]>
Date: Mon, 07 Apr 2025 16:51:40 +0200

On Mon, 2025-04-07 at 15:48 +0100, Simon Windsor wrote:
> After upgrading a Db from Pg13 (Centos) to Pg16(Ubuntu) some queries
> on a simple, large table (200M rows) are very slow

If you used "pg_upgrade", did you ANALYZE the database?

If that is not the problem, we can't guess what your problem might be
unless you provide EXPLAIN (ANALYZE, BUFFERS, SETTINGS) output for auch a
slow query.  Ideally, add the same information for the fast v13 case.

Yours,
Laurenz Albe

---

From: Simon Windsor <[email-masked]>
Date: Mon, 7 Apr 2025 20:25:28 +0100

Hi

We used pg_dump|pg_restore to migrate the data.

The full explain plan is at https://explain.depesz.com/s/742M. The SQL

explain (analyze, buffers) select count(*) from consignments where 
(req_status_tstamp >= '2025-03-28 00:00'::timestamp and 
req_status_tstamp <= '2025-03-28 01:00'::timestamp);

takes 2-3s with the old Pg13 DB, and over a minute with Pg16

After spending many hours looking at DB settings and Statistic settings 
I am at a loss/

Simon

-- 
Simon Windsor

Eml: [email-masked]
Mob: 0755 197 9733

“There is nothing in the world that some man cannot make a little worse and sell a little cheaper, and he who considers price only is that man's lawful prey.”

---

From: Adrian Klaver <[email-masked]>
Date: Mon, 7 Apr 2025 12:36:32 -0700

> We used pg_dump|pg_restore to migrate the data.

Did you do an ANALYZE on the Postgres 16 instance after the pg_restore?

> takes 2-3s with the old Pg13 DB, and over a minute with Pg16

Do you have an EXPLAIN ANALYZE for the Postgres 13 case?

-- 
Adrian Klaver
[email-masked]

---

From: Laurenz Albe <[email-masked]>
Date: Tue, 08 Apr 2025 08:40:11 +0200

> After spending many hours looking at DB settings and Statistic settings 

Thanks.

- Can you show the index definitions on that table?

- If you "SET enable_seqscan = off;" in an interactive session, PostgreSQL
  should choose an index scan if possible.  Can you show the
  EXPLAIN (ANALYZE, BUFFERS) output for that execution?

Yours,
Laurenz Albe