This KB provides guidance for identifying and resolving Primary Key (PK) duplication errors that typically occur due to data corruption or constraint issues.
updatemgrlogXX.txt
for corruption errors./pgsql/data/pg_log
for corruption errors.logs/sps/pgsql/dailypgbackupX.txt
for corruption errors.Look for errors similar to:
If the above PK duplication / index corruption error is confirmed:
Stop the application server.
Take a snapshot of the PostgreSQL folder (pgsql
) before attempting any corruption resolution.
This ensures you have a rollback point in case the cleanup process causes unexpected issues.
If the error is different from the above PK duplication issue → do not proceed with cleanup. Instead:
Collect the complete pgsql
folder in debugging mode.
Move the ticket to G3-Sunil Team for detailed corruption analysis and resolution.
pgsql
folder and move the ticket to the G3-Sunil Team.Use the following template query inside a transaction to remove duplicate rows:
ERROR: duplicate key value violates unique constraint "requestnotificationtodesc_pk"
PK column = notificationid
DELETE FROM requestnotificationtodesc a USING requestnotificationtodesc b WHERE a.ctid < b.ctid AND a.notificationid = b.notificationid;
Error:
ERROR: duplicate key value violates unique constraint "adsadsyncobjattributes_pk"
PK columns = object_id, attrib_ldap_name
DELETE FROM adsadsyncobjattributes a USING adsadsyncobjattributes b WHERE a.ctid < b.ctid AND a.object_id = b.object_id AND a.attrib_ldap_name = b.attrib_ldap_name;
If an error message appears while executing the cleanup query, rollback the transaction immediately to prevent partial changes.
Check whether the error is due to a syntax mistake. If so, correct the query and re-execute it.
If the query still fails for reasons other than syntax, collect the complete pgsql
folder and move the ticket to the G3-Sunil Team.
Use the \d
command in psql
to describe the table: \d <table_name>;
Identify the primary key column(s) from the output.
If the table has a single-column PK, update the query with that column.
If the table has a composite PK, include all PK columns in the query.
Run the following maintenance queries on the database: (Note : Don't run below query within transaction)
Confirm that above commands executed successfully.
If errors occur and the error mention index corruption, re-check the inconsistent table as per Steps 3–4, delete duplicate records, and re-run REINDEX
and VACUUM
.
If any other type of error occurs, collect the complete pgsql
folder in debugging mode and move the ticket to G3-Sunil Team for corruption analysis and resolution.
After completing the cleanup and validation:
Run a pg_dump
command from the pgsql/bin/
directory to ensure the database can be backed up successfully