-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpg_log_userqueries.c
1486 lines (1366 loc) · 35.6 KB
/
pg_log_userqueries.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* pg_log_userqueries.c
* Log statement according to the user.
*
*
* Copyright (c) 2011-2025, Guillaume Lelarge (Dalibo),
*
*-------------------------------------------------------------------------
*/
#ident "pg_log_userqueries version 1.5"
#include "postgres.h"
#include <unistd.h>
#include <regex.h>
#include <syslog.h>
#include <sys/stat.h>
#include <time.h>
/* to log statement duration */
#include <utils/timestamp.h>
#include <access/xact.h>
#include <storage/proc.h>
#if PG_VERSION_NUM >= 140000
// to log query_id
#include <utils/backend_status.h>
#endif
/*
* We won't use PostgreSQL regexps,
* and as they redefine some system regexps types, we make sure we don't
* redefine them
*/
#define _REGEX_H_
#include "funcapi.h"
#include "tcop/utility.h"
#include "libpq/libpq-be.h" /* For Port▸▸ ▸ ▸ ▸ ▸ */
#include "miscadmin.h" /* For MyProcPort▸ ▸ ▸ ▸ */
PG_MODULE_MAGIC;
#ifndef PG_SYSLOG_LIMIT
#define PG_SYSLOG_LIMIT 1024
#endif
#ifndef LOCAL_DEBUG
#define LOCAL_DEBUG 1
#endif
/*---- Local variables ----*/
static const struct config_enum_entry server_message_level_options[] = {
{"debug", DEBUG2, true},
{"debug5", DEBUG5, false},
{"debug4", DEBUG4, false},
{"debug3", DEBUG3, false},
{"debug2", DEBUG2, false},
{"debug1", DEBUG1, false},
{"info", INFO, false},
{"notice", NOTICE, false},
{"warning", WARNING, false},
{"error", ERROR, false},
{"log", LOG, false},
{"fatal", FATAL, false},
{"panic", PANIC, false},
{NULL, 0, false}
};
static const struct config_enum_entry log_destination_options[] = {
{"stderr", 1, false},
{"syslog", 2, false},
{NULL, 0}
};
static const struct config_enum_entry syslog_facility_options[] = {
{"local0", LOG_LOCAL0, false},
{"local1", LOG_LOCAL1, false},
{"local2", LOG_LOCAL2, false},
{"local3", LOG_LOCAL3, false},
{"local4", LOG_LOCAL4, false},
{"local5", LOG_LOCAL5, false},
{"local6", LOG_LOCAL6, false},
{"local7", LOG_LOCAL7, false},
{NULL, 0}
};
static int log_level = WARNING;
static char * log_label = NULL;
static char * log_user = NULL;
static char * log_user_blacklist = NULL;
static char * log_query_id = NULL;
static char * log_query_id_blacklist = NULL;
static char * log_db = NULL;
static char * log_db_blacklist = NULL;
static char * log_addr = NULL;
static char * log_addr_blacklist = NULL;
static char * log_query = NULL;
static char * log_query_blacklist = NULL;
static char * log_app = NULL;
static char * log_app_blacklist = NULL;
static bool log_superusers = false;
static int regex_flags = REG_NOSUB;
static regex_t usr_regexv;
static regex_t usr_bl_regexv;
static regex_t db_regexv;
static regex_t db_bl_regexv;
static regex_t addr_regexv;
static regex_t addr_bl_regexv;
static regex_t app_regexv;
static regex_t app_bl_regexv;
static regex_t query_regexv;
static regex_t query_bl_regexv;
static regex_t query_id_regexv;
static regex_t query_id_bl_regexv;
static bool openlog_done = false;
static char * syslog_ident = NULL;
static int log_destination = 1; /* aka stderr */
static int syslog_facility = LOG_LOCAL0;
static int syslog_level = LOG_NOTICE;
static time_t ref_time = 0;
static char * file_switchoff = NULL;
static bool switch_off = false;
static int time_switchoff = 300;
static bool match_all = false;
static bool enable_log_duration = false;
/* Saved hook values in case of unload */
static ExecutorEnd_hook_type prev_ExecutorEnd = NULL;
#if PG_VERSION_NUM >= 90000
static ProcessUtility_hook_type prev_ProcessUtility = NULL;
#endif
/*---- Function declarations ----*/
void _PG_init(void);
void _PG_fini(void);
static void pgluq_ExecutorEnd(QueryDesc *queryDesc);
#if PG_VERSION_NUM >= 140000
static void pgluq_ProcessUtility(PlannedStmt *pstmt,
const char *queryString,
bool readOnlyTree,
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest, QueryCompletion *qc);
#elif PG_VERSION_NUM >= 130000
static void pgluq_ProcessUtility(PlannedStmt *pstmt,
const char *queryString, ProcessUtilityContext context, ParamListInfo params,
QueryEnvironment *queryEnv, DestReceiver *dest, QueryCompletion *qc);
#elif PG_VERSION_NUM >= 100000
static void pgluq_ProcessUtility(PlannedStmt *pstmt,
const char *queryString, ProcessUtilityContext context, ParamListInfo params,
QueryEnvironment *queryEnv, DestReceiver *dest, char *completionTag);
#elif PG_VERSION_NUM >= 90300
static void pgluq_ProcessUtility(Node *parsetree,
const char *queryString, ProcessUtilityContext context, ParamListInfo params,
DestReceiver *dest, char *completionTag);
#elif PG_VERSION_NUM >= 90000
static void pgluq_ProcessUtility(Node *parsetree,
const char *queryString, ParamListInfo params, bool isTopLevel,
DestReceiver *dest, char *completionTag);
#endif
#if PG_VERSION_NUM >= 140000
static bool pgluq_check_log(PlannedStmt *pstmt);
#else
static bool pgluq_check_log(void);
#endif
static void pgluq_log(const char *query);
static void write_syslog(int level, char *line);
static char *log_prefix(const char *query);
static bool check_switchoff(void);
static bool check_time_switch(void);
extern char *get_database_name(Oid dbid);
extern int pg_mbcliplen(const char *mbstr, int len, int limit);
static bool pgluq_checkitem(const char *item,
const char *log_wl, regex_t *regex_wl,
const char *log_bl, regex_t *regex_bl);
#if PG_VERSION_NUM >= 140000
static bool pgluq_checkBLitem(const char *item,
const char *log_bl, regex_t *regex_bl);
static bool pgluq_checkWLitem(const char *item,
const char *log_wl, regex_t *regex_wl);
#endif
/*
* Module load callback
*/
void
_PG_init(void)
{
/*
* In order to create our shared memory area, we have to be loaded via
* shared_preload_libraries. If not, fall out without hooking into any of
* the main system. (We don't throw error here because it seems useful to
* allow the pgluq_log functions to be created even when the module
* isn't active. The functions must protect themselves against
* being called then, however.)
*/
if (!process_shared_preload_libraries_in_progress)
return;
/*
* Define (or redefine) custom GUC variables.
*/
DefineCustomStringVariable( "pg_log_userqueries.log_query_id",
"Log statement according to the given query_id.",
NULL,
&log_query_id,
NULL,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomStringVariable( "pg_log_userqueries.log_query_id_blacklist",
"Do not log statement with query_id in this list.",
NULL,
&log_query_id_blacklist,
NULL,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomStringVariable( "pg_log_userqueries.log_label",
"Label in front of the user query.",
NULL,
&log_label,
"pg_log_userqueries",
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomEnumVariable( "pg_log_userqueries.log_level",
"Selects level of log (same options than log_min_messages).",
NULL,
&log_level,
log_level,
server_message_level_options,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL);
DefineCustomStringVariable( "pg_log_userqueries.log_user",
"Log statement according to the given user.",
NULL,
&log_user,
NULL,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomStringVariable( "pg_log_userqueries.log_user_blacklist",
"Do not log statement from user in this list.",
NULL,
&log_user_blacklist,
NULL,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomStringVariable( "pg_log_userqueries.log_db",
"Log statement according to the given database.",
NULL,
&log_db,
NULL,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomStringVariable( "pg_log_userqueries.log_db_blacklist",
"Do not log statement in databases in this list.",
NULL,
&log_db_blacklist,
NULL,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomStringVariable( "pg_log_userqueries.log_addr",
"Log statement according to the given client IP address.",
NULL,
&log_addr,
NULL,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomStringVariable( "pg_log_userqueries.log_addr_blacklist",
"Do not log statement from addresses in this list.",
NULL,
&log_addr_blacklist,
NULL,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomStringVariable( "pg_log_userqueries.log_app",
"Log statement according to the given application name.",
NULL,
&log_app,
NULL,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomStringVariable( "pg_log_userqueries.log_app_blacklist",
"Do not log statement from the applications in this list.",
NULL,
&log_app_blacklist,
NULL,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomEnumVariable( "pg_log_userqueries.log_destination",
"Selects log destination (either stderr or syslog).",
NULL,
&log_destination,
log_destination,
log_destination_options,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL);
DefineCustomEnumVariable( "pg_log_userqueries.syslog_facility",
"Selects syslog level of log (same options than PostgreSQL syslog_facility).",
NULL,
&syslog_facility,
syslog_facility,
syslog_facility_options,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL);
DefineCustomStringVariable( "pg_log_userqueries.syslog_ident",
"Select syslog program identity name.",
NULL,
&syslog_ident,
"pg_log_userqueries",
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomBoolVariable( "pg_log_userqueries.log_superusers",
"Enable log of superusers.",
NULL,
&log_superusers,
false,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL);
DefineCustomStringVariable( "pg_log_userqueries.file_switchoff",
"If file exists, owned by root with the right properties, switch off the trace log.",
NULL,
&file_switchoff,
NULL,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL);
DefineCustomIntVariable( "pg_log_userqueries.time_switchoff",
"Time interval between file_switchoff existence checks.",
NULL,
&time_switchoff,
300, /* 5 min by default */
30,
3600,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL);
DefineCustomStringVariable( "pg_log_userqueries.log_query",
"Log statement according to the given regular expression.",
NULL,
&log_query,
NULL,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomBoolVariable( "pg_log_userqueries.match_all",
"Log statement only when all defined conditions for log_user, log_db, log_addr, log_app and log_query match.",
NULL,
&match_all,
false,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL);
DefineCustomStringVariable( "pg_log_userqueries.log_query_blacklist",
"Do not log statement in this list.",
NULL,
&log_query_blacklist,
NULL,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL );
DefineCustomBoolVariable( "pg_log_userqueries.log_duration",
"Enable log of query duration.",
NULL,
&enable_log_duration,
false,
PGC_POSTMASTER,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL);
/* Reserve the GUC prefix */
#if PG_VERSION_NUM < 150000
EmitWarningsOnPlaceholders("pg_log_userqueries");
#else
MarkGUCPrefixReserved("pg_log_userqueries");
#endif
/* Add support to extended regex search */
regex_flags |= REG_EXTENDED;
/* Compile regexp for query_id */
if (log_query_id != NULL)
{
char *tmp;
tmp = palloc(sizeof(char) * (strlen(log_query_id) + 5));
sprintf(tmp, "^(%s)$", log_query_id);
if (regcomp(&query_id_regexv, tmp, regex_flags) != 0)
{
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("pg_log_userqueries: invalid query_id pattern %s", tmp)));
log_query_id = NULL;
}
pfree(tmp);
}
/* Compile regexp for query_id blacklist */
if (log_query_id_blacklist != NULL)
{
char *tmp;
tmp = palloc(sizeof(char) * (strlen(log_query_id_blacklist) + 5));
sprintf(tmp, "^(%s)$", log_query_id_blacklist);
if (regcomp(&query_id_bl_regexv, tmp, regex_flags) != 0)
{
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("pg_log_userqueries: invalid query_id blacklist pattern %s", tmp)));
log_query_id_blacklist = NULL;
}
pfree(tmp);
}
/* Compile regexp for user name */
if (log_user != NULL)
{
char *tmp;
tmp = palloc(sizeof(char) * (strlen(log_user) + 5));
sprintf(tmp, "^(%s)$", log_user);
if (regcomp(&usr_regexv, tmp, regex_flags) != 0)
{
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("pg_log_userqueries: invalid user pattern %s", tmp)));
log_user = NULL;
}
pfree(tmp);
}
/* Compile regexp for user name blacklist */
if (log_user_blacklist != NULL)
{
char *tmp;
tmp = palloc(sizeof(char) * (strlen(log_user_blacklist) + 5));
sprintf(tmp, "^(%s)$", log_user_blacklist);
if (regcomp(&usr_bl_regexv, tmp, regex_flags) != 0)
{
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("pg_log_userqueries: invalid user blacklist pattern %s", tmp)));
log_user_blacklist = NULL;
}
pfree(tmp);
}
/* Compile regexp for db name */
if (log_db != NULL)
{
char *tmp;
tmp = palloc(sizeof(char) * (strlen(log_db) + 5));
sprintf(tmp, "^(%s)$", log_db);
if (regcomp(&db_regexv, tmp, regex_flags) != 0)
{
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("pg_log_userqueries: invalid database pattern %s", tmp)));
log_db = NULL;
}
pfree(tmp);
}
/* Compile regexp for db name blacklist */
if (log_db_blacklist != NULL)
{
char *tmp;
tmp = palloc(sizeof(char) * (strlen(log_db_blacklist) + 5));
sprintf(tmp, "^(%s)$", log_db_blacklist);
if (regcomp(&db_bl_regexv, tmp, regex_flags) != 0)
{
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("pg_log_userqueries: invalid database blacklist pattern %s", tmp)));
log_db_blacklist = NULL;
}
pfree(tmp);
}
/* Compile regexp for inet addr */
if (log_addr != NULL)
{
char *tmp;
tmp = palloc(sizeof(char) * (strlen(log_addr) + 5));
sprintf(tmp, "^(%s)$", log_addr);
if (regcomp(&addr_regexv, tmp, regex_flags) != 0)
{
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("pg_log_userqueries: invalid address pattern %s", tmp)));
log_addr = NULL;
}
pfree(tmp);
}
/* Compile regexp for inet addr blacklist */
if (log_addr_blacklist != NULL)
{
char *tmp;
tmp = palloc(sizeof(char) * (strlen(log_addr_blacklist) + 5));
sprintf(tmp, "^(%s)$", log_addr_blacklist);
if (regcomp(&addr_bl_regexv, tmp, regex_flags) != 0)
{
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("pg_log_userqueries: invalid address blacklist pattern %s", tmp)));
log_addr_blacklist = NULL;
}
pfree(tmp);
}
/* Compile regexp for application name */
if (log_app != NULL)
{
char *tmp;
tmp = palloc(sizeof(char) * (strlen(log_app) + 5));
sprintf(tmp, "^(%s)$", log_app);
if (regcomp(&app_regexv, tmp, regex_flags) != 0)
{
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("pg_log_userqueries: invalid application name pattern %s", tmp)));
log_app = NULL;
}
pfree(tmp);
}
/* Compile regexp for application name blacklist */
if (log_app_blacklist != NULL)
{
char *tmp;
tmp = palloc(sizeof(char) * (strlen(log_app_blacklist) + 5));
sprintf(tmp, "^(%s)$", log_app_blacklist);
if (regcomp(&app_bl_regexv, tmp, regex_flags) != 0)
{
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("pg_log_userqueries: invalid application name blacklist pattern %s", tmp)));
log_app_blacklist = NULL;
}
pfree(tmp);
}
/* Compile rexgep to log statement */
if (log_query != NULL)
{
if (regcomp(&query_regexv, log_query, regex_flags) != 0)
{
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("pg_log_userqueries: invalid statement regexp pattern %s", log_query)));
log_query = NULL;
}
}
/* Compile rexgep from the log statement blacklist */
if (log_query_blacklist != NULL)
{
if (regcomp(&query_bl_regexv, log_query_blacklist, regex_flags) != 0)
{
ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("pg_log_userqueries: invalid statement regexp blacklist pattern %s", log_query_blacklist)));
log_query_blacklist = NULL;
}
}
/* Open syslog descriptor, if required */
if (log_destination == 2 /* aka syslog */)
{
/* Find syslog_level according to the user log_level setting */
switch (log_level)
{
case DEBUG5:
case DEBUG4:
case DEBUG3:
case DEBUG2:
case DEBUG1:
syslog_level = LOG_DEBUG;
break;
case LOG:
case COMMERROR:
case INFO:
syslog_level = LOG_INFO;
break;
case NOTICE:
case WARNING:
syslog_level = LOG_NOTICE;
break;
case ERROR:
syslog_level = LOG_WARNING;
break;
case FATAL:
syslog_level = LOG_ERR;
break;
case PANIC:
default:
syslog_level = LOG_CRIT;
break;
}
/* Open syslog descriptor */
openlog(syslog_ident, LOG_PID | LOG_NDELAY | LOG_NOWAIT, syslog_facility);
openlog_done = true;
}
/*
* Install hooks.
*/
prev_ExecutorEnd = ExecutorEnd_hook;
ExecutorEnd_hook = pgluq_ExecutorEnd;
#if PG_VERSION_NUM >= 90000
prev_ProcessUtility = ProcessUtility_hook;
ProcessUtility_hook = pgluq_ProcessUtility;
#endif
}
/*
* Module unload callback
*/
void
_PG_fini(void)
{
/* Close syslog descriptor, if required */
if (openlog_done)
{
closelog();
openlog_done = false;
}
/* Uninstall hooks. */
ExecutorEnd_hook = prev_ExecutorEnd;
#if PG_VERSION_NUM >= 90000
ProcessUtility_hook = prev_ProcessUtility;
#endif
/* free the memory allocated by regcomp.
* This is safe since we made sure the log_* parameters where reset
* to NULL in case regcomp fails.
*/
if (log_user != NULL)
regfree(&usr_regexv);
if (log_user_blacklist != NULL)
regfree(&usr_bl_regexv);
if (log_db != NULL)
regfree(&db_regexv);
if (log_db_blacklist != NULL)
regfree(&db_bl_regexv);
if (log_addr != NULL)
regfree(&addr_regexv);
if (log_addr_blacklist != NULL)
regfree(&addr_bl_regexv);
if (log_app != NULL)
regfree(&app_regexv);
if (log_app_blacklist != NULL)
regfree(&app_bl_regexv);
if (log_query != NULL)
regfree(&query_regexv);
if (log_query_blacklist != NULL)
regfree(&query_bl_regexv);
if (log_query_id != NULL)
regfree(&query_id_regexv);
if (log_query_id_blacklist != NULL)
regfree(&query_id_bl_regexv);
}
/*
* ExecutorEnd hook: store results if needed
*/
static void
pgluq_ExecutorEnd(QueryDesc *queryDesc)
{
#if PG_VERSION_NUM >= 140000
if (pgluq_check_log(queryDesc->plannedstmt))
#else
if (pgluq_check_log())
#endif
pgluq_log(queryDesc->sourceText);
if (prev_ExecutorEnd)
prev_ExecutorEnd(queryDesc);
else
standard_ExecutorEnd(queryDesc);
}
/*
* ProcessUtility hook
* (only available from 9.0 releases)
*/
#if PG_VERSION_NUM >= 140000
static void pgluq_ProcessUtility(PlannedStmt *pstmt,
const char *queryString,
bool readOnlyTree,
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest, QueryCompletion *qc)
{
PG_TRY();
{
if (prev_ProcessUtility)
prev_ProcessUtility(pstmt, queryString, readOnlyTree, context,
params, queryEnv, dest, qc);
else
standard_ProcessUtility(pstmt, queryString, readOnlyTree, context,
params, queryEnv, dest, qc);
}
PG_CATCH();
{
PG_RE_THROW();
}
PG_END_TRY();
if (pgluq_check_log(pstmt))
pgluq_log(queryString);
}
#elif PG_VERSION_NUM >= 130000
static void
pgluq_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
ProcessUtilityContext context, ParamListInfo params,
QueryEnvironment *queryEnv, DestReceiver *dest,
QueryCompletion *qc)
{
PG_TRY();
{
if (prev_ProcessUtility)
prev_ProcessUtility(pstmt, queryString, context,
params, queryEnv, dest, qc);
else
standard_ProcessUtility(pstmt, queryString, context,
params, queryEnv, dest, qc);
}
PG_CATCH();
{
PG_RE_THROW();
}
PG_END_TRY();
if (pgluq_check_log())
pgluq_log(queryString);
}
#elif PG_VERSION_NUM >= 100000
static void
pgluq_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
ProcessUtilityContext context, ParamListInfo params,
QueryEnvironment *queryEnv, DestReceiver *dest,
char *completionTag)
{
PG_TRY();
{
if (prev_ProcessUtility)
prev_ProcessUtility(pstmt, queryString, context,
params, queryEnv, dest, completionTag);
else
standard_ProcessUtility(pstmt, queryString, context,
params, queryEnv, dest, completionTag);
}
PG_CATCH();
{
PG_RE_THROW();
}
PG_END_TRY();
if (pgluq_check_log())
pgluq_log(queryString);
}
#elif PG_VERSION_NUM >= 90300
static void
pgluq_ProcessUtility(Node *parsetree, const char *queryString,
ProcessUtilityContext context, ParamListInfo params,
DestReceiver *dest, char *completionTag)
{
PG_TRY();
{
if (prev_ProcessUtility)
prev_ProcessUtility(parsetree, queryString, context,
params, dest, completionTag);
else
standard_ProcessUtility(parsetree, queryString, context,
params, dest, completionTag);
}
PG_CATCH();
{
PG_RE_THROW();
}
PG_END_TRY();
if (pgluq_check_log())
pgluq_log(queryString);
}
#elif PG_VERSION_NUM >= 90000
static void
pgluq_ProcessUtility(Node *parsetree, const char *queryString,
ParamListInfo params, bool isTopLevel,
DestReceiver *dest, char *completionTag)
{
PG_TRY();
{
if (prev_ProcessUtility)
prev_ProcessUtility(parsetree, queryString, params,
isTopLevel, dest, completionTag);
else
standard_ProcessUtility(parsetree, queryString, params,
isTopLevel, dest, completionTag);
}
PG_CATCH();
{
PG_RE_THROW();
}
PG_END_TRY();
if (pgluq_check_log())
pgluq_log(queryString);
}
#endif
/*
* Check an item
*/
static bool pgluq_checkitem(const char *item,
const char *log_wl, regex_t *regex_wl,
const char *log_bl, regex_t *regex_bl)
{
bool has_passed_wl = true;
bool has_passed_bl = true;
if ((log_wl != NULL) && (regexec(regex_wl, item, 0, 0, 0) != 0))
has_passed_wl = false;
if ((log_bl != NULL) && (regexec(regex_bl, item, 0, 0, 0) == 0))
has_passed_bl= false;
if (! has_passed_wl || ! has_passed_bl) {
return false;
}
return true;
}
#if PG_VERSION_NUM >= 140000
/*
* Check an item and a blacklist
*/
static bool pgluq_checkBLitem(const char *item,
const char *log_bl, regex_t *regex_bl)
{
bool has_passed_bl = true;
if ((log_bl != NULL) && (regexec(regex_bl, item, 0, 0, 0) == 0))
has_passed_bl= false;
return has_passed_bl;
}
/*
* Check an item and a whitelist
*/
static bool pgluq_checkWLitem(const char *item,
const char *log_wl, regex_t *regex_wl)
{
bool has_passed_wl = true;
if ((log_wl != NULL) && (regexec(regex_wl, item, 0, 0, 0) != 0))
has_passed_wl = false;
return has_passed_wl;
}
#endif
/*
* Check if we should log
*/
/* I need a PlannedStmt to get the query_id */
#if PG_VERSION_NUM >= 140000
static bool pgluq_check_log(PlannedStmt *pstmt)
#else
static bool pgluq_check_log()
#endif
{
/* object's name */
char *dbname = NULL;
char *username = NULL;
char *addr = NULL;
char *appname = NULL;
bool ret = false;
bool rc;
#if PG_VERSION_NUM >= 140000
char *query_id = NULL;
uint64 int_query_id = UINT64CONST(0);
char buf[256];
#endif
if (check_switchoff())
return false;
/* query_id exists since v14 */
#if PG_VERSION_NUM >= 140000
int_query_id = pstmt->queryId;
snprintf(buf, sizeof buf, "%lu", int_query_id);
query_id = pstrdup((const char*) buf);
#endif
#if PG_VERSION_NUM >= 90602
/*
* We don't want to log the activity of background workers.
*/
if (MyProc->isBackgroundWorker)
return false;
#endif
/* Get the user name */
#if PG_VERSION_NUM >= 90500
username = GetUserNameFromId(GetUserId(), false);
#else
username = GetUserNameFromId(GetUserId());
#endif
/* Get the database name */