1

I have an array that I can update from multiple workers as follows: UPDATE my_table SET arr = array_append(my_table.arr, element).

The problem pops up when two workers try to update the array at the same time. For instance, both workers see that at the moment of the update, the array is empty and try to append an element as follows: array_append(empty_arr, element_a) and array_append(empty_arr, element_b). In the end, the final content of the array will be: arr[element_a] or arr[element_b] because one of the workers will finish updating the array earlier and its value will be overwritten with the value from the other worker. How can I ensure that both values are written in the array: arr[element_a, element_b]?

asked Jan 25, 2021 at 9:20

1 Answer 1

1

There is nothing you need to do, because this kind of anomaly cannot occur in PostgreSQL.

When the second transaction gets the lock on the row, it will re-read it and see the value modified by the first transaction.

See the documentation for details.

answered Jan 25, 2021 at 12:10

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.