diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2018-10-06 12:00:10 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2018-10-06 12:00:10 -0400 |
commit | 58454d0bb07c222fae19c4ced38beef36e27015b (patch) | |
tree | 0362c7b12af8f305b959168898671de5f4a4e695 /src/backend/access/transam/xact.c | |
parent | 142cfd3cd82efd4d661f8a8d88960602a439a744 (diff) | |
download | postgresql-58454d0bb07c222fae19c4ced38beef36e27015b.tar.gz postgresql-58454d0bb07c222fae19c4ced38beef36e27015b.zip |
Propagate xactStartTimestamp and stmtStartTimestamp to parallel workers.
Previously, a worker process would establish values for these based on
its own start time. In v10 and up, this can trivially be shown to cause
misbehavior of transaction_timestamp(), timestamp_in(), and related
functions which are (perhaps unwisely?) marked parallel-safe. It seems
likely that other behaviors might diverge from what happens in the parent
as well.
It's not as trivial to demonstrate problems in 9.6 or 9.5, but I'm sure
it's still possible, so back-patch to all branches containing parallel
worker infrastructure.
In HEAD only, mark now() and statement_timestamp() as parallel-safe
(other affected functions already were). While in theory we could
still squeeze that change into v11, it doesn't seem important enough
to force a last-minute catversion bump.
Konstantin Knizhnik, whacked around a bit by me
Discussion: https://postgr.es/m/6406dbd2-5d37-4cb6-6eb2-9c44172c7e7c@postgrespro.ru
Diffstat (limited to 'src/backend/access/transam/xact.c')
-rw-r--r-- | src/backend/access/transam/xact.c | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index 9004e38e6d4..b028b318e5c 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -702,6 +702,22 @@ GetCurrentCommandId(bool used) } /* + * SetParallelStartTimestamps + * + * In a parallel worker, we should inherit the parent transaction's + * timestamps rather than setting our own. The parallel worker + * infrastructure must call this to provide those values before + * calling StartTransaction() or SetCurrentStatementStartTimestamp(). + */ +void +SetParallelStartTimestamps(TimestampTz xact_ts, TimestampTz stmt_ts) +{ + Assert(IsParallelWorker()); + xactStartTimestamp = xact_ts; + stmtStartTimestamp = stmt_ts; +} + +/* * GetCurrentTransactionStartTimestamp */ TimestampTz @@ -735,11 +751,17 @@ GetCurrentTransactionStopTimestamp(void) /* * SetCurrentStatementStartTimestamp + * + * In a parallel worker, this should already have been provided by a call + * to SetParallelStartTimestamps(). */ void SetCurrentStatementStartTimestamp(void) { - stmtStartTimestamp = GetCurrentTimestamp(); + if (!IsParallelWorker()) + stmtStartTimestamp = GetCurrentTimestamp(); + else + Assert(stmtStartTimestamp != 0); } /* @@ -1893,10 +1915,16 @@ StartTransaction(void) /* * 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. + * GetCurrentTimestamp() call (which'd be expensive anyway). In a + * parallel worker, this should already have been provided by a call to + * SetParallelStartTimestamps(). + * + * Also, mark xactStopTimestamp as unset. */ - xactStartTimestamp = stmtStartTimestamp; + if (!IsParallelWorker()) + xactStartTimestamp = stmtStartTimestamp; + else + Assert(xactStartTimestamp != 0); xactStopTimestamp = 0; pgstat_report_xact_timestamp(xactStartTimestamp); |