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