mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-31 12:47:22 +02:00
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
This commit is contained in:
parent
c905b67b79
commit
6bf278df89
@ -86,6 +86,8 @@ typedef struct FixedParallelState
|
|||||||
PGPROC *parallel_master_pgproc;
|
PGPROC *parallel_master_pgproc;
|
||||||
pid_t parallel_master_pid;
|
pid_t parallel_master_pid;
|
||||||
BackendId parallel_master_backend_id;
|
BackendId parallel_master_backend_id;
|
||||||
|
TimestampTz xact_ts;
|
||||||
|
TimestampTz stmt_ts;
|
||||||
|
|
||||||
/* Mutex protects remaining fields. */
|
/* Mutex protects remaining fields. */
|
||||||
slock_t mutex;
|
slock_t mutex;
|
||||||
@ -321,6 +323,8 @@ InitializeParallelDSM(ParallelContext *pcxt)
|
|||||||
fps->parallel_master_pgproc = MyProc;
|
fps->parallel_master_pgproc = MyProc;
|
||||||
fps->parallel_master_pid = MyProcPid;
|
fps->parallel_master_pid = MyProcPid;
|
||||||
fps->parallel_master_backend_id = MyBackendId;
|
fps->parallel_master_backend_id = MyBackendId;
|
||||||
|
fps->xact_ts = GetCurrentTransactionStartTimestamp();
|
||||||
|
fps->stmt_ts = GetCurrentStatementStartTimestamp();
|
||||||
SpinLockInit(&fps->mutex);
|
SpinLockInit(&fps->mutex);
|
||||||
fps->last_xlog_end = 0;
|
fps->last_xlog_end = 0;
|
||||||
shm_toc_insert(pcxt->toc, PARALLEL_KEY_FIXED, fps);
|
shm_toc_insert(pcxt->toc, PARALLEL_KEY_FIXED, fps);
|
||||||
@ -1303,6 +1307,13 @@ ParallelWorkerMain(Datum main_arg)
|
|||||||
fps->parallel_master_pid))
|
fps->parallel_master_pid))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Restore transaction and statement start-time timestamps. This must
|
||||||
|
* happen before anything that would start a transaction, else asserts in
|
||||||
|
* xact.c will fire.
|
||||||
|
*/
|
||||||
|
SetParallelStartTimestamps(fps->xact_ts, fps->stmt_ts);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Identify the entry point to be called. In theory this could result in
|
* Identify the entry point to be called. In theory this could result in
|
||||||
* loading an additional library, though most likely the entry point is in
|
* loading an additional library, though most likely the entry point is in
|
||||||
|
@ -693,6 +693,22 @@ GetCurrentCommandId(bool used)
|
|||||||
return currentCommandId;
|
return currentCommandId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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
|
* GetCurrentTransactionStartTimestamp
|
||||||
*/
|
*/
|
||||||
@ -727,11 +743,17 @@ GetCurrentTransactionStopTimestamp(void)
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* SetCurrentStatementStartTimestamp
|
* SetCurrentStatementStartTimestamp
|
||||||
|
*
|
||||||
|
* In a parallel worker, this should already have been provided by a call
|
||||||
|
* to SetParallelStartTimestamps().
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
SetCurrentStatementStartTimestamp(void)
|
SetCurrentStatementStartTimestamp(void)
|
||||||
{
|
{
|
||||||
stmtStartTimestamp = GetCurrentTimestamp();
|
if (!IsParallelWorker())
|
||||||
|
stmtStartTimestamp = GetCurrentTimestamp();
|
||||||
|
else
|
||||||
|
Assert(stmtStartTimestamp != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -1886,10 +1908,16 @@ StartTransaction(void)
|
|||||||
/*
|
/*
|
||||||
* set transaction_timestamp() (a/k/a now()). We want this to be the same
|
* 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
|
* as the first command's statement_timestamp(), so don't do a fresh
|
||||||
* GetCurrentTimestamp() call (which'd be expensive anyway). Also, mark
|
* GetCurrentTimestamp() call (which'd be expensive anyway). In a
|
||||||
* xactStopTimestamp as unset.
|
* 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;
|
xactStopTimestamp = 0;
|
||||||
pgstat_report_xact_timestamp(xactStartTimestamp);
|
pgstat_report_xact_timestamp(xactStartTimestamp);
|
||||||
|
|
||||||
|
@ -359,6 +359,7 @@ extern SubTransactionId GetCurrentSubTransactionId(void);
|
|||||||
extern void MarkCurrentTransactionIdLoggedIfAny(void);
|
extern void MarkCurrentTransactionIdLoggedIfAny(void);
|
||||||
extern bool SubTransactionIsActive(SubTransactionId subxid);
|
extern bool SubTransactionIsActive(SubTransactionId subxid);
|
||||||
extern CommandId GetCurrentCommandId(bool used);
|
extern CommandId GetCurrentCommandId(bool used);
|
||||||
|
extern void SetParallelStartTimestamps(TimestampTz xact_ts, TimestampTz stmt_ts);
|
||||||
extern TimestampTz GetCurrentTransactionStartTimestamp(void);
|
extern TimestampTz GetCurrentTransactionStartTimestamp(void);
|
||||||
extern TimestampTz GetCurrentStatementStartTimestamp(void);
|
extern TimestampTz GetCurrentStatementStartTimestamp(void);
|
||||||
extern TimestampTz GetCurrentTransactionStopTimestamp(void);
|
extern TimestampTz GetCurrentTransactionStopTimestamp(void);
|
||||||
|
Loading…
Reference in New Issue
Block a user