问题
I have the following Entity Attribute value table :
CREATE TABLE key_value_pair (
id serial NOT NULL PRIMARY KEY,
key varchar(255) NOT NULL,
value varchar(255),
is_active boolean
);
CREATE UNIQUE INDEX key_value_pair_key_if_is_active_true_unique ON key_value_pair (key) WHERE is_active = true;
Sample entries in this table are :
id | key | value | is_active
----+-------------+-------+-----------
1 | temperature | 2 | f
2 | temperature | 12 | f
3 | temperature | 15 | f
4 | temperature | 19 | f
5 | temperature | 23 | t
(5 rows)
Thus, at any point in time, for any given key, only 1 true is_active entry should be present.
I am running the following upsert statement on this table :
INSERT INTO key_value_pair (key, value, is_active) VALUES ('temperature','20', true)
ON CONFLICT (key, is_active)
DO UPDATE
SET value = '33', is_active = true;
But, it fails with:
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
What I am wondering is why it is not using the unique partial index key_value_pair_key_if_is_active_true_unique.
The upsert works if I let go of the "at any point in time, for any given key, only 1 true is_active entry should be present" clause and change the index to:
CREATE UNIQUE INDEX key_value_pair_key_if_is_active_true_unique ON key_value_pair (key, is_active);
I read documentation on the Postgres website that partial indexes will be used by the ON CONFLICT clause. I wonder why it is not used in this case. What am I missing here, or what error am I making?
回答1:
You have to use an index predicate to use a partial unique index. Read in the documentation:
index_predicate
Used to allow inference of partial unique indexes. Any indexes that satisfy the predicate (which need not actually be partial indexes) can be inferred. Follows CREATE INDEX format.
In this case:
INSERT INTO key_value_pair (key, value, is_active) VALUES ('temperature','20', false)
ON CONFLICT (key) WHERE is_active
DO UPDATE
SET value = '33', is_active = true;
回答2:
Another example:
> users
id | name | colour | active
---+---------+----------+--------
1 | 'greg' | 'blue' | false
2 | 'kobus' | 'pink' | true
--------------------------------------------------------------------------
CREATE UNIQUE INDEX index_name
--columns applicable to partial index
ON users (name, colour)
--partial index condition **
WHERE not active
--------------------------------------------------------------------------
INSERT INTO users (name, colour, active)
--multiple inserts
VALUES ('greg', 'blue', false), --this already exists for [false], conflict
('pieter', 'blue', true),
('kobus', 'pink', false) --this already exists for [true], no conflict
ON CONFLICT (name, colour)
--partial index condition **same as original partial index condition
WHERE not active
--conflict action
DO UPDATE SET
active = not EXCLUDED.active
--on conflict example, change some value, i.e. update [active]
Row where name = greg
and colour = blue
for active = false
will now be updated to active = true
, the rest will be inserted
来源:https://stackoverflow.com/questions/46727740/partial-index-not-used-in-on-conflict-clause-while-performing-an-upsert-in-postg