'Indexes not working after Postgresql Upgrade (10.18 -> 11.14)

Solved: Doing a vacuum on all of the old tables resolved the index issues. Autovacuum was never going to happen because they were all static.

Simplest query that replicates problem:

    SELECT group_id
    FROM table_1
    LEFT JOIN table_2
    ON gp_id = grid_point_id

table_1 in this instance is a small temporary table (fewer than 20 rows at the moment) and converting it to a regular table does not solve the issue. table_2 is ~5gb

The index itself is actually just the primary key (grid_point_id, hour) on old and new tables. This join would have used the index on all tables before the upgrade, but after the upgrade the index created by the primary key is no longer used on tables built before the upgrade. Tables built after the upgrade use the index correctly.

Even simpler queries (select * from old_table WHERE grid_point_id = x) work correctly with the old index/primary key.

Edit:

The postgresql database is hosted on Amazon Aurora and was updated using AWS web tools, rather than calling pg_upgrade manually, although I assume they are the same under the hood.

Explain Old Table:

Hash Right Join  (cost=13.60..1509824.84 rows=36187080 width=4) (actual time=51.625..23145.155 rows=448 loops=1)
  Hash Cond: ("table_2".grid_point_id = table_1.gp_id)
  ->  Seq Scan on "table_2"  (cost=0.00..978313.50 rows=45233850 width=4) (actual time=0.010..15493.724 rows=42603904 loops=1)
  ->  Hash  (cost=11.60..11.60 rows=160 width=8) (actual time=0.010..0.010 rows=2 loops=1)
        Buckets: 1024  Batches: 1  Memory Usage: 9kB
        ->  Seq Scan on table_1  (cost=0.00..11.60 rows=160 width=8) (actual time=0.006..0.007 rows=2 loops=1)
Planning Time: 0.237 ms
Execution Time: 23145.329 ms

Explain New Table:

Nested Loop Left Join  (cost=0.56..1718.80 rows=35525 width=4) (actual time=0.024..0.129 rows=448 loops=1)
  ->  Seq Scan on table_1  (cost=0.00..11.60 rows=160 width=8) (actual time=0.004..0.004 rows=2 loops=1)
  ->  Index Only Scan using "table_2_pkey" on "table_2"  (cost=0.56..8.45 rows=222 width=4) (actual time=0.012..0.043 rows=224 loops=2)
        Index Cond: (grid_point_id = table_1.gp_id)
        Heap Fetches: 0
Planning Time: 0.222 ms
Execution Time: 0.164 ms


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source