Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ggKs1-0003Ex-Ew for pgsql-bugs@arkaria.postgresql.org; Mon, 07 Jan 2019 02:42:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ggKrz-0003f9-4M for pgsql-bugs@arkaria.postgresql.org; Mon, 07 Jan 2019 02:42:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ggKrx-0003f1-ET for pgsql-bugs@lists.postgresql.org; Mon, 07 Jan 2019 02:42:26 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ggKrt-0006RY-1v for pgsql-bugs@postgresql.org; Mon, 07 Jan 2019 02:42:25 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 3B02222326; Sun, 6 Jan 2019 21:42:18 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Sun, 06 Jan 2019 21:42:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm3; bh=CUjm7QH9OyBDWtY0kRtyBo1Kqwb hHshME26z3Jxj380=; b=Hj5p3GBOOXV7ku82UdAO3dlOZXaMx6pKJ3gXKePaBWz 59DaLTPIKYBr/fQThX4iF/rhz4cZVZ1iPpCqr1z//ATT15tpubm7pHV/gbebax7Y sqJqRw1o13UUcxtncVArsu6qks12pJ00JGhrDn2qIcM1/t8Y2LPfUJ1xCPbOrpVd Bqk8EWs1O1omr5W8jVjmai7euOox9dMKRORLAyyMdpXJzo0nUCFjLl+CCjp1DIeE ggQHq7rQT0IPh9txrjI8FJgjNBEv2K6h7ZGoUq9AKAjDa7XqUW6usF9ise6jiYWI 8U96cRmAYRfF4+QMw7ofsEVK6uRZ07N0uOdMrDYHrfg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=CUjm7Q H9OyBDWtY0kRtyBo1KqwbhHshME26z3Jxj380=; b=JEcNgDK27xOn1Eserfdiog 4j2hXhWLIvaQ32iVAtMkN8tA+jJ6GlAxP4Mw83g0uzHlDYaaHKlYruMjv4dhzUv0 0YdISREc2uQeMfn632kj3rHZ2TZSU8WwMbHBeNiseSrpe7/ZB/pt2DSW+bSQ9Ta5 D0Rrid8W2T7Sg0W6tRkiM2xVBq7PoiVpN2DmaPX/BtCNRVTz1h/20LMj3v+cSXKb VjatPkhxGYBlw50lbJ7BeOgfrgSMmt8lzP5juNtIaED5JEk1zq48s0G9z0amWXDb rXmnndYzXSEQVJ1cCs0A8TxSd1HtLEEn3hPxth3KjCWkHe9HOdc9EtBNK8W+L+iA == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrvdeigdehudculddtuddrgedtkedrtddtmd cutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdljedtmdenucfjughrpeffhffvuffkfhggtggujgesghdtreertddt vdenucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlsehprg hquhhivghrrdighiiiqeenucfkphepiedurddvudehrddufeejrdduiedvnecurfgrrhgr mhepmhgrihhlfhhrohhmpehmihgthhgrvghlsehprghquhhivghrrdighiiinecuvehluh hsthgvrhfuihiivgeptd X-ME-Proxy: Received: from paquier.xyz (c137162.net61215.cablenet.ne.jp [61.215.137.162]) by mail.messagingengine.com (Postfix) with ESMTPA id 3265AE40A1; Sun, 6 Jan 2019 21:42:14 -0500 (EST) Date: Mon, 7 Jan 2019 11:42:11 +0900 From: Michael Paquier To: Tom Lane Cc: Alexey Bashtanov , pgsql-bugs@postgresql.org, mmitar@gmail.com Subject: Re: Is temporary functions feature official/supported? Found some issues with it. Message-ID: <20190107024211.GA22498@paquier.xyz> References: <5d910e2e-0db8-ec06-dd5f-baec420513c3@imap.cc> <22938.1546456949@sss.pgh.pa.us> <52794060-3339-e699-a8d5-e3dd7c81fa95@imap.cc> <10857.1546467488@sss.pgh.pa.us> <20190104131351.GF2067@paquier.xyz> <9176.1546613679@sss.pgh.pa.us> <20190105000037.GA1626@paquier.xyz> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="cmJC7u66zC7hs+87" Content-Disposition: inline In-Reply-To: <20190105000037.GA1626@paquier.xyz> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --cmJC7u66zC7hs+87 Content-Type: multipart/mixed; boundary="HlL+5n6rz5pIUxbD" Content-Disposition: inline --HlL+5n6rz5pIUxbD Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, Jan 05, 2019 at 09:00:37AM +0900, Michael Paquier wrote: > I think I can get that worked out with a back-patchable approach, > still it looks a bit sensitive because of the creation-pending logic > which relies on the assertion at the top of InitTempTableNamespace, > which is a case we may want to handle with an extra flag for the > caller of InitTempTableNamespace(), aka "fail if myTempNamespace is > valid instead of just returning back". Looking at this stuff, I have been able to come up with the attached, which introduces a new flag mode for MyXactFlags, which gets set in a couple of code paths so as PREPARE TRANSACTIOn complains: - When attempting a LOCK on a temporary relation. - When dropping objects part of a temporary namespace. - When trying to access the temporary schema of a session, where it may be initialized (or not if already present). I have added test cases for things I could come up with, particularly the LOCK and DROP cases which I have thought about after hacking the code. I think that something among those lines should be back-patched. Thoughts? -- Michael --HlL+5n6rz5pIUxbD Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="temp-object-2pc.patch" Content-Transfer-Encoding: quoted-printable diff --git a/doc/src/sgml/ref/prepare_transaction.sgml b/doc/src/sgml/ref/p= repare_transaction.sgml index d958f7a06f..5016ca287e 100644 --- a/doc/src/sgml/ref/prepare_transaction.sgml +++ b/doc/src/sgml/ref/prepare_transaction.sgml @@ -98,9 +98,9 @@ PREPARE TRANSACTION tran= saction_id =20 It is not currently allowed to PREPARE a transaction= that - has executed any operations involving temporary tables, - created any cursors WITH HOLD, or executed - LISTEN, UNLISTEN, or + has executed any operations involving temporary tables or the session's + temporary namespace, created any cursors WITH HOLD, = or + executed LISTEN, UNLISTEN, or NOTIFY. Those features are too tightly tied to the current session to be useful in a transaction to be prepare= d. diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam= /xact.c index f665e38ecf..55deff3a5a 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -2278,6 +2278,18 @@ PrepareTransaction(void) (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cannot PREPARE a transaction that has operated on temporary t= ables"))); =20 + /* + * Similarly, PREPARE TRANSACTION is not allowed if the temporary + * namespace has been involved in this transaction as we cannot allow + * it to create, lock, or even drop objects within it as this can + * mess up with this session or even a follow-up session trying to + * use the same temporary namespace. + */ + if ((MyXactFlags & XACT_FLAGS_ACCESSEDTEMPNAMESPACE)) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot PREPARE a transaction that has operated on temporary n= amespace"))); + /* * Likewise, don't allow PREPARE after pg_export_snapshot. This could be * supported if we added cleanup logic to twophase.c, but for now it diff --git a/src/backend/catalog/namespace.c b/src/backend/catalog/namespac= e.c index 5a6eb495d4..101b919066 100644 --- a/src/backend/catalog/namespace.c +++ b/src/backend/catalog/namespace.c @@ -192,6 +192,7 @@ char *namespace_search_path =3D NULL; =20 /* Local functions */ static void recomputeNamespacePath(void); +static void AccessTempTableNamespace(bool force); static void InitTempTableNamespace(void); static void RemoveTempRelations(Oid tempNamespaceId); static void RemoveTempRelationsCallback(int code, Datum arg); @@ -459,9 +460,8 @@ RangeVarGetCreationNamespace(const RangeVar *newRelatio= n) /* check for pg_temp alias */ if (strcmp(newRelation->schemaname, "pg_temp") =3D=3D 0) { - /* Initialize temp namespace if first time through */ - if (!OidIsValid(myTempNamespace)) - InitTempTableNamespace(); + /* Initialize temp namespace */ + AccessTempTableNamespace(false); return myTempNamespace; } /* use exact schema given */ @@ -470,9 +470,8 @@ RangeVarGetCreationNamespace(const RangeVar *newRelatio= n) } else if (newRelation->relpersistence =3D=3D RELPERSISTENCE_TEMP) { - /* Initialize temp namespace if first time through */ - if (!OidIsValid(myTempNamespace)) - InitTempTableNamespace(); + /* Initialize temp namespace */ + AccessTempTableNamespace(false); return myTempNamespace; } else @@ -482,7 +481,7 @@ RangeVarGetCreationNamespace(const RangeVar *newRelatio= n) if (activeTempCreationPending) { /* Need to initialize temp namespace */ - InitTempTableNamespace(); + AccessTempTableNamespace(true); return myTempNamespace; } namespaceId =3D activeCreationNamespace; @@ -2921,9 +2920,8 @@ LookupCreationNamespace(const char *nspname) /* check for pg_temp alias */ if (strcmp(nspname, "pg_temp") =3D=3D 0) { - /* Initialize temp namespace if first time through */ - if (!OidIsValid(myTempNamespace)) - InitTempTableNamespace(); + /* Initialize temp namespace */ + AccessTempTableNamespace(false); return myTempNamespace; } =20 @@ -2986,9 +2984,8 @@ QualifiedNameGetCreationNamespace(List *names, char *= *objname_p) /* check for pg_temp alias */ if (strcmp(schemaname, "pg_temp") =3D=3D 0) { - /* Initialize temp namespace if first time through */ - if (!OidIsValid(myTempNamespace)) - InitTempTableNamespace(); + /* Initialize temp namespace */ + AccessTempTableNamespace(false); return myTempNamespace; } /* use exact schema given */ @@ -3002,7 +2999,7 @@ QualifiedNameGetCreationNamespace(List *names, char *= *objname_p) if (activeTempCreationPending) { /* Need to initialize temp namespace */ - InitTempTableNamespace(); + AccessTempTableNamespace(true); return myTempNamespace; } namespaceId =3D activeCreationNamespace; @@ -3832,6 +3829,38 @@ recomputeNamespacePath(void) list_free(oidlist); } =20 +/* + * AccessTempTableNamespace + * Provide access to a temporary namespace, potentially creating it + * if not present yet. This routine registers if the namespace gets + * in use in this transaction. 'force' can be set to true to allow + * the caller to enforce the creation of the temporary namespace for + * use in this backend, which happens for one pending for creation. + */ +static void +AccessTempTableNamespace(bool force) +{ + /* + * Make note that this temporary namespace has been accessed in this + * transaction. + */ + MyXactFlags |=3D XACT_FLAGS_ACCESSEDTEMPNAMESPACE; + + /* + * If the caller attempting to access a temporary schema expects + * the creation of the namespace to be pending and should be enforced, + * then go through the creation. + */ + if (!force && OidIsValid(myTempNamespace)) + return; + + /* + * The temporary tablespace does not exist yet and it wanted, so + * initialize it. + */ + InitTempTableNamespace(); +} + /* * InitTempTableNamespace * Initialize temp table namespace on first use in a particular backend @@ -4275,7 +4304,7 @@ fetch_search_path(bool includeImplicit) */ if (activeTempCreationPending) { - InitTempTableNamespace(); + AccessTempTableNamespace(true); recomputeNamespacePath(); } =20 diff --git a/src/backend/commands/dropcmds.c b/src/backend/commands/dropcmd= s.c index 1dc1c746f1..8f7f7c232d 100644 --- a/src/backend/commands/dropcmds.c +++ b/src/backend/commands/dropcmds.c @@ -14,6 +14,7 @@ */ #include "postgres.h" =20 +#include "access/xact.h" #include "access/heapam.h" #include "access/htup_details.h" #include "catalog/dependency.h" @@ -107,6 +108,13 @@ RemoveObjects(DropStmt *stmt) check_object_ownership(GetUserId(), stmt->removeType, address, object, relation); =20 + /* + * Make note that this temporary namespace has been accessed in this + * transaction. + */ + if (OidIsValid(namespaceId) && isTempNamespace(namespaceId)) + MyXactFlags |=3D XACT_FLAGS_ACCESSEDTEMPNAMESPACE; + /* Release any relcache reference count, but keep lock until commit. */ if (relation) heap_close(relation, NoLock); diff --git a/src/backend/commands/lockcmds.c b/src/backend/commands/lockcmd= s.c index 1b33be1895..02e101ac1e 100644 --- a/src/backend/commands/lockcmds.c +++ b/src/backend/commands/lockcmds.c @@ -14,6 +14,7 @@ */ #include "postgres.h" =20 +#include "access/xact.h" #include "catalog/namespace.h" #include "catalog/pg_inherits.h" #include "commands/lockcmds.h" @@ -83,6 +84,7 @@ RangeVarCallbackForLockTable(const RangeVar *rv, Oid reli= d, Oid oldrelid, { LOCKMODE lockmode =3D *(LOCKMODE *) arg; char relkind; + char relpersistence; AclResult aclresult; =20 if (!OidIsValid(relid)) @@ -100,6 +102,11 @@ RangeVarCallbackForLockTable(const RangeVar *rv, Oid r= elid, Oid oldrelid, errmsg("\"%s\" is not a table or a view", rv->relname))); =20 + /* Make note that a temporary relation has been accessed */ + relpersistence =3D get_rel_persistence(relid); + if (relpersistence =3D=3D RELPERSISTENCE_TEMP) + MyXactFlags |=3D XACT_FLAGS_ACCESSEDTEMPREL; + /* Check permissions. */ aclresult =3D LockTableAclCheck(relid, lockmode, GetUserId()); if (aclresult !=3D ACLCHECK_OK) diff --git a/src/include/access/xact.h b/src/include/access/xact.h index 169cf2834c..ed21a13896 100644 --- a/src/include/access/xact.h +++ b/src/include/access/xact.h @@ -98,6 +98,11 @@ extern int MyXactFlags; */ #define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK (1U << 1) =20 +/* + * XACT_FLAGS_ACCESSEDTEMPNAMESPACE - set when a temporary namespace is + * accessed. We don't allow PREPARE TRANSACTION in that case. + */ +#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE (1U << 2) =20 /* * start- and end-of-transaction callbacks for dynamically loaded modules diff --git a/src/test/regress/expected/temp.out b/src/test/regress/expected= /temp.out index f018f17ca0..a8021dc2f0 100644 --- a/src/test/regress/expected/temp.out +++ b/src/test/regress/expected/temp.out @@ -301,3 +301,60 @@ select relname from pg_class where relname like 'temp_= inh_oncommit_test%'; (1 row) =20 drop table temp_inh_oncommit_test; +-- Tests with two-phase commit +-- Transactions creating objects in a temporary namespace cannot be used +-- with two-phase commit. +-- These cases generate errors about temporary namespace. +-- Function creation +begin; +create function pg_temp.twophase_func() returns void as + $$ select '2pc_func'::text $$ language sql; +prepare transaction 'twophase_func'; +ERROR: cannot PREPARE a transaction that has operated on temporary namesp= ace +-- Function drop +create function pg_temp.twophase_func() returns void as + $$ select '2pc_func'::text $$ language sql; +begin; +drop function pg_temp.twophase_func(); +prepare transaction 'twophase_func'; +ERROR: cannot PREPARE a transaction that has operated on temporary namesp= ace +-- Operator creation +begin; +create operator pg_temp.@@ (leftarg =3D int4, rightarg =3D int4, procedure= =3D int4mi); +prepare transaction 'twophase_operator'; +ERROR: cannot PREPARE a transaction that has operated on temporary namesp= ace +-- These generate errors about temporary tables. +begin; +create type pg_temp.twophase_type as (a int); +prepare transaction 'twophase_type'; +ERROR: cannot PREPARE a transaction that has operated on temporary tables +begin; +create view pg_temp.twophase_view as select 1; +prepare transaction 'twophase_view'; +ERROR: cannot PREPARE a transaction that has operated on temporary tables +begin; +create sequence pg_temp.twophase_seq; +prepare transaction 'twophase_sequence'; +ERROR: cannot PREPARE a transaction that has operated on temporary tables +-- Temporary tables cannot be used with two-phase commit. +create temp table twophase_tab (a int); +begin; +select a from twophase_tab; + a=20 +--- +(0 rows) + +prepare transaction 'twophase_tab'; +ERROR: cannot PREPARE a transaction that has operated on temporary tables +begin; +insert into twophase_tab values (1); +prepare transaction 'twophase_tab'; +ERROR: cannot PREPARE a transaction that has operated on temporary tables +begin; +lock twophase_tab in access exclusive mode; +prepare transaction 'twophase_tab'; +ERROR: cannot PREPARE a transaction that has operated on temporary tables +begin; +drop table twophase_tab; +prepare transaction 'twophase_tab'; +ERROR: cannot PREPARE a transaction that has operated on temporary tables diff --git a/src/test/regress/sql/temp.sql b/src/test/regress/sql/temp.sql index 1beccc6ceb..1cd48153ec 100644 --- a/src/test/regress/sql/temp.sql +++ b/src/test/regress/sql/temp.sql @@ -224,3 +224,50 @@ select * from temp_inh_oncommit_test; -- one relation remains select relname from pg_class where relname like 'temp_inh_oncommit_test%'; drop table temp_inh_oncommit_test; + +-- Tests with two-phase commit +-- Transactions creating objects in a temporary namespace cannot be used +-- with two-phase commit. + +-- These cases generate errors about temporary namespace. +-- Function creation +begin; +create function pg_temp.twophase_func() returns void as + $$ select '2pc_func'::text $$ language sql; +prepare transaction 'twophase_func'; +-- Function drop +create function pg_temp.twophase_func() returns void as + $$ select '2pc_func'::text $$ language sql; +begin; +drop function pg_temp.twophase_func(); +prepare transaction 'twophase_func'; +-- Operator creation +begin; +create operator pg_temp.@@ (leftarg =3D int4, rightarg =3D int4, procedure= =3D int4mi); +prepare transaction 'twophase_operator'; + +-- These generate errors about temporary tables. +begin; +create type pg_temp.twophase_type as (a int); +prepare transaction 'twophase_type'; +begin; +create view pg_temp.twophase_view as select 1; +prepare transaction 'twophase_view'; +begin; +create sequence pg_temp.twophase_seq; +prepare transaction 'twophase_sequence'; + +-- Temporary tables cannot be used with two-phase commit. +create temp table twophase_tab (a int); +begin; +select a from twophase_tab; +prepare transaction 'twophase_tab'; +begin; +insert into twophase_tab values (1); +prepare transaction 'twophase_tab'; +begin; +lock twophase_tab in access exclusive mode; +prepare transaction 'twophase_tab'; +begin; +drop table twophase_tab; +prepare transaction 'twophase_tab'; --HlL+5n6rz5pIUxbD-- --cmJC7u66zC7hs+87 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAlwyvIMACgkQnvQgOdby QH06IxAAmJyp7w/VMaPkN+ieCW1AC/rZzjtm2E75JbEaW/Pp2DXDWkRcdtLhU3eX VDSlMlW7GRLIUENSs1QA74o7rf90l4xaX6tV52uuVtid1xrCdbudMsaUI82EFzJS jzGLgUlUODE/STI++VrZq6WLcZT6tL03OcMg1gkcVd9Rogs4c3M2W7Ww9rJduwGf jj1u6/V2gt6vd/Vx+ieIHbhAgAzbUd+CaQn+oakVUXNmG+hrszAYXQOY5i12o7KL 7JtvEx8V0jACc6ePwLvXyJQD2bUVQTV0skI5ThAl/rCee6l6jWe5HZXwGjcK2AbN MYoCKc2U00Lx8yPMGGwelRQLOD6XeUugqJUmjmGPAsQ+NRL0ZKjIPg+cOxJbBHkE 1LsyDrMS4Gkyw6+Cl2OISblTQCVuvDShUTDSJGnZ16O2Q0AZSpYWmPOyTwm9AwKg hp4elw1deVdwHpBNKWOxihHsKXahc1Jf5tvqYl71QMM75bVo8SrNDPZKsD03/1D1 VDf7T/aCeirR9Glm811TaL5WaGCpO65oS+RNcx/v2AOKtiuERZxvvQLvrC4HQGQl EgquauV8K9ng7hzJ/LXBVIToAqQvuX4vpBMt4ZWPNVsyPSKmE8OBvZPuLq7h/SeR KaDo68UCBd0XifQk6fHXG0n+q/Kt+HUZyMsmQ9KmmlVWQPH1DVk= =ZkZ0 -----END PGP SIGNATURE----- --cmJC7u66zC7hs+87--