Ok so I have a postgres database at work that we use to store application information and this database is shared across some large web applications. We recently started getting the above error in the postgres logs, and occasional failures in the web apps. Let me state for the record I am not a postgres expert, I can install it, get it running and use it, beyond that is the extent of my postgres knowledge. Anyway back to my story. So we started having this wierd failures and not knowing how to diagnose it I started googling.

The first thing I find is how to see how many processes I have running:

ps auxww | grep ^postgres | wc -l

ps auxww | grep ^postgres

The first shows you how many process are running, and the second gives you more detail. So I now I had a little bit better understanding of what was going on. I could see how many postgres process I had at any one time. But that did not help me figure out which queries and what app was causing the problems.

I googled some more and figured out how to query the active queries.

So I run this query against the stats database:

SELECT count(*) as cnt, usename, current_query FROM pg_stat_activity GROUP BY usename,current_query ORDER BY cnt DESC;

But I get “<command string not enabled>”; so more googling and I found I need to change a setting in the postgresql.conf and set the stats_command_string = true.

Ok with this done I was able to run the query above and find 1 single query that was getting repeated over and over again hundreds of times and was taking 8-10 minutes to finish, and even longer as the queries stacked up.

On further inspection, this one query is supposed to be memcached, its hit on every page of a site and should cache once and never hit the database again, but its not. Some times it does and things run great, other times it continues to not find it in cache and hit the database.

We could not figure out why this particular query out of the hundreds we store in memcache is not being cached properly. So we cache it the old fashion way in file, till we can work out why it won’t cache in memcache. Once its cached again properly, ir improperly in this case, Postgres starts working properly again. No more high connections and no more app failures.

The end, or is it? Now we have to figure out why it won’t cache in Memcache properly.

Here is how you run a command as apache in centos without giving apache a valid shell.

su -s /bin/sh apache -c “whoami”

If you want a PHP script to validate an email address here is a quick and simple PHP regular expression to do it. This script is also case-insensitive, so it will treat all characters as lower case. This script is a simple and  easy way to check the syntax and format of an email address.

The code


<?php

$email "someone@example.com";

if(eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$"$email)) {

  echo "Valid email address.";

}

else {

  echo "Invalid email address.";

}

?>

Web browser caching is good. But even now in the days of 20+Mb broadband, caching web requests still speeds browsing up. It’s also saves money in bandwidth costs.

At home, and work, I set up a caching proxy server with pfSense and Squid, rendering my browser cache effectively useless. Here’s how to disable FireFox’s browser cache completely.

  1.    Fire up FireFox
  2.    Type about:config in your address bar
  3.    Type ‘cache’ in the search bar, and look for network.http.use-cache, and double click it to set it to false. Double clicking it again will set it to true and re-enable the cache

To forcibly reload a page and all its dependencies, direct from source, ignoring local and proxy caches hold the shift key and hit reload. This applies not only to FireFox but also IE6/7 and Safari (maybe others too).

Just run the following command from the command line:

echo 'd *' | mail -N

Great little perl snippet to let you know the number of files and directories in the current directory. Run from the command line.

ls -l |perl -e 'while(<>){$h{substr($_,0,1)}+=1;} END {foreach(keys %h){print "$_ $h{$_}\n";}}'

<form method="post" action="blah.php" onSubmit="return confirm('Are you sure this is correct?');">

PHP Frameworks

Been thinking of using a PHP framework found a very interesting slideshow on the Zend Framework. Any opinions?

Found a great slide that talks about the top 10 mistakes when programming scalable applications with PHP. Discusses everything from hardware to AJAX. The AJAX piece I found to be very interesting. Check it out and give me your thoughts.

Quick and easy way to validate email strings. I picked this bit of code up from somewhere, not sure where….Think it was the Zend site. Credit goes to the original unknown author, not me.

<?php
function is_email($address) {
$rc1 = (ereg('^[-!#$%&'*+./0-9=?A-Z^_`a-z{|}~]+'.
'@'.
'
[-!#$%&'*+\/0-9=?A-Z^_`a-z{|}~]+.'.
'
[-!#$%&'*+\./0-9=?A-Z^_`a-z{|}~]+$',
$address));
$rc2 = (preg_match('/.+.ww+$/',$address));
return (
$rc1 && $rc2);
}
?>