How to reorg the indexes in DB2 database - db2

I have to reorg all the index for table
I am getting the following error
SQL Error [23505]: One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "2" constrains table "GMS4.SMS_PHYSICAL_CUSTOMER_DATA" from having duplicate values for the index key.. SQLCODE=-803, SQLSTATE=23505, DRIVER=4.16.53
One or more values in the INSERT statement, UPDATE statement, or foreign key update caused by a DELETE statement are not valid because the primary key, unique constraint or unique index identified by "2" constrains table "GMS4.SMS_PHYSICAL_CUSTOMER_DATA" from having duplicate values for the index key.. SQLCODE=-803, SQLSTATE=23505, DRIVER=4.16.53
DB2 Version 10
Please help..

I'm assuming you're on DB2 for Linux/Unix/Windows, here.
Your problem is not that you need to reorg your tables. The problem is that you are trying to insert a row, but you have a unique index on that table, which is preventing the insert.
You can see the name of the index, and the columns it is unique for by using this query:
SELECT
I.INDSCHEMA
,I.INDNAME
,C.COLNAME
FROM SYSCAT.INDEXES I
JOIN SYSCAT.INDEXCOLUSE C
ON I.INDSCHEMA = C.INDSCHEMA
AND I.INDNAME = C.INDNAME
WHERE I.IID = #indexID
AND I.TABSCHEMA = #tableSchema
AND I.TABNAME = #tableName
ORDER BY C.COLSEQ
;
You can get all of the parameters needed for this query from your error message. In this case, #indexId would be 2, #tableSchema would be GMS4, and #tableName would be SMS_PHYSICAL_CUSTOMER_DATA.

Related

insert into, on conflict do update, why fail? postgresql, pgadmin

I am trying to insert a new table into a big old table to update multiple rows ,
here is my query:
INSERT INTO site_settings (set_id, set_sit_id,set_setting_name,set_setting_type)
SELECT set_id, set_sit_id, replace(TempTable2.stp_device_pool_filter, '${siteShortName}', TempTable2.sit_short_name), set_setting_type from TempTable2 where set_setting_type='DEVICE_POOL'
ON CONFLICT (set_id) DO UPDATE
SET set_sit_id=excluded.set_sit_id,
set_setting_name=excluded.set_setting_name,
set_setting_type=excluded.set_setting_type;
it returns me the message:
duplicate key value violates unique constraint "unique_site_setting"
DETAIL: Key (set_sit_id, set_setting_name, set_setting_type)=(13, SBA123-rr, DEVICE_POOL) already exists.
However, I used to use the similar query to update a much more complicated table, it worked.
don't know what's the problem

INSERT INTO excluding ID column violates primary key uniqueness constraint

I have a Postgres 10.6 table with a serial ID column.
When I attempt to insert into it:
INSERT INTO table (col1, col2) VALUES ('foo', 'bar');
excluding the ID column from the column list, I get:
ERROR: duplicate key value violates unique constraint "customer_invoice_pkey"
Detail: Key (id)=(1234) already exists.
Subsequent runs of the query increment the ID in the error message (1235, 1236 etc.)
How can this be happening?
Having a serial column does not prevent you from inserting rows with an explicit value for id. The sequence value is only a default value that is used when id is not specified in the INSERT statement.
So there must have been some “rogue” inserts of that kind. From PostgreSQL v11 on, you can use identity columns (GENERATED ALWAYS AS IDENTITY) to make overriding the sequence value harder.
You could use the setval function to set the sequence to a value higher than the maximum id in the table to work around the problem.

Postgres UPSERT syntax confusion

I have a table with 2 columns:
channels TEXT
rowid INTEGER PRIMARY KEY
I included an index on channels
CREATE UNIQUE INDEX channels_index on mytable (lower(channels))
so that VisitToronto will be a conflict with visittoronto
All works well and the conflict fires.
ERROR: duplicate key value violates unique constraint "channels_index"
DETAIL: Key (lower(words))=(hello world) already exists.
I can not figure out the syntax to trap this conflict. ON CONFLICT channels doesn't work ON CONFLICT ON CONSTRAINT channels_index doesn't work
The closest I've got is:
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
Any direction would be appreciated.
TIA
Use the index expression, i.e. lower(channels):
insert into my_table (channels) values
('VisitToronto');
insert into my_table (channels)
values ('visittoronto')
on conflict (lower(channels)) do
update set channels = excluded.channels;
select *
from my_table;
id | channels
----+--------------
1 | visittoronto
(1 row)
You are not able to use a constraint because the index is on an expression. In the case Postgres cannot create a constraint:
alter table my_table add constraint channels_unique unique using index channels_index;
ERROR: index "channels_index" contains expressions
LINE 1: alter table my_table add constraint channels_unique unique u...
^
DETAIL: Cannot create a primary key or unique constraint using such an index.

Duplicate key during import in PostgreSQL

I am following an PostgreSQL book, and had to import a CSV file into a table census.lu_tracts.
Problem: When performing the INSERT query as shown below, I get the error:
ERROR: duplicate key value violates unique constraint "pk_lu_tracts"
DETAIL: Key (tract_id)=(25001010800) already exists.
How did the key becomes duplicate? SELECT * from lu_tracs shows 0 rows.
CREATE SCHEMA census;
set search_path=census;
CREATE TABLE lu_tracts(tract_id varchar(11), tract_long_id varchar(25)
, tract_name varchar(150)
, CONSTRAINT pk_lu_tracts PRIMARY KEY (tract_id));
INSERT INTO lu_tracts( tract_id, tract_long_id, tract_name)
SELECT geo_id2, geo_id, geo_display
FROM staging.factfinder_import
WHERE geo_id2 ~ '^[0-9]+';
The right answer is DISTINCT ON (geo_id2) which will select only one row per geo_id2 (more in the manual), it should be accompanied by an ORDER BY clause that will specify what row will be choosen.

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).