pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-general <pgsql-general@postgresql.org>
Cc: Peter Geoghegan <pg@bowt.ie>
Subject: Re: ON CONFLICT and WHERE
Date: Sun, 13 Nov 2022 13:24:51 -0800
Message-ID: <231bbc6b-3e01-9ce7-3560-0025e2361d16@aklaver.com> (raw)
In-Reply-To: <736683.1668373664@sss.pgh.pa.us>
References: <4cc58ea7-c69d-cb4a-de8a-da66d79f0f5a@aklaver.com>
	<736683.1668373664@sss.pgh.pa.us>

On 11/13/22 13:07, Tom Lane wrote:
> Adrian Klaver <adrian.klaver@aklaver.com> writes:
>> INSERT INTO books VALUES (12, 0, CURRENT_TIMESTAMP)
>> ON CONFLICT (id)
>> WHERE updated IS NULL OR updated + INTERVAL '2min' < CURRENT_TIMESTAMP
>> DO UPDATE
>> SET version = books.version + 1, updated = CURRENT_TIMESTAMP;
> 
>> I have not used WHERE with ON CONFLICT myself so it took longer then I
>> care to admit to correct the above to:
> 
>> INSERT INTO books VALUES (12, 0, CURRENT_TIMESTAMP)
>> ON CONFLICT (id)
>> DO UPDATE
>> SET version = books.version + 1, updated = CURRENT_TIMESTAMP
>> WHERE books.version IS NULL OR books.updated + INTERVAL '2min' <
>> CURRENT_TIMESTAMP;
> 
>> The question is why did the first case just ignore the WHERE instead of
>> throwing a syntax error?
> 
> A WHERE placed there is an index_predicate attachment to the ON CONFLICT
> clause.  It doesn't have any run-time effect other than to allow partial
> indexes to be chosen as arbiter indexes.  TFM explains
> 
>      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.
> 
> This strikes me as a bit of a foot-gun.  I wonder if we should make
> it safer by insisting that the resolved index be partial when there's
> a WHERE clause here.  (This documentation text is about as clear as
> mud, too.  What does "inferred" mean here?  I think it means "chosen as
> arbiter index", but maybe I misunderstand.)

Alright I see how another use of WHERE comes into play.

I do agree with the clarity of the description, especially after looking 
at the example:

"
Insert new distributor if possible; otherwise DO NOTHING. Example 
assumes a unique index has been defined that constrains values appearing 
in the did column on a subset of rows where the is_active Boolean column 
evaluates to true:

-- This statement could infer a partial unique index on "did"
-- with a predicate of "WHERE is_active", but it could also
-- just use a regular unique constraint on "did"
INSERT INTO distributors (did, dname) VALUES (10, 'Conrad International')
     ON CONFLICT (did) WHERE is_active DO NOTHING;
"

I honestly cannot figure out what that is saying.

> 
> 			regards, tom lane

-- 
Adrian Klaver
adrian.klaver@aklaver.com






view thread (5+ messages)  latest in thread

Message-ID: <231bbc6b-3e01-9ce7-3560-0025e2361d16@aklaver.com>
Permalink:  ../231bbc6b-3e01-9ce7-3560-0025e2361d16@aklaver.com/
Also on:    postgresql.org/message-id/231bbc6b-3e01-9ce7-3560-0025e2361d16@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, tgl@sss.pgh.pa.us, pg@bowt.ie
  Subject: Re: ON CONFLICT and WHERE
  In-Reply-To: <231bbc6b-3e01-9ce7-3560-0025e2361d16@aklaver.com>
  References: <4cc58ea7-c69d-cb4a-de8a-da66d79f0f5a@aklaver.com> <736683.1668373664@sss.pgh.pa.us> <231bbc6b-3e01-9ce7-3560-0025e2361d16@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox