Column with PK constraint contains null values in Postgres - postgresql

We are using postgres version 9.3 in our production databases.
We found that some of them contain null values ​​in a column that has a PK constraint. In a given table, there were 53 null-value records in a column with PK. We found the same problem with some columns with constraint NOT NULL. We found the inconsistency when we tried to update an column of the table that has this problem and received an error message regarding the constraints. How can data be inconsistent if constraints exist? Is this a postgres bug or some kind of database corruption?

Related

Is it possible to get another field of row I'm trying to duplicate in PSQL or MyBatis?

I have a table 'client', which has 3 columns - id, siebel_id, phone_number.
PhoneNumber has a unique constraint. If I save a new client with an existing number, I'll get an error ERROR: duplicate key value violates unique constraint "phone_number_unique".
Is it possible to make PSQL or MyBatis showing 'siebel_id' of a record where the phone number already saved?
I mean to get a message like
'ERROR: duplicate key value violates unique constraint "phone_number_unique"
Detail: Key (phone_number)=(+79991234567) already exists on siebel_id...'
No, it's not possible to tweak the internal message that the PostgreSQL database engine returns accompannying an error. Well... unless you recompiled the whole PostgreSQL database from scratch, and I would assume this is off the table.
However, you can easily search for the offending row using SQL, as in:
select siebel_id from client where phone_number = '+79991234567';

PostgreSQL Unique Key column contains duplicate records

I have a table in PG 9.6 with unique key constraint on it. I took the backup using pg_dump and restore it in PG12. While restoring I got error for the table mentioned with unique key constraint failed. After checked in PG 9.6, Column contains few duplicates.
Here Question is
How is it possible for containing duplicates in column with unique key constraint ?
Also, how to fix this error in PG12 without deleting duplicates (PG 9.6 contains duplicates)?

Imported data, duplicate key value violates unique constraint

I am migrating data from MSSQL.
I created the database in PostgreSQL via npgsql generated migration. I moved the data across and now when the code tries to insert a value I am getting
'duplicate key value violates unique constraint'
The npgsql tries to insert a column with Id 1..how ever the table already has Id over a thousand.
Npgsql.EntityFrameworkCore.PostgreSQL is 2.2.3 (latest)
In my context builder, I have
modelBuilder.ForNpgsqlUseIdentityColumns();
In which direction should I dig to resolve such an issue?
The code runs fine if the database is empty and doesn't have any imported data
Thank you
The values inserted during the migration contained the primary key value, so the sequence behind the column wasn't incremented and is kept at 1. A normal insert - without specifying the PK value - calls the sequence, get the 1, which already exists in the table.
To fix it, you can bump the sequence to the current max value.
SELECT setval(
pg_get_serial_sequence('myschema.mytable','mycolumn'),
max(mycolumn))
FROM myschema.mytable;
If you already know the sequence name, you can shorten it to
SELECT setval('my_sequence_name', max(mycolumn))
FROM myschema.mytable;

DB2 -- unique constraint on multiple columns

I'm trying to add a unique constraint on 2 columns on DB2-- docType column and title column. The values of each column can repeat in themselves. however, the values of (docType, title) pairs should not repeat.
I tried so far
ALTER TABLE externalfiles
ADD CONSTRAINT logicalKey UNIQUE (doctype, title)
and
alter table externalfiles add unique (doctype, title)
, and got the following error to both:
Operation not allowed for reason code "7" on table "PIT.EXTERNALFILES".. SQLCODE=-668, SQLSTATE=57016, DRIVER=4.21.29
Isn't this any allowed??
DB2 Unique Constraint over multiple Columns suggests creating indexes as alternative to this. Haven't tried indexing them yet-- however, i'm wondering why the unique constraint isn't working.
TIA.
This web search on db2 luw sqlstate 57016 returns a number of links that suggest a prior ALTER may require that a REORG be performed before the ADD CONSTRAINT can be effected.
Perhaps most notable was the following doc link and snippet of text describing the RC7 [apparently SQLERRMC=7]:
DB2 for Linux UNIX and Windows 9.7.0
->Database fundamentals
->Messages
->SQL Messages
->SQL0500 - SQL0999
->SQL0668N
…
7 • The table is in the reorg pending state. This can occur after an
ALTER TABLE statement containing a REORG-recommended operation.
…
First try to reorganize table by
call sysproc.admin_cmd ('reorg table <TABLE_NAME>');
Than try again to create unique index. It should work.

ERROR: duplicate key value violates unique constraint in postgreSQL

i am getting a unique constraint issue in postgresql while updating a table. I have a table with 3 columns and an unique constraint on one of the column(internal_state). This table will have only two columns and values for internal_state are 1,0.
The update query is
UPDATE backfeed_state SET internal_state = internal_state - 1
WHERE EXISTS (SELECT 1 FROM backfeed_state d2 WHERE d2.internal_state = 1 )
Running this query is fine in MSSqlserver but in postgre it is throwing unique constraint error.
What i understand is in SQLServer after updating all the rows then only constraint on the columns are checking but in postgre after updating each row, constraints are checking. So after updating the first row(internal_state value from 1 to 0) postgre is checking the constraint and throwing error even before updating the second row.
Is there a way to avoid this situation?
http://www.postgresql.org/docs/9.0/static/sql-createtable.html in section "Non-deferred Uniqueness Constraints" - "When a UNIQUE or PRIMARY KEY constraint is not deferrable, PostgreSQL checks for uniqueness immediately whenever a row is inserted or modified."
Changing your unique constraint to deferrable will hold off checking until the end of the update. Either use SET CONSTRAINTS to disable at the session level (which is annoyingly repetitive) or drop and re-create the uniqueness constraint with the deferrable option (I'm not aware of an ALTER construct to do that without dropping).