pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <haramrae@gmail.com>
To: Roman Guryanov <r.guryanov.integrix@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: SELECT FOR UPDATE returns zero rows with CTE
Date: Fri, 17 Sep 2021 11:36:31 +0200
Message-ID: <E782221B-C09C-4521-A79B-A6DF3F7B61DA@gmail.com> (raw)
In-Reply-To: <CAJaMrZaLovOobUj3TX_ONCW=-vmj1BD-3OvOpj+-4Wr3KKkZNg@mail.gmail.com>
References: <CAJaMrZaLovOobUj3TX_ONCW=-vmj1BD-3OvOpj+-4Wr3KKkZNg@mail.gmail.com>



> On 17 Sep 2021, at 8:32, Roman Guryanov <r.guryanov.integrix@gmail.com> wrote:
> 
> Hello, could you check my problem.
> Why does SELECT FOR UPDATE return 0 rows in the scenario below? (execution in transaction)
> 
> If delete 'FOR UPDATE', 1 row returned
> 
> Test case:
> DROP TABLE IF EXISTS
>  t1;
> 
> CREATE TABLE t1 (_pk serial, t1c1 integer, t1c2 integer
> , t1c3 text);
> 
> insert into t1 (t1c1, t1c2, t1c3) values(123456789, 100, 'string_value_1'
> );

(…cut everything related to unused t2…)

> 
> WITH
>     cte1 
> AS
>  (    
> UPDATE
>  t1
> SET t1c3 = 'string_value_1'
> WHERE t1c1 = 123456789
> returning t1c1, t1c2
>     ),
>     cte2 
> AS
>  (
> SELECT * FROM
>  t1      
> WHERE t1c1 = 123456789          
> AND t1c2 = (SELECT t1c2 FROM cte1)
> FOR UPDATE
>     )
> 
> SELECT * FROM cte2;


Most likely the outer select returns 0 rows because you locked the rows you expected in cte2 and didn’t perform an update on those locked rows yet.

I suspect your intention for this query is to first lock the rows, then update them and then select them, but instead you start with updating them, then lock those rows after the fact and then you try to select those locked rows.

Also, selecting the updated rows by t1c2 in cte2 seems rather risky, as that is a rather different selection criterium than you use for the actual update. It’s okay for this single-row example, but if you had a table full of data, you would now have locked all rows with the value t1c2 = 100 for update. If that update never happens (or the locking doesn’t get rolled back), well…

Regards,

Alban Hertroys
--
There is always an exception to always.









view thread (3+ messages)  latest in thread

Message-ID: <E782221B-C09C-4521-A79B-A6DF3F7B61DA@gmail.com>
Permalink:  ../E782221B-C09C-4521-A79B-A6DF3F7B61DA@gmail.com/
Also on:    postgresql.org/message-id/E782221B-C09C-4521-A79B-A6DF3F7B61DA@gmail.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: haramrae@gmail.com, r.guryanov.integrix@gmail.com
  Subject: Re: SELECT FOR UPDATE returns zero rows with CTE
  In-Reply-To: <E782221B-C09C-4521-A79B-A6DF3F7B61DA@gmail.com>
  References: <CAJaMrZaLovOobUj3TX_ONCW=-vmj1BD-3OvOpj+-4Wr3KKkZNg@mail.gmail.com> <E782221B-C09C-4521-A79B-A6DF3F7B61DA@gmail.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