Commit b5f7cff8 authored by Tom Lane's avatar Tom Lane

Clean up the rather historically encumbered interface to now() and

current time: provide a GetCurrentTimestamp() function that returns
current time in the form of a TimestampTz, instead of separate time_t
and microseconds fields.  This is what all the callers really want
anyway, and it eliminates low-level dependencies on AbsoluteTime,
which is a deprecated datatype that will have to disappear eventually.
parent c33d5758
#include "btree_gist.h" #include "btree_gist.h"
#include "btree_utils_num.h" #include "btree_utils_num.h"
#include "utils/datetime.h"
typedef struct typedef struct
{ {
Timestamp lower; Timestamp lower;
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
#include "executor/spi.h" /* this is what you need to work with SPI */ #include "executor/spi.h" /* this is what you need to work with SPI */
#include "commands/trigger.h" /* -"- and triggers */ #include "commands/trigger.h" /* -"- and triggers */
#include "miscadmin.h" /* for GetPgUserName() */ #include "miscadmin.h" /* for GetPgUserName() */
#include "utils/nabstime.h"
#include <ctype.h> /* tolower () */ #include <ctype.h> /* tolower () */
#define ABSTIMEOID 702 /* it should be in pg_type.h */ #define ABSTIMEOID 702 /* it should be in pg_type.h */
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.208 2005/06/28 05:08:51 tgl Exp $ * $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.209 2005/06/29 22:51:53 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -168,12 +168,11 @@ static SubTransactionId currentSubTransactionId; ...@@ -168,12 +168,11 @@ static SubTransactionId currentSubTransactionId;
static CommandId currentCommandId; static CommandId currentCommandId;
/* /*
* These vars hold the value of now(), ie, the transaction start time. * This is the value of now(), ie, the transaction start time.
* This does not change as we enter and exit subtransactions, so we don't * This does not change as we enter and exit subtransactions, so we don't
* keep it inside the TransactionState stack. * keep it inside the TransactionState stack.
*/ */
static AbsoluteTime xactStartTime; /* integer part */ static TimestampTz xactStartTimestamp;
static int xactStartTimeUsec; /* microsecond part */
/* /*
* GID to be used for preparing the current transaction. This is also * GID to be used for preparing the current transaction. This is also
...@@ -420,28 +419,15 @@ GetCurrentCommandId(void) ...@@ -420,28 +419,15 @@ GetCurrentCommandId(void)
return currentCommandId; return currentCommandId;
} }
/*
* GetCurrentTransactionStartTime
*/
AbsoluteTime
GetCurrentTransactionStartTime(void)
{
return xactStartTime;
}
/* /*
* GetCurrentTransactionStartTimeUsec * GetCurrentTransactionStartTimestamp
*/ */
AbsoluteTime TimestampTz
GetCurrentTransactionStartTimeUsec(int *msec) GetCurrentTransactionStartTimestamp(void)
{ {
*msec = xactStartTimeUsec; return xactStartTimestamp;
return xactStartTime;
} }
/* /*
* GetCurrentTransactionNestLevel * GetCurrentTransactionNestLevel
* *
...@@ -1391,7 +1377,7 @@ StartTransaction(void) ...@@ -1391,7 +1377,7 @@ StartTransaction(void)
/* /*
* set now() * set now()
*/ */
xactStartTime = GetCurrentAbsoluteTimeUsec(&(xactStartTimeUsec)); xactStartTimestamp = GetCurrentTimestamp();
/* /*
* initialize current transaction state fields * initialize current transaction state fields
...@@ -1633,8 +1619,6 @@ PrepareTransaction(void) ...@@ -1633,8 +1619,6 @@ PrepareTransaction(void)
TransactionId xid = GetCurrentTransactionId(); TransactionId xid = GetCurrentTransactionId();
GlobalTransaction gxact; GlobalTransaction gxact;
TimestampTz prepared_at; TimestampTz prepared_at;
AbsoluteTime PreparedSec; /* integer part */
int PreparedUSec; /* microsecond part */
ShowTransactionState("PrepareTransaction"); ShowTransactionState("PrepareTransaction");
...@@ -1697,8 +1681,7 @@ PrepareTransaction(void) ...@@ -1697,8 +1681,7 @@ PrepareTransaction(void)
*/ */
s->state = TRANS_PREPARE; s->state = TRANS_PREPARE;
PreparedSec = GetCurrentAbsoluteTimeUsec(&PreparedUSec); prepared_at = GetCurrentTimestamp();
prepared_at = AbsoluteTimeUsecToTimestampTz(PreparedSec, PreparedUSec);
/* Tell bufmgr and smgr to prepare for commit */ /* Tell bufmgr and smgr to prepare for commit */
BufmgrCommit(); BufmgrCommit();
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.203 2005/06/19 21:34:01 tgl Exp $ * $PostgreSQL: pgsql/src/backend/access/transam/xlog.c,v 1.204 2005/06/29 22:51:53 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include "storage/spin.h" #include "storage/spin.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/guc.h" #include "utils/guc.h"
#include "utils/nabstime.h"
#include "utils/relcache.h" #include "utils/relcache.h"
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/bootstrap/bootparse.y,v 1.76 2005/04/14 01:38:15 tgl Exp $ * $PostgreSQL: pgsql/src/backend/bootstrap/bootparse.y,v 1.77 2005/06/29 22:51:54 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -47,7 +47,6 @@ ...@@ -47,7 +47,6 @@
#include "storage/off.h" #include "storage/off.h"
#include "storage/smgr.h" #include "storage/smgr.h"
#include "tcop/dest.h" #include "tcop/dest.h"
#include "utils/nabstime.h"
#include "utils/rel.h" #include "utils/rel.h"
#define atooid(x) ((Oid) strtoul((x), NULL, 10)) #define atooid(x) ((Oid) strtoul((x), NULL, 10))
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/bootstrap/bootscanner.l,v 1.39 2005/03/11 19:13:42 momjian Exp $ * $PostgreSQL: pgsql/src/backend/bootstrap/bootscanner.l,v 1.40 2005/06/29 22:51:54 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -34,7 +34,6 @@ ...@@ -34,7 +34,6 @@
#include "storage/fd.h" #include "storage/fd.h"
#include "storage/itemptr.h" #include "storage/itemptr.h"
#include "storage/off.h" #include "storage/off.h"
#include "utils/nabstime.h"
#include "utils/rel.h" #include "utils/rel.h"
/* Not needed now that this file is compiled as part of bootparse. */ /* Not needed now that this file is compiled as part of bootparse. */
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/backend/libpq/crypt.c,v 1.63 2005/06/28 05:08:56 tgl Exp $ * $PostgreSQL: pgsql/src/backend/libpq/crypt.c,v 1.64 2005/06/29 22:51:54 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
#include "miscadmin.h" #include "miscadmin.h"
#include "storage/fd.h" #include "storage/fd.h"
#include "nodes/pg_list.h" #include "nodes/pg_list.h"
#include "utils/nabstime.h" #include "utils/timestamp.h"
int int
...@@ -149,19 +149,13 @@ md5_crypt_verify(const Port *port, const char *role, char *client_pass) ...@@ -149,19 +149,13 @@ md5_crypt_verify(const Port *port, const char *role, char *client_pass)
else else
{ {
TimestampTz vuntil; TimestampTz vuntil;
AbsoluteTime sec;
int usec;
TimestampTz curtime;
vuntil = DatumGetTimestampTz(DirectFunctionCall3(timestamptz_in, vuntil = DatumGetTimestampTz(DirectFunctionCall3(timestamptz_in,
CStringGetDatum(valuntil), CStringGetDatum(valuntil),
ObjectIdGetDatum(InvalidOid), ObjectIdGetDatum(InvalidOid),
Int32GetDatum(-1))); Int32GetDatum(-1)));
sec = GetCurrentAbsoluteTimeUsec(&usec); if (vuntil < GetCurrentTimestamp())
curtime = AbsoluteTimeUsecToTimestampTz(sec, usec);
if (vuntil < curtime)
retval = STATUS_ERROR; retval = STATUS_ERROR;
else else
retval = STATUS_OK; retval = STATUS_OK;
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* *
* Copyright (c) 2001-2005, PostgreSQL Global Development Group * Copyright (c) 2001-2005, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.97 2005/06/28 05:08:59 tgl Exp $ * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.98 2005/06/29 22:51:55 tgl Exp $
* ---------- * ----------
*/ */
#include "postgres.h" #include "postgres.h"
...@@ -2026,10 +2026,8 @@ pgstat_add_backend(PgStat_MsgHdr *msg) ...@@ -2026,10 +2026,8 @@ pgstat_add_backend(PgStat_MsgHdr *msg)
/* Put this new backend into the slot */ /* Put this new backend into the slot */
beentry->procpid = msg->m_procpid; beentry->procpid = msg->m_procpid;
beentry->start_sec = beentry->start_timestamp = GetCurrentTimestamp();
GetCurrentAbsoluteTimeUsec(&beentry->start_usec); beentry->activity_start_timestamp = 0;
beentry->activity_start_sec = 0;
beentry->activity_start_usec = 0;
beentry->activity[0] = '\0'; beentry->activity[0] = '\0';
/* /*
...@@ -2665,8 +2663,7 @@ pgstat_recv_activity(PgStat_MsgActivity *msg, int len) ...@@ -2665,8 +2663,7 @@ pgstat_recv_activity(PgStat_MsgActivity *msg, int len)
StrNCpy(entry->activity, msg->m_what, PGSTAT_ACTIVITY_SIZE); StrNCpy(entry->activity, msg->m_what, PGSTAT_ACTIVITY_SIZE);
entry->activity_start_sec = entry->activity_start_timestamp = GetCurrentTimestamp();
GetCurrentAbsoluteTimeUsec(&entry->activity_start_usec);
} }
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.455 2005/06/28 05:08:59 tgl Exp $ * $PostgreSQL: pgsql/src/backend/postmaster/postmaster.c,v 1.456 2005/06/29 22:51:55 tgl Exp $
* *
* NOTES * NOTES
* *
...@@ -118,6 +118,7 @@ ...@@ -118,6 +118,7 @@
#include "storage/proc.h" #include "storage/proc.h"
#include "tcop/tcopprot.h" #include "tcop/tcopprot.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/datetime.h"
#include "utils/guc.h" #include "utils/guc.h"
#include "utils/memutils.h" #include "utils/memutils.h"
#include "utils/ps_status.h" #include "utils/ps_status.h"
...@@ -222,9 +223,6 @@ static bool FatalError = false; /* T if recovering from backend crash */ ...@@ -222,9 +223,6 @@ static bool FatalError = false; /* T if recovering from backend crash */
bool ClientAuthInProgress = false; /* T during new-client bool ClientAuthInProgress = false; /* T during new-client
* authentication */ * authentication */
/* Backend startup time */
TimestampTz StartTime;
/* /*
* State for assigning random salts and cancel keys. * State for assigning random salts and cancel keys.
* Also, the global MyCancelKey passes the cancel key assigned to a given * Also, the global MyCancelKey passes the cancel key assigned to a given
...@@ -333,7 +331,7 @@ typedef struct ...@@ -333,7 +331,7 @@ typedef struct
InheritableSocket pgStatPipe0; InheritableSocket pgStatPipe0;
InheritableSocket pgStatPipe1; InheritableSocket pgStatPipe1;
pid_t PostmasterPid; pid_t PostmasterPid;
TimestampTz StartTime; TimestampTz PgStartTime;
#ifdef WIN32 #ifdef WIN32
HANDLE PostmasterHandle; HANDLE PostmasterHandle;
HANDLE initial_signal_pipe; HANDLE initial_signal_pipe;
...@@ -376,9 +374,6 @@ PostmasterMain(int argc, char *argv[]) ...@@ -376,9 +374,6 @@ PostmasterMain(int argc, char *argv[])
char *userDoption = NULL; char *userDoption = NULL;
int i; int i;
AbsoluteTime StartTimeSec; /* integer part */
int StartTimeUSec; /* microsecond part */
/* This will call exit() if strdup() fails. */ /* This will call exit() if strdup() fails. */
progname = get_progname(argv[0]); progname = get_progname(argv[0]);
...@@ -922,10 +917,9 @@ PostmasterMain(int argc, char *argv[]) ...@@ -922,10 +917,9 @@ PostmasterMain(int argc, char *argv[])
StartupPID = StartupDataBase(); StartupPID = StartupDataBase();
/* /*
* Get start up time * Remember postmaster startup time
*/ */
StartTimeSec = GetCurrentAbsoluteTimeUsec(&StartTimeUSec); PgStartTime = GetCurrentTimestamp();
StartTime = AbsoluteTimeUsecToTimestampTz(StartTimeSec, StartTimeUSec);
status = ServerLoop(); status = ServerLoop();
...@@ -3613,7 +3607,7 @@ save_backend_variables(BackendParameters *param, Port *port, ...@@ -3613,7 +3607,7 @@ save_backend_variables(BackendParameters *param, Port *port,
write_inheritable_socket(&param->pgStatPipe1, pgStatPipe[1], childPid); write_inheritable_socket(&param->pgStatPipe1, pgStatPipe[1], childPid);
param->PostmasterPid = PostmasterPid; param->PostmasterPid = PostmasterPid;
param->StartTime = StartTime; param->PgStartTime = PgStartTime;
#ifdef WIN32 #ifdef WIN32
param->PostmasterHandle = PostmasterHandle; param->PostmasterHandle = PostmasterHandle;
...@@ -3816,7 +3810,7 @@ restore_backend_variables(BackendParameters *param, Port *port) ...@@ -3816,7 +3810,7 @@ restore_backend_variables(BackendParameters *param, Port *port)
read_inheritable_socket(&pgStatPipe[1], &param->pgStatPipe1); read_inheritable_socket(&pgStatPipe[1], &param->pgStatPipe1);
PostmasterPid = param->PostmasterPid; PostmasterPid = param->PostmasterPid;
StartTime = param->StartTime; PgStartTime = param->PgStartTime;
#ifdef WIN32 #ifdef WIN32
PostmasterHandle = param->PostmasterHandle; PostmasterHandle = param->PostmasterHandle;
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.450 2005/06/22 17:45:45 tgl Exp $ * $PostgreSQL: pgsql/src/backend/tcop/postgres.c,v 1.451 2005/06/29 22:51:55 tgl Exp $
* *
* NOTES * NOTES
* this is the "main" module of the postgres backend and * this is the "main" module of the postgres backend and
...@@ -149,9 +149,6 @@ static int UseNewLine = 0; /* Use EOF as query delimiters */ ...@@ -149,9 +149,6 @@ static int UseNewLine = 0; /* Use EOF as query delimiters */
#endif /* TCOP_DONTUSENEWLINE */ #endif /* TCOP_DONTUSENEWLINE */
/* Backend startup time */
TimestampTz StartTime;
/* ---------------------------------------------------------------- /* ----------------------------------------------------------------
* decls for routines only used in this file * decls for routines only used in this file
* ---------------------------------------------------------------- * ----------------------------------------------------------------
...@@ -2373,9 +2370,6 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2373,9 +2370,6 @@ PostgresMain(int argc, char *argv[], const char *username)
sigjmp_buf local_sigjmp_buf; sigjmp_buf local_sigjmp_buf;
volatile bool send_rfq = true; volatile bool send_rfq = true;
AbsoluteTime StartTimeSec; /* integer part */
int StartTimeUSec; /* microsecond part */
#define PendingConfigOption(name,val) \ #define PendingConfigOption(name,val) \
(guc_names = lappend(guc_names, pstrdup(name)), \ (guc_names = lappend(guc_names, pstrdup(name)), \
guc_values = lappend(guc_values, pstrdup(val))) guc_values = lappend(guc_values, pstrdup(val)))
...@@ -2966,13 +2960,10 @@ PostgresMain(int argc, char *argv[], const char *username) ...@@ -2966,13 +2960,10 @@ PostgresMain(int argc, char *argv[], const char *username)
pgstat_bestart(); pgstat_bestart();
/* /*
* Get stand-alone backend startup time * Remember stand-alone backend startup time
*/ */
if (!IsUnderPostmaster) if (!IsUnderPostmaster)
{ PgStartTime = GetCurrentTimestamp();
StartTimeSec = GetCurrentAbsoluteTimeUsec(&StartTimeUSec);
StartTime = AbsoluteTimeUsecToTimestampTz(StartTimeSec, StartTimeUSec);
}
/* /*
* POSTGRES main processing loop begins here * POSTGRES main processing loop begins here
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.150 2005/05/27 21:31:23 momjian Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.151 2005/06/29 22:51:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <limits.h> #include <limits.h>
#include <math.h> #include <math.h>
#include "access/xact.h"
#include "miscadmin.h" #include "miscadmin.h"
#include "utils/datetime.h" #include "utils/datetime.h"
#include "utils/guc.h" #include "utils/guc.h"
...@@ -674,6 +675,41 @@ j2day(int date) ...@@ -674,6 +675,41 @@ j2day(int date)
} /* j2day() */ } /* j2day() */
/*
* GetCurrentDateTime()
*
* Get the transaction start time ("now()") broken down as a struct pg_tm.
*/
void
GetCurrentDateTime(struct pg_tm * tm)
{
int tz;
fsec_t fsec;
timestamp2tm(GetCurrentTransactionStartTimestamp(), &tz, tm, &fsec,
NULL, NULL);
/* Note: don't pass NULL tzp to timestamp2tm; affects behavior */
}
/*
* GetCurrentTimeUsec()
*
* Get the transaction start time ("now()") broken down as a struct pg_tm,
* including fractional seconds and timezone offset.
*/
void
GetCurrentTimeUsec(struct pg_tm * tm, fsec_t *fsec, int *tzp)
{
int tz;
timestamp2tm(GetCurrentTransactionStartTimestamp(), &tz, tm, fsec,
NULL, NULL);
/* Note: don't pass NULL tzp to timestamp2tm; affects behavior */
if (tzp != NULL)
*tzp = tz;
}
/* TrimTrailingZeros() /* TrimTrailingZeros()
* ... resulting from printing numbers with full precision. * ... resulting from printing numbers with full precision.
*/ */
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/nabstime.c,v 1.133 2005/06/15 00:34:08 momjian Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/nabstime.c,v 1.134 2005/06/29 22:51:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -27,7 +27,7 @@ ...@@ -27,7 +27,7 @@
#include "miscadmin.h" #include "miscadmin.h"
#include "pgtime.h" #include "pgtime.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/timestamp.h" #include "utils/nabstime.h"
#define MIN_DAYNUM -24856 /* December 13, 1901 */ #define MIN_DAYNUM -24856 /* December 13, 1901 */
#define MAX_DAYNUM 24854 /* January 18, 2038 */ #define MAX_DAYNUM 24854 /* January 18, 2038 */
...@@ -99,84 +99,6 @@ GetCurrentAbsoluteTime(void) ...@@ -99,84 +99,6 @@ GetCurrentAbsoluteTime(void)
} }
/*
* GetCurrentAbsoluteTimeUsec()
*
* Get the current system time (relative to Unix epoch), including fractional
* seconds expressed as microseconds.
*/
AbsoluteTime
GetCurrentAbsoluteTimeUsec(int *usec)
{
time_t now;
struct timeval tp;
gettimeofday(&tp, NULL);
now = tp.tv_sec;
*usec = tp.tv_usec;
return (AbsoluteTime) now;
}
/*
* AbsoluteTimeUsecToTimestampTz()
*
* Convert system time including microseconds to TimestampTz representation.
*/
TimestampTz
AbsoluteTimeUsecToTimestampTz(AbsoluteTime sec, int usec)
{
TimestampTz result;
#ifdef HAVE_INT64_TIMESTAMP
result = ((sec - ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY))
* USECS_PER_SEC) + usec;
#else
result = sec - ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY)
+ (usec / 1000000.0);
#endif
return result;
}
/*
* GetCurrentDateTime()
*
* Get the transaction start time ("now()") broken down as a struct pg_tm.
*/
void
GetCurrentDateTime(struct pg_tm * tm)
{
int tz;
abstime2tm(GetCurrentTransactionStartTime(), &tz, tm, NULL);
}
/*
* GetCurrentTimeUsec()
*
* Get the transaction start time ("now()") broken down as a struct pg_tm,
* including fractional seconds and timezone offset.
*/
void
GetCurrentTimeUsec(struct pg_tm * tm, fsec_t *fsec, int *tzp)
{
int tz;
int usec;
abstime2tm(GetCurrentTransactionStartTimeUsec(&usec), &tz, tm, NULL);
/* Note: don't pass NULL tzp to abstime2tm; affects behavior */
if (tzp != NULL)
*tzp = tz;
#ifdef HAVE_INT64_TIMESTAMP
*fsec = usec;
#else
*fsec = usec / 1000000.0;
#endif
}
void void
abstime2tm(AbsoluteTime _time, int *tzp, struct pg_tm * tm, char **tzn) abstime2tm(AbsoluteTime _time, int *tzp, struct pg_tm * tm, char **tzn)
{ {
...@@ -458,15 +380,6 @@ abstime_cmp_internal(AbsoluteTime a, AbsoluteTime b) ...@@ -458,15 +380,6 @@ abstime_cmp_internal(AbsoluteTime a, AbsoluteTime b)
if (b == INVALID_ABSTIME) if (b == INVALID_ABSTIME)
return -1; /* non-INVALID < INVALID */ return -1; /* non-INVALID < INVALID */
#if 0
/* CURRENT is no longer stored internally... */
/* XXX this is broken, should go away: */
if (a == CURRENT_ABSTIME)
a = GetCurrentTransactionStartTime();
if (b == CURRENT_ABSTIME)
b = GetCurrentTransactionStartTime();
#endif
if (a > b) if (a > b)
return 1; return 1;
else if (a == b) else if (a == b)
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.23 2005/06/28 05:09:00 tgl Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.24 2005/06/29 22:51:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -341,13 +341,9 @@ pg_stat_get_backend_activity(PG_FUNCTION_ARGS) ...@@ -341,13 +341,9 @@ pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
Datum Datum
pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS) pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
{ {
PgStat_StatBeEntry *beentry; int32 beid = PG_GETARG_INT32(0);
int32 beid;
AbsoluteTime sec;
int usec;
TimestampTz result; TimestampTz result;
PgStat_StatBeEntry *beentry;
beid = PG_GETARG_INT32(0);
if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL) if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
PG_RETURN_NULL(); PG_RETURN_NULL();
...@@ -355,31 +351,24 @@ pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS) ...@@ -355,31 +351,24 @@ pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
if (!superuser() && beentry->userid != GetUserId()) if (!superuser() && beentry->userid != GetUserId())
PG_RETURN_NULL(); PG_RETURN_NULL();
sec = beentry->activity_start_sec; result = beentry->activity_start_timestamp;
usec = beentry->activity_start_usec;
/* /*
* No time recorded for start of current query -- this is the case if * No time recorded for start of current query -- this is the case if
* the user hasn't enabled query-level stats collection. * the user hasn't enabled query-level stats collection.
*/ */
if (sec == 0 && usec == 0) if (result == 0)
PG_RETURN_NULL(); PG_RETURN_NULL();
result = AbsoluteTimeUsecToTimestampTz(sec, usec);
PG_RETURN_TIMESTAMPTZ(result); PG_RETURN_TIMESTAMPTZ(result);
} }
Datum Datum
pg_stat_get_backend_start(PG_FUNCTION_ARGS) pg_stat_get_backend_start(PG_FUNCTION_ARGS)
{ {
PgStat_StatBeEntry *beentry; int32 beid = PG_GETARG_INT32(0);
int32 beid;
AbsoluteTime sec;
int usec;
TimestampTz result; TimestampTz result;
PgStat_StatBeEntry *beentry;
beid = PG_GETARG_INT32(0);
if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL) if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
PG_RETURN_NULL(); PG_RETURN_NULL();
...@@ -387,14 +376,11 @@ pg_stat_get_backend_start(PG_FUNCTION_ARGS) ...@@ -387,14 +376,11 @@ pg_stat_get_backend_start(PG_FUNCTION_ARGS)
if (!superuser() && beentry->userid != GetUserId()) if (!superuser() && beentry->userid != GetUserId())
PG_RETURN_NULL(); PG_RETURN_NULL();
sec = beentry->start_sec; result = beentry->start_timestamp;
usec = beentry->start_usec;
if (sec == 0 && usec == 0) if (result == 0) /* probably can't happen? */
PG_RETURN_NULL(); PG_RETURN_NULL();
result = AbsoluteTimeUsecToTimestampTz(sec, usec);
PG_RETURN_TIMESTAMPTZ(result); PG_RETURN_TIMESTAMPTZ(result);
} }
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.182 2005/06/13 23:14:48 tgl Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.183 2005/06/29 22:51:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -105,6 +105,7 @@ ...@@ -105,6 +105,7 @@
#include "utils/datum.h" #include "utils/datum.h"
#include "utils/int8.h" #include "utils/int8.h"
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
#include "utils/nabstime.h"
#include "utils/pg_locale.h" #include "utils/pg_locale.h"
#include "utils/selfuncs.h" #include "utils/selfuncs.h"
#include "utils/syscache.h" #include "utils/syscache.h"
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.126 2005/06/15 00:34:09 momjian Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.127 2005/06/29 22:51:56 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
#include "parser/scansup.h" #include "parser/scansup.h"
#include "utils/array.h" #include "utils/array.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/datetime.h"
/* /*
* gcc's -ffast-math switch breaks routines that expect exact results from * gcc's -ffast-math switch breaks routines that expect exact results from
...@@ -38,6 +40,10 @@ ...@@ -38,6 +40,10 @@
#endif #endif
/* Set at postmaster start */
TimestampTz PgStartTime;
#ifdef HAVE_INT64_TIMESTAMP #ifdef HAVE_INT64_TIMESTAMP
static int64 time2t(const int hour, const int min, const int sec, const fsec_t fsec); static int64 time2t(const int hour, const int min, const int sec, const fsec_t fsec);
...@@ -927,21 +933,39 @@ EncodeSpecialTimestamp(Timestamp dt, char *str) ...@@ -927,21 +933,39 @@ EncodeSpecialTimestamp(Timestamp dt, char *str)
Datum Datum
now(PG_FUNCTION_ARGS) now(PG_FUNCTION_ARGS)
{ {
TimestampTz result; PG_RETURN_TIMESTAMPTZ(GetCurrentTransactionStartTimestamp());
AbsoluteTime sec;
int usec;
sec = GetCurrentTransactionStartTimeUsec(&usec);
result = AbsoluteTimeUsecToTimestampTz(sec, usec);
PG_RETURN_TIMESTAMPTZ(result);
} }
Datum Datum
pgsql_postmaster_start_time(PG_FUNCTION_ARGS) pgsql_postmaster_start_time(PG_FUNCTION_ARGS)
{ {
PG_RETURN_TIMESTAMPTZ(StartTime); PG_RETURN_TIMESTAMPTZ(PgStartTime);
}
/*
* GetCurrentTimestamp -- get the current operating system time
*
* Result is in the form of a TimestampTz value, and is expressed to the
* full precision of the gettimeofday() syscall
*/
TimestampTz
GetCurrentTimestamp(void)
{
TimestampTz result;
struct timeval tp;
gettimeofday(&tp, NULL);
result = tp.tv_sec -
((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
#ifdef HAVE_INT64_TIMESTAMP
result = (result * USECS_PER_SEC) + tp.tv_usec;
#else
result = result + (tp.tv_usec / 1000000.0);
#endif
return result;
} }
void void
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/access/xact.h,v 1.77 2005/06/17 22:32:48 tgl Exp $ * $PostgreSQL: pgsql/src/include/access/xact.h,v 1.78 2005/06/29 22:51:57 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#include "access/xlog.h" #include "access/xlog.h"
#include "storage/relfilenode.h" #include "storage/relfilenode.h"
#include "nodes/pg_list.h" #include "nodes/pg_list.h"
#include "utils/nabstime.h" #include "utils/timestamp.h"
/* /*
...@@ -140,8 +140,7 @@ extern TransactionId GetCurrentTransactionId(void); ...@@ -140,8 +140,7 @@ extern TransactionId GetCurrentTransactionId(void);
extern TransactionId GetCurrentTransactionIdIfAny(void); extern TransactionId GetCurrentTransactionIdIfAny(void);
extern SubTransactionId GetCurrentSubTransactionId(void); extern SubTransactionId GetCurrentSubTransactionId(void);
extern CommandId GetCurrentCommandId(void); extern CommandId GetCurrentCommandId(void);
extern AbsoluteTime GetCurrentTransactionStartTime(void); extern TimestampTz GetCurrentTransactionStartTimestamp(void);
extern AbsoluteTime GetCurrentTransactionStartTimeUsec(int *usec);
extern int GetCurrentTransactionNestLevel(void); extern int GetCurrentTransactionNestLevel(void);
extern bool TransactionIdIsCurrentTransactionId(TransactionId xid); extern bool TransactionIdIsCurrentTransactionId(TransactionId xid);
extern void CommandCounterIncrement(void); extern void CommandCounterIncrement(void);
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* *
* Copyright (c) 2001-2005, PostgreSQL Global Development Group * Copyright (c) 2001-2005, PostgreSQL Global Development Group
* *
* $PostgreSQL: pgsql/src/include/pgstat.h,v 1.31 2005/06/28 05:09:04 tgl Exp $ * $PostgreSQL: pgsql/src/include/pgstat.h,v 1.32 2005/06/29 22:51:57 tgl Exp $
* ---------- * ----------
*/ */
#ifndef PGSTAT_H #ifndef PGSTAT_H
...@@ -13,8 +13,8 @@ ...@@ -13,8 +13,8 @@
#include "libpq/pqcomm.h" #include "libpq/pqcomm.h"
#include "utils/hsearch.h" #include "utils/hsearch.h"
#include "utils/nabstime.h"
#include "utils/rel.h" #include "utils/rel.h"
#include "utils/timestamp.h"
/* ---------- /* ----------
* The types of backend/postmaster -> collector messages * The types of backend/postmaster -> collector messages
...@@ -233,10 +233,8 @@ typedef struct PgStat_StatBeEntry ...@@ -233,10 +233,8 @@ typedef struct PgStat_StatBeEntry
{ {
/* An entry is non-empty iff procpid > 0 */ /* An entry is non-empty iff procpid > 0 */
int procpid; int procpid;
AbsoluteTime start_sec; TimestampTz start_timestamp;
int start_usec; TimestampTz activity_start_timestamp;
AbsoluteTime activity_start_sec;
int activity_start_usec;
char activity[PGSTAT_ACTIVITY_SIZE]; char activity[PGSTAT_ACTIVITY_SIZE];
/* /*
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/utils/nabstime.h,v 1.45 2004/12/31 22:03:46 pgsql Exp $ * $PostgreSQL: pgsql/src/include/utils/nabstime.h,v 1.46 2005/06/29 22:51:57 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -162,8 +162,6 @@ extern Datum timeofday(PG_FUNCTION_ARGS); ...@@ -162,8 +162,6 @@ extern Datum timeofday(PG_FUNCTION_ARGS);
/* non-fmgr-callable support routines */ /* non-fmgr-callable support routines */
extern AbsoluteTime GetCurrentAbsoluteTime(void); extern AbsoluteTime GetCurrentAbsoluteTime(void);
extern AbsoluteTime GetCurrentAbsoluteTimeUsec(int *usec);
extern TimestampTz AbsoluteTimeUsecToTimestampTz(AbsoluteTime sec, int usec);
extern void abstime2tm(AbsoluteTime time, int *tzp, struct pg_tm * tm, char **tzn); extern void abstime2tm(AbsoluteTime time, int *tzp, struct pg_tm * tm, char **tzn);
#endif /* NABSTIME_H */ #endif /* NABSTIME_H */
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/utils/timestamp.h,v 1.45 2005/06/15 00:34:10 momjian Exp $ * $PostgreSQL: pgsql/src/include/utils/timestamp.h,v 1.46 2005/06/29 22:51:57 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -156,6 +156,10 @@ typedef double fsec_t; ...@@ -156,6 +156,10 @@ typedef double fsec_t;
#define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK) #define INTERVAL_RANGE(t) (((t) >> 16) & INTERVAL_RANGE_MASK)
/* Set at postmaster start */
extern TimestampTz PgStartTime;
/* /*
* timestamp.c prototypes * timestamp.c prototypes
*/ */
...@@ -258,10 +262,10 @@ extern Datum now(PG_FUNCTION_ARGS); ...@@ -258,10 +262,10 @@ extern Datum now(PG_FUNCTION_ARGS);
extern Datum pgsql_postmaster_start_time(PG_FUNCTION_ARGS); extern Datum pgsql_postmaster_start_time(PG_FUNCTION_ARGS);
extern TimestampTz StartTime;
/* Internal routines (not fmgr-callable) */ /* Internal routines (not fmgr-callable) */
extern TimestampTz GetCurrentTimestamp(void);
extern int tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *dt); extern int tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *dt);
extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm, extern int timestamp2tm(Timestamp dt, int *tzp, struct pg_tm * tm,
fsec_t *fsec, char **tzn, pg_tz *attimezone); fsec_t *fsec, char **tzn, pg_tz *attimezone);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment