]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/pgstatfuncs.c
8.4 pgindent run, with new combined Linux/FreeBSD/MinGW typedef list
[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.54 2009/06/11 14:49:04 momjian 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(10, 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
431                 funcctx->tuple_desc = BlessTupleDesc(tupdesc);
432
433                 funcctx->user_fctx = palloc0(sizeof(int));
434                 if (PG_ARGISNULL(0))
435                 {
436                         /* Get all backends */
437                         funcctx->max_calls = pgstat_fetch_stat_numbackends();
438                 }
439                 else
440                 {
441                         /*
442                          * Get one backend - locate by pid.
443                          *
444                          * We lookup the backend early, so we can return zero rows if it
445                          * doesn't exist, instead of returning a single row full of NULLs.
446                          */
447                         int                     pid = PG_GETARG_INT32(0);
448                         int                     i;
449                         int                     n = pgstat_fetch_stat_numbackends();
450
451                         for (i = 1; i <= n; i++)
452                         {
453                                 PgBackendStatus *be = pgstat_fetch_stat_beentry(i);
454
455                                 if (be)
456                                 {
457                                         if (be->st_procpid == pid)
458                                         {
459                                                 *(int *) (funcctx->user_fctx) = i;
460                                                 break;
461                                         }
462                                 }
463                         }
464
465                         if (*(int *) (funcctx->user_fctx) == 0)
466                                 /* Pid not found, return zero rows */
467                                 funcctx->max_calls = 0;
468                         else
469                                 funcctx->max_calls = 1;
470                 }
471
472                 MemoryContextSwitchTo(oldcontext);
473         }
474
475         /* stuff done on every call of the function */
476         funcctx = SRF_PERCALL_SETUP();
477
478         if (funcctx->call_cntr < funcctx->max_calls)
479         {
480                 /* for each row */
481                 Datum           values[10];
482                 bool            nulls[10];
483                 HeapTuple       tuple;
484                 PgBackendStatus *beentry;
485                 SockAddr        zero_clientaddr;
486
487                 MemSet(values, 0, sizeof(values));
488                 MemSet(nulls, 0, sizeof(nulls));
489
490                 if (*(int *) (funcctx->user_fctx) > 0)
491                         /* Get specific pid slot */
492                         beentry = pgstat_fetch_stat_beentry(*(int *) (funcctx->user_fctx));
493                 else
494                         /* Get the next one in the list */
495                         beentry = pgstat_fetch_stat_beentry(funcctx->call_cntr + 1);            /* 1-based index */
496                 if (!beentry)
497                 {
498                         int                     i;
499
500                         for (i = 0; i < sizeof(nulls) / sizeof(nulls[0]); i++)
501                                 nulls[i] = true;
502
503                         nulls[3] = false;
504                         values[3] = CStringGetTextDatum("<backend information not available>");
505
506                         tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
507                         SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
508                 }
509
510                 /* Values available to all callers */
511                 values[0] = ObjectIdGetDatum(beentry->st_databaseid);
512                 values[1] = Int32GetDatum(beentry->st_procpid);
513                 values[2] = ObjectIdGetDatum(beentry->st_userid);
514
515                 /* Values only available to same user or superuser */
516                 if (superuser() || beentry->st_userid == GetUserId())
517                 {
518                         if (*(beentry->st_activity) == '\0')
519                         {
520                                 values[3] = CStringGetTextDatum("<command string not enabled>");
521                         }
522                         else
523                         {
524                                 values[3] = CStringGetTextDatum(beentry->st_activity);
525                         }
526
527                         values[4] = BoolGetDatum(beentry->st_waiting);
528
529                         if (beentry->st_xact_start_timestamp != 0)
530                                 values[5] = TimestampTzGetDatum(beentry->st_xact_start_timestamp);
531                         else
532                                 nulls[5] = true;
533
534                         if (beentry->st_activity_start_timestamp != 0)
535                                 values[6] = TimestampTzGetDatum(beentry->st_activity_start_timestamp);
536                         else
537                                 nulls[6] = true;
538
539                         if (beentry->st_proc_start_timestamp != 0)
540                                 values[7] = TimestampTzGetDatum(beentry->st_proc_start_timestamp);
541                         else
542                                 nulls[7] = true;
543
544                         /* A zeroed client addr means we don't know */
545                         memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
546                         if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
547                                            sizeof(zero_clientaddr) == 0))
548                         {
549                                 nulls[8] = true;
550                                 nulls[9] = true;
551                         }
552                         else
553                         {
554                                 if (beentry->st_clientaddr.addr.ss_family == AF_INET
555 #ifdef HAVE_IPV6
556                                         || beentry->st_clientaddr.addr.ss_family == AF_INET6
557 #endif
558                                         )
559                                 {
560                                         char            remote_host[NI_MAXHOST];
561                                         char            remote_port[NI_MAXSERV];
562                                         int                     ret;
563
564                                         remote_host[0] = '\0';
565                                         remote_port[0] = '\0';
566                                         ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
567                                                                                          beentry->st_clientaddr.salen,
568                                                                                          remote_host, sizeof(remote_host),
569                                                                                          remote_port, sizeof(remote_port),
570                                                                                          NI_NUMERICHOST | NI_NUMERICSERV);
571                                         if (ret)
572                                         {
573                                                 nulls[8] = true;
574                                                 nulls[9] = true;
575                                         }
576                                         else
577                                         {
578                                                 clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
579                                                 values[8] = DirectFunctionCall1(inet_in,
580                                                                                            CStringGetDatum(remote_host));
581                                                 values[9] = Int32GetDatum(atoi(remote_port));
582                                         }
583                                 }
584                                 else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
585                                 {
586                                         /*
587                                          * Unix sockets always reports NULL for host and -1 for
588                                          * port, so it's possible to tell the difference to
589                                          * connections we have no permissions to view, or with
590                                          * errors.
591                                          */
592                                         nulls[8] = true;
593                                         values[9] = DatumGetInt32(-1);
594                                 }
595                                 else
596                                 {
597                                         /* Unknown address type, should never happen */
598                                         nulls[8] = true;
599                                         nulls[9] = true;
600                                 }
601                         }
602                 }
603                 else
604                 {
605                         /* No permissions to view data about this session */
606                         values[3] = CStringGetTextDatum("<insufficient privilege>");
607                         nulls[4] = true;
608                         nulls[5] = true;
609                         nulls[6] = true;
610                         nulls[7] = true;
611                         nulls[8] = true;
612                         nulls[9] = true;
613                 }
614
615                 tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
616
617                 SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));
618         }
619         else
620         {
621                 /* nothing left */
622                 SRF_RETURN_DONE(funcctx);
623         }
624 }
625
626
627 Datum
628 pg_backend_pid(PG_FUNCTION_ARGS)
629 {
630         PG_RETURN_INT32(MyProcPid);
631 }
632
633
634 Datum
635 pg_stat_get_backend_pid(PG_FUNCTION_ARGS)
636 {
637         int32           beid = PG_GETARG_INT32(0);
638         PgBackendStatus *beentry;
639
640         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
641                 PG_RETURN_NULL();
642
643         PG_RETURN_INT32(beentry->st_procpid);
644 }
645
646
647 Datum
648 pg_stat_get_backend_dbid(PG_FUNCTION_ARGS)
649 {
650         int32           beid = PG_GETARG_INT32(0);
651         PgBackendStatus *beentry;
652
653         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
654                 PG_RETURN_NULL();
655
656         PG_RETURN_OID(beentry->st_databaseid);
657 }
658
659
660 Datum
661 pg_stat_get_backend_userid(PG_FUNCTION_ARGS)
662 {
663         int32           beid = PG_GETARG_INT32(0);
664         PgBackendStatus *beentry;
665
666         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
667                 PG_RETURN_NULL();
668
669         PG_RETURN_OID(beentry->st_userid);
670 }
671
672
673 Datum
674 pg_stat_get_backend_activity(PG_FUNCTION_ARGS)
675 {
676         int32           beid = PG_GETARG_INT32(0);
677         PgBackendStatus *beentry;
678         const char *activity;
679
680         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
681                 activity = "<backend information not available>";
682         else if (!superuser() && beentry->st_userid != GetUserId())
683                 activity = "<insufficient privilege>";
684         else if (*(beentry->st_activity) == '\0')
685                 activity = "<command string not enabled>";
686         else
687                 activity = beentry->st_activity;
688
689         PG_RETURN_TEXT_P(cstring_to_text(activity));
690 }
691
692
693 Datum
694 pg_stat_get_backend_waiting(PG_FUNCTION_ARGS)
695 {
696         int32           beid = PG_GETARG_INT32(0);
697         bool            result;
698         PgBackendStatus *beentry;
699
700         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
701                 PG_RETURN_NULL();
702
703         if (!superuser() && beentry->st_userid != GetUserId())
704                 PG_RETURN_NULL();
705
706         result = beentry->st_waiting;
707
708         PG_RETURN_BOOL(result);
709 }
710
711
712 Datum
713 pg_stat_get_backend_activity_start(PG_FUNCTION_ARGS)
714 {
715         int32           beid = PG_GETARG_INT32(0);
716         TimestampTz result;
717         PgBackendStatus *beentry;
718
719         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
720                 PG_RETURN_NULL();
721
722         if (!superuser() && beentry->st_userid != GetUserId())
723                 PG_RETURN_NULL();
724
725         result = beentry->st_activity_start_timestamp;
726
727         /*
728          * No time recorded for start of current query -- this is the case if the
729          * user hasn't enabled query-level stats collection.
730          */
731         if (result == 0)
732                 PG_RETURN_NULL();
733
734         PG_RETURN_TIMESTAMPTZ(result);
735 }
736
737
738 Datum
739 pg_stat_get_backend_xact_start(PG_FUNCTION_ARGS)
740 {
741         int32           beid = PG_GETARG_INT32(0);
742         TimestampTz result;
743         PgBackendStatus *beentry;
744
745         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
746                 PG_RETURN_NULL();
747
748         if (!superuser() && beentry->st_userid != GetUserId())
749                 PG_RETURN_NULL();
750
751         result = beentry->st_xact_start_timestamp;
752
753         if (result == 0)                        /* not in a transaction */
754                 PG_RETURN_NULL();
755
756         PG_RETURN_TIMESTAMPTZ(result);
757 }
758
759
760 Datum
761 pg_stat_get_backend_start(PG_FUNCTION_ARGS)
762 {
763         int32           beid = PG_GETARG_INT32(0);
764         TimestampTz result;
765         PgBackendStatus *beentry;
766
767         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
768                 PG_RETURN_NULL();
769
770         if (!superuser() && beentry->st_userid != GetUserId())
771                 PG_RETURN_NULL();
772
773         result = beentry->st_proc_start_timestamp;
774
775         if (result == 0)                        /* probably can't happen? */
776                 PG_RETURN_NULL();
777
778         PG_RETURN_TIMESTAMPTZ(result);
779 }
780
781
782 Datum
783 pg_stat_get_backend_client_addr(PG_FUNCTION_ARGS)
784 {
785         int32           beid = PG_GETARG_INT32(0);
786         PgBackendStatus *beentry;
787         SockAddr        zero_clientaddr;
788         char            remote_host[NI_MAXHOST];
789         int                     ret;
790
791         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
792                 PG_RETURN_NULL();
793
794         if (!superuser() && beentry->st_userid != GetUserId())
795                 PG_RETURN_NULL();
796
797         /* A zeroed client addr means we don't know */
798         memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
799         if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
800                            sizeof(zero_clientaddr) == 0))
801                 PG_RETURN_NULL();
802
803         switch (beentry->st_clientaddr.addr.ss_family)
804         {
805                 case AF_INET:
806 #ifdef HAVE_IPV6
807                 case AF_INET6:
808 #endif
809                         break;
810                 default:
811                         PG_RETURN_NULL();
812         }
813
814         remote_host[0] = '\0';
815         ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
816                                                          beentry->st_clientaddr.salen,
817                                                          remote_host, sizeof(remote_host),
818                                                          NULL, 0,
819                                                          NI_NUMERICHOST | NI_NUMERICSERV);
820         if (ret)
821                 PG_RETURN_NULL();
822
823         clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
824
825         PG_RETURN_INET_P(DirectFunctionCall1(inet_in,
826                                                                                  CStringGetDatum(remote_host)));
827 }
828
829 Datum
830 pg_stat_get_backend_client_port(PG_FUNCTION_ARGS)
831 {
832         int32           beid = PG_GETARG_INT32(0);
833         PgBackendStatus *beentry;
834         SockAddr        zero_clientaddr;
835         char            remote_port[NI_MAXSERV];
836         int                     ret;
837
838         if ((beentry = pgstat_fetch_stat_beentry(beid)) == NULL)
839                 PG_RETURN_NULL();
840
841         if (!superuser() && beentry->st_userid != GetUserId())
842                 PG_RETURN_NULL();
843
844         /* A zeroed client addr means we don't know */
845         memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
846         if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
847                            sizeof(zero_clientaddr) == 0))
848                 PG_RETURN_NULL();
849
850         switch (beentry->st_clientaddr.addr.ss_family)
851         {
852                 case AF_INET:
853 #ifdef HAVE_IPV6
854                 case AF_INET6:
855 #endif
856                         break;
857                 case AF_UNIX:
858                         PG_RETURN_INT32(-1);
859                 default:
860                         PG_RETURN_NULL();
861         }
862
863         remote_port[0] = '\0';
864         ret = pg_getnameinfo_all(&beentry->st_clientaddr.addr,
865                                                          beentry->st_clientaddr.salen,
866                                                          NULL, 0,
867                                                          remote_port, sizeof(remote_port),
868                                                          NI_NUMERICHOST | NI_NUMERICSERV);
869         if (ret)
870                 PG_RETURN_NULL();
871
872         PG_RETURN_DATUM(DirectFunctionCall1(int4in,
873                                                                                 CStringGetDatum(remote_port)));
874 }
875
876
877 Datum
878 pg_stat_get_db_numbackends(PG_FUNCTION_ARGS)
879 {
880         Oid                     dbid = PG_GETARG_OID(0);
881         int32           result;
882         int                     tot_backends = pgstat_fetch_stat_numbackends();
883         int                     beid;
884
885         result = 0;
886         for (beid = 1; beid <= tot_backends; beid++)
887         {
888                 PgBackendStatus *beentry = pgstat_fetch_stat_beentry(beid);
889
890                 if (beentry && beentry->st_databaseid == dbid)
891                         result++;
892         }
893
894         PG_RETURN_INT32(result);
895 }
896
897
898 Datum
899 pg_stat_get_db_xact_commit(PG_FUNCTION_ARGS)
900 {
901         Oid                     dbid = PG_GETARG_OID(0);
902         int64           result;
903         PgStat_StatDBEntry *dbentry;
904
905         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
906                 result = 0;
907         else
908                 result = (int64) (dbentry->n_xact_commit);
909
910         PG_RETURN_INT64(result);
911 }
912
913
914 Datum
915 pg_stat_get_db_xact_rollback(PG_FUNCTION_ARGS)
916 {
917         Oid                     dbid = PG_GETARG_OID(0);
918         int64           result;
919         PgStat_StatDBEntry *dbentry;
920
921         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
922                 result = 0;
923         else
924                 result = (int64) (dbentry->n_xact_rollback);
925
926         PG_RETURN_INT64(result);
927 }
928
929
930 Datum
931 pg_stat_get_db_blocks_fetched(PG_FUNCTION_ARGS)
932 {
933         Oid                     dbid = PG_GETARG_OID(0);
934         int64           result;
935         PgStat_StatDBEntry *dbentry;
936
937         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
938                 result = 0;
939         else
940                 result = (int64) (dbentry->n_blocks_fetched);
941
942         PG_RETURN_INT64(result);
943 }
944
945
946 Datum
947 pg_stat_get_db_blocks_hit(PG_FUNCTION_ARGS)
948 {
949         Oid                     dbid = PG_GETARG_OID(0);
950         int64           result;
951         PgStat_StatDBEntry *dbentry;
952
953         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
954                 result = 0;
955         else
956                 result = (int64) (dbentry->n_blocks_hit);
957
958         PG_RETURN_INT64(result);
959 }
960
961
962 Datum
963 pg_stat_get_db_tuples_returned(PG_FUNCTION_ARGS)
964 {
965         Oid                     dbid = PG_GETARG_OID(0);
966         int64           result;
967         PgStat_StatDBEntry *dbentry;
968
969         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
970                 result = 0;
971         else
972                 result = (int64) (dbentry->n_tuples_returned);
973
974         PG_RETURN_INT64(result);
975 }
976
977
978 Datum
979 pg_stat_get_db_tuples_fetched(PG_FUNCTION_ARGS)
980 {
981         Oid                     dbid = PG_GETARG_OID(0);
982         int64           result;
983         PgStat_StatDBEntry *dbentry;
984
985         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
986                 result = 0;
987         else
988                 result = (int64) (dbentry->n_tuples_fetched);
989
990         PG_RETURN_INT64(result);
991 }
992
993
994 Datum
995 pg_stat_get_db_tuples_inserted(PG_FUNCTION_ARGS)
996 {
997         Oid                     dbid = PG_GETARG_OID(0);
998         int64           result;
999         PgStat_StatDBEntry *dbentry;
1000
1001         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1002                 result = 0;
1003         else
1004                 result = (int64) (dbentry->n_tuples_inserted);
1005
1006         PG_RETURN_INT64(result);
1007 }
1008
1009
1010 Datum
1011 pg_stat_get_db_tuples_updated(PG_FUNCTION_ARGS)
1012 {
1013         Oid                     dbid = PG_GETARG_OID(0);
1014         int64           result;
1015         PgStat_StatDBEntry *dbentry;
1016
1017         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1018                 result = 0;
1019         else
1020                 result = (int64) (dbentry->n_tuples_updated);
1021
1022         PG_RETURN_INT64(result);
1023 }
1024
1025
1026 Datum
1027 pg_stat_get_db_tuples_deleted(PG_FUNCTION_ARGS)
1028 {
1029         Oid                     dbid = PG_GETARG_OID(0);
1030         int64           result;
1031         PgStat_StatDBEntry *dbentry;
1032
1033         if ((dbentry = pgstat_fetch_stat_dbentry(dbid)) == NULL)
1034                 result = 0;
1035         else
1036                 result = (int64) (dbentry->n_tuples_deleted);
1037
1038         PG_RETURN_INT64(result);
1039 }
1040
1041 Datum
1042 pg_stat_get_bgwriter_timed_checkpoints(PG_FUNCTION_ARGS)
1043 {
1044         PG_RETURN_INT64(pgstat_fetch_global()->timed_checkpoints);
1045 }
1046
1047 Datum
1048 pg_stat_get_bgwriter_requested_checkpoints(PG_FUNCTION_ARGS)
1049 {
1050         PG_RETURN_INT64(pgstat_fetch_global()->requested_checkpoints);
1051 }
1052
1053 Datum
1054 pg_stat_get_bgwriter_buf_written_checkpoints(PG_FUNCTION_ARGS)
1055 {
1056         PG_RETURN_INT64(pgstat_fetch_global()->buf_written_checkpoints);
1057 }
1058
1059 Datum
1060 pg_stat_get_bgwriter_buf_written_clean(PG_FUNCTION_ARGS)
1061 {
1062         PG_RETURN_INT64(pgstat_fetch_global()->buf_written_clean);
1063 }
1064
1065 Datum
1066 pg_stat_get_bgwriter_maxwritten_clean(PG_FUNCTION_ARGS)
1067 {
1068         PG_RETURN_INT64(pgstat_fetch_global()->maxwritten_clean);
1069 }
1070
1071 Datum
1072 pg_stat_get_buf_written_backend(PG_FUNCTION_ARGS)
1073 {
1074         PG_RETURN_INT64(pgstat_fetch_global()->buf_written_backend);
1075 }
1076
1077 Datum
1078 pg_stat_get_buf_alloc(PG_FUNCTION_ARGS)
1079 {
1080         PG_RETURN_INT64(pgstat_fetch_global()->buf_alloc);
1081 }
1082
1083
1084 /* Discard the active statistics snapshot */
1085 Datum
1086 pg_stat_clear_snapshot(PG_FUNCTION_ARGS)
1087 {
1088         pgstat_clear_snapshot();
1089
1090         PG_RETURN_VOID();
1091 }
1092
1093
1094 /* Reset all counters for the current database */
1095 Datum
1096 pg_stat_reset(PG_FUNCTION_ARGS)
1097 {
1098         pgstat_reset_counters();
1099
1100         PG_RETURN_VOID();
1101 }