PostgreSQL plug-in hook mechanism
Internal_load_library postgresql- > PG_init = (PG_init_t) pg_dlsym (file_scanner- > handle, "_ PG_init"); if (PG_init) (* PG_init) (); internal_unload_library (const char * libname)-> PG_fini = (PG_fini_t) pg_dlsym (file_scanner- > handle, "_ PG_fini") If (PG_fini) (* PG_fini) (); take ClientAuthentication_hook_type as an example auth.h:// declares the function used by the plug-in extern void ClientAuthentication (Port * port); / * Hook for plugins to get control in ClientAuthentication () * / typedef void (* ClientAuthentication_hook_type) (Port *, int); extern PGDLLIMPORT ClientAuthentication_hook_type ClientAuthentication_hook The auth.c:// global variable is initialized to NULL, and the initialization assignment is made in the _ PG_init function. If the plug-in is loaded, ClientAuthentication_hook is ClientAuthentication_hook_type ClientAuthentication_hook = NULL;//. If ClientAuthentication_hook is assigned, execute the implanted code InitPostgres- > PerformAuthentication- > ClientAuthentication- > if (ClientAuthentication_hook) (* ClientAuthentication_hook) (port, status); auth_delay.c:static ClientAuthentication_hook_type original_client_auth_hook = NULL / * * Module Load Callback * / void _ PG_init (void) {/ * Define custom GUC variables * / DefineCustomIntVariable ("auth_delay.milliseconds", "Milliseconds to delay before reporting authentication failure", NULL, & auth_delay_milliseconds, 0 0, INT_MAX / 1000, PGC_SIGHUP, GUC_UNIT_MS, NULL, NULL, NULL) / * Install Hooks * / original_client_auth_hook = ClientAuthentication_hook; ClientAuthentication_hook= auth_delay_checks;} / * this function is called if uninstalled, which actually assigns ClientAuthentication_hook back to the original value * / void_PG_fini (void) {ClientAuthentication_hook=original_client_auth_hook } / * * / static void auth_delay_checks (Port * port, int status) {if (original_client_auth_hook) original_client_auth_hook (port, status); if (status! = STATUS_OK) {pg_usleep (1000L * auth_delay_milliseconds);}}