pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Philippe VIEGAS <p.viegas@loxodata.com>
To: pgsql-general@lists.postgresql.org
Subject: Index scan for PK constraint validation
Date: Wed, 8 Feb 2023 13:09:51 +0100
Message-ID: <337ea717-3d64-6b72-1243-770ae2ff9a04@loxodata.com> (raw)

Hi all,

I was wondering why the index statistics usage were not reflecting the 
index usage for primary key constraint validation ?

When we create a table with a primary key, PostgreSQL creates a Btree 
index for the validation of this constraint : index is defined unique 
(indisunique=t) and primary (indisprimary=t) from the pg_index and 
pg_class catalogs.

Inserting data on the above created table and selecting based on the 
primary key column will increment the idx_scan from 
`pg_stat_user_indexes` view.
But if we insert again in this table with a conflicting id, the primary 
key is being validated using the index as stated in our example :

ERROR: 23505: duplicate keyvalueviolates uniqueconstraint"testpkidx_pkey"
DETAIL: Key(id)=(1) already exists.
SCHEMANAME: public
TABLENAME: testpkidx
CONSTRAINTNAME: testpkidx_pkey
LOCATION: _bt_check_unique, nbtinsert.c:664

But checking again the `pg_stat_user_indexes` view, the idx_scan 
attribute does not seem to be incremented.

Is the statistics of index usage for constraint validation being 
computed elsewhere ?


Are we missing something ?


Thanks in advance.

Regards.
-- 
Philippe VIEGAS

view thread (2+ messages)  latest in thread

Message-ID: <337ea717-3d64-6b72-1243-770ae2ff9a04@loxodata.com>
Permalink:  ../337ea717-3d64-6b72-1243-770ae2ff9a04@loxodata.com/
Also on:    postgresql.org/message-id/337ea717-3d64-6b72-1243-770ae2ff9a04@loxodata.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: p.viegas@loxodata.com
  Subject: Re: Index scan for PK constraint validation
  In-Reply-To: <337ea717-3d64-6b72-1243-770ae2ff9a04@loxodata.com>
  References: <337ea717-3d64-6b72-1243-770ae2ff9a04@loxodata.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox