Thread: resource logging to optimize DBMS queries: how match access_log to pg log?

resource logging to optimize DBMS queries: how match access_log to pg log?

From
Scott Weikart
Date:
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);
    }

Re: resource logging to optimize DBMS queries: how match access_log to pg log?

From
Scott Weikart
Date:
Responding to my own email...

On Friday 01 March 2002  6:36 pm, Scott Weikart wrote:
> 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?

I just figured out the obvious solution to my first problem: don't
rely on the PostgreSQL log, instead have the PHP application log the
queries itself.

However, I would still like to know the answer to this problem:

> 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

just a question:
have you tried to trace the connection?
It may reveal some additional information, may not.

[pg_trace() pg_untrace()]

----- Original Message -----
From: "Scott Weikart" <ScottW@benetech.org>
To: <pgsql-php@postgresql.org>
Cc: "Scott Weikart" <scottw@benetech.org>
Sent: Sunday, March 03, 2002 12:25 AM
Subject: Re: [PHP] resource logging to optimize DBMS queries: how match access_log to pg log?


| Responding to my own email...
|
| On Friday 01 March 2002  6:36 pm, Scott Weikart wrote:
| > 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?
|
| I just figured out the obvious solution to my first problem: don't
| rely on the PostgreSQL log, instead have the PHP application log the
| queries itself.
|
| However, I would still like to know the answer to this problem:
|
| > 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
|
| ---------------------------(end of broadcast)---------------------------
| TIP 3: if posting/reading through Usenet, please send an appropriate
| subscribe-nomail command to majordomo@postgresql.org so that your
| message can get through to the mailing list cleanly