Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

postgresql - Concurrent update of an array field in Postgres

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]?

question from:https://stackoverflow.com/questions/65882144/concurrent-update-of-an-array-field-in-postgres

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...