_PG_init首先使用DefineCustomTypeVariable定义GUC变量,以pgaudit.log为例,auditLog是pgaudit.c中定义的char *指针变量,check_pgaudit_log和assign_pgaudit_log是pgaudit.c中定义的函数。
/* Define GUC variables and install hooks upon module load. */
void _PG_init(void) {
...
/* Define pgaudit.log */
DefineCustomStringVariable(
"pgaudit.log",
"Specifies which classes of statements will be logged by session audit "
"logging. Multiple classes can be provided using a comma-separated "
"list and classes can be subtracted by prefacing the class with a "
"- sign.",
NULL,
&auditLog,
"none",
PGC_SUSET,
GUC_LIST_INPUT | GUC_NOT_IN_SAMPLE,
check_pgaudit_log,
assign_pgaudit_log,
NULL);
....
将我们定义的钩子加入PG的钩子链。如下图就是pgaudit.c中定义的临时钩子指针变量,用于保存PG已经引入的钩子变量,然后将我们定义的钩子挂在标准的钩子上,也就是我们熟悉的单链表头插法。
/* Install our hook functions after saving the existing pointers to preserve the chains. */
next_ExecutorStart_hook = ExecutorStart_hook;
ExecutorStart_hook = pgaudit_ExecutorStart_hook;
next_ExecutorCheckPerms_hook = ExecutorCheckPerms_hook;
ExecutorCheckPerms_hook = pgaudit_ExecutorCheckPerms_hook;
next_ProcessUtility_hook = ProcessUtility_hook;
ProcessUtility_hook = pgaudit_ProcessUtility_hook;
next_object_access_hook = object_access_hook;
object_access_hook = pgaudit_object_access_hook;
/* The following hook functions are required to get rows */
next_ExecutorRun_hook = ExecutorRun_hook;
ExecutorRun_hook = pgaudit_ExecutorRun_hook;
next_ExecutorEnd_hook = ExecutorEnd_hook;
ExecutorEnd_hook = pgaudit_ExecutorEnd_hook;
https://github.com/pgaudit/pgaudit/blob/master/pgaudit.c
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。