BUG #15180: Alter table add column if not exists with unique constraint will add extra duplicate

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: olav(at)backupbay(dot)com
Subject: BUG #15180: Alter table add column if not exists with unique constraint will add extra duplicate
Date: 2018-04-30 14:22:32
Message-ID: 152509815280.19803.16118194452213577808@wrigleys.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 15180
Logged by: Olav Gjerde
Email address: olav(at)backupbay(dot)com
PostgreSQL version: 10.1
Operating system: x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 6.3.0
Description:

Alter table add column if not exists with unique constraint will add extra
duplicate of the unique constraint when the column exists.

Example:
ALTER TABLE api_values ADD COLUMN IF NOT EXISTS master_key bigint NULL
UNIQUE;

If you run this several times you will get more unique constraints:
"api_values_master_key_key" UNIQUE CONSTRAINT, btree (master_key)
"api_values_master_key_key1" UNIQUE CONSTRAINT, btree (master_key)
"api_values_master_key_key2" UNIQUE CONSTRAINT, btree (master_key)

Workaround is just dropping the constraint if exits before the alter table
add column statement. But I am afraid a lot of developers will enter this
trap as it is kinda unexpected behavior. I read there is a similar problem
with serial/sequences.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2018-04-30 15:02:50 BUG #15181: pg_dump - missing schema in CREATE TEXT SEARCH DICTIONARY
Previous Message Pavel Stehule 2018-04-30 06:28:27 Re: BUG #15179: Related to copy command