resource logging to optimize DBMS queries: how match access_log to pg log? - Mailing list pgsql-php

From Scott Weikart
Subject resource logging to optimize DBMS queries: how match access_log to pg log?
Date
Msg-id 02030118361905.01861@sandino.dnsalias.org
Whole thread Raw
Responses Re: resource logging to optimize DBMS queries: how match access_log to pg log?  (Scott Weikart <ScottW@benetech.org>)
List pgsql-php
I've inherited a large PHP application that needs tuning.  I've
written the following function that I run at the end of every web
page to log resource usage, so I can decide which web pages generate
the most load, and focus my optimization efforts.

But, to quantify the PostgreSQL load, I don't know how to gather any
other information besides the number of queries.

I can set

    debug_print_query = true
    log_pid = true

in postgres.conf, so that I can see which queries were run during a
particular database connection.

But, how can I match the PHP log with the PID of the database
connection?  Is there an SQL command I can run to find out my
connections PID, or some other identifier that I can put in
PostgreSQL's log?

Also, is there a query I can run that will quantify the amount of
resources that PostgreSQL had to use to satisfy all the queries made
during the connection?

-scott
=============================================================================
// These two commands are run at the start of every page (by
// putting them into an include file that all pages use)

    // collect initial data for resource logging
    $startWallTime = gettimeofday();
    $startRusage   = getrusage();

// these lines are put inside my low-level function that sends
// queries to PostgreSQL

    global $queryCount;
    $queryCount += 1;

// This function is called at the end of every page

    function logResourcesUsed()
    {
        global $startWallTime, $startRusage, $queryCount;
        global $REMOTE_ADDR, $REQUEST_METHOD, $REQUEST_URI;

        $endWallTime = gettimeofday();
        $endRusage = getrusage();
        $CPUtime = posix_times(); // this data is reset for each page
        $date = date("ymd:His");

        if ( ($fp = fopen(RESOURCE_LOG, "a")) == FALSE)
            return;

        $msecs  = ($endWallTime["sec"]  - $startWallTime["sec"]) * 1000;
        $msecs += ($endWallTime["usec"] - $startWallTime["usec"]) / 1000;

        # verbose format to make debugging easier
        $format = "%s %s %s %s PID=%d msecs=%d utime=%d stime=%d " .
            "cutime=%d cstime=%d pagefault=%d nswap=%d queries=%d\n";

        # machine readable format, the default
        $format = "%s %s %s %s %d %d %d %d %d %d %d %d %d\n";

        $record = sprintf($format,
                          $REMOTE_ADDR, $date, $REQUEST_METHOD, $REQUEST_URI,
                          getmypid(), $msecs,
                          $CPUtime["utime"], $CPUtime["stime"],
                          $CPUtime["cutime"], $CPUtime["cstime"],
                          $endRusage["ru_majflt"] - $startRusage["ru_majflt"],
                          $endRusage["ru_nswap"] - $startRusage["ru_nswap"],
                          $queryCount);

        fputs($fp, $record);
        fclose($fp);
    }

pgsql-php by date:

Previous
From: Alby
Date:
Subject: Please Quote
Next
From: Scott Weikart
Date:
Subject: Re: resource logging to optimize DBMS queries: how match access_log to pg log?