7

The documentation says:

The locking clauses cannot be used in contexts where returned rows cannot be clearly identified with individual table rows; for example they cannot be used with aggregation.

How do I work around this in the setting of a recursive query, that is implemented with UNION (ALL)?

Is there a better solution than joining the result of the recursive query with the table again, this time with FOR UPDATE? I would have to lock the whole table for this query to make sure that nothing changes concurrently to the join, right?

1
  • 1
    What makes you think that aggregation has anything to do with a recursive query? Commented Aug 31, 2018 at 19:07

1 Answer 1

0

What makes you think that aggregation has anything to do with a recursive query? Seems to work for me,

CREATE TABLE foo(pk,fk,description)
AS VALUES
    ( 1 , null , 'domains' ),
    ( 2 , 1 , 'people' ),
    ( 3 , 1 , 'cars' ),
    ( 4 , 2 , 'tom' ),
    ( 5 , 2 , 'smith' ),
    ( 6 , 3 , 'vmw' ),
    ( 7 , 2 , 'betty' ),
    ( 8 , 3 , 'ford' );

WITH RECURSIVE t(pk, fk, description, level) AS (
    SELECT pk, fk, ARRAY[description], 0
    FROM foo
    WHERE fk IS NULL
    UNION ALL
        SELECT foo.pk, foo.fk, t.description || foo.description, t.level+1
        FROM t
        JOIN foo ON (foo.fk = t.pk)
)
SELECT *
FROM t
FOR UPDATE;

Example taken from example on this question on dba.se

10
  • 1
    WITH RECURSIVE contains a UNION ALL doesn't it? ;) Won't there be a race condition in your example when foo is altered during the recursive query? The rows are locked only after the SELECT FROM t ran, right? I need the rows to be locked as soon as they are encountered in the recursive query.
    – moi
    Commented Aug 31, 2018 at 20:27
  • You don't know what rows you're going to visit until you do the recursion. As you're doing the recursion the rows are added to the output result set and locked. Just like anything with READ COMMITTED and SELECT .. FOR UPDATE Commented Aug 31, 2018 at 20:38
  • You might be right, but can you cite any source to prove your claim? I would believe that the rows are locked after the result set is complete and not as soon as they are added. Following this I understand that the "outer" select runs somewhat concurrently to the recursive CTE, but I'd like to be sure that the locking happens immediately as soon as a row is visited.
    – moi
    Commented Aug 31, 2018 at 20:58
  • "but I'd like to be sure that the locking happens immediately as soon as a row is visited." if you're asking a database-internals question then my guess is you'll have to to rewrite a totally new question asking when the actual locking happens. As such, I'm not going to dive through the source to figure that out. My assumption is the rows returned are locked immediately, but I can see that being a complex issue covering cases like index-only scans and the like. Commented Aug 31, 2018 at 21:07
  • Of cause I don't expect you to dig through the source code only to answer my question. Thanks for your help so far!
    – moi
    Commented Aug 31, 2018 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.