]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/pgstatfuncs.c
Add support for an application_name parameter, which is displayed in
[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-2009, 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.55 2009/11/28 23:38:07 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include "funcapi.h"
18 #include "miscadmin.h"
19 #include "pgstat.h"
20 #include "catalog/pg_type.h"
21 #include "utils/builtins.h"
22 #include "utils/inet.h"
23 #include "libpq/ip.h"
24
25 /* bogus ... these externs should be in a header file */
26 extern Datum pg_stat_get_numscans(PG_FUNCTION_ARGS);
27 extern Datum pg_stat_get_tuples_returned(PG_FUNCTION_ARGS);
28 extern Datum pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS);
29 extern Datum pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS);
30 extern Datum pg_stat_get_tuples_updated(PG_FUNCTION_ARGS);
31 extern Datum pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS);
32 extern Datum pg_stat_get_tuples_hot_updated(PG_FUNCTION_ARGS);
33 extern Datum pg_stat_get_live_tuples(PG_FUNCTION_ARGS);
34 extern Datum pg_stat_get_dead_tuples(PG_FUNCTION_ARGS);
35 extern Datum pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS);
36 extern Datum pg_stat_get_blocks_hit(PG_FUNCTION_ARGS);
37 extern Datum pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS);
38 extern Datum pg_stat_get_last_autovacuum_time(PG_FUNCTION_ARGS);
39 extern Datum pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS);
40 extern Datum pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS);
41
42 extern Datum pg_stat_get_function_calls(PG_FUNCTION_ARGS);
43 extern Datum pg_stat_get_function_time(PG_FUNCTION_ARGS);
44 extern Datum pg_stat_get_function_self_time(PG_FUNCTION_ARGS);
45
46 extern Datum pg_stat_get_backend_idset(PG_FUNCTION_ARGS);
47 extern Datum pg_stat_get_activity(PG_FUNCTION_ARGS);
48 extern Datum pg_backend_pid(PG_FUNCTION_ARGS);
49 extern Datum pg_stat_get_backend_pid(PG_FUNCTION_ARGS);
50 extern Datum pg_stat_get_backend_dbid(PG_FUNCTION_ARGS);
51 extern Datum pg_stat_get_backend_userid(PG_FUNCTION_ARGS);
52 extern Datum pg_stat_get_backend_activity(PG_FUNCTION_ARGS);
53 extern Datum pg_stat_get_backend_waiting(PG_FUNCTION_ARGS);
54 extern Datum pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS);
55 extern Datum pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS);
56 extern Datum pg_stat_get_backend_start(PG_FUNCTION_ARGS);
57 extern Datum pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS);
58 extern Datum pg_stat_get_backend_client_port(PG_FUNCTION_ARGS);
59
60 extern Datum pg_stat_get_db_numbackends(PG_FUNCTION_ARGS);
61 extern Datum pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS);
62 extern Datum pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS);
63 extern Datum pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS);
64 extern Datum pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS);
65 extern Datum pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS);
66 extern Datum pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS);
67 extern Datum pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS);
68 extern Datum pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS);
69 extern Datum pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS);
70
71 extern Datum pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS);
72 extern Datum pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS);
73 extern Datum pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS);
74 extern Datum pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS);
75 extern Datum pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS);
76 extern Datum pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS);
77 extern Datum pg_stat_get_buf_alloc(PG_FUNCTION_ARGS);
78
79 extern Datum pg_stat_clear_snapshot(PG_FUNCTION_ARGS);
80 extern Datum pg_stat_reset(PG_FUNCTION_ARGS);
81
82 /* Global bgwriter statistics, from bgwriter.c */
83 extern PgStat_MsgBgWriter bgwriterStats;
84
85 Datum
86 pg_stat_get_numscans(PG_FUNCTION_ARGS)
87 {
88         Oid                     relid = PG_GETARG_OID(0);
89         int64           result;
90         PgStat_StatTabEntry *tabentry;
91
92         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
93                 result = 0;
94         else
95                 result = (int64) (tabentry->numscans);
96
97         PG_RETURN_INT64(result);
98 }
99
100
101 Datum
102 pg_stat_get_tuples_returned(PG_FUNCTION_ARGS)
103 {
104         Oid                     relid = PG_GETARG_OID(0);
105         int64           result;
106         PgStat_StatTabEntry *tabentry;
107
108         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
109                 result = 0;
110         else
111                 result = (int64) (tabentry->tuples_returned);
112
113         PG_RETURN_INT64(result);
114 }
115
116
117 Datum
118 pg_stat_get_tuples_fetched(PG_FUNCTION_ARGS)
119 {
120         Oid                     relid = PG_GETARG_OID(0);
121         int64           result;
122         PgStat_StatTabEntry *tabentry;
123
124         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
125                 result = 0;
126         else
127                 result = (int64) (tabentry->tuples_fetched);
128
129         PG_RETURN_INT64(result);
130 }
131
132
133 Datum
134 pg_stat_get_tuples_inserted(PG_FUNCTION_ARGS)
135 {
136         Oid                     relid = PG_GETARG_OID(0);
137         int64           result;
138         PgStat_StatTabEntry *tabentry;
139
140         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
141                 result = 0;
142         else
143                 result = (int64) (tabentry->tuples_inserted);
144
145         PG_RETURN_INT64(result);
146 }
147
148
149 Datum
150 pg_stat_get_tuples_updated(PG_FUNCTION_ARGS)
151 {
152         Oid                     relid = PG_GETARG_OID(0);
153         int64           result;
154         PgStat_StatTabEntry *tabentry;
155
156         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
157                 result = 0;
158         else
159                 result = (int64) (tabentry->tuples_updated);
160
161         PG_RETURN_INT64(result);
162 }
163
164
165 Datum
166 pg_stat_get_tuples_deleted(PG_FUNCTION_ARGS)
167 {
168         Oid                     relid = PG_GETARG_OID(0);
169         int64           result;
170         PgStat_StatTabEntry *tabentry;
171
172         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
173                 result = 0;
174         else
175                 result = (int64) (tabentry->tuples_deleted);
176
177         PG_RETURN_INT64(result);
178 }
179
180
181 Datum
182 pg_stat_get_tuples_hot_updated(PG_FUNCTION_ARGS)
183 {
184         Oid                     relid = PG_GETARG_OID(0);
185         int64           result;
186         PgStat_StatTabEntry *tabentry;
187
188         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
189                 result = 0;
190         else
191                 result = (int64) (tabentry->tuples_hot_updated);
192
193         PG_RETURN_INT64(result);
194 }
195
196
197 Datum
198 pg_stat_get_live_tuples(PG_FUNCTION_ARGS)
199 {
200         Oid                     relid = PG_GETARG_OID(0);
201         int64           result;
202         PgStat_StatTabEntry *tabentry;
203
204         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
205                 result = 0;
206         else
207                 result = (int64) (tabentry->n_live_tuples);
208
209         PG_RETURN_INT64(result);
210 }
211
212
213 Datum
214 pg_stat_get_dead_tuples(PG_FUNCTION_ARGS)
215 {
216         Oid                     relid = PG_GETARG_OID(0);
217         int64           result;
218         PgStat_StatTabEntry *tabentry;
219
220         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
221                 result = 0;
222         else
223                 result = (int64) (tabentry->n_dead_tuples);
224
225         PG_RETURN_INT64(result);
226 }
227
228
229 Datum
230 pg_stat_get_blocks_fetched(PG_FUNCTION_ARGS)
231 {
232         Oid                     relid = PG_GETARG_OID(0);
233         int64           result;
234         PgStat_StatTabEntry *tabentry;
235
236         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
237                 result = 0;
238         else
239                 result = (int64) (tabentry->blocks_fetched);
240
241         PG_RETURN_INT64(result);
242 }
243
244
245 Datum
246 pg_stat_get_blocks_hit(PG_FUNCTION_ARGS)
247 {
248         Oid                     relid = PG_GETARG_OID(0);
249         int64           result;
250         PgStat_StatTabEntry *tabentry;
251
252         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
253                 result = 0;
254         else
255                 result = (int64) (tabentry->blocks_hit);
256
257         PG_RETURN_INT64(result);
258 }
259
260 Datum
261 pg_stat_get_last_vacuum_time(PG_FUNCTION_ARGS)
262 {
263         Oid                     relid = PG_GETARG_OID(0);
264         TimestampTz result;
265         PgStat_StatTabEntry *tabentry;
266
267         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
268                 result = 0;
269         else
270                 result = tabentry->vacuum_timestamp;
271
272         if (result == 0)
273                 PG_RETURN_NULL();
274         else
275                 PG_RETURN_TIMESTAMPTZ(result);
276 }
277
278 Datum
279 pg_stat_get_last_autovacuum_time(PG_FUNCTION_ARGS)
280 {
281         Oid                     relid = PG_GETARG_OID(0);
282         TimestampTz result;
283         PgStat_StatTabEntry *tabentry;
284
285         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
286                 result = 0;
287         else
288                 result = tabentry->autovac_vacuum_timestamp;
289
290         if (result == 0)
291                 PG_RETURN_NULL();
292         else
293                 PG_RETURN_TIMESTAMPTZ(result);
294 }
295
296 Datum
297 pg_stat_get_last_analyze_time(PG_FUNCTION_ARGS)
298 {
299         Oid                     relid = PG_GETARG_OID(0);
300         TimestampTz result;
301         PgStat_StatTabEntry *tabentry;
302
303         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
304                 result = 0;
305         else
306                 result = tabentry->analyze_timestamp;
307
308         if (result == 0)
309                 PG_RETURN_NULL();
310         else
311                 PG_RETURN_TIMESTAMPTZ(result);
312 }
313
314 Datum
315 pg_stat_get_last_autoanalyze_time(PG_FUNCTION_ARGS)
316 {
317         Oid                     relid = PG_GETARG_OID(0);
318         TimestampTz result;
319         PgStat_StatTabEntry *tabentry;
320
321         if ((tabentry = pgstat_fetch_stat_tabentry(relid)) == NULL)
322                 result = 0;
323         else
324                 result = tabentry->autovac_analyze_timestamp;
325
326         if (result == 0)
327                 PG_RETURN_NULL();
328         else
329                 PG_RETURN_TIMESTAMPTZ(result);
330 }
331
332 Datum
333 pg_stat_get_function_calls(PG_FUNCTION_ARGS)
334 {
335         Oid                     funcid = PG_GETARG_OID(0);
336         PgStat_StatFuncEntry *funcentry;
337
338         if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
339                 PG_RETURN_NULL();
340         PG_RETURN_INT64(funcentry->f_numcalls);
341 }
342
343 Datum
344 pg_stat_get_function_time(PG_FUNCTION_ARGS)
345 {
346         Oid                     funcid = PG_GETARG_OID(0);
347         PgStat_StatFuncEntry *funcentry;
348
349         if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
350                 PG_RETURN_NULL();
351         PG_RETURN_INT64(funcentry->f_time);
352 }
353
354 Datum
355 pg_stat_get_function_self_time(PG_FUNCTION_ARGS)
356 {
357         Oid                     funcid = PG_GETARG_OID(0);
358         PgStat_StatFuncEntry *funcentry;
359
360         if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
361                 PG_RETURN_NULL();
362         PG_RETURN_INT64(funcentry->f_time_self);
363 }
364
365 Datum
366 pg_stat_get_backend_idset(PG_FUNCTION_ARGS)
367 {
368         FuncCallContext *funcctx;
369         int                *fctx;
370         int32           result;
371
372         /* stuff done only on the first call of the function */
373         if (SRF_IS_FIRSTCALL())
374         {
375                 /* create a function context for cross-call persistence */
376                 funcctx = SRF_FIRSTCALL_INIT();
377
378                 fctx = MemoryContextAlloc(funcctx->multi_call_memory_ctx,
379                                                                   2 * sizeof(int));
380                 funcctx->user_fctx = fctx;
381
382                 fctx[0] = 0;
383                 fctx[1] = pgstat_fetch_stat_numbackends();
384         }
385
386         /* stuff done on every call of the function */
387         funcctx = SRF_PERCALL_SETUP();
388         fctx = funcctx->user_fctx;
389
390         fctx[0] += 1;
391         result = fctx[0];
392
393         if (result <= fctx[1])
394         {
395                 /* do when there is more left to send */
396                 SRF_RETURN_NEXT(funcctx, Int32GetDatum(result));
397         }
398         else
399         {
400                 /* do when there is no more left */
401                 SRF_RETURN_DONE(funcctx);
402         }
403 }
404
405 Datum
406 pg_stat_get_activity(PG_FUNCTION_ARGS)
407 {
408         FuncCallContext *funcctx;
409
410         if (SRF_IS_FIRSTCALL())
411         {
412                 MemoryContext oldcontext;
413                 TupleDesc       tupdesc;
414
415                 funcctx = SRF_FIRSTCALL_INIT();
416
417                 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
418
419                 tupdesc = CreateTemplateTupleDesc(11, false);
420                 TupleDescInitEntry(tupdesc, (AttrNumber) 1, "datid", OIDOID, -1, 0);
421                 TupleDescInitEntry(tupdesc, (AttrNumber) 2, "procpid", INT4OID, -1, 0);
422                 TupleDescInitEntry(tupdesc, (AttrNumber) 3, "usesysid", OIDOID, -1, 0);
423                 TupleDescInitEntry(tupdesc, (AttrNumber) 4, "current_query", TEXTOID, -1, 0);
424                 TupleDescInitEntry(tupdesc, (AttrNumber) 5, "waiting", BOOLOID, -1, 0);
425                 TupleDescInitEntry(tupdesc, (AttrNumber) 6, "act_start", TIMESTAMPTZOID, -1, 0);
426                 TupleDescInitEntry(tupdesc, (AttrNumber) 7, "query_start", TIMESTAMPTZOID, -1, 0);
427                 TupleDescInitEntry(tupdesc, (AttrNumber) 8, "backend_start", TIMESTAMPTZOID, -1, 0);
428                 TupleDescInitEntry(tupdesc, (AttrNumber) 9, "client_addr", INETOID, -1, 0);
429                 TupleDescInitEntry(tupdesc, (AttrNumber) 10, "client_port", INT4OID, -1, 0);
430                 TupleDescInitEntry(tupdesc, (AttrNumber) 11, "application_name", TEXTOID, -1, 0);
431
432                 funcctx->tuple_desc = BlessTupleDesc(tupdesc);
433
434                 funcctx->user_fctx = palloc0(sizeof(int));
435                 if (PG_ARGISNULL(0))
436                 {
437                         /* Get all backends */
438                         funcctx->max_calls = pgstat_fetch_stat_numbackends();
439                 }
440                 else
441                 {
442                         /*
443                          * Get one backend - locate by pid.
444                          *
445                          * We lookup the backend early, so we can return zero rows if it
446                          * doesn't exist, instead of returning a single row full of NULLs.
447                          */
448                         int                     pid = PG_GETARG_INT32(0);
449                         int                     i;
450                         int                     n = pgstat_fetch_stat_numbackends();
451
452                         for (i = 1; i <= n; i++)
453                         {
454                                 PgBackendStatus *be = pgstat_fetch_stat_beentry(i);
455
456                                 if (be)
457                                 {
458                                         if (be->st_procpid == pid)
459                                         {
460                                                 *(int *) (funcctx->user_fctx) = i;
461                                                 break;
462                                         }
463                                 }
464                         }
465
466                         if (*(int *) (funcctx->user_fctx) == 0)
467                                 /* Pid not found, return zero rows */
468                                 funcctx->max_calls = 0;
469                         else
470                                 funcctx->max_calls = 1;
471                 }
472
473                 MemoryContextSwitchTo(oldcontext);
474         }
475
476         /* stuff done on every call of the function */
477         funcctx = SRF_PERCALL_SETUP();
478
479         if (funcctx->call_cntr < funcctx->max_calls)
480         {
481                 /* for each row */
482                 Datum           values[11];
483                 bool            nulls[11];
484                 HeapTuple       tuple;
485                 PgBackendStatus *beentry;
486                 SockAddr        zero_clientaddr;
487
488                 MemSet(values, 0, sizeof(values));
489                 MemSet(nulls, 0, sizeof(nulls));
490
491                 if (*(int *) (funcctx->user_fctx) > 0)
492                         /* Get specific pid slot */
493                         beentry = pgstat_fetch_stat_beentry(*(int *) (funcctx->user_fctx));
494                 else
495                         /* Get the next one in the list */
496                         beentry = pgstat_fetch_stat_beentry(funcctx->call_cntr + 1);            /* 1-based index */
497                 if (!beentry)
498                 {
499                         int                     i;
500
501                         for (i = 0; i < sizeof(nulls) / sizeof(nulls[0]); i++)
502                                 nulls[i] = true;
503
504                         nulls[3] = false;
505                         values[3] = CStringGetTextDatum("<backend information not available>");
506
507                         tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
508                         SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
509                 }
510
511                 /* Values available to all callers */
512                 values[0] = ObjectIdGetDatum(beentry->st_databaseid);
513                 values[1] = Int32GetDatum(beentry->st_procpid);
514                 values[2] = ObjectIdGetDatum(beentry->st_userid);
515
516                 /* Values only available to same user or superuser */
517                 if (superuser() || beentry->st_userid == GetUserId())
518                 {
519                         if (*(beentry->st_activity) == '\0')
520                         {
521                                 values[3] = CStringGetTextDatum("<command string not enabled>");
522                         }
523                         else
524                         {
525                                 values[3] = CStringGetTextDatum(beentry->st_activity);
526                         }
527
528                         values[4] = BoolGetDatum(beentry->st_waiting);
529
530                         if (beentry->st_xact_start_timestamp != 0)
531                                 values[5] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
532                         else
533                                 nulls[5] = true;
534
535                         if (beentry->st_activity_start_timestamp != 0)
536                                 values[6] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
537                         else
538                                 nulls[6] = true;
539
540                         if (beentry->st_proc_start_timestamp != 0)
541                                 values[7] = TimestampTzGetDatum(beentry->st_proc_start_timestamp);
542                         else
543                                 nulls[7] = true;
544
545                         /* A zeroed client addr means we don't know */
546                         memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
547                         if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
548                                            sizeof(zero_clientaddr) == 0))
549                         {
550                                 nulls[8] = true;
551                                 nulls[9] = true;
552                         }
553                         else
554                         {
555                                 if (beentry->st_clientaddr.addr.ss_family == AF_INET
556 #ifdef HAVE_IPV6
557                                         || beentry->st_clientaddr.addr.ss_family == AF_INET6
558 #endif
559                                         )
560                                 {
561                                         char            remote_host[NI_MAXHOST];
562                                         char            remote_port[NI_MAXSERV];
563                                         int                     ret;
564
565                                         remote_host[0] = '\0';
566                                         remote_port[0] = '\0';
567                                         ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
568                                                                                          beentry->st_clientaddr.salen,
569                                                                                          remote_host, sizeof(remote_host),
570                                                                                          remote_port, sizeof(remote_port),
571                                                                                          NI_NUMERICHOST | NI_NUMERICSERV);
572                                         if (ret)
573                                         {
574                                                 nulls[8] = true;
575                                                 nulls[9] = true;
576                                         }
577                                         else
578                                         {
579                                                 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
580                                                 values[8] = DirectFunctionCall1(inet_in,
581                                                                                            CStringGetDatum(remote_host));
582                                                 values[9] = Int32GetDatum(atoi(remote_port));
583                                         }
584                                 }
585                                 else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
586                                 {
587                                         /*
588                                          * Unix sockets always reports NULL for host and -1 for
589                                          * port, so it's possible to tell the difference to
590                                          * connections we have no permissions to view, or with
591                                          * errors.
592                                          */
593                                         nulls[8] = true;
594                                         values[9] = DatumGetInt32(-1);
595                                 }
596                                 else
597                                 {
598                                         /* Unknown address type, should never happen */
599                                         nulls[8] = true;
600                                         nulls[9] = true;
601                                 }
602                         }
603
604                         /* application name */
605                         if (beentry->st_appname)
606                             values[10] = CStringGetTextDatum(beentry->st_appname);
607                         else
608                                 nulls[10] = true;
609                 }
610                 else
611                 {
612                         /* No permissions to view data about this session */
613                         values[3] = CStringGetTextDatum("<insufficient privilege>");
614                         nulls[4] = true;
615                         nulls[5] = true;
616                         nulls[6] = true;
617                         nulls[7] = true;
618                         nulls[8] = true;
619                         nulls[9] = true;
620                         nulls[10] = true;
621                 }
622
623                 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
624
625                 SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
626         }
627         else
628         {
629                 /* nothing left */
630                 SRF_RETURN_DONE(funcctx);
631         }
632 }
633
634
635 Datum
636 pg_backend_pid(PG_FUNCTION_ARGS)
637 {
638         PG_RETURN_INT32(MyProcPid);
639 }
640
641
642 Datum
643 pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
644 {
645         int32           beid = PG_GETARG_INT32(0);
646         PgBackendStatus *beentry;
647
648         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
649                 PG_RETURN_NULL();
650
651         PG_RETURN_INT32(beentry->st_procpid);
652 }
653
654
655 Datum
656 pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
657 {
658         int32           beid = PG_GETARG_INT32(0);
659         PgBackendStatus *beentry;
660
661         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
662                 PG_RETURN_NULL();
663
664         PG_RETURN_OID(beentry->st_databaseid);
665 }
666
667
668 Datum
669 pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
670 {
671         int32           beid = PG_GETARG_INT32(0);
672         PgBackendStatus *beentry;
673
674         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
675                 PG_RETURN_NULL();
676
677         PG_RETURN_OID(beentry->st_userid);
678 }
679
680
681 Datum
682 pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
683 {
684         int32           beid = PG_GETARG_INT32(0);
685         PgBackendStatus *beentry;
686         const char *activity;
687
688         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
689                 activity = "<backend information not available>";
690         else if (!superuser() && beentry->st_userid != GetUserId())
691                 activity = "<insufficient privilege>";
692         else if (*(beentry->st_activity) == '\0')
693                 activity = "<command string not enabled>";
694         else
695                 activity = beentry->st_activity;
696
697         PG_RETURN_TEXT_P(cstring_to_text(activity));
698 }
699
700
701 Datum
702 pg_stat_get_backend_waiting(PG_FUNCTION_ARGS)
703 {
704         int32           beid = PG_GETARG_INT32(0);
705         bool            result;
706         PgBackendStatus *beentry;
707
708         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
709                 PG_RETURN_NULL();
710
711         if (!superuser() && beentry->st_userid != GetUserId())
712                 PG_RETURN_NULL();
713
714         result = beentry->st_waiting;
715
716         PG_RETURN_BOOL(result);
717 }
718
719
720 Datum
721 pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
722 {
723         int32           beid = PG_GETARG_INT32(0);
724         TimestampTz result;
725         PgBackendStatus *beentry;
726
727         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
728                 PG_RETURN_NULL();
729
730         if (!superuser() && beentry->st_userid != GetUserId())
731                 PG_RETURN_NULL();
732
733         result = beentry->st_activity_start_timestamp;
734
735         /*
736          * No time recorded for start of current query -- this is the case if the
737          * user hasn't enabled query-level stats collection.
738          */
739         if (result == 0)
740                 PG_RETURN_NULL();
741
742         PG_RETURN_TIMESTAMPTZ(result);
743 }
744
745
746 Datum
747 pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS)
748 {
749         int32           beid = PG_GETARG_INT32(0);
750         TimestampTz result;
751         PgBackendStatus *beentry;
752
753         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
754                 PG_RETURN_NULL();
755
756         if (!superuser() && beentry->st_userid != GetUserId())
757                 PG_RETURN_NULL();
758
759         result = beentry->st_xact_start_timestamp;
760
761         if (result == 0)                        /* not in a transaction */
762                 PG_RETURN_NULL();
763
764         PG_RETURN_TIMESTAMPTZ(result);
765 }
766
767
768 Datum
769 pg_stat_get_backend_start(PG_FUNCTION_ARGS)
770 {
771         int32           beid = PG_GETARG_INT32(0);
772         TimestampTz result;
773         PgBackendStatus *beentry;
774
775         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
776                 PG_RETURN_NULL();
777
778         if (!superuser() && beentry->st_userid != GetUserId())
779                 PG_RETURN_NULL();
780
781         result = beentry->st_proc_start_timestamp;
782
783         if (result == 0)                        /* probably can't happen? */
784                 PG_RETURN_NULL();
785
786         PG_RETURN_TIMESTAMPTZ(result);
787 }
788
789
790 Datum
791 pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
792 {
793         int32           beid = PG_GETARG_INT32(0);
794         PgBackendStatus *beentry;
795         SockAddr        zero_clientaddr;
796         char            remote_host[NI_MAXHOST];
797         int                     ret;
798
799         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
800                 PG_RETURN_NULL();
801
802         if (!superuser() && beentry->st_userid != GetUserId())
803                 PG_RETURN_NULL();
804
805         /* A zeroed client addr means we don't know */
806         memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
807         if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
808                            sizeof(zero_clientaddr) == 0))
809                 PG_RETURN_NULL();
810
811         switch (beentry->st_clientaddr.addr.ss_family)
812         {
813                 case AF_INET:
814 #ifdef HAVE_IPV6
815                 case AF_INET6:
816 #endif
817                         break;
818                 default:
819                         PG_RETURN_NULL();
820         }
821
822         remote_host[0] = '\0';
823         ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
824                                                          beentry->st_clientaddr.salen,
825                                                          remote_host, sizeof(remote_host),
826                                                          NULL, 0,
827                                                          NI_NUMERICHOST | NI_NUMERICSERV);
828         if (ret)
829                 PG_RETURN_NULL();
830
831         clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
832
833         PG_RETURN_INET_P(DirectFunctionCall1(inet_in,
834                                                                                  CStringGetDatum(remote_host)));
835 }
836
837 Datum
838 pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
839 {
840         int32           beid = PG_GETARG_INT32(0);
841         PgBackendStatus *beentry;
842         SockAddr        zero_clientaddr;
843         char            remote_port[NI_MAXSERV];
844         int                     ret;
845
846         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
847                 PG_RETURN_NULL();
848
849         if (!superuser() && beentry->st_userid != GetUserId())
850                 PG_RETURN_NULL();
851
852         /* A zeroed client addr means we don't know */
853         memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
854         if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
855                            sizeof(zero_clientaddr) == 0))
856                 PG_RETURN_NULL();
857
858         switch (beentry->st_clientaddr.addr.ss_family)
859         {
860                 case AF_INET:
861 #ifdef HAVE_IPV6
862                 case AF_INET6:
863 #endif
864                         break;
865                 case AF_UNIX:
866                         PG_RETURN_INT32(-1);
867                 default:
868                         PG_RETURN_NULL();
869         }
870
871         remote_port[0] = '\0';
872         ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
873                                                          beentry->st_clientaddr.salen,
874                                                          NULL, 0,
875                                                          remote_port, sizeof(remote_port),
876                                                          NI_NUMERICHOST | NI_NUMERICSERV);
877         if (ret)
878                 PG_RETURN_NULL();
879
880         PG_RETURN_DATUM(DirectFunctionCall1(int4in,
881                                                                                 CStringGetDatum(remote_port)));
882 }
883
884
885 Datum
886 pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
887 {
888         Oid                     dbid = PG_GETARG_OID(0);
889         int32           result;
890         int                     tot_backends = pgstat_fetch_stat_numbackends();
891         int                     beid;
892
893         result = 0;
894         for (beid = 1; beid <= tot_backends; beid++)
895         {
896                 PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
897
898                 if (beentry && beentry->st_databaseid == dbid)
899                         result++;
900         }
901
902         PG_RETURN_INT32(result);
903 }
904
905
906 Datum
907 pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
908 {
909         Oid                     dbid = PG_GETARG_OID(0);
910         int64           result;
911         PgStat_StatDBEntry *dbentry;
912
913         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
914                 result = 0;
915         else
916                 result = (int64) (dbentry->n_xact_commit);
917
918         PG_RETURN_INT64(result);
919 }
920
921
922 Datum
923 pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
924 {
925         Oid                     dbid = PG_GETARG_OID(0);
926         int64           result;
927         PgStat_StatDBEntry *dbentry;
928
929         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
930                 result = 0;
931         else
932                 result = (int64) (dbentry->n_xact_rollback);
933
934         PG_RETURN_INT64(result);
935 }
936
937
938 Datum
939 pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
940 {
941         Oid                     dbid = PG_GETARG_OID(0);
942         int64           result;
943         PgStat_StatDBEntry *dbentry;
944
945         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
946                 result = 0;
947         else
948                 result = (int64) (dbentry->n_blocks_fetched);
949
950         PG_RETURN_INT64(result);
951 }
952
953
954 Datum
955 pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
956 {
957         Oid                     dbid = PG_GETARG_OID(0);
958         int64           result;
959         PgStat_StatDBEntry *dbentry;
960
961         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
962                 result = 0;
963         else
964                 result = (int64) (dbentry->n_blocks_hit);
965
966         PG_RETURN_INT64(result);
967 }
968
969
970 Datum
971 pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS)
972 {
973         Oid                     dbid = PG_GETARG_OID(0);
974         int64           result;
975         PgStat_StatDBEntry *dbentry;
976
977         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
978                 result = 0;
979         else
980                 result = (int64) (dbentry->n_tuples_returned);
981
982         PG_RETURN_INT64(result);
983 }
984
985
986 Datum
987 pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS)
988 {
989         Oid                     dbid = PG_GETARG_OID(0);
990         int64           result;
991         PgStat_StatDBEntry *dbentry;
992
993         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
994                 result = 0;
995         else
996                 result = (int64) (dbentry->n_tuples_fetched);
997
998         PG_RETURN_INT64(result);
999 }
1000
1001
1002 Datum
1003 pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS)
1004 {
1005         Oid                     dbid = PG_GETARG_OID(0);
1006         int64           result;
1007         PgStat_StatDBEntry *dbentry;
1008
1009         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1010                 result = 0;
1011         else
1012                 result = (int64) (dbentry->n_tuples_inserted);
1013
1014         PG_RETURN_INT64(result);
1015 }
1016
1017
1018 Datum
1019 pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS)
1020 {
1021         Oid                     dbid = PG_GETARG_OID(0);
1022         int64           result;
1023         PgStat_StatDBEntry *dbentry;
1024
1025         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1026                 result = 0;
1027         else
1028                 result = (int64) (dbentry->n_tuples_updated);
1029
1030         PG_RETURN_INT64(result);
1031 }
1032
1033
1034 Datum
1035 pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS)
1036 {
1037         Oid                     dbid = PG_GETARG_OID(0);
1038         int64           result;
1039         PgStat_StatDBEntry *dbentry;
1040
1041         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1042                 result = 0;
1043         else
1044                 result = (int64) (dbentry->n_tuples_deleted);
1045
1046         PG_RETURN_INT64(result);
1047 }
1048
1049 Datum
1050 pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS)
1051 {
1052         PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
1053 }
1054
1055 Datum
1056 pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS)
1057 {
1058         PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
1059 }
1060
1061 Datum
1062 pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS)
1063 {
1064         PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
1065 }
1066
1067 Datum
1068 pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS)
1069 {
1070         PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
1071 }
1072
1073 Datum
1074 pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS)
1075 {
1076         PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
1077 }
1078
1079 Datum
1080 pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS)
1081 {
1082         PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
1083 }
1084
1085 Datum
1086 pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
1087 {
1088         PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
1089 }
1090
1091
1092 /* Discard the active statistics snapshot */
1093 Datum
1094 pg_stat_clear_snapshot(PG_FUNCTION_ARGS)
1095 {
1096         pgstat_clear_snapshot();
1097
1098         PG_RETURN_VOID();
1099 }
1100
1101
1102 /* Reset all counters for the current database */
1103 Datum
1104 pg_stat_reset(PG_FUNCTION_ARGS)
1105 {
1106         pgstat_reset_counters();
1107
1108         PG_RETURN_VOID();
1109 }