pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedINSERT locking order
2+ messages / 2 participants
[nested] [flat]
* INSERT locking order
@ 2020-01-09 20:50 Matthew Woodcraft <matthew@woodcraft.me.uk>
2020-01-09 21:27 ` Re: INSERT locking order Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 2+ messages in thread
From: Matthew Woodcraft @ 2020-01-09 20:50 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
In an INSERT, are the rows guaranteed to be inserted in the order that
the source query returns its rows, for locking purposes?
For example, if have
CREATE TABLE foo (
foo_id INTEGER,
PRIMARY KEY (foo_id)
);
and I run two concurrent copies of
INSERT INTO foo (SELECT n FROM ... ORDER BY n);
is there any guarantee that I'll get a unique constraint violation
rather than a deadlock?
-M-
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: INSERT locking order
2020-01-09 20:50 INSERT locking order Matthew Woodcraft <matthew@woodcraft.me.uk>
@ 2020-01-09 21:27 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 2+ messages in thread
From: Tom Lane @ 2020-01-09 21:27 UTC (permalink / raw)
To: Matthew Woodcraft <matthew@woodcraft.me.uk>; +Cc: pgsql-general@lists.postgresql.org
Matthew Woodcraft <matthew@woodcraft.me.uk> writes:
> In an INSERT, are the rows guaranteed to be inserted in the order that
> the source query returns its rows, for locking purposes?
I dunno that we "guarantee" that, but it's hard to see why the
code would behave any differently, at present.
> and I run two concurrent copies of
> INSERT INTO foo (SELECT n FROM ... ORDER BY n);
> is there any guarantee that I'll get a unique constraint violation
> rather than a deadlock?
Well, the issue there is not about the physical insertion order
but the order in which the uniqueness checks happen. I think
you'd be all right with a traditional-style PG index, but maybe
not with a deferrable unique constraint.
regards, tom lane
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2020-01-09 21:27 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-01-09 20:50 INSERT locking order Matthew Woodcraft <matthew@woodcraft.me.uk>
2020-01-09 21:27 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox