A
A
aarifkhamdi2019-10-04 21:28:25
PostgreSQL
aarifkhamdi, 2019-10-04 21:28:25

Is there a difference between an expression and a transaction?

If two requests (read and update) occur within the same READ COMMITED transaction, then, of course, we cannot be sure that the update will definitely occur. The data may have changed in another transaction.
How about one expression? Judging from the documentation, the same lines are visible inside the expression. Can we be sure that the update will definitely happen in the next construction (we assume that SELECT returned some data)?

WITH t (
    SELECT id
    FROM test
    WHERE (id = 1) AND (value = 2)
    RETURNING id
)
UPDATE test
SET value = 3
FROM t
WHERE
        (test.id = t.id)
    AND (t.value = t.value)

Additionally: is there any way to check this so as not to ask such questions. Is there any tool to pause the request and see what happens if X happens?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2019-10-04
@aarifkhamdi

Can we be sure that the update will definitely happen in the next construction (we assume that SELECT returned some data)?

No we can not.
Two concurrent requests might see the same row versions in cte (won't cross on read locks even with concurrent writers since mvcc) and then go update the same rows. On row update locks, their transactions are serialized. By the way, you can also catch a deadlock if there are several rows for updating and they will return from the request in a different order.
gdb can pause anything. True, further is an interesting point that you need to find exactly where to put the break point.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question