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

Commit27c3e3d

Browse files
committed
Remove redundant gettimeofday() calls to the extent practical without
changing semantics too much. statement_timestamp is now set immediatelyupon receipt of a client command message, and the various places that usedto do their own gettimeofday() calls to mark command startup are referencedto that instead. I have also made stats_command_string use that samevalue for pg_stat_activity.query_start for both the command itself andits eventual replacement by <IDLE> or <idle in transaction>. There wassome debate about that, but no argument that seemed convincing enough tojustify an extra gettimeofday() call.
1 parent47a37ae commit27c3e3d

File tree

11 files changed

+268
-225
lines changed

11 files changed

+268
-225
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.220 2006/04/25 00:25:17 momjian Exp $
13+
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.221 2006/06/20 22:51:59 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -167,9 +167,10 @@ static SubTransactionId currentSubTransactionId;
167167
staticCommandIdcurrentCommandId;
168168

169169
/*
170-
* This is the value of now(), ie, the transaction start time.
171-
* This does not change as we enter and exit subtransactions, so we don't
172-
* keep it inside the TransactionState stack.
170+
* xactStartTimestamp is the value of transaction_timestamp().
171+
* stmtStartTimestamp is the value of statement_timestamp().
172+
* These do not change as we enter and exit subtransactions, so we don't
173+
* keep them inside the TransactionState stack.
173174
*/
174175
staticTimestampTzxactStartTimestamp;
175176
staticTimestampTzstmtStartTimestamp;
@@ -1386,7 +1387,9 @@ StartTransaction(void)
13861387
XactLockTableInsert(s->transactionId);
13871388

13881389
/*
1389-
* now() and statement_timestamp() should be the same time
1390+
* set transaction_timestamp() (a/k/a now()). We want this to be the
1391+
* same as the first command's statement_timestamp(), so don't do a
1392+
* fresh GetCurrentTimestamp() call (which'd be expensive anyway).
13901393
*/
13911394
xactStartTimestamp=stmtStartTimestamp;
13921395

‎src/backend/commands/prepare.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Copyright (c) 2002-2006, PostgreSQL Global Development Group
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.52 2006/04/25 14:11:54 momjian Exp $
13+
* $PostgreSQL: pgsql/src/backend/commands/prepare.c,v 1.53 2006/06/20 22:51:59 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -373,7 +373,7 @@ StorePreparedStatement(const char *stmt_name,
373373
entry->plan_list=plan_list;
374374
entry->argtype_list=argtype_list;
375375
entry->context=entrycxt;
376-
entry->prepare_time=GetCurrentTimestamp();
376+
entry->prepare_time=GetCurrentStatementStartTimestamp();
377377
entry->from_sql=from_sql;
378378

379379
MemoryContextSwitchTo(oldcxt);

‎src/backend/postmaster/pgstat.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
*Copyright (c) 2001-2006, PostgreSQL Global Development Group
1515
*
16-
*$PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.129 2006/06/19 01:51:21 tgl Exp $
16+
*$PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.130 2006/06/20 22:52:00 tgl Exp $
1717
* ----------
1818
*/
1919
#include"postgres.h"
@@ -1368,8 +1368,14 @@ pgstat_bestart(void)
13681368
/*
13691369
* To minimize the time spent modifying the entry, fetch all the
13701370
* needed data first.
1371+
*
1372+
* If we have a MyProcPort, use its session start time (for consistency,
1373+
* and to save a kernel call).
13711374
*/
1372-
proc_start_timestamp=GetCurrentTimestamp();
1375+
if (MyProcPort)
1376+
proc_start_timestamp=MyProcPort->SessionStartTime;
1377+
else
1378+
proc_start_timestamp=GetCurrentTimestamp();
13731379
userid=GetSessionUserId();
13741380

13751381
/*
@@ -1464,7 +1470,7 @@ pgstat_report_activity(const char *cmd_str)
14641470
* To minimize the time spent modifying the entry, fetch all the
14651471
* needed data first.
14661472
*/
1467-
start_timestamp=GetCurrentTimestamp();
1473+
start_timestamp=GetCurrentStatementStartTimestamp();
14681474

14691475
len=strlen(cmd_str);
14701476
len=pg_mbcliplen(cmd_str,len,PGBE_ACTIVITY_SIZE-1);

‎src/backend/postmaster/postmaster.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*
3838
*
3939
* IDENTIFICATION
40-
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.487 2006/06/19 01:51:21 tgl Exp $
40+
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.488 2006/06/20 22:52:00 tgl Exp $
4141
*
4242
* NOTES
4343
*
@@ -99,7 +99,6 @@
9999
#include"commands/async.h"
100100
#include"lib/dllist.h"
101101
#include"libpq/auth.h"
102-
#include"libpq/crypt.h"
103102
#include"libpq/libpq.h"
104103
#include"libpq/pqcomm.h"
105104
#include"libpq/pqsignal.h"
@@ -2609,8 +2608,9 @@ BackendInitialize(Port *port)
26092608

26102609
ClientAuthInProgress= true;/* limit visibility of log messages */
26112610

2612-
/* save start time for end of session reporting */
2613-
gettimeofday(&(port->session_start),NULL);
2611+
/* save process start time */
2612+
port->SessionStartTime=GetCurrentTimestamp();
2613+
port->session_start=timestamptz_to_time_t(port->SessionStartTime);
26142614

26152615
/* set these to empty in case they are needed before we set them up */
26162616
port->remote_host="";
@@ -2749,6 +2749,8 @@ BackendRun(Port *port)
27492749
char**av;
27502750
intmaxac;
27512751
intac;
2752+
longsecs;
2753+
intusecs;
27522754
charprotobuf[32];
27532755
inti;
27542756

@@ -2758,7 +2760,9 @@ BackendRun(Port *port)
27582760
* a new random sequence in the random() library function.
27592761
*/
27602762
random_seed=0;
2761-
srandom((unsignedint) (MyProcPid ^port->session_start.tv_usec));
2763+
/* slightly hacky way to get integer microseconds part of timestamptz */
2764+
TimestampDifference(0,port->SessionStartTime,&secs,&usecs);
2765+
srandom((unsignedint) (MyProcPid ^usecs));
27622766

27632767
/* ----------------
27642768
* Now, build the argv vector that will be given to PostgresMain.

‎src/backend/storage/lmgr/proc.c

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.174 2006/04/14 03:38:55 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.175 2006/06/20 22:52:00 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -42,6 +42,7 @@
4242
#include"storage/proc.h"
4343
#include"storage/procarray.h"
4444
#include"storage/spin.h"
45+
#include"utils/timestamp.h"
4546

4647

4748
/* GUC variables */
@@ -73,7 +74,7 @@ static volatile bool deadlock_timeout_active = false;
7374
volatileboolcancel_from_timeout= false;
7475

7576
/* statement_fin_time is valid only if statement_timeout_active is true */
76-
staticstructtimevalstatement_fin_time;
77+
staticTimestampTzstatement_fin_time;
7778

7879

7980
staticvoidRemoveProcFromArray(intcode,Datumarg);
@@ -1105,29 +1106,32 @@ ProcSendSignal(int pid)
11051106
bool
11061107
enable_sig_alarm(intdelayms,boolis_statement_timeout)
11071108
{
1108-
structtimevalfin_time;
1109+
TimestampTzfin_time;
11091110
structitimervaltimeval;
11101111

1111-
/* Compute target timeout time if we will need it */
1112-
if (is_statement_timeout||statement_timeout_active)
1113-
{
1114-
gettimeofday(&fin_time,NULL);
1115-
fin_time.tv_sec+=delayms /1000;
1116-
fin_time.tv_usec+= (delayms %1000)*1000;
1117-
if (fin_time.tv_usec >=1000000)
1118-
{
1119-
fin_time.tv_sec++;
1120-
fin_time.tv_usec-=1000000;
1121-
}
1122-
}
1123-
11241112
if (is_statement_timeout)
11251113
{
1126-
/* Begin statement-level timeout */
1114+
/*
1115+
* Begin statement-level timeout
1116+
*
1117+
* Note that we compute statement_fin_time with reference to the
1118+
* statement_timestamp, but apply the specified delay without any
1119+
* correction; that is, we ignore whatever time has elapsed since
1120+
* statement_timestamp was set. In the normal case only a small
1121+
* interval will have elapsed and so this doesn't matter, but there
1122+
* are corner cases (involving multi-statement query strings with
1123+
* embedded COMMIT or ROLLBACK) where we might re-initialize the
1124+
* statement timeout long after initial receipt of the message.
1125+
* In such cases the enforcement of the statement timeout will be
1126+
* a bit inconsistent. This annoyance is judged not worth the cost
1127+
* of performing an additional gettimeofday() here.
1128+
*/
11271129
Assert(!deadlock_timeout_active);
1130+
fin_time=GetCurrentStatementStartTimestamp();
1131+
fin_time=TimestampTzPlusMilliseconds(fin_time,delayms);
11281132
statement_fin_time=fin_time;
1129-
statement_timeout_active= true;
11301133
cancel_from_timeout= false;
1134+
statement_timeout_active= true;
11311135
}
11321136
elseif (statement_timeout_active)
11331137
{
@@ -1145,10 +1149,10 @@ enable_sig_alarm(int delayms, bool is_statement_timeout)
11451149
* to the state variables.The deadlock checker may get run earlier
11461150
* than normal, but that does no harm.
11471151
*/
1152+
fin_time=GetCurrentTimestamp();
1153+
fin_time=TimestampTzPlusMilliseconds(fin_time,delayms);
11481154
deadlock_timeout_active= true;
1149-
if (fin_time.tv_sec>statement_fin_time.tv_sec||
1150-
(fin_time.tv_sec==statement_fin_time.tv_sec&&
1151-
fin_time.tv_usec >=statement_fin_time.tv_usec))
1155+
if (fin_time >=statement_fin_time)
11521156
return true;
11531157
}
11541158
else
@@ -1225,16 +1229,14 @@ disable_sig_alarm(bool is_statement_timeout)
12251229
staticbool
12261230
CheckStatementTimeout(void)
12271231
{
1228-
structtimevalnow;
1232+
TimestampTznow;
12291233

12301234
if (!statement_timeout_active)
12311235
return true;/* do nothing if not active */
12321236

1233-
gettimeofday(&now,NULL);
1237+
now=GetCurrentTimestamp();
12341238

1235-
if (now.tv_sec>statement_fin_time.tv_sec||
1236-
(now.tv_sec==statement_fin_time.tv_sec&&
1237-
now.tv_usec >=statement_fin_time.tv_usec))
1239+
if (now >=statement_fin_time)
12381240
{
12391241
/* Time to die */
12401242
statement_timeout_active= false;
@@ -1244,16 +1246,21 @@ CheckStatementTimeout(void)
12441246
else
12451247
{
12461248
/* Not time yet, so (re)schedule the interrupt */
1249+
longsecs;
1250+
intusecs;
12471251
structitimervaltimeval;
12481252

1253+
TimestampDifference(now,statement_fin_time,
1254+
&secs,&usecs);
1255+
/*
1256+
* It's possible that the difference is less than a microsecond;
1257+
* ensure we don't cancel, rather than set, the interrupt.
1258+
*/
1259+
if (secs==0&&usecs==0)
1260+
usecs=1;
12491261
MemSet(&timeval,0,sizeof(structitimerval));
1250-
timeval.it_value.tv_sec=statement_fin_time.tv_sec-now.tv_sec;
1251-
timeval.it_value.tv_usec=statement_fin_time.tv_usec-now.tv_usec;
1252-
if (timeval.it_value.tv_usec<0)
1253-
{
1254-
timeval.it_value.tv_sec--;
1255-
timeval.it_value.tv_usec+=1000000;
1256-
}
1262+
timeval.it_value.tv_sec=secs;
1263+
timeval.it_value.tv_usec=usecs;
12571264
if (setitimer(ITIMER_REAL,&timeval,NULL))
12581265
return false;
12591266
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp