Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPjGw-0000fD-Fx for pgsql-general@arkaria.postgresql.org; Wed, 08 Feb 2023 12:09:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pPjGv-0005si-Dd for pgsql-general@arkaria.postgresql.org; Wed, 08 Feb 2023 12:09:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPjGu-0005sM-QT for pgsql-general@lists.postgresql.org; Wed, 08 Feb 2023 12:09:57 +0000 Received: from mail.loxodata.com ([213.251.186.71]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPjGs-0006bK-PF for pgsql-general@lists.postgresql.org; Wed, 08 Feb 2023 12:09:56 +0000 Received: from authenticated-user (mail.loxodata.com [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.loxodata.com (Postfix) with ESMTPSA id 8DF88800A5 for ; Wed, 8 Feb 2023 12:09:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=loxodata.com; s=mail; t=1675858192; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=fEWWM+vj+1i1hIKnfwrphxRAumT+w3kKqNdviCSMw6U=; b=rn9BpJcFBCPTGXynRCVllxS8UEiNHAXoa647cOwXpSv/OOQrVG56nN25s9Wt+H2Ss6oq2v +7Pv9LQRAR5QmmMLJ15I2pMyEc9YehBUd268++VzmqNL3QKFhMNAJDvV3DgpTfdPQDA/vq UCGkmkG6gkgbcriLdaTaUpa0A59hLk0= ARC-Authentication-Results: i=1; mail.loxodata.com; auth=pass smtp.mailfrom=p.viegas@loxodata.com ARC-Seal: i=1; s=mail; d=loxodata.com; t=1675858192; a=rsa-sha256; cv=none; b=KBAeWU6yN711+5dPXqt2i2ACHnAxUaqKkgiMgSnEsMYbkwaEqxNfRFSA7Tkyb9ow5mgfZ1 X/Y/0D8UWHrOco6okEA1BhXNWBxgXn+TKd+qGINDQ9isDs4iXm69sfFUBQgsqdRwxZCf83 R/pZw/VGP4zVN0f4anqkJ7Y+j+pSb6c= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=loxodata.com; s=mail; t=1675858192; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=fEWWM+vj+1i1hIKnfwrphxRAumT+w3kKqNdviCSMw6U=; b=b94kxuCeH+QbmNvCYdkf0uftAgZkVVJDfoXr263hAyyktOMV0fUhi4ilbgRPFK05HmIFxU 7Ib5d/UpLVrSdKnLtzlYpqAWiEBo+OXbH6W5uzJm/y3psS3uzbmSRFTnYXVw2LcMt+TgLg 2AOkACXL1DQMbvHfma+C/rbWZRJe63k= Content-Type: multipart/alternative; boundary="------------ixNrnThX8DSd53Jtz2OlD8uT" Message-ID: <337ea717-3d64-6b72-1243-770ae2ff9a04@loxodata.com> Date: Wed, 8 Feb 2023 13:09:51 +0100 MIME-Version: 1.0 Content-Language: fr To: pgsql-general@lists.postgresql.org From: Philippe VIEGAS Subject: Index scan for PK constraint validation List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------ixNrnThX8DSd53Jtz2OlD8uT Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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 --------------ixNrnThX8DSd53Jtz2OlD8uT Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Hi all,

I was wondering why the index statistics usage were not reflecting the index usage for primary key constraint validation ?<= br>
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=3Dt) and primary (indisprimary=3Dt) fro= m 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 key value violates uniqueconstraint "testpkidx_pkey= "
DETAIL: Key (id= )=3D(1) already exists= .
SCHEMA NAME= : public
TABLE NAME= : testpkidx
CONST= RAINT NAME: testpkidx_pkey
LOCATION: _bt_chec= k_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
--------------ixNrnThX8DSd53Jtz2OlD8uT--