pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedIs temporary functions feature official/supported? Found some issues with it.
22+ messages / 5 participants
[nested] [flat]
Commits from this thread:
2019-01-18 c5660e0aa5 3e4fdb3bc0 b15160bc71 Restrict the use of temporary namespace in two-phase transactions
* Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-02 17:33 Alexey Bashtanov <bashtanov@imap.cc>
0 siblings, 1 reply; 22+ messages in thread
From: Alexey Bashtanov @ 2019-01-02 17:33 UTC (permalink / raw)
To: pgsql-bugs; +Cc: mmitar@gmail.com
Hello,
Is creating functions in pg_temp schema something we support?
It's undocumented as far as I could see, but maybe I missed it.
People seem to use it.
I found that a combination of temporary functions and prepared
transactions can lead to:
1) all other sessions being unable to create a temporary table until the
prepared transaction is finished (reproducible);
2) data corruption in pg_namespace, server crash (happened a few times,
but I'm not yet sure how to reproduce).
Is it something worth reporting in more detail?
Best,
Alex
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-02 19:22 Tom Lane <tgl@sss.pgh.pa.us>
parent: Alexey Bashtanov <bashtanov@imap.cc>
0 siblings, 1 reply; 22+ messages in thread
From: Tom Lane @ 2019-01-02 19:22 UTC (permalink / raw)
To: Alexey Bashtanov <bashtanov@imap.cc>; +Cc: pgsql-bugs; mmitar@gmail.com
Alexey Bashtanov <bashtanov@imap.cc> writes:
> Is creating functions in pg_temp schema something we support?
Yeah.
> I found that a combination of temporary functions and prepared
> transactions can lead to:
> 1) all other sessions being unable to create a temporary table until the
> prepared transaction is finished (reproducible);
Locking issue perhaps? It's not a bug if a prepared transaction
is holding a lock.
> 2) data corruption in pg_namespace, server crash (happened a few times,
> but I'm not yet sure how to reproduce).
That would be interesting.
regards, tom lane
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-02 21:55 Alexey Bashtanov <bashtanov@imap.cc>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 22+ messages in thread
From: Alexey Bashtanov @ 2019-01-02 21:55 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs; mmitar@gmail.com
>> 1) all other sessions being unable to create a temporary table until the
>> prepared transaction is finished (reproducible);
> Locking issue perhaps?
For sure.
session1:
l=> begin;
BEGIN
l=> create function pg_temp.foo() returns void as $$ begin end; $$
language plpgsql;
CREATE FUNCTION
l=> prepare transaction 'z';
PREPARE TRANSACTION
l=> \q
session2:
l=> create temp table t();
^CCancel request sent
... blabla when inserting (0,15) into "pg_namespace_nspname_index" ...
(sorry, server was running in non-english locale)
reprocuced in master and in 10.5
> It's not a bug if a prepared transaction
> is holding a lock.
The weird thing is the lock comes on namespace level locking against
creating any temporary object.
It isn't the case for without prepared transactions, neither can it be
achieved with CREATE TEMP TABLE only with no functions involved.
>> 2) data corruption in pg_namespace, server crash (happened a few times,
>> but I'm not yet sure how to reproduce).
> That would be interesting.
I cannot reproduce it on 10.5.
Will try on master later and get back to you if I have any luck.
Best,
Alex
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-02 22:18 Tom Lane <tgl@sss.pgh.pa.us>
parent: Alexey Bashtanov <bashtanov@imap.cc>
0 siblings, 3 replies; 22+ messages in thread
From: Tom Lane @ 2019-01-02 22:18 UTC (permalink / raw)
To: Alexey Bashtanov <bashtanov@imap.cc>; +Cc: pgsql-bugs; mmitar@gmail.com
Alexey Bashtanov <bashtanov@imap.cc> writes:
> session1:
> l=> begin;
> BEGIN
> l=> create function pg_temp.foo() returns void as $$ begin end; $$
> language plpgsql;
> CREATE FUNCTION
> l=> prepare transaction 'z';
> PREPARE TRANSACTION
> l=> \q
> session2:
> l=> create temp table t();
> ^CCancel request sent
> ... blabla when inserting (0,15) into "pg_namespace_nspname_index" ...
> (sorry, server was running in non-english locale)
Hm. I can reproduce this if I start in a virgin database, but not
otherwise. I think the problem is that the prepared transaction has
created the pg_temp_NN schema for its session, and therefore there
is an uncommitted pg_namespace entry for that schema name, and the
second session is also trying to create that schema (because it has
the same backend ID) so it blocks waiting to see if that index
entry commits.
Another problem is that if the pg_temp_NN schema did already exist
the original session gets hung up on exit, trying to delete the
uncommitted function from its temp schema. (This prevents other
sessions from acquiring the same backend ID, thus masking the
problem from casual inspection.)
None of this is specific to functions, it'd happen for any temp
object.
Maybe we ought to forbid prepared transactions from creating (or
deleting?) any temp objects. I seem to remember that we already
made some restrictions of that sort, but they clearly weren't
sufficient to prevent all problems.
Or we could just say "if it hurts, don't do that". The whole thing
is only a problem if you leave prepared transactions sitting around
for a long time, and that's already a bad idea.
> It isn't the case for without prepared transactions, neither can it be
> achieved with CREATE TEMP TABLE only with no functions involved.
I think your experiments likely didn't account for the different
behavior depending on whether pg_temp_NN exists yet. There's no
reason this would be special to functions.
regards, tom lane
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-02 22:54 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
2 siblings, 0 replies; 22+ messages in thread
From: Alvaro Herrera @ 2019-01-02 22:54 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs@postgresql.org, mmitar@gmail.com, Dimitri Fontaine <dimitri@citusdata.com>
On 2019-Jan-02, Tom Lane wrote:
> Maybe we ought to forbid prepared transactions from creating (or
> deleting?) any temp objects. I seem to remember that we already
> made some restrictions of that sort, but they clearly weren't
> sufficient to prevent all problems.
We make that check at transaction prepare time, but obviously there's no
way to do it any earlier since we don't know ahead of time whether the
transaction is going to do the normal commit/abort or become prepared.
Moreover, Dimitri has recently posted a patch[1] allowing prepared
transactions to commit if their temp tables are ON COMMIT DROP, which
conflicts with this approach. This seems problematic if the pg_temp_NN
entry is reused.
Maybe we should make temp namespace names more unique if we want to
add extra features :-(
[1] https://postgr.es/m/m2d0pllvqy.fsf@dimitris-macbook-pro.home
--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-04 13:13 Alexey Bashtanov <bashtanov@imap.cc>
parent: Tom Lane <tgl@sss.pgh.pa.us>
2 siblings, 0 replies; 22+ messages in thread
From: Alexey Bashtanov @ 2019-01-04 13:13 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs; alvherre@2ndquadrant.com; dimitri@citusdata.com
> Hm. I can reproduce this if I start in a virgin database, but not
> otherwise.
for a database with a history of temporary namespaces we just need more
parallel prepared transactions
> There's no reason this would be special to functions.
temp functions are allowed in prepared transactions, unlike tables/views
-- that's the reason I thought about functions
Unfortunately I failed to reproduce a crash or a data corruption.
The most interesting I could see was the following:
--------example 1-------
-- session 1:
begin;
create function pg_temp.foo() returns void as $$begin end;$$ language
plpgsql;
prepare transaction 'z';
create function pg_temp.foo() returns void as $$begin end;$$ language
plpgsql;
-- session 2:
commit prepared 'z';
-- session 1 prints:
-- ERROR: duplicate key value violates unique constraint
"pg_proc_proname_args_nsp_index"
-- DETAIL: Key (proname, proargtypes, pronamespace)=(foo, , 16385)
already exists.
--------example 2-------
-- session 1:
begin;
create function pg_temp.foo() returns void as $$begin end;$$ language
plpgsql;
-- session 2:
begin;
create function pg_temp.foo() returns void as $$begin end;$$ language
plpgsql;
-- session 1:
prepare transaction 'z';
\q
-- session 2:
prepare transaction 'y';
\q
-- session 3:
begin;
create temp table t();
-- session 4:
commit prepared 'z';
commit prepared 'y';
-- session 3 prints:
-- ERROR: duplicate key value violates unique constraint
"pg_namespace_nspname_index"
-- LINE 1: create temp table t();
-- ^
-- DETAIL: Key (nspname)=(pg_temp_3) already exists.
Best,
Alex
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-04 13:13 Michael Paquier <michael@paquier.xyz>
parent: Tom Lane <tgl@sss.pgh.pa.us>
2 siblings, 2 replies; 22+ messages in thread
From: Michael Paquier @ 2019-01-04 13:13 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; mmitar@gmail.com
On Wed, Jan 02, 2019 at 05:18:08PM -0500, Tom Lane wrote:
> Hm. I can reproduce this if I start in a virgin database, but not
> otherwise. I think the problem is that the prepared transaction has
> created the pg_temp_NN schema for its session, and therefore there
> is an uncommitted pg_namespace entry for that schema name, and the
> second session is also trying to create that schema (because it has
> the same backend ID) so it blocks waiting to see if that index
> entry commits.
That should not be allowed to commit directly. I think that we should
just add a new value for MyXactFlags which tracks the transaction
where the temporary namespace has been created, and generate an error
if trying to use 2PC in this case. A separate flag looks necessary to
me so as we don't bump on "cannot PREPARE a transaction that has
operated on temporary tables" in this case. That would be
back-patchable, and if both XACT_FLAGS_ACCESSEDTEMPREL and the new
flag are set then we just blow up on the original error message when
committing.
Opinions?
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-04 14:54 Tom Lane <tgl@sss.pgh.pa.us>
parent: Michael Paquier <michael@paquier.xyz>
1 sibling, 1 reply; 22+ messages in thread
From: Tom Lane @ 2019-01-04 14:54 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; mmitar@gmail.com
Michael Paquier <michael@paquier.xyz> writes:
> On Wed, Jan 02, 2019 at 05:18:08PM -0500, Tom Lane wrote:
>> Hm. I can reproduce this if I start in a virgin database, but not
>> otherwise. I think the problem is that the prepared transaction has
>> created the pg_temp_NN schema for its session, and therefore there
>> is an uncommitted pg_namespace entry for that schema name, and the
>> second session is also trying to create that schema (because it has
>> the same backend ID) so it blocks waiting to see if that index
>> entry commits.
> That should not be allowed to commit directly. I think that we should
> just add a new value for MyXactFlags which tracks the transaction
> where the temporary namespace has been created, and generate an error
> if trying to use 2PC in this case. A separate flag looks necessary to
> me so as we don't bump on "cannot PREPARE a transaction that has
> operated on temporary tables" in this case.
Hm, I had forgotten that we had such an error message. Really that
restriction needs to apply to any object in the temp namespace, not only
tables.
regards, tom lane
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-04 15:43 Alvaro Herrera <alvherre@2ndquadrant.com>
parent: Michael Paquier <michael@paquier.xyz>
1 sibling, 1 reply; 22+ messages in thread
From: Alvaro Herrera @ 2019-01-04 15:43 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; mmitar@gmail.com
On 2019-Jan-04, Michael Paquier wrote:
> That should not be allowed to commit directly. I think that we should
> just add a new value for MyXactFlags which tracks the transaction
> where the temporary namespace has been created, and generate an error
> if trying to use 2PC in this case.
That implies that a 2PC transaction will fail if it's run in a session
for which the temp namespace doesn't previously exist. I think it's a
fairly ugly failure mode, and one that normal testing will not catch
because it'll occur very rarely. An app that detects this problem at
run time will have to create a random temp object, commit normally, then
re-run the 2PC transaction from the start ... a lot of code to deal with
something that shouldn't happen in the first place.
I wonder if we can somehow create the temp schema in a way that makes it
immediately visible to everyone, and not depend on the commit status of
the creating transaction -- say mark the tuple with xmin=frozenXid or
something like ugly that.
--
Álvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-04 16:19 Tom Lane <tgl@sss.pgh.pa.us>
parent: Alvaro Herrera <alvherre@2ndquadrant.com>
0 siblings, 0 replies; 22+ messages in thread
From: Tom Lane @ 2019-01-04 16:19 UTC (permalink / raw)
To: Alvaro Herrera <alvherre@2ndquadrant.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; mmitar@gmail.com
Alvaro Herrera <alvherre@2ndquadrant.com> writes:
> On 2019-Jan-04, Michael Paquier wrote:
>> That should not be allowed to commit directly. I think that we should
>> just add a new value for MyXactFlags which tracks the transaction
>> where the temporary namespace has been created, and generate an error
>> if trying to use 2PC in this case.
> That implies that a 2PC transaction will fail if it's run in a session
> for which the temp namespace doesn't previously exist. I think it's a
> fairly ugly failure mode, and one that normal testing will not catch
> because it'll occur very rarely. An app that detects this problem at
> run time will have to create a random temp object, commit normally, then
> re-run the 2PC transaction from the start ... a lot of code to deal with
> something that shouldn't happen in the first place.
> I wonder if we can somehow create the temp schema in a way that makes it
> immediately visible to everyone, and not depend on the commit status of
> the creating transaction -- say mark the tuple with xmin=frozenXid or
> something like ugly that.
That's not sufficient to solve the problem, because there are really
two issues here. Even if the temp schema already exists, we cannot
allow a 2PC transaction to create/drop/lock objects in it, because
that will mess things up for the surrounding session, or the next
session to use the same temp schema: trying to clear out the schema
will either fail or block.
regards, tom lane
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-05 00:00 Michael Paquier <michael@paquier.xyz>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 22+ messages in thread
From: Michael Paquier @ 2019-01-05 00:00 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; mmitar@gmail.com
On Fri, Jan 04, 2019 at 09:54:39AM -0500, Tom Lane wrote:
> Hm, I had forgotten that we had such an error message. Really that
> restriction needs to apply to any object in the temp namespace, not only
> tables.
The cleanest way I can think we need to set up the flag each time
InitTempTableNamespace() *could* be called. If myTempNamespace is
valid, the routine may however not be called. So an idea is to return
immediately from InitTempTableNamespace() if myTempNamespace is valid
instead, which would mean removing its assertion at the top. Using a
wrapper on top of InitTempTableNamespace() is also possible but I
think that we should design things so as only one code path sets
MyXactFlags, and one good thing is that InitTempTableNamespace() is
the only code path setting myTempNamespace.
For the tests, it is possible to use "\c -" to enforce a reconnection
which would be able to discard the previous temp namespace and allow
reproducibility, however as this problem applies to any objects which
can be schema-qualified this is not necessary for all the tests.
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".
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-07 02:42 Michael Paquier <michael@paquier.xyz>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 22+ messages in thread
From: Michael Paquier @ 2019-01-07 02:42 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; mmitar@gmail.com
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
Attachments:
temp-object-2pc.patch (text/x-diff, 12.8K) download
diff --git a/doc/src/sgml/ref/prepare_transaction.sgml b/doc/src/sgml/ref/prepare_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 <replaceable class="parameter">transaction_id</replaceable>
<para>
It is not currently allowed to <command>PREPARE</command> a transaction that
- has executed any operations involving temporary tables,
- created any cursors <literal>WITH HOLD</literal>, or executed
- <command>LISTEN</command>, <command>UNLISTEN</command>, or
+ has executed any operations involving temporary tables or the session's
+ temporary namespace, created any cursors <literal>WITH HOLD</literal>, or
+ executed <command>LISTEN</command>, <command>UNLISTEN</command>, or
<command>NOTIFY</command>.
Those features are too tightly
tied to the current session to be useful in a transaction to be prepared.
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 tables")));
+ /*
+ * 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 namespace")));
+
/*
* 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/namespace.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 = NULL;
/* 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 *newRelation)
/* check for pg_temp alias */
if (strcmp(newRelation->schemaname, "pg_temp") == 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 *newRelation)
}
else if (newRelation->relpersistence == 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 *newRelation)
if (activeTempCreationPending)
{
/* Need to initialize temp namespace */
- InitTempTableNamespace();
+ AccessTempTableNamespace(true);
return myTempNamespace;
}
namespaceId = activeCreationNamespace;
@@ -2921,9 +2920,8 @@ LookupCreationNamespace(const char *nspname)
/* check for pg_temp alias */
if (strcmp(nspname, "pg_temp") == 0)
{
- /* Initialize temp namespace if first time through */
- if (!OidIsValid(myTempNamespace))
- InitTempTableNamespace();
+ /* Initialize temp namespace */
+ AccessTempTableNamespace(false);
return myTempNamespace;
}
@@ -2986,9 +2984,8 @@ QualifiedNameGetCreationNamespace(List *names, char **objname_p)
/* check for pg_temp alias */
if (strcmp(schemaname, "pg_temp") == 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 = activeCreationNamespace;
@@ -3832,6 +3829,38 @@ recomputeNamespacePath(void)
list_free(oidlist);
}
+/*
+ * 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 |= 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();
}
diff --git a/src/backend/commands/dropcmds.c b/src/backend/commands/dropcmds.c
index 1dc1c746f1..8f7f7c232d 100644
--- a/src/backend/commands/dropcmds.c
+++ b/src/backend/commands/dropcmds.c
@@ -14,6 +14,7 @@
*/
#include "postgres.h"
+#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);
+ /*
+ * Make note that this temporary namespace has been accessed in this
+ * transaction.
+ */
+ if (OidIsValid(namespaceId) && isTempNamespace(namespaceId))
+ MyXactFlags |= 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/lockcmds.c
index 1b33be1895..02e101ac1e 100644
--- a/src/backend/commands/lockcmds.c
+++ b/src/backend/commands/lockcmds.c
@@ -14,6 +14,7 @@
*/
#include "postgres.h"
+#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 relid, Oid oldrelid,
{
LOCKMODE lockmode = *(LOCKMODE *) arg;
char relkind;
+ char relpersistence;
AclResult aclresult;
if (!OidIsValid(relid))
@@ -100,6 +102,11 @@ RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid,
errmsg("\"%s\" is not a table or a view",
rv->relname)));
+ /* Make note that a temporary relation has been accessed */
+ relpersistence = get_rel_persistence(relid);
+ if (relpersistence == RELPERSISTENCE_TEMP)
+ MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPREL;
+
/* Check permissions. */
aclresult = LockTableAclCheck(relid, lockmode, GetUserId());
if (aclresult != 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)
+/*
+ * 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)
/*
* 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)
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 namespace
+-- 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 namespace
+-- Operator creation
+begin;
+create operator pg_temp.@@ (leftarg = int4, rightarg = int4, procedure = int4mi);
+prepare transaction 'twophase_operator';
+ERROR: cannot PREPARE a transaction that has operated on temporary namespace
+-- 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
+---
+(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 = int4, rightarg = int4, procedure = 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';
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-09 07:00 Masahiko Sawada <sawada.mshk@gmail.com>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 22+ messages in thread
From: Masahiko Sawada @ 2019-01-09 07:00 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; mmitar@gmail.com
On Mon, Jan 7, 2019 at 11:42 AM Michael Paquier <michael@paquier.xyz> wrote:
>
> 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).
>
Thank you for the patch. I've looked at the patch and have one question.
/*
* XACT_FLAGS_ACCESSEDTEMPREL - set when a temporary relation is accessed. We
* don't allow PREPARE TRANSACTION in that case.
*/
#define XACT_FLAGS_ACCESSEDTEMPREL (1U << 0)
(snip)
+/*
+ * 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)
The cases where we set XACT_FLAGS_ACCESSEDTEMPNAMESPACE seems to
include the cases setting XACT_FLAGS_ACCESSEDTEMPREL. Is there any
path where we access a temporary relation without accessing temprary
namespace?
Regards,
--
Masahiko Sawada
NIPPON TELEGRAPH AND TELEPHONE CORPORATION
NTT Open Source Software Center
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-09 07:30 Michael Paquier <michael@paquier.xyz>
parent: Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 1 reply; 22+ messages in thread
From: Michael Paquier @ 2019-01-09 07:30 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; mmitar@gmail.com
On Wed, Jan 09, 2019 at 04:00:51PM +0900, Masahiko Sawada wrote:
> /*
> * XACT_FLAGS_ACCESSEDTEMPREL - set when a temporary relation is accessed. We
> * don't allow PREPARE TRANSACTION in that case.
> */
> #define XACT_FLAGS_ACCESSEDTEMPREL (1U << 0)
> (snip)
> +/*
> + * 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)
>
> The cases where we set XACT_FLAGS_ACCESSEDTEMPNAMESPACE seems to
> include the cases setting XACT_FLAGS_ACCESSEDTEMPREL. Is there any
> path where we access a temporary relation without accessing temporary
> namespace?
This case would set ACCESSEDTEMPREL but not ACCESSEDTEMPNAMESPACE:
create temp table twophase_tab (a int);
begin;
select a from twophase_tab;
prepare transaction 'twophase_tab';
I kept the original flag mainly for compatibility with the past
handling so as the error message remains constant and back-patchable
for application relying on the existing behavior. I think that for
HEAD we could consider moving on with an approach where we have only
ACCESSEDTEMPNAMESPACE, still we may want to make a difference for
transactions which have actually tried to take any kind of locks on
the temporary relation.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-10 02:47 Masahiko Sawada <sawada.mshk@gmail.com>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 22+ messages in thread
From: Masahiko Sawada @ 2019-01-10 02:47 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; mmitar@gmail.com
On Wed, Jan 9, 2019 at 4:30 PM Michael Paquier <michael@paquier.xyz> wrote:
>
> On Wed, Jan 09, 2019 at 04:00:51PM +0900, Masahiko Sawada wrote:
> > /*
> > * XACT_FLAGS_ACCESSEDTEMPREL - set when a temporary relation is accessed. We
> > * don't allow PREPARE TRANSACTION in that case.
> > */
> > #define XACT_FLAGS_ACCESSEDTEMPREL (1U << 0)
> > (snip)
> > +/*
> > + * 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)
> >
> > The cases where we set XACT_FLAGS_ACCESSEDTEMPNAMESPACE seems to
> > include the cases setting XACT_FLAGS_ACCESSEDTEMPREL. Is there any
> > path where we access a temporary relation without accessing temporary
> > namespace?
>
> This case would set ACCESSEDTEMPREL but not ACCESSEDTEMPNAMESPACE:
> create temp table twophase_tab (a int);
> begin;
> select a from twophase_tab;
> prepare transaction 'twophase_tab';
Thank you.
>
> I kept the original flag mainly for compatibility with the past
> handling so as the error message remains constant and back-patchable
> for application relying on the existing behavior.
Understood. Sounds good.
> I think that for
> HEAD we could consider moving on with an approach where we have only
> ACCESSEDTEMPNAMESPACE, still we may want to make a difference for
> transactions which have actually tried to take any kind of locks on
> the temporary relation.
Yes, I was confused that in spite of the relation is a kind of
database object we separate two cases, relation and database objects
other than relation. So I thought that we can merge these flags to
something like XACT_FLAGS_ACCESSEDTEMPOBJ tracking whether the
transaction created, locked or dropped objects such as tables,
functions and datatypes on a temoprary namespace.
I've found a corner-case issue with this patch; this issue still
happens when we create the extension on the temprary namespace,
because it can be done without accessing the namespace if the
temporary namespace is already created.
postgres(1:59232)=# create table pg_temp.e(c int);
CREATE TABLE
postgres(1:59232)=# begin;
BEGIN
postgres(1:59232)=# create extension pgcrypto schema pg_temp_3;
CREATE EXTENSION
postgres(1:59232)=# prepare transaction 'a';
PREPARE TRANSACTION
postgres(1:59232)=# create extension pgcrypto;
(hang..)
To fix we could have get_namepace_oid set
XACT_FLAGS_ACCESSEDTEMPNAMESPACE (not calling
InitTempTableNamespace()) if the nspname is pg_temp_NNN but it could
influence many places.
Also, since even the current_schame() and the current_scheames() could
create the temporary namespace when executing fetch_search_path()
while 'pg_temp' appears the head of seach_patch, for example, the
following transaction cannot prepre. But it possible coulld be
acceptable by users as this is very corner case and it could be
regards as accessing the temporary namespace actually.
postgres(1:59025)=# set search_path to pg_temp, "$user", public;
SET
postgres(1:59025)=# begin;
BEGIN
postgres(1:59025)=# select current_schema;
current_schema
----------------
pg_temp_3
(1 row)
postgres(1:59025)=# prepare transaction 'a';
ERROR: cannot PREPARE a transaction that has operated on temporary namespace
Regards,
--
Masahiko Sawada
NIPPON TELEGRAPH AND TELEPHONE CORPORATION
NTT Open Source Software Center
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-10 03:45 Michael Paquier <michael@paquier.xyz>
parent: Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 1 reply; 22+ messages in thread
From: Michael Paquier @ 2019-01-10 03:45 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; mmitar@gmail.com
On Thu, Jan 10, 2019 at 11:47:32AM +0900, Masahiko Sawada wrote:
> Yes, I was confused that in spite of the relation is a kind of
> database object we separate two cases, relation and database objects
> other than relation. So I thought that we can merge these flags to
> something like XACT_FLAGS_ACCESSEDTEMPOBJ tracking whether the
> transaction created, locked or dropped objects such as tables,
> functions and datatypes on a temoprary namespace.
Sure. For HEAD I don't mind doing so and that makes sense. On
back-branches though that's another story...
> I've found a corner-case issue with this patch; this issue still
> happens when we create the extension on the temprary namespace,
> because it can be done without accessing the namespace if the
> temporary namespace is already created.
>
> postgres(1:59232)=# create table pg_temp.e(c int);
> CREATE TABLE
> postgres(1:59232)=# begin;
> BEGIN
> postgres(1:59232)=# create extension pgcrypto schema pg_temp_3;
> CREATE EXTENSION
> postgres(1:59232)=# prepare transaction 'a';
> PREPARE TRANSACTION
> postgres(1:59232)=# create extension pgcrypto;
> (hang..)
There is a reason why I have split that part into a separate patch and
a separate thread:
https://commitfest.postgresql.org/22/1969/
The thing is that this behavior is rather unstable, for example I bump
easily into that:
=# create extension dblink schema pg_temp_3;
ERROR: 42883: function dblink_connect_u(text) does not exist
> Also, since even the current_schame() and the current_schemas() could
> create the temporary namespace when executing fetch_search_path()
> while 'pg_temp' appears the head of seach_patch, for example, the
> following transaction cannot prepre. But it possible could be
> acceptable by users as this is very corner case and it could be
> regards as accessing the temporary namespace actually.
Yeah, but the temporary schema gets created, which is something that
we want to avoid, so 2PC must fail on that case as well or you would
run again into the same locking hazards if the same temp schema gets
reused in a different backend.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-10 11:44 Masahiko Sawada <sawada.mshk@gmail.com>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 22+ messages in thread
From: Masahiko Sawada @ 2019-01-10 11:44 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; mmitar@gmail.com
On Thu, Jan 10, 2019 at 12:45 PM Michael Paquier <michael@paquier.xyz> wrote:
>
> On Thu, Jan 10, 2019 at 11:47:32AM +0900, Masahiko Sawada wrote:
> > Yes, I was confused that in spite of the relation is a kind of
> > database object we separate two cases, relation and database objects
> > other than relation. So I thought that we can merge these flags to
> > something like XACT_FLAGS_ACCESSEDTEMPOBJ tracking whether the
> > transaction created, locked or dropped objects such as tables,
> > functions and datatypes on a temoprary namespace.
>
> Sure. For HEAD I don't mind doing so and that makes sense. On
> back-branches though that's another story...
Agreed.
>
> > I've found a corner-case issue with this patch; this issue still
> > happens when we create the extension on the temprary namespace,
> > because it can be done without accessing the namespace if the
> > temporary namespace is already created.
> >
> > postgres(1:59232)=# create table pg_temp.e(c int);
> > CREATE TABLE
> > postgres(1:59232)=# begin;
> > BEGIN
> > postgres(1:59232)=# create extension pgcrypto schema pg_temp_3;
> > CREATE EXTENSION
> > postgres(1:59232)=# prepare transaction 'a';
> > PREPARE TRANSACTION
> > postgres(1:59232)=# create extension pgcrypto;
> > (hang..)
>
> There is a reason why I have split that part into a separate patch and
> a separate thread:
> https://commitfest.postgresql.org/22/1969/
>
> The thing is that this behavior is rather unstable, for example I bump
> easily into that:
> =# create extension dblink schema pg_temp_3;
> ERROR: 42883: function dblink_connect_u(text) does not exist
Yes, IIUC this issue happen only when creating the extension that
doesn't access the function after created it. For example, dblink does
REVOKE for dblink_connect_u() after creation.
Regards,
--
Masahiko Sawada
NIPPON TELEGRAPH AND TELEPHONE CORPORATION
NTT Open Source Software Center
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-14 00:45 Michael Paquier <michael@paquier.xyz>
parent: Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 1 reply; 22+ messages in thread
From: Michael Paquier @ 2019-01-14 00:45 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; mmitar@gmail.com
On Thu, Jan 10, 2019 at 08:44:13PM +0900, Masahiko Sawada wrote:
> Yes, IIUC this issue happen only when creating the extension that
> doesn't access the function after created it. For example, dblink does
> REVOKE for dblink_connect_u() after creation.
I am not sure if Robert is a fan of simply forbiding that as per the
question here:
https://www.postgresql.org/message-id/CA+TgmoZJdGcGFm+coHHCbBM3CfPqpjisdiux4Pa9QA3dFFQasw@mail.gmail...
Anyway, we could take for now the separate approach to prevent the
case of CREATE EXTENSION with 2PC if trying to use a temporary
schema as more holes are closed with this stuff. Attached is an
updated patch to do so. This adds a regression test, which would fail
if we decide to prevent the behavior afterwards. This uses two tricks
to avoid CONTEXT and NOTICE messages which include the temporary
session name to keep the test stable:
\set SHOW_CONTEXT never
SET client_min_messages TO 'warning';
Thoughts?
--
Michael
Attachments:
temp-object-2pc-v2.patch (text/x-diff, 16.3K) download
diff --git a/doc/src/sgml/ref/prepare_transaction.sgml b/doc/src/sgml/ref/prepare_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 <replaceable class="parameter">transaction_id</replaceable>
<para>
It is not currently allowed to <command>PREPARE</command> a transaction that
- has executed any operations involving temporary tables,
- created any cursors <literal>WITH HOLD</literal>, or executed
- <command>LISTEN</command>, <command>UNLISTEN</command>, or
+ has executed any operations involving temporary tables or the session's
+ temporary namespace, created any cursors <literal>WITH HOLD</literal>, or
+ executed <command>LISTEN</command>, <command>UNLISTEN</command>, or
<command>NOTIFY</command>.
Those features are too tightly
tied to the current session to be useful in a transaction to be prepared.
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 tables")));
+ /*
+ * 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 namespace")));
+
/*
* 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/namespace.c
index 5a6eb495d4..c55a8dd38c 100644
--- a/src/backend/catalog/namespace.c
+++ b/src/backend/catalog/namespace.c
@@ -192,6 +192,7 @@ char *namespace_search_path = NULL;
/* 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 *newRelation)
/* check for pg_temp alias */
if (strcmp(newRelation->schemaname, "pg_temp") == 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 *newRelation)
}
else if (newRelation->relpersistence == 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 *newRelation)
if (activeTempCreationPending)
{
/* Need to initialize temp namespace */
- InitTempTableNamespace();
+ AccessTempTableNamespace(true);
return myTempNamespace;
}
namespaceId = activeCreationNamespace;
@@ -2921,9 +2920,8 @@ LookupCreationNamespace(const char *nspname)
/* check for pg_temp alias */
if (strcmp(nspname, "pg_temp") == 0)
{
- /* Initialize temp namespace if first time through */
- if (!OidIsValid(myTempNamespace))
- InitTempTableNamespace();
+ /* Initialize temp namespace */
+ AccessTempTableNamespace(false);
return myTempNamespace;
}
@@ -2986,9 +2984,8 @@ QualifiedNameGetCreationNamespace(List *names, char **objname_p)
/* check for pg_temp alias */
if (strcmp(schemaname, "pg_temp") == 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 = activeCreationNamespace;
@@ -3832,6 +3829,38 @@ recomputeNamespacePath(void)
list_free(oidlist);
}
+/*
+ * 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 |= 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 is 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();
}
diff --git a/src/backend/commands/dropcmds.c b/src/backend/commands/dropcmds.c
index 1dc1c746f1..980ce89c62 100644
--- a/src/backend/commands/dropcmds.c
+++ b/src/backend/commands/dropcmds.c
@@ -14,6 +14,7 @@
*/
#include "postgres.h"
+#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);
+ /*
+ * Make note if a temporary namespace has been accessed in this
+ * transaction.
+ */
+ if (OidIsValid(namespaceId) && isTempNamespace(namespaceId))
+ MyXactFlags |= 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/extension.c b/src/backend/commands/extension.c
index 1f85f223a0..c355d4d10d 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -1473,6 +1473,13 @@ CreateExtensionInternal(char *extensionName,
list_free(search_path);
}
+ /*
+ * Make note if a temporary namespace has been accessed in this
+ * transaction.
+ */
+ if (isTempNamespace(schemaOid))
+ MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPNAMESPACE;
+
/*
* We don't check creation rights on the target namespace here. If the
* extension script actually creates any objects there, it will fail if
diff --git a/src/backend/commands/lockcmds.c b/src/backend/commands/lockcmds.c
index 1b33be1895..4cd7ec2d5f 100644
--- a/src/backend/commands/lockcmds.c
+++ b/src/backend/commands/lockcmds.c
@@ -14,6 +14,7 @@
*/
#include "postgres.h"
+#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 relid, Oid oldrelid,
{
LOCKMODE lockmode = *(LOCKMODE *) arg;
char relkind;
+ char relpersistence;
AclResult aclresult;
if (!OidIsValid(relid))
@@ -100,6 +102,14 @@ RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid,
errmsg("\"%s\" is not a table or a view",
rv->relname)));
+ /*
+ * Make note if a temporary relation has been accessed in this
+ * transaction.
+ */
+ relpersistence = get_rel_persistence(relid);
+ if (relpersistence == RELPERSISTENCE_TEMP)
+ MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPREL;
+
/* Check permissions. */
aclresult = LockTableAclCheck(relid, lockmode, GetUserId());
if (aclresult != 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)
+/*
+ * 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)
/*
* start- and end-of-transaction callbacks for dynamically loaded modules
diff --git a/src/test/modules/test_extensions/expected/test_extensions.out b/src/test/modules/test_extensions/expected/test_extensions.out
index 28d86c4b87..83d776d58c 100644
--- a/src/test/modules/test_extensions/expected/test_extensions.out
+++ b/src/test/modules/test_extensions/expected/test_extensions.out
@@ -121,3 +121,34 @@ Objects in extension "test_ext8"
-- dropping it should still work
drop extension test_ext8;
+-- Test creation of extension in temporary schema with two-phase commit,
+-- which should not work. This function wrapper is useful for portability.
+-- Avoid noise caused by CONTEXT and NOTICE messages including the temporary
+-- schema name.
+\set SHOW_CONTEXT never
+SET client_min_messages TO 'warning';
+-- First enforce presence of temporary schema.
+CREATE TEMP TABLE test_ext4_tab ();
+CREATE OR REPLACE FUNCTION create_extension_with_temp_schema()
+ RETURNS VOID AS $$
+ DECLARE
+ tmpschema text;
+ query text;
+ BEGIN
+ SELECT INTO tmpschema pg_my_temp_schema()::regnamespace;
+ query := 'CREATE EXTENSION test_ext4 SCHEMA ' || tmpschema || ' CASCADE;';
+ RAISE NOTICE 'query %', query;
+ EXECUTE query;
+ END; $$ LANGUAGE plpgsql;
+BEGIN;
+SELECT create_extension_with_temp_schema();
+ create_extension_with_temp_schema
+-----------------------------------
+
+(1 row)
+
+PREPARE TRANSACTION 'twophase_extension';
+ERROR: cannot PREPARE a transaction that has operated on temporary namespace
+-- Clean up
+DROP TABLE test_ext4_tab;
+DROP FUNCTION create_extension_with_temp_schema()
diff --git a/src/test/modules/test_extensions/sql/test_extensions.sql b/src/test/modules/test_extensions/sql/test_extensions.sql
index 9e64503eb5..9547bb0d53 100644
--- a/src/test/modules/test_extensions/sql/test_extensions.sql
+++ b/src/test/modules/test_extensions/sql/test_extensions.sql
@@ -64,3 +64,30 @@ end';
-- dropping it should still work
drop extension test_ext8;
+
+-- Test creation of extension in temporary schema with two-phase commit,
+-- which should not work. This function wrapper is useful for portability.
+
+-- Avoid noise caused by CONTEXT and NOTICE messages including the temporary
+-- schema name.
+\set SHOW_CONTEXT never
+SET client_min_messages TO 'warning';
+-- First enforce presence of temporary schema.
+CREATE TEMP TABLE test_ext4_tab ();
+CREATE OR REPLACE FUNCTION create_extension_with_temp_schema()
+ RETURNS VOID AS $$
+ DECLARE
+ tmpschema text;
+ query text;
+ BEGIN
+ SELECT INTO tmpschema pg_my_temp_schema()::regnamespace;
+ query := 'CREATE EXTENSION test_ext4 SCHEMA ' || tmpschema || ' CASCADE;';
+ RAISE NOTICE 'query %', query;
+ EXECUTE query;
+ END; $$ LANGUAGE plpgsql;
+BEGIN;
+SELECT create_extension_with_temp_schema();
+PREPARE TRANSACTION 'twophase_extension';
+-- Clean up
+DROP TABLE test_ext4_tab;
+DROP FUNCTION create_extension_with_temp_schema()
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)
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 namespace
+-- 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 namespace
+-- Operator creation
+begin;
+create operator pg_temp.@@ (leftarg = int4, rightarg = int4, procedure = int4mi);
+prepare transaction 'twophase_operator';
+ERROR: cannot PREPARE a transaction that has operated on temporary namespace
+-- 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
+---
+(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 = int4, rightarg = int4, procedure = 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';
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-15 06:36 Masahiko Sawada <sawada.mshk@gmail.com>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 22+ messages in thread
From: Masahiko Sawada @ 2019-01-15 06:36 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; Mi Tar <mmitar@gmail.com>
On Mon, Jan 14, 2019 at 9:45 AM Michael Paquier <michael@paquier.xyz> wrote:
>
> On Thu, Jan 10, 2019 at 08:44:13PM +0900, Masahiko Sawada wrote:
> > Yes, IIUC this issue happen only when creating the extension that
> > doesn't access the function after created it. For example, dblink does
> > REVOKE for dblink_connect_u() after creation.
>
> I am not sure if Robert is a fan of simply forbiding that as per the
> question here:
> https://www.postgresql.org/message-id/CA+TgmoZJdGcGFm+coHHCbBM3CfPqpjisdiux4Pa9QA3dFFQasw@mail.gmail...
>
> Anyway, we could take for now the separate approach to prevent the
> case of CREATE EXTENSION with 2PC if trying to use a temporary
> schema as more holes are closed with this stuff. Attached is an
> updated patch to do so. This adds a regression test, which would fail
> if we decide to prevent the behavior afterwards. This uses two tricks
> to avoid CONTEXT and NOTICE messages which include the temporary
> session name to keep the test stable:
> \set SHOW_CONTEXT never
> SET client_min_messages TO 'warning';
>
> Thoughts?
Thank you for updating the patch. The patch looks good to me. And the
new regression test for CREATE EXTENSION seems to work fine but maybe
it's better to reset client_min_messages at cleanup for safety.
Regards,
--
Masahiko Sawada
NIPPON TELEGRAPH AND TELEPHONE CORPORATION
NTT Open Source Software Center
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-16 01:36 Michael Paquier <michael@paquier.xyz>
parent: Masahiko Sawada <sawada.mshk@gmail.com>
0 siblings, 1 reply; 22+ messages in thread
From: Michael Paquier @ 2019-01-16 01:36 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; Mi Tar <mmitar@gmail.com>
On Tue, Jan 15, 2019 at 03:36:56PM +0900, Masahiko Sawada wrote:
> Thank you for updating the patch. The patch looks good to me. And the
> new regression test for CREATE EXTENSION seems to work fine but maybe
> it's better to reset client_min_messages at cleanup for safety.
Sure, done. I have been working on this patch more this morning, and
here is a proposal for commit. So please let me know if there are any
objections with that.
9b013dc is the commit which has introduced MyXactFlags, so this means
that we cannot get that back-patched further down than v10. Per the
lack of complains, that's a restriction I can live with.
The first patch is the actual fix to back-patch. The second patch is
an improvement I propose only for HEAD which removes ACCESSEDTEMPREL,
replacing it with ACCESSEDTEMPNAMESPACE. For now I propose to commit
0001, then I'll spawn a new thread to discuss 0002 on -hackers as
XACT_FLAGS_ACCESSEDTEMPREL has the advantage to allow skipping ON
COMMIT DELETE if no temporary tables have been accessed. Perhaps
that's not worth bothering, but that point seems worth poking at, at
least to me.
--
Michael
Attachments: download all (3 files, .tar.gz)
0001-Restrict-more-the-use-of-temporary-namespace-in-two-.patch (text/x-diff, 19.2K) download
From eb97099e0d2a6e01a219cbdaa1e14ab0f13620d7 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Wed, 16 Jan 2019 10:14:08 +0900
Subject: [PATCH 1/2] Restrict more the use of temporary namespace in two-phase
transactions
Attempting to use a temporary table within a two phase transaction is
forbidden for ages. However, there have been uncovered grounds for
a couple of other object types and commands which work on temporary
objects with two-phase commit. In short, trying to create, lock or drop
an object on a temporary schema should not be authorized within a
two-phase transaction, as it would cause its state to create
dependencies with other sessions, causing all sorts of side effects with
the existing session or other sessions spawned later on trying to use
the same temporary schema name.
Regression tests are added to cover all the grounds found, the original
report mentioned function creation, but monitoring closer there are many
other patterns like LOCK, DROP or CREATE EXTENSION which are involved.
This is back-patched down to v10, which is where 9b013dc has introduced
MyXactFlags, something that this patch relies on.
Reported-by: Alexey Bashtanov
Author: Michael Paquier
Reviewed-by: Masahiko Sawada
Discussion: https://postgr.es/m/5d910e2e-0db8-ec06-dd5f-baec420513c3@imap.cc
Backpatch-through: 10
---
doc/src/sgml/ref/prepare_transaction.sgml | 6 +-
src/backend/access/transam/xact.c | 12 ++++
src/backend/catalog/namespace.c | 60 ++++++++++++----
src/backend/commands/dropcmds.c | 8 +++
src/backend/commands/extension.c | 7 ++
src/backend/commands/lockcmds.c | 10 +++
src/include/access/xact.h | 5 ++
.../expected/test_extensions.out | 33 +++++++++
.../test_extensions/sql/test_extensions.sql | 29 ++++++++
src/test/regress/expected/temp.out | 71 +++++++++++++++++++
src/test/regress/sql/temp.sql | 56 +++++++++++++++
11 files changed, 279 insertions(+), 18 deletions(-)
diff --git a/doc/src/sgml/ref/prepare_transaction.sgml b/doc/src/sgml/ref/prepare_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 <replaceable class="parameter">transaction_id</replaceable>
<para>
It is not currently allowed to <command>PREPARE</command> a transaction that
- has executed any operations involving temporary tables,
- created any cursors <literal>WITH HOLD</literal>, or executed
- <command>LISTEN</command>, <command>UNLISTEN</command>, or
+ has executed any operations involving temporary tables or the session's
+ temporary namespace, created any cursors <literal>WITH HOLD</literal>, or
+ executed <command>LISTEN</command>, <command>UNLISTEN</command>, or
<command>NOTIFY</command>.
Those features are too tightly
tied to the current session to be useful in a transaction to be prepared.
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index f665e38ecf..aa61ad0df1 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 tables")));
+ /*
+ * 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 namespace")));
+
/*
* 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/namespace.c
index 5a6eb495d4..a6be183095 100644
--- a/src/backend/catalog/namespace.c
+++ b/src/backend/catalog/namespace.c
@@ -192,6 +192,7 @@ char *namespace_search_path = NULL;
/* 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 *newRelation)
/* check for pg_temp alias */
if (strcmp(newRelation->schemaname, "pg_temp") == 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 *newRelation)
}
else if (newRelation->relpersistence == 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 *newRelation)
if (activeTempCreationPending)
{
/* Need to initialize temp namespace */
- InitTempTableNamespace();
+ AccessTempTableNamespace(true);
return myTempNamespace;
}
namespaceId = activeCreationNamespace;
@@ -2921,9 +2920,8 @@ LookupCreationNamespace(const char *nspname)
/* check for pg_temp alias */
if (strcmp(nspname, "pg_temp") == 0)
{
- /* Initialize temp namespace if first time through */
- if (!OidIsValid(myTempNamespace))
- InitTempTableNamespace();
+ /* Initialize temp namespace */
+ AccessTempTableNamespace(false);
return myTempNamespace;
}
@@ -2986,9 +2984,8 @@ QualifiedNameGetCreationNamespace(List *names, char **objname_p)
/* check for pg_temp alias */
if (strcmp(schemaname, "pg_temp") == 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 = activeCreationNamespace;
@@ -3832,6 +3829,39 @@ recomputeNamespacePath(void)
list_free(oidlist);
}
+/*
+ * 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 whose creation is
+ * pending.
+ */
+static void
+AccessTempTableNamespace(bool force)
+{
+ /*
+ * Make note that this temporary namespace has been accessed in this
+ * transaction.
+ */
+ MyXactFlags |= 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 is wanted, so
+ * initialize it.
+ */
+ InitTempTableNamespace();
+}
+
/*
* InitTempTableNamespace
* Initialize temp table namespace on first use in a particular backend
@@ -4275,7 +4305,7 @@ fetch_search_path(bool includeImplicit)
*/
if (activeTempCreationPending)
{
- InitTempTableNamespace();
+ AccessTempTableNamespace(true);
recomputeNamespacePath();
}
diff --git a/src/backend/commands/dropcmds.c b/src/backend/commands/dropcmds.c
index 1dc1c746f1..980ce89c62 100644
--- a/src/backend/commands/dropcmds.c
+++ b/src/backend/commands/dropcmds.c
@@ -14,6 +14,7 @@
*/
#include "postgres.h"
+#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);
+ /*
+ * Make note if a temporary namespace has been accessed in this
+ * transaction.
+ */
+ if (OidIsValid(namespaceId) && isTempNamespace(namespaceId))
+ MyXactFlags |= 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/extension.c b/src/backend/commands/extension.c
index e381a30760..4fe71196c4 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -1475,6 +1475,13 @@ CreateExtensionInternal(char *extensionName,
list_free(search_path);
}
+ /*
+ * Make note if a temporary namespace has been accessed in this
+ * transaction.
+ */
+ if (isTempNamespace(schemaOid))
+ MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPNAMESPACE;
+
/*
* We don't check creation rights on the target namespace here. If the
* extension script actually creates any objects there, it will fail if
diff --git a/src/backend/commands/lockcmds.c b/src/backend/commands/lockcmds.c
index 1b33be1895..4cd7ec2d5f 100644
--- a/src/backend/commands/lockcmds.c
+++ b/src/backend/commands/lockcmds.c
@@ -14,6 +14,7 @@
*/
#include "postgres.h"
+#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 relid, Oid oldrelid,
{
LOCKMODE lockmode = *(LOCKMODE *) arg;
char relkind;
+ char relpersistence;
AclResult aclresult;
if (!OidIsValid(relid))
@@ -100,6 +102,14 @@ RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid,
errmsg("\"%s\" is not a table or a view",
rv->relname)));
+ /*
+ * Make note if a temporary relation has been accessed in this
+ * transaction.
+ */
+ relpersistence = get_rel_persistence(relid);
+ if (relpersistence == RELPERSISTENCE_TEMP)
+ MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPREL;
+
/* Check permissions. */
aclresult = LockTableAclCheck(relid, lockmode, GetUserId());
if (aclresult != 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)
+/*
+ * 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)
/*
* start- and end-of-transaction callbacks for dynamically loaded modules
diff --git a/src/test/modules/test_extensions/expected/test_extensions.out b/src/test/modules/test_extensions/expected/test_extensions.out
index 28d86c4b87..1eec5a37d3 100644
--- a/src/test/modules/test_extensions/expected/test_extensions.out
+++ b/src/test/modules/test_extensions/expected/test_extensions.out
@@ -121,3 +121,36 @@ Objects in extension "test_ext8"
-- dropping it should still work
drop extension test_ext8;
+-- Test creation of extension in temporary schema with two-phase commit,
+-- which should not work. This function wrapper is useful for portability.
+-- Avoid noise caused by CONTEXT and NOTICE messages including the temporary
+-- schema name.
+\set SHOW_CONTEXT never
+SET client_min_messages TO 'warning';
+-- First enforce presence of temporary schema.
+CREATE TEMP TABLE test_ext4_tab ();
+CREATE OR REPLACE FUNCTION create_extension_with_temp_schema()
+ RETURNS VOID AS $$
+ DECLARE
+ tmpschema text;
+ query text;
+ BEGIN
+ SELECT INTO tmpschema pg_my_temp_schema()::regnamespace;
+ query := 'CREATE EXTENSION test_ext4 SCHEMA ' || tmpschema || ' CASCADE;';
+ RAISE NOTICE 'query %', query;
+ EXECUTE query;
+ END; $$ LANGUAGE plpgsql;
+BEGIN;
+SELECT create_extension_with_temp_schema();
+ create_extension_with_temp_schema
+-----------------------------------
+
+(1 row)
+
+PREPARE TRANSACTION 'twophase_extension';
+ERROR: cannot PREPARE a transaction that has operated on temporary namespace
+-- Clean up
+DROP TABLE test_ext4_tab;
+DROP FUNCTION create_extension_with_temp_schema();
+RESET client_min_messages;
+\unset SHOW_CONTEXT
diff --git a/src/test/modules/test_extensions/sql/test_extensions.sql b/src/test/modules/test_extensions/sql/test_extensions.sql
index 9e64503eb5..f505466ab4 100644
--- a/src/test/modules/test_extensions/sql/test_extensions.sql
+++ b/src/test/modules/test_extensions/sql/test_extensions.sql
@@ -64,3 +64,32 @@ end';
-- dropping it should still work
drop extension test_ext8;
+
+-- Test creation of extension in temporary schema with two-phase commit,
+-- which should not work. This function wrapper is useful for portability.
+
+-- Avoid noise caused by CONTEXT and NOTICE messages including the temporary
+-- schema name.
+\set SHOW_CONTEXT never
+SET client_min_messages TO 'warning';
+-- First enforce presence of temporary schema.
+CREATE TEMP TABLE test_ext4_tab ();
+CREATE OR REPLACE FUNCTION create_extension_with_temp_schema()
+ RETURNS VOID AS $$
+ DECLARE
+ tmpschema text;
+ query text;
+ BEGIN
+ SELECT INTO tmpschema pg_my_temp_schema()::regnamespace;
+ query := 'CREATE EXTENSION test_ext4 SCHEMA ' || tmpschema || ' CASCADE;';
+ RAISE NOTICE 'query %', query;
+ EXECUTE query;
+ END; $$ LANGUAGE plpgsql;
+BEGIN;
+SELECT create_extension_with_temp_schema();
+PREPARE TRANSACTION 'twophase_extension';
+-- Clean up
+DROP TABLE test_ext4_tab;
+DROP FUNCTION create_extension_with_temp_schema();
+RESET client_min_messages;
+\unset SHOW_CONTEXT
diff --git a/src/test/regress/expected/temp.out b/src/test/regress/expected/temp.out
index f018f17ca0..860f58a3bf 100644
--- a/src/test/regress/expected/temp.out
+++ b/src/test/regress/expected/temp.out
@@ -301,3 +301,74 @@ select relname from pg_class where relname like 'temp_inh_oncommit_test%';
(1 row)
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 namespace
+-- 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 namespace
+-- Operator creation
+begin;
+create operator pg_temp.@@ (leftarg = int4, rightarg = int4, procedure = int4mi);
+prepare transaction 'twophase_operator';
+ERROR: cannot PREPARE a transaction that has operated on temporary namespace
+-- 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
+---
+(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
+-- Corner case: current_schema may create a temporary schema if namespace
+-- creation is pending, so check after that. First reset the connection
+-- to remove the temporary namespace.
+\c -
+SET search_path TO 'pg_temp';
+BEGIN;
+SELECT current_schema() ~ 'pg_temp' AS is_temp_schema;
+ is_temp_schema
+----------------
+ t
+(1 row)
+
+PREPARE TRANSACTION 'twophase_search';
+ERROR: cannot PREPARE a transaction that has operated on temporary namespace
diff --git a/src/test/regress/sql/temp.sql b/src/test/regress/sql/temp.sql
index 1beccc6ceb..e634ddb9ca 100644
--- a/src/test/regress/sql/temp.sql
+++ b/src/test/regress/sql/temp.sql
@@ -224,3 +224,59 @@ 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 = int4, rightarg = int4, procedure = 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';
+
+-- Corner case: current_schema may create a temporary schema if namespace
+-- creation is pending, so check after that. First reset the connection
+-- to remove the temporary namespace.
+\c -
+SET search_path TO 'pg_temp';
+BEGIN;
+SELECT current_schema() ~ 'pg_temp' AS is_temp_schema;
+PREPARE TRANSACTION 'twophase_search';
--
2.20.1
0002-Simplify-2PC-restriction-handling-for-temporary-obje.patch (text/x-diff, 8.1K) download
From 883e60f9a369cd6eb140f054bb0b602ffa19c978 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Wed, 16 Jan 2019 10:34:13 +0900
Subject: [PATCH 2/2] Simplify 2PC restriction handling for temporary objects
There are two flags used to control access to temporary tables and
access to the temporary namespace of a session, however the first
control flag is actually a concept included in the second. This removes
the flag for temporary table tracking, keeping around only the one at
namespace level.
---
src/backend/access/heap/heapam.c | 4 ++--
src/backend/access/transam/xact.c | 19 ++++++-------------
src/backend/commands/lockcmds.c | 2 +-
src/backend/commands/tablecmds.c | 2 +-
src/include/access/xact.h | 12 +++---------
src/test/regress/expected/temp.out | 14 +++++++-------
6 files changed, 20 insertions(+), 33 deletions(-)
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 9afbc8228d..dd54810052 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -1144,7 +1144,7 @@ relation_open(Oid relationId, LOCKMODE lockmode)
/* Make note that we've accessed a temporary relation */
if (RelationUsesLocalBuffers(r))
- MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPREL;
+ MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPNAMESPACE;
pgstat_initstats(r);
@@ -1194,7 +1194,7 @@ try_relation_open(Oid relationId, LOCKMODE lockmode)
/* Make note that we've accessed a temporary relation */
if (RelationUsesLocalBuffers(r))
- MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPREL;
+ MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPNAMESPACE;
pgstat_initstats(r);
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index aa61ad0df1..0c20729c68 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -2259,13 +2259,18 @@ PrepareTransaction(void)
/* NOTIFY will be handled below */
/*
- * Don't allow PREPARE TRANSACTION if we've accessed a temporary table in
+ * Don't allow PREPARE TRANSACTION if we've accessed a temporary object in
* this transaction. Having the prepared xact hold locks on another
* backend's temp table seems a bad idea --- for instance it would prevent
* the backend from exiting. There are other problems too, such as how to
* clean up the source backend's local buffers and ON COMMIT state if the
* prepared xact includes a DROP of a temp table.
*
+ * Other objects types, like functions, operators or extensions, share the
+ * same restriction as they should not be created, locked or dropped as
+ * this can mess up with this session or even a follow-up session trying
+ * to use the same temporary namespace.
+ *
* We must check this after executing any ON COMMIT actions, because they
* might still access a temp relation.
*
@@ -2273,18 +2278,6 @@ PrepareTransaction(void)
* cases, such as a temp table created and dropped all within the
* transaction. That seems to require much more bookkeeping though.
*/
- if ((MyXactFlags & XACT_FLAGS_ACCESSEDTEMPREL))
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("cannot PREPARE a transaction that has operated on temporary tables")));
-
- /*
- * 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),
diff --git a/src/backend/commands/lockcmds.c b/src/backend/commands/lockcmds.c
index 4cd7ec2d5f..bea99b59a2 100644
--- a/src/backend/commands/lockcmds.c
+++ b/src/backend/commands/lockcmds.c
@@ -108,7 +108,7 @@ RangeVarCallbackForLockTable(const RangeVar *rv, Oid relid, Oid oldrelid,
*/
relpersistence = get_rel_persistence(relid);
if (relpersistence == RELPERSISTENCE_TEMP)
- MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPREL;
+ MyXactFlags |= XACT_FLAGS_ACCESSEDTEMPNAMESPACE;
/* Check permissions. */
aclresult = LockTableAclCheck(relid, lockmode, GetUserId());
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index d2781cbf19..79b0a0d6e8 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -13253,7 +13253,7 @@ PreCommit_on_commit_actions(void)
* relations, we can skip truncating ON COMMIT DELETE ROWS
* tables, as they must still be empty.
*/
- if ((MyXactFlags & XACT_FLAGS_ACCESSEDTEMPREL))
+ if ((MyXactFlags & XACT_FLAGS_ACCESSEDTEMPNAMESPACE))
oids_to_truncate = lappend_oid(oids_to_truncate, oc->relid);
break;
case ONCOMMIT_DROP:
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index ed21a13896..ae89920687 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -87,10 +87,10 @@ extern int synchronous_commit;
extern int MyXactFlags;
/*
- * XACT_FLAGS_ACCESSEDTEMPREL - set when a temporary relation is accessed. We
- * don't allow PREPARE TRANSACTION in that case.
+ * XACT_FLAGS_ACCESSEDTEMPNAMESPACE - set when a temporary namespace is
+ * accessed. We don't allow PREPARE TRANSACTION in that case.
*/
-#define XACT_FLAGS_ACCESSEDTEMPREL (1U << 0)
+#define XACT_FLAGS_ACCESSEDTEMPNAMESPACE (1U << 0)
/*
* XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK - records whether the top level xact
@@ -98,12 +98,6 @@ extern int MyXactFlags;
*/
#define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK (1U << 1)
-/*
- * 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)
-
/*
* 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 860f58a3bf..d9e569a46d 100644
--- a/src/test/regress/expected/temp.out
+++ b/src/test/regress/expected/temp.out
@@ -327,15 +327,15 @@ ERROR: cannot PREPARE a transaction that has operated on temporary namespace
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
+ERROR: cannot PREPARE a transaction that has operated on temporary namespace
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
+ERROR: cannot PREPARE a transaction that has operated on temporary namespace
begin;
create sequence pg_temp.twophase_seq;
prepare transaction 'twophase_sequence';
-ERROR: cannot PREPARE a transaction that has operated on temporary tables
+ERROR: cannot PREPARE a transaction that has operated on temporary namespace
-- Temporary tables cannot be used with two-phase commit.
create temp table twophase_tab (a int);
begin;
@@ -345,19 +345,19 @@ select a from twophase_tab;
(0 rows)
prepare transaction 'twophase_tab';
-ERROR: cannot PREPARE a transaction that has operated on temporary tables
+ERROR: cannot PREPARE a transaction that has operated on temporary namespace
begin;
insert into twophase_tab values (1);
prepare transaction 'twophase_tab';
-ERROR: cannot PREPARE a transaction that has operated on temporary tables
+ERROR: cannot PREPARE a transaction that has operated on temporary namespace
begin;
lock twophase_tab in access exclusive mode;
prepare transaction 'twophase_tab';
-ERROR: cannot PREPARE a transaction that has operated on temporary tables
+ERROR: cannot PREPARE a transaction that has operated on temporary namespace
begin;
drop table twophase_tab;
prepare transaction 'twophase_tab';
-ERROR: cannot PREPARE a transaction that has operated on temporary tables
+ERROR: cannot PREPARE a transaction that has operated on temporary namespace
-- Corner case: current_schema may create a temporary schema if namespace
-- creation is pending, so check after that. First reset the connection
-- to remove the temporary namespace.
--
2.20.1
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-18 00:28 Michael Paquier <michael@paquier.xyz>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 22+ messages in thread
From: Michael Paquier @ 2019-01-18 00:28 UTC (permalink / raw)
To: Masahiko Sawada <sawada.mshk@gmail.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; Mi Tar <mmitar@gmail.com>
On Wed, Jan 16, 2019 at 10:36:33AM +0900, Michael Paquier wrote:
> Sure, done. I have been working on this patch more this morning, and
> here is a proposal for commit. So please let me know if there are any
> objections with that.
Committed down to v10 which is where MyXactFlags is available.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 22+ messages in thread
* Re: Is temporary functions feature official/supported? Found some issues with it.
@ 2019-01-18 02:37 Masahiko Sawada <sawada.mshk@gmail.com>
parent: Michael Paquier <michael@paquier.xyz>
0 siblings, 0 replies; 22+ messages in thread
From: Masahiko Sawada @ 2019-01-18 02:37 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Alexey Bashtanov <bashtanov@imap.cc>; pgsql-bugs; Mi Tar <mmitar@gmail.com>
On Fri, Jan 18, 2019 at 9:28 AM Michael Paquier <michael@paquier.xyz> wrote:
>
> On Wed, Jan 16, 2019 at 10:36:33AM +0900, Michael Paquier wrote:
> > Sure, done. I have been working on this patch more this morning, and
> > here is a proposal for commit. So please let me know if there are any
> > objections with that.
>
> Committed down to v10 which is where MyXactFlags is available.
Thank you!
Regards,
--
Masahiko Sawada
NIPPON TELEGRAPH AND TELEPHONE CORPORATION
NTT Open Source Software Center
^ permalink raw reply [nested|flat] 22+ messages in thread
end of thread, other threads:[~2019-01-18 02:37 UTC | newest]
Thread overview: 22+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-01-02 17:33 Is temporary functions feature official/supported? Found some issues with it. Alexey Bashtanov <bashtanov@imap.cc>
2019-01-02 19:22 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-01-02 21:55 ` Alexey Bashtanov <bashtanov@imap.cc>
2019-01-02 22:18 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-01-02 22:54 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2019-01-04 13:13 ` Alexey Bashtanov <bashtanov@imap.cc>
2019-01-04 13:13 ` Michael Paquier <michael@paquier.xyz>
2019-01-04 14:54 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-01-05 00:00 ` Michael Paquier <michael@paquier.xyz>
2019-01-07 02:42 ` Michael Paquier <michael@paquier.xyz>
2019-01-09 07:00 ` Masahiko Sawada <sawada.mshk@gmail.com>
2019-01-09 07:30 ` Michael Paquier <michael@paquier.xyz>
2019-01-10 02:47 ` Masahiko Sawada <sawada.mshk@gmail.com>
2019-01-10 03:45 ` Michael Paquier <michael@paquier.xyz>
2019-01-10 11:44 ` Masahiko Sawada <sawada.mshk@gmail.com>
2019-01-14 00:45 ` Michael Paquier <michael@paquier.xyz>
2019-01-15 06:36 ` Masahiko Sawada <sawada.mshk@gmail.com>
2019-01-16 01:36 ` Michael Paquier <michael@paquier.xyz>
2019-01-18 00:28 ` Michael Paquier <michael@paquier.xyz>
2019-01-18 02:37 ` Masahiko Sawada <sawada.mshk@gmail.com>
2019-01-04 15:43 ` Alvaro Herrera <alvherre@2ndquadrant.com>
2019-01-04 16:19 ` 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