From: Scott Weikart <ScottW@benetech.org>
To: pgsql-php@postgresql.org
Cc: Scott Weikart <scottw@benetech.org>
Subject: resource logging to optimize DBMS queries: how match access_log to pg log?
Date: Fri, 1 Mar 2002 18:36:19 -0800
Message-ID: <02030118361905.01861@sandino.dnsalias.org> (raw)
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);
}
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-php@postgresql.org
Cc: ScottW@benetech.org
Subject: Re: resource logging to optimize DBMS queries: how match access_log to pg log?
In-Reply-To: <02030118361905.01861@sandino.dnsalias.org>
References: <02030118361905.01861@sandino.dnsalias.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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