-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpg_log_trunc.c
74 lines (61 loc) · 1.5 KB
/
pg_log_trunc.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
/* -------------------------------------------------------------------------
*
* pg_log_trunc.c
*
* Copyright (c) 2024, PostgreSQL Global Development Group
*
* IDENTIFICATION
* contrib/pg_log_trunc/pg_log_trunc.c
*
* -------------------------------------------------------------------------
*/
#include "postgres.h"
#include <limits.h>
#include "libpq/auth.h"
#include "port.h"
#include "utils/guc.h"
#include "utils/timestamp.h"
PG_MODULE_MAGIC;
/* GUC Variables */
static int pg_log_trunc_len = 0;
/* Original Hook */
static emit_log_hook_type original_emit_log_hook = NULL;
/* Log hook */
static void
pg_log_trunc_emit_log_hook(ErrorData *edata)
{
/* disabled */
if (pg_log_trunc_len == 0) {
return;
}
if (edata->internalquery && strlen(edata->internalquery) > pg_log_trunc_len)
edata->internalquery[pg_log_trunc_len] = '\0';
if (edata->message && strlen(edata->message) > pg_log_trunc_len)
edata->message[pg_log_trunc_len] = '\0';
if (original_emit_log_hook) {
original_emit_log_hook(edata);
}
}
/*
* Module Load Callback
*/
void
_PG_init(void)
{
/* Define custom GUC variables */
DefineCustomIntVariable("pg_log_trunc.field_max_lenght",
"Max field lenght in chars",
NULL,
&pg_log_trunc_len,
0,
0, INT_MAX,
PGC_USERSET,
GUC_UNIT_MEMORY,
NULL,
NULL,
NULL);
MarkGUCPrefixReserved("pg_log_trunc");
/* Install Hooks */
original_emit_log_hook = emit_log_hook;
emit_log_hook = pg_log_trunc_emit_log_hook;
}