pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
resource logging to optimize DBMS queries: how match access_log to pg log?
3+ messages / 2 participants
[nested] [flat]

* resource logging to optimize DBMS queries: how match access_log to pg log?
@ 2002-03-02 02:36  Scott Weikart <ScottW@benetech.org>
  0 siblings, 1 reply; 3+ messages in thread

From: Scott Weikart @ 2002-03-02 02:36 UTC (permalink / raw)
  To: pgsql-php; +Cc: Scott Weikart <scottw@benetech.org>

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);
    }



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: resource logging to optimize DBMS queries: how match access_log to pg log?
@ 2002-03-02 23:25  Scott Weikart <ScottW@benetech.org>
  parent: Scott Weikart <ScottW@benetech.org>
  0 siblings, 1 reply; 3+ messages in thread

From: Scott Weikart @ 2002-03-02 23:25 UTC (permalink / raw)
  To: pgsql-php; +Cc: Scott Weikart <scottw@benetech.org>

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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: resource logging to optimize DBMS queries: how match access_log to pg log?
@ 2002-03-03 10:32  Papp Gyozo <pgerzson@freestart.hu>
  parent: Scott Weikart <ScottW@benetech.org>
  0 siblings, 0 replies; 3+ messages in thread

From: Papp Gyozo @ 2002-03-03 10:32 UTC (permalink / raw)
  To: pgsql-php

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




^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2002-03-03 10:32 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-03-02 02:36 resource logging to optimize DBMS queries: how match access_log to pg log? Scott Weikart <ScottW@benetech.org>
2002-03-02 23:25 ` Scott Weikart <ScottW@benetech.org>
2002-03-03 10:32   ` Papp Gyozo <pgerzson@freestart.hu>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox