9

Does the whole table UPDATE (without specifying WHERE clause) lock a table in PostgreSQL? E.g. does it prevents rows from being deleted / inserted?

E.g. if I run UPDATE t1 SET key = 'value' Can I expect no new rows will be inserted into t1 during the UPDATE execution?

If no, can I expect UPDATE will update even the rows appeared after its start? (key has no DEFAULT 'value' in its definition)

asked Sep 30, 2019 at 12:24
2
  • If it was updating rows that where inserted after it started and there was a process that was continually inserting rows wouldn't that mean the update statement would never finish? Commented Sep 30, 2019 at 12:27
  • good point! Yes, it is barely possible UPDATE to be implemented in such manner to update newly inserted rows. Commented Sep 30, 2019 at 12:32

1 Answer 1

15

An UPDATE without a WHERE clause will lock all rows in the table, but will not lock the table itself for DML.

The rows can not be deleted from a different transaction because they are locked.

But you can insert new rows without problems (assuming they do not violate any constraints).

Any row that is inserted after the UPDATE will not be seen by the UPDATE statement and thus they won't be changed.

answered Sep 30, 2019 at 12:39
1
  • 1
    @origaminal The doc in question is at postgresql.org/docs/current/explicit-locking.html , the last four are what you're calling 'table locks' -- although even permissive locks are still a form of table lock that will prevent attempts to take an exclusive lock. Commented Oct 1, 2019 at 3:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.