|
| 1 | +/* ------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * test_session_hooks.c |
| 4 | + * Code for testing SESSION hooks. |
| 5 | + * |
| 6 | + * Copyright (c) 2010-2017, PostgreSQL Global Development Group |
| 7 | + * |
| 8 | + * IDENTIFICATION |
| 9 | + * src/test/modules/test_session_hooks/test_session_hooks.c |
| 10 | + * |
| 11 | + * ------------------------------------------------------------------------- |
| 12 | + */ |
| 13 | +#include "postgres.h" |
| 14 | + |
| 15 | +#include "access/xact.h" |
| 16 | +#include "commands/dbcommands.h" |
| 17 | +#include "executor/spi.h" |
| 18 | +#include "lib/stringinfo.h" |
| 19 | +#include "miscadmin.h" |
| 20 | +#include "tcop/tcopprot.h" |
| 21 | +#include "utils/snapmgr.h" |
| 22 | +#include "utils/builtins.h" |
| 23 | + |
| 24 | +PG_MODULE_MAGIC; |
| 25 | + |
| 26 | +/* Entry point of library loading/unloading */ |
| 27 | +void _PG_init(void); |
| 28 | +void _PG_fini(void); |
| 29 | + |
| 30 | +/* GUC variables */ |
| 31 | +static char *session_hook_username = "postgres"; |
| 32 | + |
| 33 | +/* Original Hook */ |
| 34 | +static session_start_hook_type prev_session_start_hook = NULL; |
| 35 | +static session_end_hook_type prev_session_end_hook = NULL; |
| 36 | + |
| 37 | +static void |
| 38 | +register_session_hook(const char *hook_at) |
| 39 | +{ |
| 40 | + const char *username; |
| 41 | + |
| 42 | + StartTransactionCommand(); |
| 43 | + SPI_connect(); |
| 44 | + PushActiveSnapshot(GetTransactionSnapshot()); |
| 45 | + |
| 46 | + username = GetUserNameFromId(GetUserId(), false); |
| 47 | + |
| 48 | + /* Register log just for configured username */ |
| 49 | + if (!strcmp(username, session_hook_username)) |
| 50 | + { |
| 51 | + const char *dbname; |
| 52 | + int ret; |
| 53 | + StringInfoData buf; |
| 54 | + |
| 55 | + dbname = get_database_name(MyDatabaseId); |
| 56 | + |
| 57 | + initStringInfo(&buf); |
| 58 | + |
| 59 | + appendStringInfo(&buf, "INSERT INTO session_hook_log (dbname, username, hook_at) "); |
| 60 | + appendStringInfo(&buf, "VALUES ('%s', '%s', '%s');", |
| 61 | + dbname, username, hook_at); |
| 62 | + |
| 63 | + ret = SPI_exec(buf.data, 0); |
| 64 | + if (ret != SPI_OK_INSERT) |
| 65 | + elog(ERROR, "SPI_execute failed: error code %d", ret); |
| 66 | + } |
| 67 | + |
| 68 | + SPI_finish(); |
| 69 | + PopActiveSnapshot(); |
| 70 | + CommitTransactionCommand(); |
| 71 | +} |
| 72 | + |
| 73 | +/* sample session start hook function */ |
| 74 | +static void |
| 75 | +sample_session_start_hook() |
| 76 | +{ |
| 77 | + /* Hook just normal backends */ |
| 78 | + if (MyBackendId != InvalidBackendId) |
| 79 | + { |
| 80 | + (void) register_session_hook("START"); |
| 81 | + |
| 82 | + if (prev_session_start_hook) |
| 83 | + prev_session_start_hook(); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/* sample session end hook function */ |
| 88 | +static void |
| 89 | +sample_session_end_hook() |
| 90 | +{ |
| 91 | + /* Hook just normal backends */ |
| 92 | + if (MyBackendId != InvalidBackendId) |
| 93 | + { |
| 94 | + if (prev_session_end_hook) |
| 95 | + prev_session_end_hook(); |
| 96 | + |
| 97 | + (void) register_session_hook("END"); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/* |
| 102 | + * Module Load Callback |
| 103 | + */ |
| 104 | +void |
| 105 | +_PG_init(void) |
| 106 | +{ |
| 107 | + /* Save Hooks for Unload */ |
| 108 | + prev_session_start_hook = session_start_hook; |
| 109 | + prev_session_end_hook = session_end_hook; |
| 110 | + |
| 111 | + /* Set New Hooks */ |
| 112 | + session_start_hook = sample_session_start_hook; |
| 113 | + session_end_hook = sample_session_end_hook; |
| 114 | + |
| 115 | + /* Load GUCs */ |
| 116 | + DefineCustomStringVariable("test_session_hooks.username", |
| 117 | + "Username to register log on session start or end", |
| 118 | + NULL, |
| 119 | + &session_hook_username, |
| 120 | + "postgres", |
| 121 | + PGC_SIGHUP, |
| 122 | + 0, NULL, NULL, NULL); |
| 123 | +} |
| 124 | + |
| 125 | +/* |
| 126 | + * Module Unload Callback |
| 127 | + */ |
| 128 | +void |
| 129 | +_PG_fini(void) |
| 130 | +{ |
| 131 | + /* Uninstall Hooks */ |
| 132 | + session_start_hook = prev_session_start_hook; |
| 133 | + session_end_hook = prev_session_end_hook; |
| 134 | +} |
0 commit comments