~ubuntu-branches/ubuntu/trusty/postgresql-8.4/trusty

« back to all changes in this revision

Viewing changes to src/backend/utils/adt/pgstatfuncs.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-11 16:59:35 UTC
  • mfrom: (5.1.1 karmic)
  • Revision ID: james.westby@ubuntu.com-20090711165935-jfwin6gfrxf0gfsi
Tags: 8.4.0-2
* debian/libpq-dev.install: Ship catalog/genbki.h. (Closes: #536139)
* debian/rules: Drop --enable-cassert for final release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
 *
9
9
 *
10
10
 * IDENTIFICATION
11
 
 *        $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.53 2009/01/01 17:23:49 momjian Exp $
 
11
 *        $PostgreSQL: pgsql/src/backend/utils/adt/pgstatfuncs.c,v 1.54 2009/06/11 14:49:04 momjian Exp $
12
12
 *
13
13
 *-------------------------------------------------------------------------
14
14
 */
332
332
Datum
333
333
pg_stat_get_function_calls(PG_FUNCTION_ARGS)
334
334
{
335
 
        Oid     funcid = PG_GETARG_OID(0);
 
335
        Oid                     funcid = PG_GETARG_OID(0);
336
336
        PgStat_StatFuncEntry *funcentry;
337
337
 
338
338
        if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
343
343
Datum
344
344
pg_stat_get_function_time(PG_FUNCTION_ARGS)
345
345
{
346
 
        Oid     funcid = PG_GETARG_OID(0);
 
346
        Oid                     funcid = PG_GETARG_OID(0);
347
347
        PgStat_StatFuncEntry *funcentry;
348
348
 
349
349
        if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
354
354
Datum
355
355
pg_stat_get_function_self_time(PG_FUNCTION_ARGS)
356
356
{
357
 
        Oid     funcid = PG_GETARG_OID(0);
 
357
        Oid                     funcid = PG_GETARG_OID(0);
358
358
        PgStat_StatFuncEntry *funcentry;
359
359
 
360
360
        if ((funcentry = pgstat_fetch_stat_funcentry(funcid)) == NULL)
410
410
        if (SRF_IS_FIRSTCALL())
411
411
        {
412
412
                MemoryContext oldcontext;
413
 
                TupleDesc tupdesc;
414
 
                
 
413
                TupleDesc       tupdesc;
 
414
 
415
415
                funcctx = SRF_FIRSTCALL_INIT();
416
 
                
 
416
 
417
417
                oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
418
418
 
419
419
                tupdesc = CreateTemplateTupleDesc(10, false);
441
441
                        /*
442
442
                         * Get one backend - locate by pid.
443
443
                         *
444
 
                         * We lookup the backend early, so we can return zero rows if it doesn't
445
 
                         * exist, instead of returning a single row full of NULLs.
 
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
446
                         */
447
 
                        int             pid = PG_GETARG_INT32(0);
448
 
                        int             i;
449
 
                        int             n = pgstat_fetch_stat_numbackends();
450
 
                        
 
447
                        int                     pid = PG_GETARG_INT32(0);
 
448
                        int                     i;
 
449
                        int                     n = pgstat_fetch_stat_numbackends();
 
450
 
451
451
                        for (i = 1; i <= n; i++)
452
452
                        {
453
453
                                PgBackendStatus *be = pgstat_fetch_stat_beentry(i);
 
454
 
454
455
                                if (be)
455
456
                                {
456
457
                                        if (be->st_procpid == pid)
457
458
                                        {
458
 
                                                *(int *)(funcctx->user_fctx) = i;
 
459
                                                *(int *) (funcctx->user_fctx) = i;
459
460
                                                break;
460
461
                                        }
461
462
                                }
462
463
                        }
463
464
 
464
 
                        if (*(int *)(funcctx->user_fctx) == 0)
 
465
                        if (*(int *) (funcctx->user_fctx) == 0)
465
466
                                /* Pid not found, return zero rows */
466
467
                                funcctx->max_calls = 0;
467
468
                        else
468
469
                                funcctx->max_calls = 1;
469
470
                }
470
 
                
 
471
 
471
472
                MemoryContextSwitchTo(oldcontext);
472
473
        }
473
474
 
477
478
        if (funcctx->call_cntr < funcctx->max_calls)
478
479
        {
479
480
                /* for each row */
480
 
                Datum                   values[10];
481
 
                bool                    nulls[10];
482
 
                HeapTuple               tuple;
 
481
                Datum           values[10];
 
482
                bool            nulls[10];
 
483
                HeapTuple       tuple;
483
484
                PgBackendStatus *beentry;
484
 
                SockAddr                zero_clientaddr;
 
485
                SockAddr        zero_clientaddr;
485
486
 
486
487
                MemSet(values, 0, sizeof(values));
487
488
                MemSet(nulls, 0, sizeof(nulls));
488
 
                
489
 
                if (*(int *)(funcctx->user_fctx) > 0)
 
489
 
 
490
                if (*(int *) (funcctx->user_fctx) > 0)
490
491
                        /* Get specific pid slot */
491
 
                        beentry = pgstat_fetch_stat_beentry(*(int *)(funcctx->user_fctx));
 
492
                        beentry = pgstat_fetch_stat_beentry(*(int *) (funcctx->user_fctx));
492
493
                else
493
494
                        /* Get the next one in the list */
494
 
                        beentry = pgstat_fetch_stat_beentry(funcctx->call_cntr+1); /* 1-based index */
 
495
                        beentry = pgstat_fetch_stat_beentry(funcctx->call_cntr + 1);            /* 1-based index */
495
496
                if (!beentry)
496
497
                {
497
 
                        int i;
 
498
                        int                     i;
498
499
 
499
 
                        for (i = 0; i < sizeof(nulls)/sizeof(nulls[0]); i++)
 
500
                        for (i = 0; i < sizeof(nulls) / sizeof(nulls[0]); i++)
500
501
                                nulls[i] = true;
501
502
 
502
503
                        nulls[3] = false;
543
544
                        /* A zeroed client addr means we don't know */
544
545
                        memset(&zero_clientaddr, 0, sizeof(zero_clientaddr));
545
546
                        if (memcmp(&(beentry->st_clientaddr), &zero_clientaddr,
546
 
                                                                          sizeof(zero_clientaddr) == 0))
 
547
                                           sizeof(zero_clientaddr) == 0))
547
548
                        {
548
549
                                nulls[8] = true;
549
550
                                nulls[9] = true;
554
555
#ifdef HAVE_IPV6
555
556
                                        || beentry->st_clientaddr.addr.ss_family == AF_INET6
556
557
#endif
557
 
                                   )
 
558
                                        )
558
559
                                {
559
 
                                        char        remote_host[NI_MAXHOST];
 
560
                                        char            remote_host[NI_MAXHOST];
560
561
                                        char            remote_port[NI_MAXSERV];
561
562
                                        int                     ret;
562
563
 
576
577
                                        {
577
578
                                                clean_ipv6_addr(beentry->st_clientaddr.addr.ss_family, remote_host);
578
579
                                                values[8] = DirectFunctionCall1(inet_in,
579
 
                                                                                                           CStringGetDatum(remote_host));
 
580
                                                                                           CStringGetDatum(remote_host));
580
581
                                                values[9] = Int32GetDatum(atoi(remote_port));
581
582
                                        }
582
583
                                }
583
584
                                else if (beentry->st_clientaddr.addr.ss_family == AF_UNIX)
584
585
                                {
585
586
                                        /*
586
 
                                         * Unix sockets always reports NULL for host and -1 for port, so it's
587
 
                                         * possible to tell the difference to connections we have no
588
 
                                         * permissions to view, or with errors.
 
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.
589
591
                                         */
590
592
                                        nulls[8] = true;
591
593
                                        values[9] = DatumGetInt32(-1);