pgbadger is a recent Postgres module presented during the lightning talks of PGCon 2012 by its original author Gilles Darold. It is thought as an alternative to PgFouine, able to replace it thanks to its flexibility, extensibility and performance.

So, like PGFouine, pgbadger is a Postgres log file analyzer, meaning that it is able to deparse, analyze the data of your log files and then provide statistics about your database: from overall data (query list, number of transactions), error counts (most frequent events) to more performance details like the queries that took the longest run time.
This means that basically you use in input log files from PostgreSQL, and you get in output an html or txt page that gives you all the statistics you want. So you do not need to sneak anymore in your log files to analyze what is happening anymore. Simply launch pgbadger, wait for parsing (which is pretty fast btw), and see.

Before describing more in details the functionalities of pgbadger, why is it an alternative to pgfouine?

  1. pgbadger is written in perl, pgfouine is written in php. So with pgbadger you do not need to install extra packages, Postgres core using natively perl. And well, perl is more performant than php. And php… is php…
  2. Developped by the community, for the community
  3. Latest release of pgfouine is from 2010, it doesn’t look to be that much maintained. pgbadger is a new project, young and dynamic, and more and more people are gathering to develop it.
  4. It is developped by cool guys, OK pgfouine also… That is maybe not a real argument…

Now, let’s put our hands on the beast. There are several ways to get this module.
First, you can fetch the code of the project directly from Github with those commands:
git clone https://github.com/dalibo/pgbadger.git
Also, you can download the tarball from here.
Then install it with those commands.
tar xzf pgbadger-1.x.tar.gz
cd pgbadger-1.x/
perl Makefile.PL
make && sudo make install

This will install pgbadger in /usr/bin/local and some man pages. You can refer to the README of the project for more details.

As told before, pgbadger is a log file deparser, so you need to set up the output of your log files correctly to allow pgbadger to look at your database server information. The more logging parameters you activate, the more information you will be able to get from your log files. Here are the settings I used for this post and the test below.
logging_collector = on
log_min_messages = debug1
log_min_error_statement = debug1
log_min_duration_statement = 0
log_line_prefix = '%t [%p]: [%l-1] user=%u,db=%d '
log_checkpoints = on
log_connections = on
log_disconnections = on
log_lock_waits = on
log_temp_files = 0

You will need to customize those options depending on the information you want, just do not forget that setting up the root correctly is the key for success.
At the state of pgbadger 2.0, log_statement and log_duration cannot be activated, so take care not to use them.

For the purpose of this post and in order to produce a couple of log files, I ran a 5-minute pgbench test on a fresh Postgres server.
So the logs I obtained are not at all production-like, but enough to show what pgbadger can do.

By the way, pgbadger has several options making it pretty flexible, among them you have the possibility to specify multiple log files at the same time, specify an interval of time for the analysis, and far more. It is also possible to choose output format of result. Among the possible use cases I got on top of my head:

  • Creation of a text file report, and send it to a mailing list automatically
  • Creation of an html file, and upload it automatically to a web server
  • Base log analysis on a cron with a certain time interval
  • etc.

pgbadger has few dependencies, so it makes it pretty flexible for your environments. Once again the README of the project gives more examples of use, so do not hesitate to refer to it.

So, just after my short pgbench run, I got a set of log files ready for analysis. Now it is time to parse them.
Note: the pgbench test has been done with default values without thinking, so don’t worry about the bad performance results :)
$ ./pgbadger ~/pgsql/master/pg_log/postgresql-2012-08-30_132*
[========================>] Parsed 10485768 bytes of 10485768 (100.00%)
[========================>] Parsed 10485828 bytes of 10485828 (100.00%)
[========================>] Parsed 10485851 bytes of 10485851 (100.00%)
[========================>] Parsed 10485848 bytes of 10485848 (100.00%)
[========================>] Parsed 10485839 bytes of 10485839 (100.00%)
[========================>] Parsed 982536 bytes of 982536 (100.00%)

In result, I got a file called out.html (default, but customizable) showing a bunch of data, analyzing things automatically.
The most interesting part is perhaps the performance analysis, showing you a list of the less performant queries, so this will allow you to tune your database based on the log data obtained.

Here are some pictures showing portions of the output results you will get.

pgbadger example 1

pgbadger example 1


pgbadger example 2

pgbadger example 2

I forgot to tell something: this utility is 100% compatible with Postgres-XC, the only thing you need to do is just to launch pgbadger for each node of your cluster.

So, pgbadger is light, fast and is waiting for your love. It is one of those utilities that you can use not only for production database systems, but for extra things like benchmark or performance analysis. Its installation is easy, will not heavy your system with packages you might not want, so go ahead and use it.

When creating files in Windows, those files will have the DOS format.
This creates annoying ^M characters at the end of lines, which can be seen in patches or diff files.

In order to localize them, use this command:
find . -not -type d -exec file "{}" ";" | grep CRLF
It will give an output like this for dos files:
./example.txt: ASCII text, with CRLF line terminators

Then open them in emacs, and launch that command:
M-x set-buffer-file-coding-system RET undecided-unix
M-x means escape and X.
RET is return.
Then save your file and you are done.

Here is a short script/memo to find strings inside given file.
The script is assumed to be called strfind. It is written in bash.

Here is the spec of this script.
michael@boheme:~/bin $ strfind ?
Usage: strfind [-i] [filename] [string]
Exemple: strfind "[hc]" text

You can then find strings with commands like:
strfind *.c $TEXT_SEARCH
It is also possible to ignore case distinctions.
strfind -i *.c $TEXT_SEARCH

So here is the script.
#!/bin/bash
#Find string strings in select file extension
 
#Expected base arguments
EXPECTED_ARGS=2
IFLAG=0
 
while getopts 'i' OPTION
do
 case $OPTION in
 i) #Track in repo all untracked files
   IFLAG=1
   #+1 base argument
   EXPECTED_ARGS=$(($EXPECTED_ARGS + 1))
   ;;
 ?) echo "Usage: `basename $0` [-i] [filename] [string]"
   echo "Exemple: `basename $0` \"[hc]\" text"
   exit 0
   ;;
 esac
done
 
if [ $# -ne $EXPECTED_ARGS ]
then
 echo "Usage: `basename $0` [-i] [filename] [string]"
 echo "Exemple: `basename $0` \"[hc]\" text"
 exit 1
fi
 
#Have only 2 or 3 arguments
if [ "$EXPECTED_ARGS" = "2" ]
then
 FILENAME=$1
 TXTSTRING=$2
else
 FILENAME=$2
 TXTSTRING=$3
fi
 
#Print file name and line number
OPTIONS="-Hn"
 
#Don't care about large characters
if [ "$IFLAG" = "1" ]
then
 OPTIONS=$OPTIONS"i"
fi
 
#Execute command
echo find . -name "$FILENAME" -exec grep $OPTIONS $TXTSTRING {} \;
find . -name "$FILENAME" -exec grep $OPTIONS $TXTSTRING {} \;
exit 0;

Here is a memo about RPM building in Linux environment.
First is necessary an RPM-based distribution. The most famous are Fedora and CentOS.

Let’s suppose you want to build your RPMs in the folder $RPMREPO.
You need first the correct folder tree for package creation.
mkdir $RPMREPO
cp -r /usr/src/redhat/* $RPMREPO

Then what is necessary is a spec file (Ex: spec_file.spec). It contains all the directives necessary to create the package.
You may find examples of spec files in RedHat SVN repositories like the one of PostgreSQL 9.0 package.
With a spec file, you may need a PAM file (Ex: file.pam), containing data like:
#%PAM-1.0
auth include password-auth
account include password-auth

Don’t forget that you also need a tarball (Ex: foo.tar.gz) or something equivalent containing the code.
Then copy the PAM file and the tarball inside $RPMREPO/SOURCES.
cp file.pam foo.tar.gz $RPMREPO/SOURCES
Copy the spec file to the correct folder.
cp spec_file.spec $RPMREPO/SPECS

Before building an RPM, it is necessary to set a file called .rpmmacros located in $HOME directory.
echo "%_topdir $RPMREPO" > $HOME/.rpmmacros

Then you can create the RPM with this command, assuming spec, pam and tarball files are correct.
rpmbuild -ba spec_file.spec

If no error occurred, SRPM file is located in $RPMREPO/SRPMS, RPM packages are located in $RPMREPO/RPMS/x86_64.

Here are some additional useful commands.
You can also build a RPM package from a SRPM file.
rpmbuild --rebuild $SRPM_FILE

Check content of an RPM file.
rpm -qpl $RPM_FILE

Export files of an RPM package.
rpm2cpio $RPM_FILE | cpio -idv

Sometimes you have to face some formats that are not installed by default in Ubuntu environments.
And it may be a problem if you cannot extract such archives.

Fortunately, there are some free applications provided with your distribution.
If you are not using Ubuntu, you can find debian or rpm packages easily.
In order to do that, there is this useful RPM package searcher
or this Debian package searcher.

For Ubuntu, which is well… Debian based… (But it uses an APT system to manage its distribution packages)
Here is how to install those packages with commands (geek-mode).
To install rar format manager:
sudo apt-get install unrar-free
To install 7z format manager:
sudo apt-get install p7zip

Or for beginners you can find the packages in the software package center.
For that you have just to make a research with p7zip and unrar-free for each application in the Ubuntu Software Center in the Application tab of your menu bar.

To decompress a file with p7zip, you have to do the following command:
7z x $FILE_NAME
$FILE_NAME being the name of your 7z file.

To decompress a file with unrar-free, you have to do the following command:
unrar-free x $FILE_NAME
$FILE_NAME being the name of your rar file.

©2010-2013 Michael Paquier All content is ©Copyright of Otacoo.com 2010-2013. Privacy Policy - Terms of Use