From: Thorsten Schöning <tschoening@am-soft.de>
To: pgsql-general@lists.postgresql.org
Subject: Re: Concurrenctly running CREATE TEMP TABLE IF NOT EXISTS [...] AS [...]
Date: Mon, 8 Jun 2020 16:45:24 +0200
Message-ID: <1577813443.20200608164524@am-soft.de> (raw)
In-Reply-To: <1102807465.20200608101409@am-soft.de>
References: <1102807465.20200608101409@am-soft.de>
Guten Tag Thorsten Schöning,
am Montag, 8. Juni 2020 um 10:14 schrieben Sie:
> When the table needs to be created, when is it visible to other
> threads using the same transaction, before or after executing the
> additional query?
There is a misconception here: Multiple concurrent exec doesn't seem
to be implemented at all, even though the JDBC-docs seem to allow the
sharing of connections. Execution of statements seem to lock the
underlying connection, preventing truly concurrent access by multiple
threads.
https://stackoverflow.com/a/52798543/2055163https://github.com/pgjdbc/pgjdbc/blob/ecbc48ff6ad293a2555465d4f7fb6a33a403af26/pgjdbc/src/main/java/...https://docs.oracle.com/javadb/10.8.3.0/devguide/cdevconcepts23499.htmlhttps://docs.oracle.com/javadb/10.8.3.0/devguide/cdevconcepts89498.html
So while things don't work as I originally expected, this makes
answering my question easy: Because "CREATE TEMP TABLE IF NOT EXISTS
[...] AS [...]" is ONE statement only forwarded to the driver, its
execution should be atomic with respect to other threads using the
same connection.
It shouldn't make any difference how long the calculation of the
associated query takes, because all other threads need to wait for the
whole statement anyway. And if some other thread comes with the query
subsequently, "IF NOT EXISTS" comes into play and the query should
succeed instantly.
> Am I correct that with using "IF NOT EXISTS" the associated query is
> only executed as well if the table needs to be created?[...]
An answer to that question would still be of interest to me.
> Am I correct that in my described setup I need to make sure on my own
> that only one thread creates each individual temporary table and
> executes the associated query?[...]
No, not unless truly concurrent access is available in the
JDBC-driver. If it would be OTOH, I guess the answer would be yes.
> Am I correct that because of the same transaction used by multiple
> threads I need to synchronize them on web service-level?[...]
Yes and this should be the most performant implementation anyway. In
case of really one and the same connection, everything is handled by
the same JVM already, so can be synchronized within that JVM without
roundtrips to Postgres as well.
[1]: https://stackoverflow.com/a/24089729/2055163
Mit freundlichen Grüßen,
Thorsten Schöning
--
Thorsten Schöning E-Mail: Thorsten.Schoening@AM-SoFT.de
AM-SoFT IT-Systeme http://www.AM-SoFT.de/
Telefon...........05151- 9468- 55
Fax...............05151- 9468- 88
Mobil..............0178-8 9468- 04
AM-SoFT GmbH IT-Systeme, Brandenburger Str. 7c, 31789 Hameln
AG Hannover HRB 207 694 - Geschäftsführer: Andreas Muchow
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: tschoening@am-soft.de
Subject: Re: Concurrenctly running CREATE TEMP TABLE IF NOT EXISTS [...] AS [...]
In-Reply-To: <1577813443.20200608164524@am-soft.de>
References: <1102807465.20200608101409@am-soft.de> <1577813443.20200608164524@am-soft.de>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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