Re: [BUGS] BUG #11500: PRIMARY KEY index not being used - Mailing list pgsql-bugs

From Tom Lane
Subject Re: [BUGS] BUG #11500: PRIMARY KEY index not being used
Date
Msg-id 1704045.1759687759@sss.pgh.pa.us
Whole thread Raw
In response to Re: [BUGS] BUG #11500: PRIMARY KEY index not being used  (Marko Tiikkaja <marko@joh.to>)
Responses Re: [BUGS] BUG #11500: PRIMARY KEY index not being used
List pgsql-bugs
Marko Tiikkaja <marko@joh.to> writes:
> I can't believe I actually have to do this, but run:

That's not what was asked for.  Your original report showed a
case where the planner chose orders_wait_event_idx despite
estimating that it would have to scan 80K-plus rows.
That's the case where I don't understand how it could think
that the primary key index would be even more expensive.

I'm not particularly concerned about the case you show here,
because if you run the EXPLAIN a second time it gets a lot
cheaper.  I believe the reason is that the first time is
visiting a bunch of just-deleted rows and so it has to stop
and update their hint bits, both in the heap and the index.
That is not a cost that the planner can reasonably predict
in advance, and even if we could it's not really fair to
blame it on the choice of index.  The pkey alternative only
visits one row and therefore only updates one hint bit, but
we'd have to update the rest sooner or later.  So IMO the
speed difference is largely illusory because it arises from
deferring maintenance.  You can demonstrate this by changing
the "ANALYZE orders" in the test case to "VACUUM ANALYZE
orders": the EXPLAIN still chooses orders_wait_event_idx,
but now it's fast because all that work got done by VACUUM.

            regards, tom lane



pgsql-bugs by date:

Previous
From: Etsuro Fujita
Date:
Subject: Re: TRAP: failed Assert("outerPlan != NULL") in postgres_fdw.c
Next
From: Marco Boeringa
Date:
Subject: Re: Potential "AIO / io workers" inter-worker locking issue in PG18?