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
From a7b7c4094a54f9b217332809b1abfb521a4b994d Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pete...@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.c
index 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.out
index 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.sql
index 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 via email to