forked from 2ndQuadrant/pg_sysdatetime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_sysdatetime.c
191 lines (154 loc) · 4.04 KB
/
pg_sysdatetime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "postgres.h"
#include <string.h>
#include "fmgr.h"
#include "utils/geo_decls.h"
#include "utils/datetime.h"
#include "utils/guc.h"
#if defined(WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PGDLLEXPORT void _PG_init(void);
PGDLLEXPORT void _PG_fini(void);
static bool adjust_timer_resolution;
#if defined(WIN32)
/* Timer resolution to request from Windows, in milliseconds */
static const UINT targetTimeResolution = 1;
static UINT requestedTimerRes = 0;
/* from src/port/gettimeofday.c */
/* FILETIME of Jan 1 1970 00:00:00. */
static const unsigned __int64 epoch = UINT64CONST(116444736000000000);
/* replaces gettimeofday() in src/port/gettimeofday.c */
static int
getetimeofday_highres(struct timeval * tp, struct timezone * tzp)
{
FILETIME t;
ULARGE_INTEGER ularge;
GetSystemTimeAsFileTime(&t);
ularge.LowPart = t.dwLowDateTime;
ularge.HighPart = t.dwHighDateTime;
/* from src/port/gettimeofday.c */
tp->tv_sec = (long) ((ularge.QuadPart - epoch) / 10000000L);
tp->tv_usec = (long) (((ularge.QuadPart - epoch) % 10000000L) / 10); // units of 100 nanos to units of micros
return 0;
}
/* Replaces GetCurrentTimestamp in src/backend/utils/adt/timestamp.c */
static TimestampTz
GetCurrentTimestampHighres(void)
{
TimestampTz result;
struct timeval tp;
(void) getetimeofday_highres(&tp, NULL);
result = (TimestampTz) 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;
}
static void
setTimerResolution()
{
TIMECAPS caps;
if (!adjust_timer_resolution)
return;
Assert(requestedTimerRes == 0);
if (timeGetDevCaps(&caps, sizeof(TIMECAPS)) != TIMERR_NOERROR)
{
ereport(WARNING,
(errmsg("Failed to get current Windows timer resolution"),
errdetail("Call to timeGetDevCaps(...) failed")));
}
else
{
requestedTimerRes = min(max(caps.wPeriodMin, targetTimeResolution), caps.wPeriodMax);
if (timeBeginPeriod(requestedTimerRes) != TIMERR_NOERROR)
{
ereport(WARNING,
(errmsg("Failed to set timer resolution to %d ms", requestedTimerRes),
errdetail("timeBeginPeriod(...) call failed")));
requestedTimerRes = 0;
}
}
}
static void
restoreTimerResolution()
{
if (requestedTimerRes == 0)
return;
if ( timeEndPeriod(requestedTimerRes) != TIMERR_NOERROR)
{
ereport(WARNING,
(errmsg("Failed to restore timer resolution", requestedTimerRes),
errdetail("timeEndPeriod(...) call failed")));
}
/* Even if the restore failed, all we can do is keep on going, so ... */
requestedTimerRes = 0;
}
#else
/* on non-Windows, simply use the existing GetCurrentTimestamp */
inline static TimestampTz
GetCurrentTimestampHighres(void)
{
return GetCurrentTimestamp();
}
/* Linux's timing doesn't need correction */
inline static void setTimerResolution()
{
}
inline static void restoreTimerResolution()
{
}
#endif
PG_FUNCTION_INFO_V1(pg_sysutcdatetime);
PGDLLEXPORT Datum
pg_sysutcdatetime(PG_FUNCTION_ARGS)
{
PG_RETURN_TIMESTAMP(GetCurrentTimestampHighres());
}
PG_FUNCTION_INFO_V1(pg_sysdatetimeoffset);
PGDLLEXPORT Datum
pg_sysdatetimeoffset(PG_FUNCTION_ARGS)
{
PG_RETURN_TIMESTAMPTZ(GetCurrentTimestampHighres());
}
PG_FUNCTION_INFO_V1(pg_sysdatetime);
PGDLLEXPORT Datum
pg_sysdatetime(PG_FUNCTION_ARGS)
{
Timestamp ts = GetCurrentTimestampHighres();
return DirectFunctionCall1(timestamptz_timestamp, TimestampGetDatum(ts));
}
void
adjust_timer_resolution_assign_hook(bool newval, void *extra)
{
if (newval != adjust_timer_resolution)
{
adjust_timer_resolution = newval;
restoreTimerResolution();
setTimerResolution();
}
}
PGDLLEXPORT void
_PG_init()
{
DefineCustomBoolVariable(
"pg_sysdatetime.adjust_timer_resolution",
"Increase the system timer resolution (only affects Windows)",
NULL,
&adjust_timer_resolution,
false,
PGC_SUSET, 0,
NULL, &adjust_timer_resolution_assign_hook, NULL);
setTimerResolution();
}
PGDLLEXPORT void
_PG_fini()
{
restoreTimerResolution();
}