Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit07ee62c

Browse files
committed
Propagate xactStartTimestamp and stmtStartTimestamp to parallel workers.
Previously, a worker process would establish values for these based onits own start time. In v10 and up, this can trivially be shown to causemisbehavior of transaction_timestamp(), timestamp_in(), and relatedfunctions which are (perhaps unwisely?) marked parallel-safe. It seemslikely that other behaviors might diverge from what happens in the parentas well.It's not as trivial to demonstrate problems in 9.6 or 9.5, but I'm sureit's still possible, so back-patch to all branches containing parallelworker infrastructure.In HEAD only, mark now() and statement_timestamp() as parallel-safe(other affected functions already were). While in theory we couldstill squeeze that change into v11, it doesn't seem important enoughto force a last-minute catversion bump.Konstantin Knizhnik, whacked around a bit by meDiscussion:https://postgr.es/m/6406dbd2-5d37-4cb6-6eb2-9c44172c7e7c@postgrespro.ru
1 parente954a72 commit07ee62c

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

‎src/backend/access/transam/parallel.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ typedef struct FixedParallelState
8787
PGPROC*parallel_master_pgproc;
8888
pid_tparallel_master_pid;
8989
BackendIdparallel_master_backend_id;
90+
TimestampTzxact_ts;
91+
TimestampTzstmt_ts;
9092

9193
/* Mutex protects remaining fields. */
9294
slock_tmutex;
@@ -318,6 +320,8 @@ InitializeParallelDSM(ParallelContext *pcxt)
318320
fps->parallel_master_pgproc=MyProc;
319321
fps->parallel_master_pid=MyProcPid;
320322
fps->parallel_master_backend_id=MyBackendId;
323+
fps->xact_ts=GetCurrentTransactionStartTimestamp();
324+
fps->stmt_ts=GetCurrentStatementStartTimestamp();
321325
SpinLockInit(&fps->mutex);
322326
fps->last_xlog_end=0;
323327
shm_toc_insert(pcxt->toc,PARALLEL_KEY_FIXED,fps);
@@ -1311,6 +1315,13 @@ ParallelWorkerMain(Datum main_arg)
13111315
fps->parallel_master_pid))
13121316
return;
13131317

1318+
/*
1319+
* Restore transaction and statement start-time timestamps. This must
1320+
* happen before anything that would start a transaction, else asserts in
1321+
* xact.c will fire.
1322+
*/
1323+
SetParallelStartTimestamps(fps->xact_ts,fps->stmt_ts);
1324+
13141325
/*
13151326
* Identify the entry point to be called. In theory this could result in
13161327
* loading an additional library, though most likely the entry point is in

‎src/backend/access/transam/xact.c

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,22 @@ GetCurrentCommandId(bool used)
674674
returncurrentCommandId;
675675
}
676676

677+
/*
678+
*SetParallelStartTimestamps
679+
*
680+
* In a parallel worker, we should inherit the parent transaction's
681+
* timestamps rather than setting our own. The parallel worker
682+
* infrastructure must call this to provide those values before
683+
* calling StartTransaction() or SetCurrentStatementStartTimestamp().
684+
*/
685+
void
686+
SetParallelStartTimestamps(TimestampTzxact_ts,TimestampTzstmt_ts)
687+
{
688+
Assert(IsParallelWorker());
689+
xactStartTimestamp=xact_ts;
690+
stmtStartTimestamp=stmt_ts;
691+
}
692+
677693
/*
678694
*GetCurrentTransactionStartTimestamp
679695
*/
@@ -708,11 +724,17 @@ GetCurrentTransactionStopTimestamp(void)
708724

709725
/*
710726
*SetCurrentStatementStartTimestamp
727+
*
728+
* In a parallel worker, this should already have been provided by a call
729+
* to SetParallelStartTimestamps().
711730
*/
712731
void
713732
SetCurrentStatementStartTimestamp(void)
714733
{
715-
stmtStartTimestamp=GetCurrentTimestamp();
734+
if (!IsParallelWorker())
735+
stmtStartTimestamp=GetCurrentTimestamp();
736+
else
737+
Assert(stmtStartTimestamp!=0);
716738
}
717739

718740
/*
@@ -1867,10 +1889,16 @@ StartTransaction(void)
18671889
/*
18681890
* set transaction_timestamp() (a/k/a now()). We want this to be the same
18691891
* as the first command's statement_timestamp(), so don't do a fresh
1870-
* GetCurrentTimestamp() call (which'd be expensive anyway). Also, mark
1871-
* xactStopTimestamp as unset.
1892+
* GetCurrentTimestamp() call (which'd be expensive anyway). In a
1893+
* parallel worker, this should already have been provided by a call to
1894+
* SetParallelStartTimestamps().
1895+
*
1896+
* Also, mark xactStopTimestamp as unset.
18721897
*/
1873-
xactStartTimestamp=stmtStartTimestamp;
1898+
if (!IsParallelWorker())
1899+
xactStartTimestamp=stmtStartTimestamp;
1900+
else
1901+
Assert(xactStartTimestamp!=0);
18741902
xactStopTimestamp=0;
18751903
pgstat_report_xact_timestamp(xactStartTimestamp);
18761904

‎src/include/access/xact.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ extern SubTransactionId GetCurrentSubTransactionId(void);
359359
externvoidMarkCurrentTransactionIdLoggedIfAny(void);
360360
externboolSubTransactionIsActive(SubTransactionIdsubxid);
361361
externCommandIdGetCurrentCommandId(boolused);
362+
externvoidSetParallelStartTimestamps(TimestampTzxact_ts,TimestampTzstmt_ts);
362363
externTimestampTzGetCurrentTransactionStartTimestamp(void);
363364
externTimestampTzGetCurrentStatementStartTimestamp(void);
364365
externTimestampTzGetCurrentTransactionStopTimestamp(void);

‎src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/*yyyymmddN */
56-
#defineCATALOG_VERSION_NO201810051
56+
#defineCATALOG_VERSION_NO201810061
5757

5858
#endif

‎src/include/catalog/pg_proc.dat

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,7 +2073,8 @@
20732073
{ oid => '1365', descr => 'make ACL item',
20742074
proname => 'makeaclitem', prorettype => 'aclitem',
20752075
proargtypes => 'oid oid text bool', prosrc => 'makeaclitem' },
2076-
{ oid => '3943', descr => 'show hardwired default privileges, primarily for use by the information schema',
2076+
{ oid => '3943',
2077+
descr => 'show hardwired default privileges, primarily for use by the information schema',
20772078
proname => 'acldefault', prorettype => '_aclitem', proargtypes => 'char oid',
20782079
prosrc => 'acldefault_sql' },
20792080
{ oid => '1689',
@@ -2595,13 +2596,13 @@
25952596
proname => 'timetzdate_pl', prolang => '14', prorettype => 'timestamptz',
25962597
proargtypes => 'timetz date', prosrc => 'select ($2 + $1)' },
25972598
{ oid => '1299', descr => 'current transaction time',
2598-
proname => 'now', provolatile => 's',proparallel => 'r',
2599-
prorettype => 'timestamptz',proargtypes => '', prosrc => 'now' },
2599+
proname => 'now', provolatile => 's',prorettype => 'timestamptz',
2600+
proargtypes => '', prosrc => 'now' },
26002601
{ oid => '2647', descr => 'current transaction time',
26012602
proname => 'transaction_timestamp', provolatile => 's',
26022603
prorettype => 'timestamptz', proargtypes => '', prosrc => 'now' },
26032604
{ oid => '2648', descr => 'current statement time',
2604-
proname => 'statement_timestamp', provolatile => 's', proparallel => 'r',
2605+
proname => 'statement_timestamp', provolatile => 's',
26052606
prorettype => 'timestamptz', proargtypes => '',
26062607
prosrc => 'statement_timestamp' },
26072608
{ oid => '2649', descr => 'current clock time',
@@ -10208,7 +10209,8 @@
1020810209
proname => 'pg_ls_tmpdir', procost => '10', prorows => '20', proretset => 't',
1020910210
provolatile => 'v', prorettype => 'record', proargtypes => 'oid',
1021010211
proallargtypes => '{oid,text,int8,timestamptz}', proargmodes => '{i,o,o,o}',
10211-
proargnames => '{tablespace,name,size,modification}', prosrc => 'pg_ls_tmpdir_1arg' },
10212+
proargnames => '{tablespace,name,size,modification}',
10213+
prosrc => 'pg_ls_tmpdir_1arg' },
1021210214

1021310215
# hash partitioning constraint function
1021410216
{ oid => '5028', descr => 'hash partition CHECK constraint',

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp