From: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
To: Andres Freund <andres@anarazel.de>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Cc: Alvaro Herrera <alvherre@2ndquadrant.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Bruce Momjian <bruce@momjian.us>
Cc: Michael Paquier <michael@paquier.xyz>
Subject: Re: transction_timestamp() inside of procedures
Date: Fri, 5 Oct 2018 13:28:54 +0200
Message-ID: <38e36919-8bee-c717-be99-91137ad28aaf@2ndquadrant.com> (raw)
In-Reply-To: <20181002145853.wvhcb7pbrzgjyxns@alap3.anarazel.de>
References: <20180926155443.u4zh7jactzflys3z@alvherre.pgsql>
<ec816bd0-0357-23a9-bea2-3ef1619eb340@2ndquadrant.com>
<e2ad8aaf-e1d8-499e-4de0-e3616f8d908d@2ndquadrant.com>
<20181002145853.wvhcb7pbrzgjyxns@alap3.anarazel.de>
On 02/10/2018 16:58, Andres Freund wrote:
> It's a bit weird to make this decision based on these two timestamps
> differing. For one, it only indirectly seems to be guaranteed that
> xactStartTimestamp is even set to anything here (to 0 by virtue of being
> a global var).
Maybe but it seems to be the simplest way without doing major surgery to
all this code.
Attached is an updated patch with a test case.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Attachments:v2-0001-Advance-transaction-timestamp-in-intra-procedure-.patch (text/plain, 3.9K) download
From a7b7c4094a54f9b217332809b1abfb521a4b994d Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Fri, 5 Oct 2018 12:24:34 +0200
Subject: [PATCH v2] Advance transaction timestamp in intra-procedure
transactions
---
src/backend/access/transam/xact.c | 19 +++++++-----
.../src/expected/plpgsql_transaction.out | 28 +++++++++++++++++
.../plpgsql/src/sql/plpgsql_transaction.sql | 31 +++++++++++++++++++
3 files changed, 71 insertions(+), 7 deletions(-)
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.cindex 9aa63c8792..245735420c 100644--- a/src/backend/access/transam/xact.c+++ b/src/backend/access/transam/xact.c@@ -1884,14 +1884,19 @@ StartTransaction(void)
TRACE_POSTGRESQL_TRANSACTION_START(vxid.localTransactionId);
/*
- * set transaction_timestamp() (a/k/a now()). We want this to be the same- * as the first command's statement_timestamp(), so don't do a fresh- * GetCurrentTimestamp() call (which'd be expensive anyway). Also, mark- * xactStopTimestamp as unset.- */- xactStartTimestamp = stmtStartTimestamp;- xactStopTimestamp = 0;+ * set transaction_timestamp() (a/k/a now()). Normally, we want this to+ * be the same as the first command's statement_timestamp(), so don't do a+ * fresh GetCurrentTimestamp() call (which'd be expensive anyway). But+ * for transactions started inside statements (e.g., procedure calls), we+ * want to advance the timestamp.+ */+ if (xactStartTimestamp < stmtStartTimestamp)+ xactStartTimestamp = stmtStartTimestamp;+ else+ xactStartTimestamp = GetCurrentTimestamp();
pgstat_report_xact_timestamp(xactStartTimestamp);
+ /* Mark xactStopTimestamp as unset. */+ xactStopTimestamp = 0;
/*
* initialize current transaction state fields
diff --git a/src/pl/plpgsql/src/expected/plpgsql_transaction.out b/src/pl/plpgsql/src/expected/plpgsql_transaction.outindex 77a83adab5..71e56300bc 100644--- a/src/pl/plpgsql/src/expected/plpgsql_transaction.out+++ b/src/pl/plpgsql/src/expected/plpgsql_transaction.out@@ -493,6 +493,34 @@ CALL transaction_test10b(10);
9
(1 row)
+-- transaction timestamp vs. statement timestamp+CREATE PROCEDURE transaction_test11()+LANGUAGE plpgsql+AS $$+DECLARE+ s1 timestamp with time zone;+ s2 timestamp with time zone;+ s3 timestamp with time zone;+ t1 timestamp with time zone;+ t2 timestamp with time zone;+ t3 timestamp with time zone;+BEGIN+ s1 := statement_timestamp();+ t1 := transaction_timestamp();+ ASSERT s1 = t1;+ COMMIT;+ s2 := statement_timestamp();+ t2 := transaction_timestamp();+ ASSERT s2 = s1;+ ASSERT t2 > t1;+ ROLLBACK;+ s3 := statement_timestamp();+ t3 := transaction_timestamp();+ ASSERT s3 = s1;+ ASSERT t3 > t2;+END;+$$;+CALL transaction_test11();
DROP TABLE test1;
DROP TABLE test2;
DROP TABLE test3;
diff --git a/src/pl/plpgsql/src/sql/plpgsql_transaction.sql b/src/pl/plpgsql/src/sql/plpgsql_transaction.sqlindex 0ed9ab873a..e2089f56ad 100644--- a/src/pl/plpgsql/src/sql/plpgsql_transaction.sql+++ b/src/pl/plpgsql/src/sql/plpgsql_transaction.sql@@ -412,6 +412,37 @@ CREATE PROCEDURE transaction_test10b(INOUT x int)
CALL transaction_test10b(10);
+-- transaction timestamp vs. statement timestamp+CREATE PROCEDURE transaction_test11()+LANGUAGE plpgsql+AS $$+DECLARE+ s1 timestamp with time zone;+ s2 timestamp with time zone;+ s3 timestamp with time zone;+ t1 timestamp with time zone;+ t2 timestamp with time zone;+ t3 timestamp with time zone;+BEGIN+ s1 := statement_timestamp();+ t1 := transaction_timestamp();+ ASSERT s1 = t1;+ COMMIT;+ s2 := statement_timestamp();+ t2 := transaction_timestamp();+ ASSERT s2 = s1;+ ASSERT t2 > t1;+ ROLLBACK;+ s3 := statement_timestamp();+ t3 := transaction_timestamp();+ ASSERT s3 = s1;+ ASSERT t3 > t2;+END;+$$;++CALL transaction_test11();++
DROP TABLE test1;
DROP TABLE test2;
DROP TABLE test3;
base-commit: ff347f8aff04865680c19ffc818460bb2afaad5b
--
2.19.0
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-hackers@postgresql.org
Cc: peter.eisentraut@2ndquadrant.com, andres@anarazel.de, alvherre@2ndquadrant.com, tgl@sss.pgh.pa.us, bruce@momjian.us, michael@paquier.xyz
Subject: Re: transction_timestamp() inside of procedures
In-Reply-To: <38e36919-8bee-c717-be99-91137ad28aaf@2ndquadrant.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox