Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jiJJG-0005Q2-L4 for pgsql-general@arkaria.postgresql.org; Mon, 08 Jun 2020 15:03:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jiJJF-00040l-JC for pgsql-general@arkaria.postgresql.org; Mon, 08 Jun 2020 15:03:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jiJFr-0003BT-9A for pgsql-general@lists.postgresql.org; Mon, 08 Jun 2020 15:00:03 +0000 Received: from mail.am-soft.de ([83.218.36.120]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jiJFo-000831-Kr for pgsql-general@lists.postgresql.org; Mon, 08 Jun 2020 15:00:02 +0000 Received: from mail.am-soft.de (localhost [127.0.0.1]) by mail.am-soft.de (Postfix) with ESMTP id 955C129CDC88 for ; Mon, 8 Jun 2020 16:59:59 +0200 (CEST) Received: from tschoening-nb.fritz.box (unknown [77.78.178.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.am-soft.de (Postfix) with ESMTPSA id 74FDE29CDC86 for ; Mon, 8 Jun 2020 16:59:57 +0200 (CEST) Date: Mon, 8 Jun 2020 16:45:24 +0200 From: =?utf-8?Q?Thorsten_Sch=C3=B6ning?= Organization: AM-SoFT GmbH IT-Systeme Message-ID: <1577813443.20200608164524@am-soft.de> To: pgsql-general@lists.postgresql.org Subject: Re: Concurrenctly running CREATE TEMP TABLE IF NOT EXISTS [...] AS [...] In-Reply-To: <1102807465.20200608101409@am-soft.de> References: <1102807465.20200608101409@am-soft.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-AV-Checked: ClamAV using ClamSMTP List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Guten Tag Thorsten Sch=C3=B6ning, 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/2055163 https://github.com/pgjdbc/pgjdbc/blob/ecbc48ff6ad293a2555465d4f7fb6a33a403a= f26/pgjdbc/src/main/java/org/postgresql/core/v3/QueryExecutorImpl.java#L276 https://docs.oracle.com/javadb/10.8.3.0/devguide/cdevconcepts23499.html https://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=C3=BC=C3=9Fen, Thorsten Sch=C3=B6ning --=20 Thorsten Sch=C3=B6ning 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=C3=A4ftsf=C3=BChrer: Andreas Muchow