]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/pgstatfuncs.c
Convert pg_stat_get_backend_idset to use the existing SRF support.
[postgresql] / src / backend / utils / adt / pgstatfuncs.c
1 /*-------------------------------------------------------------------------
2  *
3  * pgstatfuncs.c
4  *        Functions for accessing the statistics collector data
5  *
6  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.19 2004/10/01 21:03:42 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include "access/xact.h"
18 #include "catalog/pg_shadow.h"
19 #include "fmgr.h"
20 #include "funcapi.h"
21 #include "miscadmin.h"
22 #include "nodes/execnodes.h"
23 #include "pgstat.h"
24 #include "utils/hsearch.h"
25
26 /* bogus ... these externs should be in a header file */
27 extern Datum pg_stat_get_numscans(PG_FUNCTION_ARGS);
28 extern Datum pg_stat_get_tuples_returned(PG_FUNCTION_ARGS);
29 extern Datum pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS);
30 extern Datum pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS);
31 extern Datum pg_stat_get_tuples_updated(PG_FUNCTION_ARGS);
32 extern Datum pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS);
33 extern Datum pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS);
34 extern Datum pg_stat_get_blocks_hit(PG_FUNCTION_ARGS);
35
36 extern Datum pg_stat_get_backend_idset(PG_FUNCTION_ARGS);
37 extern Datum pg_backend_pid(PG_FUNCTION_ARGS);
38 extern Datum pg_stat_reset(PG_FUNCTION_ARGS);
39 extern Datum pg_stat_get_backend_pid(PG_FUNCTION_ARGS);
40 extern Datum pg_stat_get_backend_dbid(PG_FUNCTION_ARGS);
41 extern Datum pg_stat_get_backend_userid(PG_FUNCTION_ARGS);
42 extern Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS);
43 extern Datum pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS);
44
45 extern Datum pg_stat_get_db_numbackends(PG_FUNCTION_ARGS);
46 extern Datum pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS);
47 extern Datum pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS);
48 extern Datum pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS);
49 extern Datum pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS);
50
51
52 Datum
53 pg_stat_get_numscans(PG_FUNCTION_ARGS)
54 {
55         PgStat_StatTabEntry *tabentry;
56         Oid                     relid;
57         int64           result;
58
59         relid = PG_GETARG_OID(0);
60
61         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
62                 result = 0;
63         else
64                 result = (int64) (tabentry->numscans);
65
66         PG_RETURN_INT64(result);
67 }
68
69
70 Datum
71 pg_stat_get_tuples_returned(PG_FUNCTION_ARGS)
72 {
73         PgStat_StatTabEntry *tabentry;
74         Oid                     relid;
75         int64           result;
76
77         relid = PG_GETARG_OID(0);
78
79         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
80                 result = 0;
81         else
82                 result = (int64) (tabentry->tuples_returned);
83
84         PG_RETURN_INT64(result);
85 }
86
87
88 Datum
89 pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS)
90 {
91         PgStat_StatTabEntry *tabentry;
92         Oid                     relid;
93         int64           result;
94
95         relid = PG_GETARG_OID(0);
96
97         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
98                 result = 0;
99         else
100                 result = (int64) (tabentry->tuples_fetched);
101
102         PG_RETURN_INT64(result);
103 }
104
105
106 Datum
107 pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS)
108 {
109         PgStat_StatTabEntry *tabentry;
110         Oid                     relid;
111         int64           result;
112
113         relid = PG_GETARG_OID(0);
114
115         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
116                 result = 0;
117         else
118                 result = (int64) (tabentry->tuples_inserted);
119
120         PG_RETURN_INT64(result);
121 }
122
123
124 Datum
125 pg_stat_get_tuples_updated(PG_FUNCTION_ARGS)
126 {
127         PgStat_StatTabEntry *tabentry;
128         Oid                     relid;
129         int64           result;
130
131         relid = PG_GETARG_OID(0);
132
133         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
134                 result = 0;
135         else
136                 result = (int64) (tabentry->tuples_updated);
137
138         PG_RETURN_INT64(result);
139 }
140
141
142 Datum
143 pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS)
144 {
145         PgStat_StatTabEntry *tabentry;
146         Oid                     relid;
147         int64           result;
148
149         relid = PG_GETARG_OID(0);
150
151         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
152                 result = 0;
153         else
154                 result = (int64) (tabentry->tuples_deleted);
155
156         PG_RETURN_INT64(result);
157 }
158
159
160 Datum
161 pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS)
162 {
163         PgStat_StatTabEntry *tabentry;
164         Oid                     relid;
165         int64           result;
166
167         relid = PG_GETARG_OID(0);
168
169         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
170                 result = 0;
171         else
172                 result = (int64) (tabentry->blocks_fetched);
173
174         PG_RETURN_INT64(result);
175 }
176
177
178 Datum
179 pg_stat_get_blocks_hit(PG_FUNCTION_ARGS)
180 {
181         PgStat_StatTabEntry *tabentry;
182         Oid                     relid;
183         int64           result;
184
185         relid = PG_GETARG_OID(0);
186
187         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
188                 result = 0;
189         else
190                 result = (int64) (tabentry->blocks_hit);
191
192         PG_RETURN_INT64(result);
193 }
194
195
196 Datum
197 pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
198 {
199         FuncCallContext *funcctx;
200         int                *fctx;
201         int32           result;
202
203         /* stuff done only on the first call of the function */
204         if (SRF_IS_FIRSTCALL())
205         {
206                 /* create a function context for cross-call persistence */
207                 funcctx = SRF_FIRSTCALL_INIT();
208
209                 fctx = MemoryContextAlloc(funcctx->multi_call_memory_ctx,
210                                                                   2 * sizeof(int));
211                 funcctx->user_fctx = fctx;
212
213                 fctx[0] = 0;
214                 fctx[1] = pgstat_fetch_stat_numbackends();
215         }
216
217         /* stuff done on every call of the function */
218         funcctx = SRF_PERCALL_SETUP();
219         fctx = funcctx->user_fctx;
220
221         fctx[0] += 1;
222         result = fctx[0];
223
224         if (result <= fctx[1])
225         {
226                 /* do when there is more left to send */
227                 SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
228         }
229         else
230         {
231                 /* do when there is no more left */
232                 SRF_RETURN_DONE(funcctx);
233         }
234 }
235
236
237 Datum
238 pg_backend_pid(PG_FUNCTION_ARGS)
239 {
240         PG_RETURN_INT32(MyProcPid);
241 }
242
243 /*
244  * Built-in function for resetting the counters
245  */
246 Datum
247 pg_stat_reset(PG_FUNCTION_ARGS)
248 {
249         pgstat_reset_counters();
250
251         PG_RETURN_BOOL(true);
252 }
253
254 Datum
255 pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
256 {
257         PgStat_StatBeEntry *beentry;
258         int32           beid;
259
260         beid = PG_GETARG_INT32(0);
261
262         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
263                 PG_RETURN_NULL();
264
265         PG_RETURN_INT32(beentry->procpid);
266 }
267
268
269 Datum
270 pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
271 {
272         PgStat_StatBeEntry *beentry;
273         int32           beid;
274
275         beid = PG_GETARG_INT32(0);
276
277         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
278                 PG_RETURN_NULL();
279
280         PG_RETURN_OID(beentry->databaseid);
281 }
282
283
284 Datum
285 pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
286 {
287         PgStat_StatBeEntry *beentry;
288         int32           beid;
289
290         beid = PG_GETARG_INT32(0);
291
292         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
293                 PG_RETURN_NULL();
294
295         PG_RETURN_INT32(beentry->userid);
296 }
297
298
299 Datum
300 pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
301 {
302         PgStat_StatBeEntry *beentry;
303         int32           beid;
304         int                     len;
305         char       *activity;
306         text       *result;
307
308         beid = PG_GETARG_INT32(0);
309
310         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
311                 activity = "<backend information not available>";
312         else if (!superuser() && beentry->userid != GetUserId())
313                 activity = "<insufficient privilege>";
314         else if (*(beentry->activity) == '\0')
315                 activity = "<command string not enabled>";
316         else
317                 activity = beentry->activity;
318
319         len = strlen(activity);
320         result = palloc(VARHDRSZ + len);
321         VARATT_SIZEP(result) = VARHDRSZ + len;
322         memcpy(VARDATA(result), activity, len);
323
324         PG_RETURN_TEXT_P(result);
325 }
326
327
328 Datum
329 pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
330 {
331         PgStat_StatBeEntry *beentry;
332         int32           beid;
333         AbsoluteTime sec;
334         int                     usec;
335         TimestampTz result;
336
337         beid = PG_GETARG_INT32(0);
338
339         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
340                 PG_RETURN_NULL();
341
342         if (!superuser() && beentry->userid != GetUserId())
343                 PG_RETURN_NULL();
344
345         sec = beentry->activity_start_sec;
346         usec = beentry->activity_start_usec;
347
348         /*
349          * No time recorded for start of current query -- this is the case if
350          * the user hasn't enabled query-level stats collection.
351          */
352         if (sec == 0 && usec == 0)
353                 PG_RETURN_NULL();
354
355         result = AbsoluteTimeUsecToTimestampTz(sec, usec);
356
357         PG_RETURN_TIMESTAMPTZ(result);
358 }
359
360
361 Datum
362 pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
363 {
364         PgStat_StatDBEntry *dbentry;
365         Oid                     dbid;
366         int32           result;
367
368         dbid = PG_GETARG_OID(0);
369
370         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
371                 result = 0;
372         else
373                 result = (int32) (dbentry->n_backends);
374
375         PG_RETURN_INT32(result);
376 }
377
378
379 Datum
380 pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
381 {
382         PgStat_StatDBEntry *dbentry;
383         Oid                     dbid;
384         int64           result;
385
386         dbid = PG_GETARG_OID(0);
387
388         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
389                 result = 0;
390         else
391                 result = (int64) (dbentry->n_xact_commit);
392
393         PG_RETURN_INT64(result);
394 }
395
396
397 Datum
398 pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
399 {
400         PgStat_StatDBEntry *dbentry;
401         Oid                     dbid;
402         int64           result;
403
404         dbid = PG_GETARG_OID(0);
405
406         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
407                 result = 0;
408         else
409                 result = (int64) (dbentry->n_xact_rollback);
410
411         PG_RETURN_INT64(result);
412 }
413
414
415 Datum
416 pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
417 {
418         PgStat_StatDBEntry *dbentry;
419         Oid                     dbid;
420         int64           result;
421
422         dbid = PG_GETARG_OID(0);
423
424         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
425                 result = 0;
426         else
427                 result = (int64) (dbentry->n_blocks_fetched);
428
429         PG_RETURN_INT64(result);
430 }
431
432
433 Datum
434 pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
435 {
436         PgStat_StatDBEntry *dbentry;
437         Oid                     dbid;
438         int64           result;
439
440         dbid = PG_GETARG_OID(0);
441
442         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
443                 result = 0;
444         else
445                 result = (int64) (dbentry->n_blocks_hit);
446
447         PG_RETURN_INT64(result);
448 }