pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Index scan for PK constraint validation
2+ messages / 2 participants
[nested] [flat]

* Index scan for PK constraint validation
@ 2023-02-08 12:09  Philippe VIEGAS <p.viegas@loxodata.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Philippe VIEGAS @ 2023-02-08 12:09 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Index scan for PK constraint validation
@ 2023-02-08 13:25  David G. Johnston <david.g.johnston@gmail.com>
  parent: Philippe VIEGAS <p.viegas@loxodata.com>
  0 siblings, 0 replies; 2+ messages in thread

From: David G. Johnston @ 2023-02-08 13:25 UTC (permalink / raw)
  To: Philippe VIEGAS <p.viegas@loxodata.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

On Wednesday, February 8, 2023, Philippe VIEGAS <p.viegas@loxodata.com>
wrote:

> 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.
>
>
The select produces the index scan, not the insert.  The insert doesn’t
scan, it populates and aborts if that population fails.  It doesn’t check
first.

David J.

^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2023-02-08 13:25 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-02-08 12:09 Index scan for PK constraint validation Philippe VIEGAS <p.viegas@loxodata.com>
2023-02-08 13:25 ` David G. Johnston <david.g.johnston@gmail.com>

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