26
26
#include "utils/tuplestore.h"
27
27
#include "utils/timestamp.h"
28
28
#include "utils/acl.h"
29
+ #include "utils/guc.h"
29
30
#include "catalog/pg_authid.h"
30
31
#include "funcapi.h"
31
32
#include "c.h"
@@ -41,6 +42,9 @@ PG_MODULE_MAGIC;
41
42
extern void _PG_init (void );
42
43
extern void _PG_fini (void );
43
44
45
+ /* GUC variables */
46
+ static int log_period_guc = 0 ;
47
+
44
48
/* Number of output arguments (columns) for various API versions */
45
49
#define PG_AUTH_MON_COLS_V1_0 6
46
50
#define PG_AUTH_MON_COLS_V1_1 7
@@ -122,6 +126,19 @@ _PG_init(void)
122
126
123
127
original_client_auth_hook = ClientAuthentication_hook ;
124
128
ClientAuthentication_hook = auth_monitor ;
129
+
130
+ DefineCustomIntVariable ("pg_auth_mon.log_period" ,
131
+ "Duration between logging pg_auth_mon data to PG log (in minutes)." ,
132
+ NULL ,
133
+ & log_period_guc ,
134
+ 0 , // 0 means the feature is off
135
+ 0 ,
136
+ 10080 , // one week
137
+ PGC_SIGHUP ,
138
+ GUC_UNIT_MIN ,
139
+ NULL ,
140
+ NULL ,
141
+ NULL );
125
142
}
126
143
127
144
/*
@@ -232,7 +249,7 @@ auth_monitor(Port *port, int status)
232
249
hba_reject = false,
233
250
fail = false;
234
251
235
- int waittime = 1000 * 60 * 60 * 24 ; // log at most once in an day
252
+ int waittime_msec ;
236
253
TimestampTz now = GetCurrentTimestamp ();
237
254
238
255
/*
@@ -308,10 +325,14 @@ auth_monitor(Port *port, int status)
308
325
fai -> last_successful_login_at = GetCurrentTimestamp ();
309
326
}
310
327
311
- if ((TimestampDifferenceExceeds (* last_log_timestamp , now , waittime ))) {
328
+ waittime_msec = 1000 * 60 * log_period_guc ;
329
+ if (waittime_msec > 0 && (TimestampDifferenceExceeds (* last_log_timestamp , now , waittime_msec )))
330
+ {
312
331
* last_log_timestamp = now ;
313
332
LWLockRelease (auth_mon_lock );
314
333
log_pg_auth_mon_data ();
334
+ // prevent double-release of auth_mon_lock
335
+ return ;
315
336
}
316
337
317
338
LWLockRelease (auth_mon_lock );
0 commit comments