gCoh
gCoh

Reputation: 3089

Postgres create table asynchronously

i'm struggling with postgres and async task queue

i'm trying to create a new table - whatever worker reach this point first - create the table

using the statment

create table if not exists

'table already exists' exception raised

this is really wierd, because when doing so whith single worker - i.e trying to create the table twice synchronously, the second time it writes a notice (not exception)

Upvotes: 1

Views: 1418

Answers (1)

Abelisto
Abelisto

Reputation: 15624

Not the answer, just for info how it would be reproduced, so be patient.

Open two terminals, say tty1 and tty2, open psql in each.

tty1:

nd@postgres=# begin;
BEGIN
*nd@postgres=# create table if not exists foo();
CREATE TABLE
*nd@postgres=# 

tty2:

nd@postgres=# begin;
BEGIN
*nd@postgres=# create table if not exists foo();

(waiting for lock)

tty1:

*nd@postgres=# commit;
COMMIT

tty2:

ERROR:  duplicate key value violates unique constraint "pg_type_typname_nsp_index"
DETAIL:  Key (typname, typnamespace)=(foo, 16386) already exists.
!nd@postgres=# 

Not sure that PostgreSQL should be more smart in such cases. IMO something wrong with the application logic...

Upvotes: 2

Related Questions