Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 321 lines (262 sloc) 9.19 kb
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/*-------------------------------------------------------------------------
*
* log_functions.c
* Log steps and/or statements inside a PL/pgsql function.
*
*
* Copyright (c) 2011-2013, Guillaume Lelarge (Dalibo),
* guillaume.lelarge@dalibo.com
*
*-------------------------------------------------------------------------
*/


/**********************************************************************
* Headers
**********************************************************************/

#include <stdio.h>
#include "postgres.h"
#if PG_VERSION_NUM >= 90300
#include "access/htup_details.h"
#endif
#include "utils/syscache.h"
#include "catalog/pg_proc.h"
#include "plpgsql.h"
#include "utils/guc.h"

PG_MODULE_MAGIC;

/**********************************************************************
* GUC variables
**********************************************************************/

static bool log_functions_log_declare = true;
static bool log_functions_log_function_begin = true;
static bool log_functions_log_function_end = true;
static bool log_functions_log_statement_begin = false;
static bool log_functions_log_statement_end = false;

/**********************************************************************
* Function prototypes
**********************************************************************/

void load_plugin( PLpgSQL_plugin * hooks );
static void profiler_init(PLpgSQL_execstate * estate, PLpgSQL_function * func);
static void profiler_func_beg(PLpgSQL_execstate * estate, PLpgSQL_function * func);
static void profiler_func_end(PLpgSQL_execstate * estate, PLpgSQL_function * func);
static void profiler_stmt_beg(PLpgSQL_execstate * estate, PLpgSQL_stmt * stmt);
static void profiler_stmt_end(PLpgSQL_execstate * estate, PLpgSQL_stmt * stmt);
char *decode_stmt_type(int typ);

/**********************************************************************
* Other variables
**********************************************************************/

static PLpgSQL_plugin plugin_funcs = { profiler_init, profiler_func_beg, profiler_func_end, profiler_stmt_beg, profiler_stmt_end };

/**********************************************************************
* Function definitions
**********************************************************************/

/* -------------------------------------------------------------------
* _PG_init()
* ------------------------------------------------------------------*/

void _PG_init( void )
{
PLpgSQL_plugin ** var_ptr = (PLpgSQL_plugin **) find_rendezvous_variable("PLpgSQL_plugin");

*var_ptr = &plugin_funcs;

/* Define custom GUC variables. */
DefineCustomBoolVariable("log_functions.log_declare",
"Logs the start of the DECLARE block.",
NULL,
&log_functions_log_declare,
true,
PGC_SUSET,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL);
DefineCustomBoolVariable("log_functions.log_function_begin",
"Logs the start of the BEGIN/END block.",
NULL,
&log_functions_log_function_begin,
true,
PGC_SUSET,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL);
DefineCustomBoolVariable("log_functions.log_function_end",
"Logs the end of the BEGIN/END block.",
NULL,
&log_functions_log_function_end,
true,
PGC_SUSET,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL);
DefineCustomBoolVariable("log_functions.log_statement_begin",
"Logs the start of a statement.",
NULL,
&log_functions_log_statement_begin,
false,
PGC_SUSET,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL);
DefineCustomBoolVariable("log_functions.log_statement_end",
"Logs the end of a statement.",
NULL,
&log_functions_log_statement_end,
false,
PGC_SUSET,
0,
#if PG_VERSION_NUM >= 90100
NULL,
#endif
NULL,
NULL);
}

/* -------------------------------------------------------------------
* load_plugin()
* ------------------------------------------------------------------*/

void load_plugin( PLpgSQL_plugin * hooks )
{
hooks->func_setup = profiler_init;
hooks->func_beg = profiler_func_beg;
hooks->func_end = profiler_func_end;
hooks->stmt_beg = profiler_stmt_beg;
hooks->stmt_end = profiler_stmt_end;
}

/* -------------------------------------------------------------------
* findProcName()
* ------------------------------------------------------------------*/

static char * findProcName(Oid oid)
{
HeapTuple procTuple;
char *procName;

procTuple = SearchSysCache(PROCOID, ObjectIdGetDatum(oid), 0, 0, 0);

if(!HeapTupleIsValid(procTuple))
elog(ERROR, "log_functions: cache lookup for proc %u failed", oid);

procName = NameStr(((Form_pg_proc) GETSTRUCT(procTuple))->proname);
ReleaseSysCache(procTuple);

return procName;
}

/* -------------------------------------------------------------------
* profiler_init()
* ------------------------------------------------------------------*/

static void profiler_init( PLpgSQL_execstate * estate, PLpgSQL_function * func )
{
if (log_functions_log_declare)
elog(LOG, "log_functions, DECLARE, %s", findProcName(func->fn_oid));
}

/* -------------------------------------------------------------------
* profiler_func_beg()
* ------------------------------------------------------------------*/

static void profiler_func_beg( PLpgSQL_execstate * estate, PLpgSQL_function * func )
{
if (log_functions_log_function_begin)
elog(LOG, "log_functions, BEGIN, %s", findProcName(func->fn_oid));
}

/* -------------------------------------------------------------------
* profiler_func_end()
* ------------------------------------------------------------------*/

static void profiler_func_end( PLpgSQL_execstate * estate, PLpgSQL_function * func )
{
if (log_functions_log_function_end)
elog(LOG, "log_functions, END, %s", findProcName(func->fn_oid));
}

/* -------------------------------------------------------------------
* profiler_stmt_beg()
* ------------------------------------------------------------------*/

static void profiler_stmt_beg( PLpgSQL_execstate * estate, PLpgSQL_stmt * stmt )
{
if (log_functions_log_statement_begin)
elog(LOG, "log_functions, STMT START, line %d, type %s", stmt->lineno, decode_stmt_type(stmt->cmd_type));
}

/* -------------------------------------------------------------------
* profiler_stmt_end()
* ------------------------------------------------------------------*/

static void profiler_stmt_end( PLpgSQL_execstate * estate, PLpgSQL_stmt * stmt )
{
if (log_functions_log_statement_end)
elog(LOG, "log_functions, STMT STOP, line %d, type %s", stmt->lineno, decode_stmt_type(stmt->cmd_type));
}

/* -------------------------------------------------------------------
* decode_stmt_type()
* ------------------------------------------------------------------*/

char *decode_stmt_type(int typ)
{
char *decoded_type = "";

    switch (typ)
    {
        case PLPGSQL_STMT_BLOCK:
            decoded_type = "BLOCK";
            break;

        case PLPGSQL_STMT_ASSIGN:
            decoded_type = "ASSIGN";
            break;

        case PLPGSQL_STMT_PERFORM:
            decoded_type = "PERFORM";
            break;

        case PLPGSQL_STMT_GETDIAG:
            decoded_type = "GETDIAG";
            break;

        case PLPGSQL_STMT_IF:
            decoded_type = "IF";
            break;

        case PLPGSQL_STMT_CASE:
            decoded_type = "CASE";
            break;

        case PLPGSQL_STMT_LOOP:
            decoded_type = "LOOP";
            break;

        case PLPGSQL_STMT_WHILE:
            decoded_type = "WHILE";
            break;

        case PLPGSQL_STMT_FORI:
            decoded_type = "FORI";
            break;

        case PLPGSQL_STMT_FORS:
            decoded_type = "FORS";
            break;

        case PLPGSQL_STMT_FORC:
            decoded_type = "FORC";
            break;

        case PLPGSQL_STMT_EXIT:
            decoded_type = "EXIT";
            break;

        case PLPGSQL_STMT_RETURN:
            decoded_type = "RETURN";
            break;

        case PLPGSQL_STMT_RETURN_NEXT:
            decoded_type = "RETURN NEXT";
            break;

        case PLPGSQL_STMT_RETURN_QUERY:
            decoded_type = "RETURN QUERY";
            break;

        case PLPGSQL_STMT_RAISE:
            decoded_type = "RAISE";
            break;

        case PLPGSQL_STMT_EXECSQL:
            decoded_type = "EXEC SQL";
            break;

        case PLPGSQL_STMT_DYNEXECUTE:
            decoded_type = "DYNEXECUTE";
            break;

        case PLPGSQL_STMT_DYNFORS:
            decoded_type = "DYNFORS";
            break;

        case PLPGSQL_STMT_OPEN:
            decoded_type = "OPEN";
            break;

        case PLPGSQL_STMT_FETCH:
            decoded_type = "FETCH";
            break;

        case PLPGSQL_STMT_CLOSE:
            decoded_type = "CLOSE";
            break;

        default:
            sprintf(decoded_type, "unkown (%d)", typ);
    }

    return decoded_type;
}
Something went wrong with that request. Please try again.