Re: DELETE not seeming to use the PK index..

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Wells Oliver <wells(dot)oliver(at)gmail(dot)com>
Cc: pgsql-admin <pgsql-admin(at)postgresql(dot)org>
Subject: Re: DELETE not seeming to use the PK index..
Date: 2020-07-21 00:27:14
Message-ID: 769076.1595291234@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

Wells Oliver <wells(dot)oliver(at)gmail(dot)com> writes:
> As you might guess, it takes forever. Why is it not using the index?

Because it thinks it's going to have to delete half of the table
(36M / 73M rows). That's pretty obviously coming from a fallback default
selectivity estimate, which makes one wonder if your statistics for the
tables are up to date, or indeed exist at all.

> I tried a VACUUM FULL ANALYZE before, no change to the planner.

Hmph. That should have updated things, but the numbers for temptable
at least are obviously not coming from any actual statistics. (The
"200" for number of distinct values is another telltale default.)
If temptable actually is a temp table, was it included in the VACUUM
or did you create it after?

Are there any non-built-in datatypes or operators involved here?
Perhaps you've enabled RLS and it's preventing access to the stats?

> Should I create a secondary index just on pkcol1? I thought it'd use the PK
> index since it's the first column.

This is not a lack-of-index problem, or at least it won't be until
you get rowcount estimates that would encourage the planner to think
that an index would be helpful. A rule of thumb is that if the
query needs to fetch more than a percent or two of the table, an
index is likely not worth the trouble.

regards, tom lane

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message jian xu 2020-07-21 13:20:23 Re: checkpoint process use too much memory
Previous Message Wells Oliver 2020-07-20 23:55:23 DELETE not seeming to use the PK index..