Add support for user-defined I/O conversion casts.
[PostgreSQL.git] / src / backend / executor / instrument.c
blob60c1b19c18a5365807add152ef34a7fb8f4d64af
1 /*-------------------------------------------------------------------------
3 * instrument.c
4 * functions for instrumentation of plan execution
7 * Copyright (c) 2001-2008, PostgreSQL Global Development Group
9 * IDENTIFICATION
10 * $PostgreSQL$
12 *-------------------------------------------------------------------------
14 #include "postgres.h"
16 #include <unistd.h>
18 #include "executor/instrument.h"
21 /* Allocate new instrumentation structure(s) */
22 Instrumentation *
23 InstrAlloc(int n)
25 Instrumentation *instr = palloc0(n * sizeof(Instrumentation));
27 /* we don't need to do any initialization except zero 'em */
29 return instr;
32 /* Entry to a plan node */
33 void
34 InstrStartNode(Instrumentation *instr)
36 if (INSTR_TIME_IS_ZERO(instr->starttime))
37 INSTR_TIME_SET_CURRENT(instr->starttime);
38 else
39 elog(DEBUG2, "InstrStartNode called twice in a row");
42 /* Exit from a plan node */
43 void
44 InstrStopNode(Instrumentation *instr, double nTuples)
46 instr_time endtime;
48 /* count the returned tuples */
49 instr->tuplecount += nTuples;
51 if (INSTR_TIME_IS_ZERO(instr->starttime))
53 elog(DEBUG2, "InstrStopNode called without start");
54 return;
57 INSTR_TIME_SET_CURRENT(endtime);
58 INSTR_TIME_ACCUM_DIFF(instr->counter, endtime, instr->starttime);
60 INSTR_TIME_SET_ZERO(instr->starttime);
62 /* Is this the first tuple of this cycle? */
63 if (!instr->running)
65 instr->running = true;
66 instr->firsttuple = INSTR_TIME_GET_DOUBLE(instr->counter);
70 /* Finish a run cycle for a plan node */
71 void
72 InstrEndLoop(Instrumentation *instr)
74 double totaltime;
76 /* Skip if nothing has happened, or already shut down */
77 if (!instr->running)
78 return;
80 if (!INSTR_TIME_IS_ZERO(instr->starttime))
81 elog(DEBUG2, "InstrEndLoop called on running node");
83 /* Accumulate per-cycle statistics into totals */
84 totaltime = INSTR_TIME_GET_DOUBLE(instr->counter);
86 instr->startup += instr->firsttuple;
87 instr->total += totaltime;
88 instr->ntuples += instr->tuplecount;
89 instr->nloops += 1;
91 /* Reset for next cycle (if any) */
92 instr->running = false;
93 INSTR_TIME_SET_ZERO(instr->starttime);
94 INSTR_TIME_SET_ZERO(instr->counter);
95 instr->firsttuple = 0;
96 instr->tuplecount = 0;