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 the one below, where the index name is mentioned (e.g., convertodesc_pk
):
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.<index_name>
with the index name from the error message):Once the table name and PK column(s) are identified, use the template below 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.
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, verify that the database can be backed up successfully using either of the following methods. If the command completes without errors, the corruption has been resolved successfully. If the dump fails, collect the complete pgsql folder and move the ticket to G3-Sunil Team.
pg_dump
CommandRun the following command from the pgsql/bin/
directory to validate the backup.
Replace <port_number>
with the actual PostgreSQL port in your environment
If preferred, you can run the manual backup script instead. Note that this method usually takes longer than pg_dump
.
Execute the backup script located in the bin/
directory:
On Windows: backUpData.bat
On Linux/Unix: backUpData.sh
If the PostgreSQL version is 15+, please make sure to mention G3-sunil team in the ticket stating that corruption was encountered in this version, since we are actively monitoring these cases.
The provided queries are safe cleanup queries that retain one valid row while removing duplicate
Always execute the fix in a transaction block (BEGIN … COMMIT) to avoid accidental data loss.