forked from okbob/pltoolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
89 lines (76 loc) · 1.93 KB
/
utils.c
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
/*************************************
* Author: Pavel Stehule @2010
*
* if you like this sw, then send postcard from your home on address
*
* Pavel Stehule
* Skalice 12
* 256 01 Benesov u Prahy
* Czech Republic
*
* licenced under BSD licence
*/
#include "postgres.h"
#include "funcapi.h"
#include "utils/lsyscache.h"
#include <time.h>
PG_FUNCTION_INFO_V1(pst_counter);
Datum pst_counter(PG_FUNCTION_ARGS);
typedef struct
{
long int iterations;
int freq;
Oid typoutput;
time_t start;
} counter_cache;
/*
* raise notice every n call,
* returns input without change
*/
Datum
pst_counter(PG_FUNCTION_ARGS)
{
Datum value = PG_GETARG_DATUM(0);
counter_cache *ptr = (counter_cache *) fcinfo->flinfo->fn_extra;
if (ptr == NULL)
{
fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt,
sizeof(counter_cache));
ptr = (counter_cache *) fcinfo->flinfo->fn_extra;
ptr->iterations = 0;
ptr->start = time(NULL);
ptr->typoutput = InvalidOid;
if (PG_ARGISNULL(1))
elog(ERROR, "second parameter (output frequency) must not be NULL");
ptr->freq = PG_GETARG_INT32(1);
if (!PG_ARGISNULL(2) && PG_GETARG_BOOL(2))
{
Oid valtype;
Oid typoutput;
bool typIsVarlena;
valtype = get_fn_expr_argtype(fcinfo->flinfo, 0);
getTypeOutputInfo(valtype, &typoutput, &typIsVarlena);
ptr->typoutput = typoutput;
}
}
if (++ptr->iterations % ptr->freq == 0)
{
time_t delta = (time(NULL) - ptr->start);
if (!OidIsValid(ptr->typoutput))
{
elog(NOTICE, "[%ld] processed %ld rows", delta, ptr->iterations);
}
else
{
/* show a processed row, when it's requested */
if (PG_ARGISNULL(0))
elog(NOTICE, "[%ld] processed %ld rows, current value is null", delta, ptr->iterations);
else
{
elog(NOTICE, "[%ld] processed %ld rows, current value is '%s'", delta, ptr->iterations,
OidOutputFunctionCall(ptr->typoutput, value));
}
}
}
PG_RETURN_DATUM(value);
}