BUG #17434: CREATE/DROP DATABASE can be executed in the same transaction with other commands
The following bug has been logged on the website:
Bug reference: 17434
Logged by: Yugo Nagata
Email address: nagata@sraoss.co.jp
PostgreSQL version: 14.2
Operating system: Ubuntu
Description:
CREATE/DROP DATABASE can be executed in the same transaction with other
commands when we use pipeline mode in pgbench or libpq API. If the
transaction aborts, this causes an inconsistency between the system catalog
and base directory.
Here is an example using the pgbench /startpipeline meta command.
----------------------------------------------------
(1) Confirm that there are four databases from psql and directories in
base.
$ psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access
privileges
-----------+--------+----------+-------------+-------------+-----------------------
postgres | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
template0 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/"yugo-n"
+
| | | | |
"yugo-n"=CTc/"yugo-n"
template1 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/"yugo-n"
+
| | | | |
"yugo-n"=CTc/"yugo-n"
test0 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
(4 rows)
$ ls data/base/
1 13014 13015 16409 pgsql_tmp
(2) Execute CREATE DATABASE in a transaction, and the transaction fails.
$ cat pipeline_createdb.sql
\startpipeline
create database test;
select 1/0;
\endpipeline
$ pgbench -t 1 -f pipeline_createdb.sql -M extended
pgbench (14.2)
starting vacuum...end.
pgbench: error: client 0 script 0 aborted in command 3 query 0:
....
(3) There are still four databases but a new directory was created in
base.
$ psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access
privileges
-----------+--------+----------+-------------+-------------+-----------------------
postgres | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
template0 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/"yugo-n"
+
| | | | |
"yugo-n"=CTc/"yugo-n"
template1 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/"yugo-n"
+
| | | | |
"yugo-n"=CTc/"yugo-n"
test0 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
(4 rows)
$ ls data/base/
1 13014 13015 16409 16411 pgsql_tmp
(4) Next, execute DROP DATABASE in a transaction, and the transaction
fails.
$ cat pipeline_dropdb.sql
\startpipeline
drop database test0;
select 1/0;
\endpipeline
$ pgbench -t 1 -f pipeline_dropdb.sql -M extended
pgbench (14.2)
starting vacuum...end.
pgbench: error: client 0 script 0 aborted in command 3 query 0:
...
(5) There are still four databases but the corresponding directory was
deleted in base.
$ psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access
privileges
-----------+--------+----------+-------------+-------------+-----------------------
postgres | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
template0 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/"yugo-n"
+
| | | | |
"yugo-n"=CTc/"yugo-n"
template1 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/"yugo-n"
+
| | | | |
"yugo-n"=CTc/"yugo-n"
test0 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
(4 rows)
$ ls data/base/
1 13014 13015 16411 pgsql_tmp
(6) We cannot connect the database "test0".
$ psql test0
psql: error: connection to server on socket "/tmp/.s.PGSQL.25435" failed:
FATAL: database "test0" does not exist
DETAIL: The database subdirectory "base/16409" is missing.
----------------------------------------------------
Detailed discussions are here;
/messages/by-id/20220301151704.76adaaefa8ed5d6c12ac3079@sraoss.co.jp
Did we make any decision on this?
---------------------------------------------------------------------------
On Fri, Mar 11, 2022 at 11:11:54AM +0000, PG Bug reporting form wrote:
The following bug has been logged on the website:
Bug reference: 17434
Logged by: Yugo Nagata
Email address: nagata@sraoss.co.jp
PostgreSQL version: 14.2
Operating system: Ubuntu
Description:CREATE/DROP DATABASE can be executed in the same transaction with other
commands when we use pipeline mode in pgbench or libpq API. If the
transaction aborts, this causes an inconsistency between the system catalog
and base directory.Here is an example using the pgbench /startpipeline meta command.
----------------------------------------------------
(1) Confirm that there are four databases from psql and directories in
base.$ psql -l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+--------+----------+-------------+-------------+----------------------- postgres | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | template0 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/"yugo-n" + | | | | | "yugo-n"=CTc/"yugo-n" template1 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/"yugo-n" + | | | | | "yugo-n"=CTc/"yugo-n" test0 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | (4 rows)$ ls data/base/
1 13014 13015 16409 pgsql_tmp(2) Execute CREATE DATABASE in a transaction, and the transaction fails.
$ cat pipeline_createdb.sql
\startpipeline
create database test;
select 1/0;
\endpipeline$ pgbench -t 1 -f pipeline_createdb.sql -M extended
pgbench (14.2)
starting vacuum...end.
pgbench: error: client 0 script 0 aborted in command 3 query 0:
....(3) There are still four databases but a new directory was created in
base.$ psql -l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+--------+----------+-------------+-------------+----------------------- postgres | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | template0 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/"yugo-n" + | | | | | "yugo-n"=CTc/"yugo-n" template1 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/"yugo-n" + | | | | | "yugo-n"=CTc/"yugo-n" test0 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | (4 rows)$ ls data/base/
1 13014 13015 16409 16411 pgsql_tmp(4) Next, execute DROP DATABASE in a transaction, and the transaction
fails.$ cat pipeline_dropdb.sql
\startpipeline
drop database test0;
select 1/0;
\endpipeline$ pgbench -t 1 -f pipeline_dropdb.sql -M extended
pgbench (14.2)
starting vacuum...end.
pgbench: error: client 0 script 0 aborted in command 3 query 0:
...(5) There are still four databases but the corresponding directory was
deleted in base.$ psql -l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+--------+----------+-------------+-------------+----------------------- postgres | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | template0 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/"yugo-n" + | | | | | "yugo-n"=CTc/"yugo-n" template1 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/"yugo-n" + | | | | | "yugo-n"=CTc/"yugo-n" test0 | yugo-n | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | (4 rows)$ ls data/base/
1 13014 13015 16411 pgsql_tmp(6) We cannot connect the database "test0".
$ psql test0
psql: error: connection to server on socket "/tmp/.s.PGSQL.25435" failed:
FATAL: database "test0" does not exist
DETAIL: The database subdirectory "base/16409" is missing.
----------------------------------------------------Detailed discussions are here;
/messages/by-id/20220301151704.76adaaefa8ed5d6c12ac3079@sraoss.co.jp
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
Indecision is a decision. Inaction is an action. Mark Batterson
Bruce Momjian <bruce@momjian.us> writes:
Did we make any decision on this?
Hmm, that one seems to have slipped past me. I agree it doesn't
look good. But why isn't the PreventInTransactionBlock() check
blocking the command from even starting?
regards, tom lane
On Thu, Jul 14, 2022 at 5:37 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Bruce Momjian <bruce@momjian.us> writes:
Did we make any decision on this?
Hmm, that one seems to have slipped past me. I agree it doesn't
look good. But why isn't the PreventInTransactionBlock() check
blocking the command from even starting?
I assume because pgbench never sends a BEGIN command so the create database
sees itself in an implicit transaction and happily goes about its business,
expecting the system to commit its work immediately after it says it is
done. But that never happens, instead the next command comes along and
crashes the implicit transaction it is now sharing with the create database
command. Create database understands how to rollback if it is the one that
causes the failure but isn't designed to operate in a situation where it
has to rollback because of someone else. That isn't how implicit
transactions are supposed to work, whether in the middle of a pipeline or
otherwise. Or at least that is my, and apparently CREATE DATABASE's,
understanding of implicit transactions: one top-level command only.
Slight tangent, but while I'm trying to get my own head around this I just
want to point out that the first sentence of the following doesn't make
sense given the above understanding of implicit transactions, and the
paragraph as a whole is tough to comprehend.
If the pipeline used an implicit transaction, then operations that have
already executed are rolled back and operations that were queued to follow
the failed operation are skipped entirely. The same behavior holds if the
pipeline starts and commits a single explicit transaction (i.e. the first
statement is BEGIN and the last is COMMIT) except that the session remains
in an aborted transaction state at the end of the pipeline. If a pipeline
contains multiple explicit transactions, all transactions that committed
prior to the error remain committed, the currently in-progress transaction
is aborted, and all subsequent operations are skipped completely, including
subsequent transactions. If a pipeline synchronization point occurs with an
explicit transaction block in aborted state, the next pipeline will become
aborted immediately unless the next command puts the transaction in normal
mode with ROLLBACK.
https://www.postgresql.org/docs/current/libpq-pipeline-mode.html#LIBPQ-PIPELINE-USING
I don't know what the answer is here but I don't think "tell the user not
to do that" is appropriate.
David J.
"David G. Johnston" <david.g.johnston@gmail.com> writes:
On Thu, Jul 14, 2022 at 5:37 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Hmm, that one seems to have slipped past me. I agree it doesn't
look good. But why isn't the PreventInTransactionBlock() check
blocking the command from even starting?
I assume because pgbench never sends a BEGIN command so the create database
sees itself in an implicit transaction and happily goes about its business,
expecting the system to commit its work immediately after it says it is
done.
Yeah. Upon inspection, the fundamental problem here is that in extended
query protocol we typically don't issue finish_xact_command() until we
get a Sync message. So even though everything looks kosher when
PreventInTransactionBlock() runs, the client can send another statement
which will be executed in the same transaction, risking trouble.
Here's a draft patch to fix this. We basically just need to force
finish_xact_command() in the same way as we do for transaction control
statements. I considered using the same technology as the code uses
for transaction control --- that is, statically check for the types of
statements that are trouble --- but after reviewing the set of callers
of PreventInTransactionBlock() I gave that up as unmaintainable. So
what this does is make PreventInTransactionBlock() set a flag to be
checked later, back in exec_execute_message. I was initially going
to make that be a new boolean global, but I happened to notice the
MyXactFlags variable which seems entirely suited to this use-case.
One thing that I'm dithering over is whether to add a check of the
new flag in exec_simple_query. As things currently stand that would
be redundant, but it seems like doing things the same way in both
of those functions might be more future-proof and understandable.
(Note the long para I added to justify not doing it ;-))
regards, tom lane
Attachments:
ensure-immediate-commit-in-extended-protocol-1.patchtext/x-diff; charset=us-ascii; name=ensure-immediate-commit-in-extended-protocol-1.patchDownload
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 116de1175b..ce1417b8f0 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -3453,6 +3453,9 @@ AbortCurrentTransaction(void)
* could issue more commands and possibly cause a failure after the statement
* completes). Subtransactions are verboten too.
*
+ * We must also set XACT_FLAGS_NEEDIMMEDIATECOMMIT in MyXactFlags, to ensure
+ * that postgres.c follows through by committing after the statement is done.
+ *
* isTopLevel: passed down from ProcessUtility to determine whether we are
* inside a function. (We will always fail if this is false, but it's
* convenient to centralize the check here instead of making callers do it.)
@@ -3494,7 +3497,9 @@ PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
CurrentTransactionState->blockState != TBLOCK_STARTED)
elog(FATAL, "cannot prevent transaction chain");
- /* all okay */
+
+ /* All okay. Set the flag to make sure the right thing happens later. */
+ MyXactFlags |= XACT_FLAGS_NEEDIMMEDIATECOMMIT;
}
/*
@@ -3591,6 +3596,13 @@ IsInTransactionBlock(bool isTopLevel)
CurrentTransactionState->blockState != TBLOCK_STARTED)
return true;
+ /*
+ * If we tell the caller we're not in a transaction block, then inform
+ * postgres.c that it had better commit when the statement is done.
+ * Otherwise our report could be a lie.
+ */
+ MyXactFlags |= XACT_FLAGS_NEEDIMMEDIATECOMMIT;
+
return false;
}
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 6f18b68856..320bbe2b1e 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -2092,32 +2092,16 @@ exec_execute_message(const char *portal_name, long max_rows)
/*
* We must copy the sourceText and prepStmtName into MessageContext in
- * case the portal is destroyed during finish_xact_command. Can avoid the
- * copy if it's not an xact command, though.
+ * case the portal is destroyed during finish_xact_command. We do not
+ * make a copy of the portalParams though, preferring to just not print
+ * them in that case.
*/
- if (is_xact_command)
- {
- sourceText = pstrdup(portal->sourceText);
- if (portal->prepStmtName)
- prepStmtName = pstrdup(portal->prepStmtName);
- else
- prepStmtName = "<unnamed>";
-
- /*
- * An xact command shouldn't have any parameters, which is a good
- * thing because they wouldn't be around after finish_xact_command.
- */
- portalParams = NULL;
- }
+ sourceText = pstrdup(portal->sourceText);
+ if (portal->prepStmtName)
+ prepStmtName = pstrdup(portal->prepStmtName);
else
- {
- sourceText = portal->sourceText;
- if (portal->prepStmtName)
- prepStmtName = portal->prepStmtName;
- else
- prepStmtName = "<unnamed>";
- portalParams = portal->portalParams;
- }
+ prepStmtName = "<unnamed>";
+ portalParams = portal->portalParams;
/*
* Report query to various monitoring facilities.
@@ -2216,13 +2200,30 @@ exec_execute_message(const char *portal_name, long max_rows)
if (completed)
{
- if (is_xact_command)
+ if (is_xact_command || (MyXactFlags & XACT_FLAGS_NEEDIMMEDIATECOMMIT))
{
/*
* If this was a transaction control statement, commit it. We
* will start a new xact command for the next command (if any).
+ * Likewise if the statement required immediate commit.
+ *
+ * Note: the reason exec_simple_query() doesn't need to check
+ * XACT_FLAGS_NEEDIMMEDIATECOMMIT is that it always does
+ * finish_xact_command() at the end of the query string, and the
+ * implicit-transaction-block mechanism prevents grouping such
+ * statements into multi-query strings. In extended query
+ * protocol, we ordinarily wouldn't force commit until Sync is
+ * received, which creates a hazard if the client tries to
+ * pipeline such statements.
*/
finish_xact_command();
+
+ /*
+ * These commands typically don't have any parameters, and even if
+ * one did we couldn't print them now because the storage went
+ * away during finish_xact_command. So pretend there were none.
+ */
+ portalParams = NULL;
}
else
{
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 7d2b35213d..300baae120 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -107,6 +107,12 @@ extern PGDLLIMPORT int MyXactFlags;
*/
#define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK (1U << 1)
+/*
+ * XACT_FLAGS_NEEDIMMEDIATECOMMIT - records whether the top level statement
+ * is one that requires immediate commit, such as CREATE DATABASE.
+ */
+#define XACT_FLAGS_NEEDIMMEDIATECOMMIT (1U << 2)
+
/*
* start- and end-of-transaction callbacks for dynamically loaded modules
*/
On Fri, Jul 15, 2022 at 2:06 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
"David G. Johnston" <david.g.johnston@gmail.com> writes:
On Thu, Jul 14, 2022 at 5:37 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Hmm, that one seems to have slipped past me. I agree it doesn't
look good. But why isn't the PreventInTransactionBlock() check
blocking the command from even starting?I assume because pgbench never sends a BEGIN command so the create
database
sees itself in an implicit transaction and happily goes about its
business,
expecting the system to commit its work immediately after it says it is
done.Yeah. Upon inspection, the fundamental problem here is that in extended
query protocol we typically don't issue finish_xact_command() until we
get a Sync message. So even though everything looks kosher when
PreventInTransactionBlock() runs, the client can send another statement
which will be executed in the same transaction, risking trouble.
Here's a draft patch to fix this. We basically just need to force
finish_xact_command() in the same way as we do for transaction control
statements. I considered using the same technology as the code uses
for transaction control --- that is, statically check for the types of
statements that are trouble --- but after reviewing the set of callers
of PreventInTransactionBlock() I gave that up as unmaintainable.
This seems like too narrow a fix though. The fact that a sync message is
the thing causing the commit of the implicit transaction in the extended
query protocol has been exposed as a latent bug in the system by the
introduction of the Pipeline functionality in libpq that relies on the
"should" in message protocol's:
"At completion of each series of extended-query messages, the frontend
should issue a Sync message. This parameterless message causes the backend
to close the current transaction if it's not inside a BEGIN/COMMIT
transaction block (“close” meaning to commit if no error, or roll back if
error)." [1]https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
However, the implicit promise of the extended query protocol, which only
allows one command to be executed at a time, is that each command, no
matter whether it must execute "outside of a transaction", that executes in
the implicit transaction block will commit at the end of the command.
I don't see needing to update simple_query_exec to recognize this flag, if
it survives, so long as we describe the flag as an implementation detail
related to the extended query protocol promise to commit implicit
transactions regardless of when the sync command arrives.
Plus, the simple query protocol doesn't have the same one command per
transaction promise. Any attempts at equivalency between the two really
doesn't have a strong foundation to work from. I could see that code
comment you wrote being part of the commit message for why
exec_simple_query was not touched but I don't find any particular value in
having it remain as presented. If anything, a comment like that would be
README scoped describing the differences between the simply and extended
protocol.
David J.
[1]: https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
"David G. Johnston" <david.g.johnston@gmail.com> writes:
On Fri, Jul 15, 2022 at 2:06 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Here's a draft patch to fix this. We basically just need to force
finish_xact_command() in the same way as we do for transaction control
statements. I considered using the same technology as the code uses
for transaction control --- that is, statically check for the types of
statements that are trouble --- but after reviewing the set of callers
of PreventInTransactionBlock() I gave that up as unmaintainable.
This seems like too narrow a fix though.
I read this, and I have absolutely no idea what you're talking about
or what you concretely want to do differently. If it's just a
documentation question, I agree that I didn't address docs yet.
Probably we do need to put something in the protocol chapter
pointing out that some commands will commit immediately.
I'm not sure I buy your argument that there's a fundamental
difference between simple and extended query protocol in this
area. In simple protocol you can wrap an "implicit transaction"
around several commands by sending them in one query message.
What we've got here is that you can do the same thing in
extended protocol by omitting Syncs. Extended protocol's
skip-till-Sync-after-error behavior is likewise very much like
the fact that simple protocol abandons the rest of the query
string after an error.
regards, tom lane
On Mon, Jul 18, 2022 at 1:20 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
"David G. Johnston" <david.g.johnston@gmail.com> writes:
On Fri, Jul 15, 2022 at 2:06 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Here's a draft patch to fix this. We basically just need to force
finish_xact_command() in the same way as we do for transaction control
statements. I considered using the same technology as the code uses
for transaction control --- that is, statically check for the types of
statements that are trouble --- but after reviewing the set of callers
of PreventInTransactionBlock() I gave that up as unmaintainable.This seems like too narrow a fix though.
I read this, and I have absolutely no idea what you're talking about
or what you concretely want to do differently. If it's just a
documentation question, I agree that I didn't address docs yet.
Probably we do need to put something in the protocol chapter
pointing out that some commands will commit immediately.
I guess I am expecting exec_execute_message to have:
if (completed && use_implicit_block)
{
EndImplicitTransactionBlock();
finish_xact_command();
} else if (completed) [existing code continues]
Or, in terms of the protocol,
"Therefore, an Execute phase is always terminated by the appearance of
exactly one of these messages: CommandComplete, EmptyQueryResponse (if the
portal was created from an empty query string), ErrorResponse, or
PortalSuspended."
CommandComplete includes an implied commit when the implicit transaction
block is in use; which basically means sending Execute while using the
implicit transaction block will cause a commit to happen.
I don't fully understand PortalSuspended but it seems like it is indeed a
valid exception to this rule.
EmptyQueryResponse seems like it should be immaterial.
ErrorResponse seems to preempt all of these.
The implied transaction block does not count for purposes of determining
whether a command that must not be executed in a transaction block can be
executed.
Now, as you say below, the "multiple commands per implicit transaction
block in extended query mode" is an intentional design choice so the above
would indeed be incorrect. However, there is still something fishy here,
so please read below.
I'm not sure I buy your argument that there's a fundamental
difference between simple and extended query protocol in this
area. In simple protocol you can wrap an "implicit transaction"
around several commands by sending them in one query message.
What we've got here is that you can do the same thing in
extended protocol by omitting Syncs. Extended protocol's
skip-till-Sync-after-error behavior is likewise very much like
the fact that simple protocol abandons the rest of the query
string after an error.
The fact that SYNC has the side effect of ending the implicit transaction
block is a POLA violation to me and the root of my misunderstanding here.
I suppose it is too late to change at this point. I can at least see that
giving the client control of the implicit transaction block, even if not
through SQL (which I suppose comes with implicit), has merit, even if this
choice of implementation is unintuitive.
In any case, I tried to extend the pgbench exercise but don't know what
went wrong. I will explain what I think would happen:
For the script:
drop table if exists performupdate;
create table performupdate (val integer);
insert into performupdate values (2);
\startpipeline
update performupdate set val = val * 2;
--create database benchtest;
select 1/0;
--rollback
\endpipeline
DO $$BEGIN RAISE NOTICE 'Value = %', (select val from performupdate); END;
$$
I get this result - the post-pipeline DO block never executes and I
expected that it would. Uncommenting the rollback made no difference. I
suppose this is just because we are abusing the tool in lieu of writing C
code. That's fine.
pgbench: client 0 executing script "/home/vagrant/pipebench.sql"
pgbench: client 0 sending drop table if exists performupdate;
pgbench: client 0 receiving
pgbench: client 0 receiving
pgbench: client 0 sending create table performupdate (val integer);
pgbench: client 0 receiving
pgbench: client 0 receiving
pgbench: client 0 sending insert into performupdate values (2);
pgbench: client 0 receiving
pgbench: client 0 receiving
pgbench: client 0 executing \startpipeline
pgbench: client 0 sending update performupdate set val = val * 2;
pgbench: client 0 sending select 1/0;
pgbench: client 0 executing \endpipeline
pgbench: client 0 receiving
pgbench: client 0 receiving
pgbench: client 0 receiving
pgbench: error: client 0 script 0 aborted in command 6 query 0:
transaction type: /home/vagrant/pipebench.sql
scaling factor: 1
query mode: extended
number of clients: 1
number of threads: 1
maximum number of tries: 1
number of transactions per client: 1
number of transactions actually processed: 0/1
number of failed transactions: 0 (NaN%)
pgbench: error: Run was aborted; the above results are incomplete.
In any case, for the above script, given the definition of pipeline mode, I
would expect that the value reported to be 2. This assumes that when
coming out of pipeline mode the system basically goes back to ReadyForQuery.
However, if I now uncomment the create database command the expectation is
either:
1. It fails to execute since an existing command is sharing the implicit
transaction, and fails the implicit transaction block, thus the reported
value is still 2
2. It succeeds, the next command executes and fails, the database creation
is undone and the update is undone, thus the reported value is still 2
What does happen, IIUC, is that both the preceding update command and the
create database are now committed and the returned value is 4
In short, we are saying that issuing a command that cannot be executed in a
transaction block within the middle of the implicit transaction block will
cause the block to implicitly commit if the command completes successfully.
From this it seems that not only should we issue a commit after executing
create database in the implicit transaction block but we also need to
commit before attempting to execute the command in the first place. The
mere presence of a such a command basically means:
COMMIT;
CREATE DATABASE...;
COMMIT;
That is what it means to be unable to be executed in a transaction block -
with an outright error if an explicit transaction block has already been
established.
David J.
David G. Johnston wrote:
drop table if exists performupdate;
create table performupdate (val integer);
insert into performupdate values (2);
\startpipeline
update performupdate set val = val * 2;
--create database benchtest;
select 1/0;
--rollback
\endpipeline
DO $$BEGIN RAISE NOTICE 'Value = %', (select val from performupdate); END;
$$I get this result - the post-pipeline DO block never executes and I
expected that it would.
pgbench stops the script on errors. If the script was reduced to
select 1/0;
DO $$BEGIN RAISE NOTICE 'print this; END; $$
the DO statement would not be executed either.
When the error happens inside a pipeline section, it's the same.
The pgbench code collects the results sent by the server to clear up,
but the script is aborted at this point, and the DO block is not going
to be sent to the server.
Best regards,
--
Daniel Vérité
https://postgresql.verite.pro/
Twitter: @DanielVerite
"David G. Johnston" <david.g.johnston@gmail.com> writes:
I guess I am expecting exec_execute_message to have:
if (completed && use_implicit_block)
{
EndImplicitTransactionBlock();
finish_xact_command();
} else if (completed) [existing code continues]
The problem with that is "where do we get use_implicit_block from"?
In simple query mode it's set if the simple-query message contains
more than one statement. But the issue we face in extended mode is
precisely that we don't know if the client will try to send another
statement before Sync.
I spent some time thinking about alternative solutions for this.
AFAICS the only other feasible approach is to continue to not do
finish_xact_command() until Sync, but change state so that any
message that tries to do other work will be rejected. But that's
not really at all attractive, for these reasons:
1. Rejecting other message types implies an error (unless we
get REALLY weird), which implies a rollback, which gets us into
the same inconsistent state as a user-issued rollback.
2. Once we've completed the CREATE DATABASE or whatever, we really
have got to commit or we end with inconsistent state. So it does
not seem like a good plan to sit and wait for the client, even if
we were certain that it'd eventually issue Sync. The longer we
sit, the more chance of something interfering --- database shutdown,
network connection drop, etc.
3. This approach winds up throwing errors for cases that used
to work, eg multiple CREATE DATABASE commands before Sync.
The immediate-silent-commit approach doesn't. The only compatibility
break is that you can't ROLLBACK after CREATE DATABASE ... but that's
precisely the case that doesn't work anyway.
Ideally we'd dodge all of this mess by making all our DDL fully
transactional and getting rid of PreventInTransactionBlock.
I'm not sure that will ever happen; but I am sad that so many
new calls of it have been introduced by the logical replication
stuff. (Doesn't look like anybody bothered to teach psql's
command_no_begin() about those, either.) In any case, that's a
long-term direction to pursue, not something that could yield
a back-patchable fix.
Anyway, here's an updated patch, now with docs. I was surprised
to realize that protocol.sgml has no explicit mention of pipelining,
even though extended query protocol was intentionally set up to make
that possible. So I added a <sect2> about that, which provides a home
for the caveat about immediate-commit commands.
regards, tom lane
Attachments:
ensure-immediate-commit-in-extended-protocol-2.patchtext/x-diff; charset=us-ascii; name=ensure-immediate-commit-in-extended-protocol-2.patchDownload
diff --git a/doc/src/sgml/protocol.sgml b/doc/src/sgml/protocol.sgml
index c0b89a3c01..6c5d1dcb1b 100644
--- a/doc/src/sgml/protocol.sgml
+++ b/doc/src/sgml/protocol.sgml
@@ -1050,6 +1050,66 @@ SELCT 1/0;<!-- this typo is intentional -->
</note>
</sect2>
+ <sect2 id="protocol-flow-pipelining">
+ <title>Pipelining</title>
+
+ <indexterm zone="protocol-flow-pipelining">
+ <primary>pipelining</primary>
+ <secondary>protocol specification</secondary>
+ </indexterm>
+
+ <para>
+ Use of the extended query protocol
+ allows <firstterm>pipelining</firstterm>, which means sending a series
+ of queries without waiting for earlier ones to complete. This reduces
+ the number of network round trips needed to complete a given series of
+ operations. However, the user must carefully consider the required
+ behavior if one of the steps fails, since later queries will already
+ be in flight to the server.
+ </para>
+
+ <para>
+ One way to deal with that is to wrap the whole query series in a
+ single transaction, and withhold sending the
+ final <command>COMMIT</command> until success of the series is known;
+ if there is any problem, send <command>ROLLBACK</command> instead.
+ This is a bit tedious though, and it still requires an extra network
+ round trip for the <command>COMMIT</command>
+ or <command>ROLLBACK</command>. Also, one might wish for some of the
+ commands to commit independently of others.
+ </para>
+
+ <para>
+ The extended query protocol provides another way to manage this
+ concern, which is to omit sending Sync messages between steps that
+ are dependent. Since, after an error, the backend will skip command
+ messages until it finds Sync, this allows later commands in a pipeline
+ to be skipped automatically when an earlier one fails, without the
+ client having to manage that explicitly with <command>COMMIT</command>
+ and <command>ROLLBACK</command>. Independently-committable segments
+ of the pipeline can be separated by Sync messages.
+ </para>
+
+ <para>
+ If the client has not issued an explicit <command>BEGIN</command>,
+ then each Sync ordinarily causes an implicit <command>COMMIT</command>
+ if the preceding step(s) succeeded, or an
+ implicit <command>ROLLBACK</command> if they failed. However, there
+ are a few DDL commands (such as <command>CREATE DATABASE</command>)
+ that force an immediate commit to preserve database consistency.
+ A Sync immediately following one of these has no effect except to
+ respond with ReadyForQuery.
+ </para>
+
+ <para>
+ When using this method, completion of the pipeline must be determined
+ by counting ReadyForQuery messages and waiting for that to reach the
+ number of Syncs sent. Counting command completion responses is
+ unreliable, since some of the commands may not be executed and thus not
+ produce a completion message.
+ </para>
+ </sect2>
+
<sect2>
<title>Function Call</title>
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 116de1175b..ce1417b8f0 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -3453,6 +3453,9 @@ AbortCurrentTransaction(void)
* could issue more commands and possibly cause a failure after the statement
* completes). Subtransactions are verboten too.
*
+ * We must also set XACT_FLAGS_NEEDIMMEDIATECOMMIT in MyXactFlags, to ensure
+ * that postgres.c follows through by committing after the statement is done.
+ *
* isTopLevel: passed down from ProcessUtility to determine whether we are
* inside a function. (We will always fail if this is false, but it's
* convenient to centralize the check here instead of making callers do it.)
@@ -3494,7 +3497,9 @@ PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
if (CurrentTransactionState->blockState != TBLOCK_DEFAULT &&
CurrentTransactionState->blockState != TBLOCK_STARTED)
elog(FATAL, "cannot prevent transaction chain");
- /* all okay */
+
+ /* All okay. Set the flag to make sure the right thing happens later. */
+ MyXactFlags |= XACT_FLAGS_NEEDIMMEDIATECOMMIT;
}
/*
@@ -3591,6 +3596,13 @@ IsInTransactionBlock(bool isTopLevel)
CurrentTransactionState->blockState != TBLOCK_STARTED)
return true;
+ /*
+ * If we tell the caller we're not in a transaction block, then inform
+ * postgres.c that it had better commit when the statement is done.
+ * Otherwise our report could be a lie.
+ */
+ MyXactFlags |= XACT_FLAGS_NEEDIMMEDIATECOMMIT;
+
return false;
}
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index d0bbd30d2b..078fbdb5a0 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -1277,6 +1277,13 @@ exec_simple_query(const char *query_string)
}
else
{
+ /*
+ * We had better not see XACT_FLAGS_NEEDIMMEDIATECOMMIT set if
+ * we're not calling finish_xact_command(). (The implicit
+ * transaction block should have prevented it from getting set.)
+ */
+ Assert(!(MyXactFlags & XACT_FLAGS_NEEDIMMEDIATECOMMIT));
+
/*
* We need a CommandCounterIncrement after every query, except
* those that start or end a transaction block.
@@ -2092,32 +2099,16 @@ exec_execute_message(const char *portal_name, long max_rows)
/*
* We must copy the sourceText and prepStmtName into MessageContext in
- * case the portal is destroyed during finish_xact_command. Can avoid the
- * copy if it's not an xact command, though.
+ * case the portal is destroyed during finish_xact_command. We do not
+ * make a copy of the portalParams though, preferring to just not print
+ * them in that case.
*/
- if (is_xact_command)
- {
- sourceText = pstrdup(portal->sourceText);
- if (portal->prepStmtName)
- prepStmtName = pstrdup(portal->prepStmtName);
- else
- prepStmtName = "<unnamed>";
-
- /*
- * An xact command shouldn't have any parameters, which is a good
- * thing because they wouldn't be around after finish_xact_command.
- */
- portalParams = NULL;
- }
+ sourceText = pstrdup(portal->sourceText);
+ if (portal->prepStmtName)
+ prepStmtName = pstrdup(portal->prepStmtName);
else
- {
- sourceText = portal->sourceText;
- if (portal->prepStmtName)
- prepStmtName = portal->prepStmtName;
- else
- prepStmtName = "<unnamed>";
- portalParams = portal->portalParams;
- }
+ prepStmtName = "<unnamed>";
+ portalParams = portal->portalParams;
/*
* Report query to various monitoring facilities.
@@ -2216,13 +2207,24 @@ exec_execute_message(const char *portal_name, long max_rows)
if (completed)
{
- if (is_xact_command)
+ if (is_xact_command || (MyXactFlags & XACT_FLAGS_NEEDIMMEDIATECOMMIT))
{
/*
* If this was a transaction control statement, commit it. We
* will start a new xact command for the next command (if any).
+ * Likewise if the statement required immediate commit. Without
+ * this provision, we wouldn't force commit until Sync is
+ * received, which creates a hazard if the client tries to
+ * pipeline immediate-commit statements.
*/
finish_xact_command();
+
+ /*
+ * These commands typically don't have any parameters, and even if
+ * one did we couldn't print them now because the storage went
+ * away during finish_xact_command. So pretend there were none.
+ */
+ portalParams = NULL;
}
else
{
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 7d2b35213d..300baae120 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -107,6 +107,12 @@ extern PGDLLIMPORT int MyXactFlags;
*/
#define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK (1U << 1)
+/*
+ * XACT_FLAGS_NEEDIMMEDIATECOMMIT - records whether the top level statement
+ * is one that requires immediate commit, such as CREATE DATABASE.
+ */
+#define XACT_FLAGS_NEEDIMMEDIATECOMMIT (1U << 2)
+
/*
* start- and end-of-transaction callbacks for dynamically loaded modules
*/
On Tue, Jul 26, 2022 at 8:08 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
"David G. Johnston" <david.g.johnston@gmail.com> writes:
I guess I am expecting exec_execute_message to have:
if (completed && use_implicit_block)
{
EndImplicitTransactionBlock();
finish_xact_command();
} else if (completed) [existing code continues]The problem with that is "where do we get use_implicit_block from"?
In simple query mode it's set if the simple-query message contains
more than one statement. But the issue we face in extended mode is
precisely that we don't know if the client will try to send another
statement before Sync.
[...]
Anyway, here's an updated patch, now with docs. I was surprised
to realize that protocol.sgml has no explicit mention of pipelining,
even though extended query protocol was intentionally set up to make
that possible. So I added a <sect2> about that, which provides a home
for the caveat about immediate-commit commands.
Thanks! This added section is clear and now affirms the understanding I've
come to with this thread, mostly. I'm still of the opinion that the
definition of "cannot be executed inside a transaction block" means that we
must "auto-sync" (implicit commit) before and after the restricted command,
not just after, and that the new section should cover this - whether we do
or do not - explicitly.
David J.
"David G. Johnston" <david.g.johnston@gmail.com> writes:
Thanks! This added section is clear and now affirms the understanding I've
come to with this thread, mostly. I'm still of the opinion that the
definition of "cannot be executed inside a transaction block" means that we
must "auto-sync" (implicit commit) before and after the restricted command,
not just after, and that the new section should cover this - whether we do
or do not - explicitly.
I'm not excited about your proposal to auto-commit before starting
the command. In the first place, we can't: we do not know whether
the command will call PreventInTransactionBlock. Restructuring to
change that seems untenable in view of past cowboy decisions about
use of PreventInTransactionBlock in the replication logic. In the
second place, it'd be a deviation from the current behavior (namely
that a failure in CREATE DATABASE et al rolls back previous un-synced
commands) that is not necessary to fix a bug, so changing that in
the back branches would be a hard sell. I don't even agree that
it's obviously better than the current behavior, so I'm not much
on board with changing it in HEAD either.
regards, tom lane
On Tue, Jul 26, 2022 at 8:37 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
"David G. Johnston" <david.g.johnston@gmail.com> writes:
Thanks! This added section is clear and now affirms the understanding
I've
come to with this thread, mostly. I'm still of the opinion that the
definition of "cannot be executed inside a transaction block" means thatwe
must "auto-sync" (implicit commit) before and after the restricted
command,
not just after, and that the new section should cover this - whether we
do
or do not - explicitly.
I'm not excited about your proposal to auto-commit before starting
the command. In the first place, we can't: we do not know whether
the command will call PreventInTransactionBlock. Restructuring to
change that seems untenable in view of past cowboy decisions about
use of PreventInTransactionBlock in the replication logic. In the
second place, it'd be a deviation from the current behavior (namely
that a failure in CREATE DATABASE et al rolls back previous un-synced
commands) that is not necessary to fix a bug, so changing that in
the back branches would be a hard sell. I don't even agree that
it's obviously better than the current behavior, so I'm not much
on board with changing it in HEAD either.
That leaves us with changing the documentation then, from:
CREATE DATABASE cannot be executed inside a transaction block.
to:
CREATE DATABASE cannot be executed inside an explicit transaction block (it
will error in this case), and will commit (or rollback on failure) any
implicit transaction it is a part of.
The content of the section you added works fine so long as we are clear
regarding the fact it can be executed in a transaction so long as it is
implicit.
David J.
"David G. Johnston" <david.g.johnston@gmail.com> writes:
That leaves us with changing the documentation then, from:
CREATE DATABASE cannot be executed inside a transaction block.
to:
CREATE DATABASE cannot be executed inside an explicit transaction block (it
will error in this case), and will commit (or rollback on failure) any
implicit transaction it is a part of.
That's not going to help anybody unless we also provide a definition of
"implicit transaction", which is a bit far afield for that man page.
I did miss a bet in the proposed pipeline addendum, though.
I should have written
... However, there
are a few DDL commands (such as <command>CREATE DATABASE</command>)
that cannot be executed inside a transaction block. If one of
these is executed in a pipeline, it will, upon success, force an
immediate commit to preserve database consistency.
That ties the info to our standard wording in the per-command man
pages.
regards, tom lane
On Tue, Jul 26, 2022 at 9:03 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
"David G. Johnston" <david.g.johnston@gmail.com> writes:
That leaves us with changing the documentation then, from:
CREATE DATABASE cannot be executed inside a transaction block.
to:
CREATE DATABASE cannot be executed inside an explicit transaction block(it
will error in this case), and will commit (or rollback on failure) any
implicit transaction it is a part of.That's not going to help anybody unless we also provide a definition of
"implicit transaction", which is a bit far afield for that man page.I did miss a bet in the proposed pipeline addendum, though.
I should have written... However, there
are a few DDL commands (such as <command>CREATE DATABASE</command>)
that cannot be executed inside a transaction block. If one of
these is executed in a pipeline, it will, upon success, force an
immediate commit to preserve database consistency.That ties the info to our standard wording in the per-command man
pages.
And we are back around to the fact that only by using libpq directly, or
via the pipeline feature of pgbench, can one actually exert control over
the implicit transaction. The psql and general SQL interface
implementation are just going to Sync after each command and so everything
looks like one transaction per command to them and only explicit
transactions matter. From that, the adjustment you describe above is
sufficient for me.
David J.
"David G. Johnston" <david.g.johnston@gmail.com> writes:
And we are back around to the fact that only by using libpq directly, or
via the pipeline feature of pgbench, can one actually exert control over
the implicit transaction. The psql and general SQL interface
implementation are just going to Sync after each command and so everything
looks like one transaction per command to them and only explicit
transactions matter.
Right.
From that, the adjustment you describe above is sufficient for me.
Cool, I'll set about back-patching.
regards, tom lane
Hi,
Thank you for treating this bug report!
On Tue, 26 Jul 2022 12:14:19 -0400
Tom Lane <tgl@sss.pgh.pa.us> wrote:
"David G. Johnston" <david.g.johnston@gmail.com> writes:
And we are back around to the fact that only by using libpq directly, or
via the pipeline feature of pgbench, can one actually exert control over
the implicit transaction. The psql and general SQL interface
implementation are just going to Sync after each command and so everything
looks like one transaction per command to them and only explicit
transactions matter.Right.
From that, the adjustment you describe above is sufficient for me.
Cool, I'll set about back-patching.
regards, tom lane
I've looked at the commited fix. What I wonder is whether a change in
IsInTransactionBlock() is necessary or not.
+ /*
+ * If we tell the caller we're not in a transaction block, then inform
+ * postgres.c that it had better commit when the statement is done.
+ * Otherwise our report could be a lie.
+ */
+ MyXactFlags |= XACT_FLAGS_NEEDIMMEDIATECOMMIT;
+
return false;
The comment says that is required to prevent the report from being
a lie. Indeed, after this function returns false, it is guaranteed
that following statements are executed in a separate transaction from
that of the current statement. However, there is no guarantee that the
current statement is running in a separate transaction from that of
the previous statements. The only caller of this function is ANALYZE
command, and this is used for the latter purpose. That is, if we are
not in a transaction block, ANALYZE can close the current transaction
and restart another one without affecting previous transactions.
(At least, ANALYZE command seems to assume it.) So,
I think the fix does not seem to make a sense.
In fact, the result of IsInTransactionBlock does not make senses at
all in pipe-line mode regardless to the fix. ANALYZE could commit all
previous commands in pipelining, and this may not be user expected
behaviour. Moreover, before the fix ANALYZE didn't close and open a
transaction if the target is only one table, but after the fix ANALYZE
always issues commit regardless to the number of table.
I am not sure if we should fix it to prevent such confusing behavior
because this breaks back-compatibility, but I prefer to fixing it.
The idea is to start an implicit transaction block if the server receive
more than one Execute messages before receiving Sync as discussed in [1]/messages/by-id/20220301151704.76adaaefa8ed5d6c12ac3079@sraoss.co.jp.
I attached the patch for this fix.
If the first command in a pipeline is DDL commands such as CREATE
DATABASE, this is allowed and immediately committed after success, as
same as the current behavior. Executing such commands in the middle of
pipeline is not allowed because the pipeline is regarded as "an implicit
transaction block" at that time. Similarly, ANALYZE in the middle of
pipeline can not close and open transaction.
[1]: /messages/by-id/20220301151704.76adaaefa8ed5d6c12ac3079@sraoss.co.jp
Regards,
Yugo Nagata
--
Yugo NAGATA <nagata@sraoss.co.jp>
Attachments:
implicit_transction_for_pipeline.patchtext/x-diff; name=implicit_transction_for_pipeline.patchDownload
diff --git a/doc/src/sgml/protocol.sgml b/doc/src/sgml/protocol.sgml
index 46ec4acd40..edbab84935 100644
--- a/doc/src/sgml/protocol.sgml
+++ b/doc/src/sgml/protocol.sgml
@@ -1078,7 +1078,9 @@ SELCT 1/0;<!-- this typo is intentional -->
<para>
The extended query protocol provides another way to manage this
concern, which is to omit sending Sync messages between steps that
- are dependent. Since, after an error, the backend will skip command
+ are dependent. When receiving another Execute message before finding
+ Sync, an implicit transaction block is started and it is closed when
+ receiving Sync. Since, after an error, the backend will skip command
messages until it finds Sync, this allows later commands in a pipeline
to be skipped automatically when an earlier one fails, without the
client having to manage that explicitly with <command>BEGIN</command>
@@ -1093,10 +1095,11 @@ SELCT 1/0;<!-- this typo is intentional -->
implicit <command>ROLLBACK</command> if they failed. However, there
are a few DDL commands (such as <command>CREATE DATABASE</command>)
that cannot be executed inside a transaction block. If one of
- these is executed in a pipeline, it will, upon success, force an
- immediate commit to preserve database consistency.
+ these is executed as a first command in a pipeline, it will, upon
+ success, force an immediate commit to preserve database consistency.
A Sync immediately following one of these has no effect except to
- respond with ReadyForQuery.
+ respond with ReadyForQuery. Executing one of these in the middle
+ of a pipeline is not allowed and causes an error.
</para>
<para>
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index ce1417b8f0..82242c082d 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -3596,13 +3596,6 @@ IsInTransactionBlock(bool isTopLevel)
CurrentTransactionState->blockState != TBLOCK_STARTED)
return true;
- /*
- * If we tell the caller we're not in a transaction block, then inform
- * postgres.c that it had better commit when the statement is done.
- * Otherwise our report could be a lie.
- */
- MyXactFlags |= XACT_FLAGS_NEEDIMMEDIATECOMMIT;
-
return false;
}
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 078fbdb5a0..a2d3bcf469 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -2138,6 +2138,15 @@ exec_execute_message(const char *portal_name, long max_rows)
*/
start_xact_command();
+ /*
+ * If we are processing more than one execute messages, start an implicit
+ * transaction block for pipelining.
+ */
+ if (MyXactFlags & XACT_FLAGS_FIRSTEXECUTE)
+ BeginImplicitTransactionBlock();
+ else
+ MyXactFlags |= XACT_FLAGS_FIRSTEXECUTE;
+
/*
* If we re-issue an Execute protocol request against an existing portal,
* then we are only fetching more rows rather than completely re-executing
@@ -4684,6 +4693,11 @@ PostgresMain(const char *dbname, const char *username)
case 'S': /* sync */
pq_getmsgend(&input_message);
+ /*
+ * If we're using an implicit transaction block for pipelining,
+ * we must close that out first.
+ */
+ EndImplicitTransactionBlock();
finish_xact_command();
send_ready_for_query = true;
break;
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 300baae120..169bc79535 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -113,6 +113,12 @@ extern PGDLLIMPORT int MyXactFlags;
*/
#define XACT_FLAGS_NEEDIMMEDIATECOMMIT (1U << 2)
+/*
+ * XACT_FLAGS_FIRSTEXECUTE - records whether the top level statement
+ * executes the first statement in a pipeline.
+ */
+#define XACT_FLAGS_FIRSTEXECUTE (1U << 3)
+
/*
* start- and end-of-transaction callbacks for dynamically loaded modules
*/
Yugo NAGATA <nagata@sraoss.co.jp> writes:
I've looked at the commited fix. What I wonder is whether a change in
IsInTransactionBlock() is necessary or not.
I've not examined ANALYZE's dependencies on this closely, but it doesn't
matter really, because I'm not willing to assume that ANALYZE is the
only caller. There could be external modules with stronger assumptions
that IsInTransactionBlock() yielding false provides guarantees equivalent
to PreventInTransactionBlock(). It did before this patch, so I think
it needs to still do so after.
In fact, the result of IsInTransactionBlock does not make senses at
all in pipe-line mode regardless to the fix. ANALYZE could commit all
previous commands in pipelining, and this may not be user expected
behaviour.
This seems pretty much isomorphic to the fact that CREATE DATABASE
will commit preceding steps in the pipeline. That's not great,
I admit; we'd not have designed it like that if we'd had complete
understanding of the behavior at the beginning. But it's acted
like that for a couple of decades now, so changing it seems far
more likely to make people unhappy than happy. The same for
ANALYZE in a pipeline.
If the first command in a pipeline is DDL commands such as CREATE
DATABASE, this is allowed and immediately committed after success, as
same as the current behavior. Executing such commands in the middle of
pipeline is not allowed because the pipeline is regarded as "an implicit
transaction block" at that time. Similarly, ANALYZE in the middle of
pipeline can not close and open transaction.
I'm not going there. If you can persuade some other committer that
this is worth breaking backward compatibility for, fine; the user
complaints will be their problem.
regards, tom lane
On Wed, Jul 27, 2022 at 7:50 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Yugo NAGATA <nagata@sraoss.co.jp> writes:
I've looked at the commited fix. What I wonder is whether a change in
IsInTransactionBlock() is necessary or not.In fact, the result of IsInTransactionBlock does not make senses at
all in pipe-line mode regardless to the fix. ANALYZE could commit all
previous commands in pipelining, and this may not be user expected
behaviour.This seems pretty much isomorphic to the fact that CREATE DATABASE
will commit preceding steps in the pipeline. That's not great,
I admit; we'd not have designed it like that if we'd had complete
understanding of the behavior at the beginning. But it's acted
like that for a couple of decades now, so changing it seems far
more likely to make people unhappy than happy. The same for
ANALYZE in a pipeline.
I agreed to leaving the description of CREATE DATABASE simplified by not
introducing the idea of implicit transactions or, equivalently,
"autocommit".
Just tossing out there that we should acknowledge that our wording in the
BEGIN Reference should remain status quo based upon the same reasoning.
"By default (without BEGIN), PostgreSQL executes transactions in
“autocommit” mode, that is, each statement is executed in its own
transaction and a commit is implicitly performed at the end of the
statement (if execution was successful, otherwise a rollback is done)."
https://www.postgresql.org/docs/current/sql-begin.html
Maybe write instead:
"By default (without BEGIN), PostgreSQL creates transactions based upon the
underlying messages passed between the client and server. Typically this
means each statement ends up having its own transaction. In any case,
statements that must not execute in a transaction (like CREATE DATABASE)
must use the default, and will always cause a commit or rollback to happen
upon completion."
It feels a bit out-of-place, maybe if the content scope is acceptable we
can work it better into the Tutorial-Advanced Features-Transaction section
and just replace the existing sentence with a link to there?
David J.
On Wed, 27 Jul 2022 22:50:55 -0400
Tom Lane <tgl@sss.pgh.pa.us> wrote:
Yugo NAGATA <nagata@sraoss.co.jp> writes:
I've looked at the commited fix. What I wonder is whether a change in
IsInTransactionBlock() is necessary or not.I've not examined ANALYZE's dependencies on this closely, but it doesn't
matter really, because I'm not willing to assume that ANALYZE is the
only caller. There could be external modules with stronger assumptions
that IsInTransactionBlock() yielding false provides guarantees equivalent
to PreventInTransactionBlock(). It did before this patch, so I think
it needs to still do so after.
Thank you for your explanation. I understood that IsInTransactionBlock()
and PreventInTransactionBlock() share the equivalent assumption.
As to ANALYZE, after investigating the code more, I found that setting XACT_FLAGS_NEEDIMMEDIATECOMMIT in IsInTransactionBlock() is needed indeed.
That is, some flags in pg_class such as relhasindex can be safely updated
only if ANALYZE is not in a transaction block and never rolled back. So,
in a pipeline, ANALYZE must be immediately committed.
However, I think we need more comments on these functions to clarify what
users can expect or not for them. It is ensured that the statement that
calls PreventInTransactionBlock() or receives false from
IsInTransactionBlock() never be rolled back if it finishes successfully.
This can eliminate the harmful influence of non-rollback-able side effects.
On the other hand, it cannot ensure that the statement calling these
functions is the first or only one in the transaction in pipelining. If
there are preceding statements in a pipeline, they are committed in the
same transaction of the current statement.
The attached patch tries to add comments explaining it on the functions.
In fact, the result of IsInTransactionBlock does not make senses at
all in pipe-line mode regardless to the fix. ANALYZE could commit all
previous commands in pipelining, and this may not be user expected
behaviour.This seems pretty much isomorphic to the fact that CREATE DATABASE
will commit preceding steps in the pipeline.
I am not sure if we can think CREATE DATABASE case and ANLALYZE case
similarly. First, CREATE DATABASE is one of the commands that cannot be
executed inside a transaction block, but ANALYZE can be. So, users would
not be able to know ANALYZE in a pipeline causes a commit from the
documentation. Second, ANALYZE issues a commit internally in an early
stage not only after it finished successfully. For example, even if
ANALYZE is failing because a not-existing column name is specified, it
issues a commit before checking the column name. This makes more hard
to know which statements will be committed and which statements not
committed in a pipeline. Also, as you know, there are other commands
that issue internal commits.
That's not great,
I admit; we'd not have designed it like that if we'd had complete
understanding of the behavior at the beginning. But it's acted
like that for a couple of decades now, so changing it seems far
more likely to make people unhappy than happy. The same for
ANALYZE in a pipeline.
If the first command in a pipeline is DDL commands such as CREATE
DATABASE, this is allowed and immediately committed after success, as
same as the current behavior. Executing such commands in the middle of
pipeline is not allowed because the pipeline is regarded as "an implicit
transaction block" at that time. Similarly, ANALYZE in the middle of
pipeline can not close and open transaction.I'm not going there. If you can persuade some other committer that
this is worth breaking backward compatibility for, fine; the user
complaints will be their problem.
I don't have no idea how to reduce the complexity explained above and
clarify the transactional behavior of pipelining to users other than the
fix I proposed in the previous post. However, I also agree that such
changing may make some people unhappy. If there is no good way and we
would not like to change the behavior, I think it is better to mention
the effects of commands that issue internal commits in the documentation
at least.
Regards,
Yugo Nagata
--
Yugo NAGATA <nagata@sraoss.co.jp>
Attachments:
comments_on_PreventInTransactionBlock.patchtext/x-diff; name=comments_on_PreventInTransactionBlock.patchDownload
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 50f092d7eb..ee9f0fafdd 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -3455,6 +3455,10 @@ AbortCurrentTransaction(void)
*
* We must also set XACT_FLAGS_NEEDIMMEDIATECOMMIT in MyXactFlags, to ensure
* that postgres.c follows through by committing after the statement is done.
+ * This ensures that the statement that calls this function never be rolled
+ * back if it finishes successfully. Note that if there are preceding statements
+ * in a pipeline, they are committed in the same transaction of the current
+ * statement.
*
* isTopLevel: passed down from ProcessUtility to determine whether we are
* inside a function. (We will always fail if this is false, but it's
@@ -3573,6 +3577,10 @@ CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
* a transaction block than when running as single commands. ANALYZE is
* currently the only example.
*
+ * Returning false ensures that the statement that calls this function never
+ * be rolled back if it finishes successfully. See comments on
+ * PreventInTransactionBlock.
+ *
* isTopLevel: passed down from ProcessUtility to determine whether we are
* inside a function.
*/
Hi,
On Tue, 9 Aug 2022 00:21:02 +0900
Yugo NAGATA <nagata@sraoss.co.jp> wrote:
On Wed, 27 Jul 2022 22:50:55 -0400
Tom Lane <tgl@sss.pgh.pa.us> wrote:Yugo NAGATA <nagata@sraoss.co.jp> writes:
I've looked at the commited fix. What I wonder is whether a change in
IsInTransactionBlock() is necessary or not.I've not examined ANALYZE's dependencies on this closely, but it doesn't
matter really, because I'm not willing to assume that ANALYZE is the
only caller. There could be external modules with stronger assumptions
that IsInTransactionBlock() yielding false provides guarantees equivalent
to PreventInTransactionBlock(). It did before this patch, so I think
it needs to still do so after.Thank you for your explanation. I understood that IsInTransactionBlock()
and PreventInTransactionBlock() share the equivalent assumption.As to ANALYZE, after investigating the code more, I found that setting XACT_FLAGS_NEEDIMMEDIATECOMMIT in IsInTransactionBlock() is needed indeed.
That is, some flags in pg_class such as relhasindex can be safely updated
only if ANALYZE is not in a transaction block and never rolled back. So,
in a pipeline, ANALYZE must be immediately committed.However, I think we need more comments on these functions to clarify what
users can expect or not for them. It is ensured that the statement that
calls PreventInTransactionBlock() or receives false from
IsInTransactionBlock() never be rolled back if it finishes successfully.
This can eliminate the harmful influence of non-rollback-able side effects.On the other hand, it cannot ensure that the statement calling these
functions is the first or only one in the transaction in pipelining. If
there are preceding statements in a pipeline, they are committed in the
same transaction of the current statement.The attached patch tries to add comments explaining it on the functions.
I forward it to the hackers list because the patch is to fix comments.
Also, I'll register it to commitfest.
The past discussion is here.
/messages/by-id/17434-d9f7a064ce2a88a3@postgresql.org
In fact, the result of IsInTransactionBlock does not make senses at
all in pipe-line mode regardless to the fix. ANALYZE could commit all
previous commands in pipelining, and this may not be user expected
behaviour.This seems pretty much isomorphic to the fact that CREATE DATABASE
will commit preceding steps in the pipeline.I am not sure if we can think CREATE DATABASE case and ANLALYZE case
similarly. First, CREATE DATABASE is one of the commands that cannot be
executed inside a transaction block, but ANALYZE can be. So, users would
not be able to know ANALYZE in a pipeline causes a commit from the
documentation. Second, ANALYZE issues a commit internally in an early
stage not only after it finished successfully. For example, even if
ANALYZE is failing because a not-existing column name is specified, it
issues a commit before checking the column name. This makes more hard
to know which statements will be committed and which statements not
committed in a pipeline. Also, as you know, there are other commands
that issue internal commits.That's not great,
I admit; we'd not have designed it like that if we'd had complete
understanding of the behavior at the beginning. But it's acted
like that for a couple of decades now, so changing it seems far
more likely to make people unhappy than happy. The same for
ANALYZE in a pipeline.If the first command in a pipeline is DDL commands such as CREATE
DATABASE, this is allowed and immediately committed after success, as
same as the current behavior. Executing such commands in the middle of
pipeline is not allowed because the pipeline is regarded as "an implicit
transaction block" at that time. Similarly, ANALYZE in the middle of
pipeline can not close and open transaction.I'm not going there. If you can persuade some other committer that
this is worth breaking backward compatibility for, fine; the user
complaints will be their problem.I don't have no idea how to reduce the complexity explained above and
clarify the transactional behavior of pipelining to users other than the
fix I proposed in the previous post. However, I also agree that such
changing may make some people unhappy. If there is no good way and we
would not like to change the behavior, I think it is better to mention
the effects of commands that issue internal commits in the documentation
at least.Regards,
Yugo Nagata--
Yugo NAGATA <nagata@sraoss.co.jp>
--
Yugo NAGATA <nagata@sraoss.co.jp>
Attachments:
comments_on_PreventInTransactionBlock.patchtext/x-diff; name=comments_on_PreventInTransactionBlock.patchDownload
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 50f092d7eb..ee9f0fafdd 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -3455,6 +3455,10 @@ AbortCurrentTransaction(void)
*
* We must also set XACT_FLAGS_NEEDIMMEDIATECOMMIT in MyXactFlags, to ensure
* that postgres.c follows through by committing after the statement is done.
+ * This ensures that the statement that calls this function never be rolled
+ * back if it finishes successfully. Note that if there are preceding statements
+ * in a pipeline, they are committed in the same transaction of the current
+ * statement.
*
* isTopLevel: passed down from ProcessUtility to determine whether we are
* inside a function. (We will always fail if this is false, but it's
@@ -3573,6 +3577,10 @@ CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
* a transaction block than when running as single commands. ANALYZE is
* currently the only example.
*
+ * Returning false ensures that the statement that calls this function never
+ * be rolled back if it finishes successfully. See comments on
+ * PreventInTransactionBlock.
+ *
* isTopLevel: passed down from ProcessUtility to determine whether we are
* inside a function.
*/
Yugo NAGATA <nagata@sraoss.co.jp> writes:
The attached patch tries to add comments explaining it on the functions.
I forward it to the hackers list because the patch is to fix comments.
What do you think of the attached wording?
I don't think the pipeline angle is of concern to anyone who might be
reading these comments with the aim of understanding what guarantees
they have. Perhaps there should be more about that in the user-facing
docs, though.
regards, tom lane
Attachments:
comments_on_PreventInTransactionBlock-2.patchtext/x-diff; charset=us-ascii; name=comments_on_PreventInTransactionBlock-2.patchDownload
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 883d6c0f70..8086b857b9 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -3448,6 +3448,10 @@ AbortCurrentTransaction(void)
* a transaction block, typically because they have non-rollback-able
* side effects or do internal commits.
*
+ * If this routine completes successfully, then the calling statement is
+ * guaranteed that if it completes without error, its results will be
+ * committed immediately.
+ *
* If we have already started a transaction block, issue an error; also issue
* an error if we appear to be running inside a user-defined function (which
* could issue more commands and possibly cause a failure after the statement
@@ -3573,6 +3577,10 @@ CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
* a transaction block than when running as single commands. ANALYZE is
* currently the only example.
*
+ * If this routine returns "false", then the calling statement is
+ * guaranteed that if it completes without error, its results will be
+ * committed immediately.
+ *
* isTopLevel: passed down from ProcessUtility to determine whether we are
* inside a function.
*/
On Sun, 06 Nov 2022 12:54:17 -0500
Tom Lane <tgl@sss.pgh.pa.us> wrote:
Yugo NAGATA <nagata@sraoss.co.jp> writes:
The attached patch tries to add comments explaining it on the functions.
I forward it to the hackers list because the patch is to fix comments.
What do you think of the attached wording?
It looks good to me. That describes the expected behaviour exactly.
I don't think the pipeline angle is of concern to anyone who might be
reading these comments with the aim of understanding what guarantees
they have. Perhaps there should be more about that in the user-facing
docs, though.
I agree with that we don't need to mention pipelining in these comments,
and that we need more in the documentation. I attached a doc patch to add
a mention of commands that do internal commit to the pipelining section.
Also, this adds a reference for the pipelining protocol to the libpq doc.
Regards,
Yugo Nagata
--
Yugo NAGATA <nagata@sraoss.co.jp>
Attachments:
pipeline_doc.patchtext/x-diff; name=pipeline_doc.patchDownload
diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index 3c9bd3d673..727a024e60 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -5058,7 +5058,8 @@ int PQflush(PGconn *conn);
While the pipeline API was introduced in
<productname>PostgreSQL</productname> 14, it is a client-side feature
which doesn't require special server support and works on any server
- that supports the v3 extended query protocol.
+ that supports the v3 extended query protocol; see <xref linkend="protocol-flow-pipelining"/>.
+
</para>
<sect2 id="libpq-pipeline-using">
diff --git a/doc/src/sgml/protocol.sgml b/doc/src/sgml/protocol.sgml
index 5fdd429e05..2edd42d7e9 100644
--- a/doc/src/sgml/protocol.sgml
+++ b/doc/src/sgml/protocol.sgml
@@ -1095,6 +1095,9 @@ SELCT 1/0;<!-- this typo is intentional -->
that cannot be executed inside a transaction block. If one of
these is executed in a pipeline, it will, upon success, force an
immediate commit to preserve database consistency.
+ In addition, execution one of some commands (such as
+ <command>VACUUM ANALYZE</command>) that start/commit transactions
+ internally also can cause an immediate commit even if it fails.
A Sync immediately following one of these has no effect except to
respond with ReadyForQuery.
</para>
Yugo NAGATA <nagata@sraoss.co.jp> writes:
Tom Lane <tgl@sss.pgh.pa.us> wrote:
What do you think of the attached wording?
It looks good to me. That describes the expected behaviour exactly.
Pushed that, then.
I don't think the pipeline angle is of concern to anyone who might be
reading these comments with the aim of understanding what guarantees
they have. Perhaps there should be more about that in the user-facing
docs, though.
I agree with that we don't need to mention pipelining in these comments,
and that we need more in the documentation. I attached a doc patch to add
a mention of commands that do internal commit to the pipelining section.
Also, this adds a reference for the pipelining protocol to the libpq doc.
Hmm ... I don't really find either of these changes to be improvements.
The fact that, say, multi-table ANALYZE uses multiple transactions
seems to me to be a property of that statement, not of the protocol.
regards, tom lane
On 08.08.22 17:21, Yugo NAGATA wrote:
In fact, the result of IsInTransactionBlock does not make senses at
all in pipe-line mode regardless to the fix. ANALYZE could commit all
previous commands in pipelining, and this may not be user expected
behaviour.This seems pretty much isomorphic to the fact that CREATE DATABASE
will commit preceding steps in the pipeline.I am not sure if we can think CREATE DATABASE case and ANLALYZE case
similarly. First, CREATE DATABASE is one of the commands that cannot be
executed inside a transaction block, but ANALYZE can be. So, users would
not be able to know ANALYZE in a pipeline causes a commit from the
documentation. Second, ANALYZE issues a commit internally in an early
stage not only after it finished successfully. For example, even if
ANALYZE is failing because a not-existing column name is specified, it
issues a commit before checking the column name. This makes more hard
to know which statements will be committed and which statements not
committed in a pipeline. Also, as you know, there are other commands
that issue internal commits.
This has broken the following use:
parse: create temporary table t1 (a int) on commit drop
bind
execute
parse: analyze t1
bind
execute
parse: select * from t1
bind
execute
sync
I think the behavior of IsInTransactionBlock() needs to be further
refined to support this. If we are worried about external callers,
maybe we need to provide a separate version. AFAICT, all the callers of
IsInTransactionBlock() over time have been in vacuum/analyze-related
code, so perhaps in master we should just move it there.
Peter Eisentraut <peter.eisentraut@enterprisedb.com> writes:
This has broken the following use:
parse: create temporary table t1 (a int) on commit drop
bind
execute
parse: analyze t1
bind
execute
parse: select * from t1
bind
execute
sync
I think the behavior of IsInTransactionBlock() needs to be further
refined to support this.
Hmm. Maybe the right way to think about this is "if we have completed an
EXECUTE, and not yet received a following SYNC, then report that we are in
a transaction block"? But I'm not sure if that breaks any other cases.
regards, tom lane
On Wed, 09 Nov 2022 11:17:29 -0500
Tom Lane <tgl@sss.pgh.pa.us> wrote:
Yugo NAGATA <nagata@sraoss.co.jp> writes:
Tom Lane <tgl@sss.pgh.pa.us> wrote:
What do you think of the attached wording?
It looks good to me. That describes the expected behaviour exactly.
Pushed that, then.
Thank you.
I don't think the pipeline angle is of concern to anyone who might be
reading these comments with the aim of understanding what guarantees
they have. Perhaps there should be more about that in the user-facing
docs, though.I agree with that we don't need to mention pipelining in these comments,
and that we need more in the documentation. I attached a doc patch to add
a mention of commands that do internal commit to the pipelining section.
Also, this adds a reference for the pipelining protocol to the libpq doc.Hmm ... I don't really find either of these changes to be improvements.
The fact that, say, multi-table ANALYZE uses multiple transactions
seems to me to be a property of that statement, not of the protocol.
Ok. Then, if we want to notice users that commands using internal commits
could unexpectedly close a transaction in pipelining, the proper place is
libpq section?
Regards,
Yugo Nagata
--
Yugo NAGATA <nagata@sraoss.co.jp>
On Wed, 09 Nov 2022 11:38:05 -0500
Tom Lane <tgl@sss.pgh.pa.us> wrote:
Peter Eisentraut <peter.eisentraut@enterprisedb.com> writes:
This has broken the following use:
parse: create temporary table t1 (a int) on commit drop
bind
execute
parse: analyze t1
bind
execute
parse: select * from t1
bind
execute
syncI think the behavior of IsInTransactionBlock() needs to be further
refined to support this.Hmm. Maybe the right way to think about this is "if we have completed an
EXECUTE, and not yet received a following SYNC, then report that we are in
a transaction block"? But I'm not sure if that breaks any other cases.
Or, in that case, regarding it as an implicit transaction if multiple commands
are executed in a pipeline as proposed in [1]/messages/by-id/20220728105134.d5ce51dd756b3149e9b9c52c@sraoss.co.jp could be another solution,
although I have once withdrawn this for not breaking backward compatibility.
Attached is the same patch of [1]/messages/by-id/20220728105134.d5ce51dd756b3149e9b9c52c@sraoss.co.jp.
[1]: /messages/by-id/20220728105134.d5ce51dd756b3149e9b9c52c@sraoss.co.jp
Regards,
Yugo Nagata
--
Yugo NAGATA <nagata@sraoss.co.jp>
Yugo NAGATA <nagata@sraoss.co.jp> writes:
Tom Lane <tgl@sss.pgh.pa.us> wrote:
Hmm. Maybe the right way to think about this is "if we have completed an
EXECUTE, and not yet received a following SYNC, then report that we are in
a transaction block"? But I'm not sure if that breaks any other cases.
Or, in that case, regarding it as an implicit transaction if multiple commands
are executed in a pipeline as proposed in [1] could be another solution,
although I have once withdrawn this for not breaking backward compatibility.
I didn't like that patch then and I still don't. In particular, it's
mighty weird to be issuing BeginImplicitTransactionBlock after we've
already completed one command of the pipeline. If that works without
obvious warts, it's only accidental.
Attached is a draft patch along the lines I speculated about above.
It breaks backwards compatibility in that PreventInTransactionBlock
commands will now be rejected if they're a non-first command in a
pipeline. I think that's okay, and arguably desirable, for HEAD
but I'm pretty uncomfortable about back-patching it.
I thought of a variant idea that I think would significantly reduce
the risk of breaking working applications, which is to restrict things
only in the case of pipelines with previous data-modifying commands.
I tried to implement that by having PreventInTransactionBlock test
if (GetTopTransactionIdIfAny() != InvalidTransactionId)
but it blew up, because we have various (mostly partitioning-related)
DDL commands that run PreventInTransactionBlock only after they've
acquired an exclusive lock on something, and LogAccessExclusiveLock
gets an XID. (That was always a horrid POLA-violating kluge that
would bite us on the rear someday, and now it has. But I can't see
trying to change that in back branches.)
Something could still be salvaged of the idea, perhaps: we could
adjust this patch so that the tests are like
if ((MyXactFlags & XACT_FLAGS_PIPELINING) &&
GetTopTransactionIdIfAny() != InvalidTransactionId)
Maybe that makes it a small enough hazard to be back-patchable.
Another objection that could be raised is the same one I made
already, that !IsInTransactionBlock() doesn't provide the same
guarantee as PreventInTransactionBlock. I'm not too happy
about that either, but given that we know of no other uses of
IsInTransactionBlock besides ANALYZE, maybe it's okay. I'm
not sure it's worth trying to avoid it anyway --- we'd just
end up with a probably-dead backwards compatibility stub.
regards, tom lane
Attachments:
account-for-pipeline-in-PreventInTransactionBlock-1.patchtext/x-diff; charset=us-ascii; name=account-for-pipeline-in-PreventInTransactionBlock-1.patchDownload
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 8086b857b9..b7c7fd9f00 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -3488,6 +3488,16 @@ PreventInTransactionBlock(bool isTopLevel, const char *stmtType)
errmsg("%s cannot run inside a subtransaction",
stmtType)));
+ /*
+ * inside a pipeline that has started an implicit transaction?
+ */
+ if (MyXactFlags & XACT_FLAGS_PIPELINING)
+ ereport(ERROR,
+ (errcode(ERRCODE_ACTIVE_SQL_TRANSACTION),
+ /* translator: %s represents an SQL statement name */
+ errmsg("%s cannot be executed within a pipeline",
+ stmtType)));
+
/*
* inside a function call?
*/
@@ -3577,9 +3587,11 @@ CheckTransactionBlock(bool isTopLevel, bool throwError, const char *stmtType)
* a transaction block than when running as single commands. ANALYZE is
* currently the only example.
*
- * If this routine returns "false", then the calling statement is
- * guaranteed that if it completes without error, its results will be
- * committed immediately.
+ * If this routine returns "false", then the calling statement is allowed
+ * to perform internal transaction-commit-and-start cycles; there is not a
+ * risk of messing up any transaction already in progress. (Note that this
+ * is not the identical guarantee provided by PreventInTransactionBlock,
+ * since we will not force a post-statement commit.)
*
* isTopLevel: passed down from ProcessUtility to determine whether we are
* inside a function.
@@ -3597,6 +3609,9 @@ IsInTransactionBlock(bool isTopLevel)
if (IsSubTransaction())
return true;
+ if (MyXactFlags & XACT_FLAGS_PIPELINING)
+ return true;
+
if (!isTopLevel)
return true;
@@ -3604,13 +3619,6 @@ IsInTransactionBlock(bool isTopLevel)
CurrentTransactionState->blockState != TBLOCK_STARTED)
return true;
- /*
- * If we tell the caller we're not in a transaction block, then inform
- * postgres.c that it had better commit when the statement is done.
- * Otherwise our report could be a lie.
- */
- MyXactFlags |= XACT_FLAGS_NEEDIMMEDIATECOMMIT;
-
return false;
}
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 3082093d1e..f084d9d43b 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -2228,6 +2228,12 @@ exec_execute_message(const char *portal_name, long max_rows)
*/
CommandCounterIncrement();
+ /*
+ * Set XACT_FLAGS_PIPELINING whenever we complete an Execute
+ * message without immediately committing the transaction.
+ */
+ MyXactFlags |= XACT_FLAGS_PIPELINING;
+
/*
* Disable statement timeout whenever we complete an Execute
* message. The next protocol message will start a fresh timeout.
@@ -2243,6 +2249,12 @@ exec_execute_message(const char *portal_name, long max_rows)
/* Portal run not complete, so send PortalSuspended */
if (whereToSendOutput == DestRemote)
pq_putemptymessage('s');
+
+ /*
+ * Set XACT_FLAGS_PIPELINING whenever we suspend an Execute message,
+ * too.
+ */
+ MyXactFlags |= XACT_FLAGS_PIPELINING;
}
/*
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index c604ee11f8..898b065b4f 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -113,6 +113,13 @@ extern PGDLLIMPORT int MyXactFlags;
*/
#define XACT_FLAGS_NEEDIMMEDIATECOMMIT (1U << 2)
+/*
+ * XACT_FLAGS_PIPELINING - set when we complete an extended-query-protocol
+ * Execute message. This is useful for detecting that an implicit transaction
+ * block has been created via pipelining.
+ */
+#define XACT_FLAGS_PIPELINING (1U << 3)
+
/*
* start- and end-of-transaction callbacks for dynamically loaded modules
*/
On Thu, 10 Nov 2022 15:33:37 -0500
Tom Lane <tgl@sss.pgh.pa.us> wrote:
Yugo NAGATA <nagata@sraoss.co.jp> writes:
Tom Lane <tgl@sss.pgh.pa.us> wrote:
Hmm. Maybe the right way to think about this is "if we have completed an
EXECUTE, and not yet received a following SYNC, then report that we are in
a transaction block"? But I'm not sure if that breaks any other cases.Or, in that case, regarding it as an implicit transaction if multiple commands
are executed in a pipeline as proposed in [1] could be another solution,
although I have once withdrawn this for not breaking backward compatibility.I didn't like that patch then and I still don't. In particular, it's
mighty weird to be issuing BeginImplicitTransactionBlock after we've
already completed one command of the pipeline. If that works without
obvious warts, it's only accidental.
Ok, I agree with that ugly part of my proposal, so I withdraw it again
if there is another acceptable solution.
Attached is a draft patch along the lines I speculated about above.
It breaks backwards compatibility in that PreventInTransactionBlock
commands will now be rejected if they're a non-first command in a
pipeline. I think that's okay, and arguably desirable, for HEAD
That patch seems good to me. It fixes the problem reported from
Peter Eisentraut. Also, this seems simple way to define what is
"pipelining" in the code.
but I'm pretty uncomfortable about back-patching it.
If we want to fix the ANALYZE problem without breaking backward
compatibility for back-patching, maybe we could fix only
IsInTransactionBlock and remain PreventInTransactionBlock as it is.
Obviously, this will break consistency of guarantee between those
functions, but if we are abandoning it eventually, it might be okay.
Anyway, if we change PreventInTransactionBlock to forbid execute
some DDLs in a pipeline, we also need to modify the doc.
I thought of a variant idea that I think would significantly reduce
the risk of breaking working applications, which is to restrict things
only in the case of pipelines with previous data-modifying commands.
I tried to implement that by having PreventInTransactionBlock testif (GetTopTransactionIdIfAny() != InvalidTransactionId)
but it blew up, because we have various (mostly partitioning-related)
DDL commands that run PreventInTransactionBlock only after they've
acquired an exclusive lock on something, and LogAccessExclusiveLock
gets an XID. (That was always a horrid POLA-violating kluge that
would bite us on the rear someday, and now it has. But I can't see
trying to change that in back branches.)Something could still be salvaged of the idea, perhaps: we could
adjust this patch so that the tests are likeif ((MyXactFlags & XACT_FLAGS_PIPELINING) &&
GetTopTransactionIdIfAny() != InvalidTransactionId)Maybe that makes it a small enough hazard to be back-patchable.
In this case, DDLs that call PreventInTransactionBlock would be
allowed in a pipeline if any data-modifying commands are yet executed.
This specification is a bit complicated and I'm not sure how many
cases are salvaged by this, but I agree that this will reduce the
hazard of breaking backward-compatibility.
Another objection that could be raised is the same one I made
already, that !IsInTransactionBlock() doesn't provide the same
guarantee as PreventInTransactionBlock. I'm not too happy
about that either, but given that we know of no other uses of
IsInTransactionBlock besides ANALYZE, maybe it's okay. I'm
not sure it's worth trying to avoid it anyway --- we'd just
end up with a probably-dead backwards compatibility stub.
One way to fix the ANALYZE problem while maintaining the
backward-compatibility for third-party tools using IsInTransactionBlock
might be to rename the function (ex. IsInTransactionBlockWithoutCommit)
and define a new function with the original name.
For example, define the followin for third party tools,
bool IsInTransactionBlock()
{
if (!IsInTransactionBlockWithoutCommit())
{
MyXactFlags |= XACT_FLAGS_NEEDIMMEDIATECOMMIT;
return false;
}
else
return true;
}
and use IsInTransactionBlockWithoutCommit in ANALYZE.
Regards,
Yugo Nagata
--
Yugo NAGATA <nagata@sraoss.co.jp>
Attached is a draft patch along the lines I speculated about above.
It breaks backwards compatibility in that PreventInTransactionBlock
commands will now be rejected if they're a non-first command in a
pipeline. I think that's okay, and arguably desirable, for HEAD
but I'm pretty uncomfortable about back-patching it.
I attempted to run these using HEAD, and it fails:
parse: create temporary table t1 (a int) on commit drop
bind
execute
parse: analyze t1
bind
execute
parse: select * from t1
bind
execute
sync
It then works fine after applying your patch!
Just for some context, this was brought by Peter E. based on an issue
reported by a customer. They are using PostgreSQL 11, and the issue
was observed after upgrading to PostgreSQL 11.17, which includes the
commit 9e3e1ac458abcda5aa03fa2a136e6fa492d58bd6. As a workaround
they downgraded the binaries to 11.16.
It would be great if we can back-patch this to all supported versions,
as the issue itself is currently affecting them all.
Regards,
Israel.
Israel Barth Rubio <barthisrael@gmail.com> writes:
It would be great if we can back-patch this to all supported versions,
as the issue itself is currently affecting them all.
In my mind, this is waiting for Peter to opine on whether it satisfies
his concern.
I'm also looking for input on whether to reject if
if ((MyXactFlags & XACT_FLAGS_PIPELINING) &&
GetTopTransactionIdIfAny() != InvalidTransactionId)
rather than just the bare
if (MyXactFlags & XACT_FLAGS_PIPELINING)
tests in the patch-as-posted.
regards, tom lane
On 25.11.22 18:06, Tom Lane wrote:
Israel Barth Rubio <barthisrael@gmail.com> writes:
It would be great if we can back-patch this to all supported versions,
as the issue itself is currently affecting them all.In my mind, this is waiting for Peter to opine on whether it satisfies
his concern.
The case I was working on is the same as Israel's. He has confirmed
that this fixes the issue we have been working on.
Peter Eisentraut <peter.eisentraut@enterprisedb.com> writes:
On 25.11.22 18:06, Tom Lane wrote:
In my mind, this is waiting for Peter to opine on whether it satisfies
his concern.
The case I was working on is the same as Israel's. He has confirmed
that this fixes the issue we have been working on.
OK, I'll make this happen soon.
regards, tom lane
I wrote:
Peter Eisentraut <peter.eisentraut@enterprisedb.com> writes:
The case I was working on is the same as Israel's. He has confirmed
that this fixes the issue we have been working on.
OK, I'll make this happen soon.
Pushed. I left out the idea of making this conditional on whether
any preceding command had performed data modification, as that seemed
to greatly complicate the explanation (since "have we performed any
data modification" is a rather squishy question from a user's viewpoint).
regards, tom lane