Re: Transactional-DDL DROP/CREATE TABLE

Поиск
Список
Период
Сортировка
От Merlin Moncure
Тема Re: Transactional-DDL DROP/CREATE TABLE
Дата
Msg-id CAHyXU0xLtYL_pMMuY+_f7KT5=WFxi_wobK_Rr3D9=0Q0nHGP5g@mail.gmail.com
обсуждение исходный текст
Ответ на Transactional-DDL DROP/CREATE TABLE  (Geoff Winkless <pgsqladmin@geoff.dj>)
Ответы Re: Transactional-DDL DROP/CREATE TABLE  (Geoff Winkless <pgsqladmin@geoff.dj>)
Список pgsql-general
On Thu, Oct 6, 2016 at 4:21 AM, Geoff Winkless <pgsqladmin@geoff.dj> wrote:
> Hi
>
> I have code that does (inside a single transaction)
>
> DROP TABLE IF EXISTS mytable; CREATE TABLE mytable ....
>
> Occasionally this produces
>
> ERROR: duplicate key value violates unique constraint
> "pg_type_typname_nsp_index" DETAIL: Key (typname,
> typnamespace)=(mytable, 2200) already exists.
>
> I can get away from this by using CREATE TABLE IF NOT EXISTS in the
> same code, but there's the potential that the wrong data will end up
> in the table if that happens, and it also seems a little.... odd.
>
> Would you not expect this transaction to be atomic? ie at commit time,
> the transaction should drop any table with the same name that has been
> created by another transaction.

Transactions do not guarantee atomicity in the sense that you mean.
MVCC rules (which DDL generally fall under) try to interleave work as
much as possible which is the problem you're facing.   What you want
is fully serialized creation which can be accomplished with advisory
lock or (better, imo) a leading

LOCK TABLE mytable;

Also, this is not a good pattern.  You ought to be using temp tables
or other mechanics to store transaction local data.

merlin


В списке pgsql-general по дате отправления:

Предыдущее
От: Moreno Andreo
Дата:
Сообщение: Re: How pg_dump works
Следующее
От: Periko Support
Дата:
Сообщение: Re: HA Cluster Solution?