Re: insert
От | Vitalii Tymchyshyn |
---|---|
Тема | Re: insert |
Дата | |
Msg-id | 4E366941.9070507@gmail.com обсуждение исходный текст |
Ответ на | Re: insert (alan <alan.miller3@gmail.com>) |
Ответы |
Re: insert
|
Список | pgsql-performance |
Hello. Please note that in multitasking environment you may have problems with your code. Two connections may check if "a" is available and if not (and both got empty "select" result), try to insert. One will succeed, another will fail if you have a unique constraint on category name (and you'd better have one). Please note that select for update won't help you much, since this is new record you are looking for, and select don't return (and lock) it. I am using "lock table <tableName> in SHARE ROW EXCLUSIVE mode" in this case. But then, if you have multiple lookup dictinaries, you need to ensure strict order of locking or you will be getting deadlocks. As for me, I did create a special application-side class to retrieve such values. If I can't find a value in main connection with simple select, I open new connection, perform table lock, check if value is in there. If it is not, add the value and commit. This may produce orphaned dictionary entries (if dictionary entry is committed an main transaction is rolled back), but this is usually OK for dictionaries. At the same time I don't introduce hard locks into main transaction and don't have to worry about deadlocks. Best regards, Vitalii Tymchyshyn
В списке pgsql-performance по дате отправления: