Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 148876329FA for ; Fri, 25 Jun 2010 17:53:49 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 10071-03 for ; Fri, 25 Jun 2010 20:53:42 +0000 (UTC) X-Greylist: delayed 00:34:45.453186 by SQLgrey-1.7.6 Received: from statler.bytemark.co.uk (statler.bytemark.co.uk [212.110.162.18]) by mail.postgresql.org (Postfix) with ESMTP id E1C3263271D for ; Fri, 25 Jun 2010 17:53:41 -0300 (ADT) Received: from tetrarch.vm.bytemark.co.uk ([89.16.174.118] helo=mail.audioboundary.com) by statler.bytemark.co.uk with esmtp (Exim 4.69) (envelope-from ) id 1OSFMZ-0006uc-Tz; Fri, 25 Jun 2010 21:18:59 +0100 Received: from [192.168.1.66] (93-97-117-199.zone5.bethere.co.uk [93.97.117.199]) by mail.audioboundary.com (Postfix) with ESMTPSA id DE411E0281; Fri, 25 Jun 2010 21:18:52 +0100 (BST) Message-ID: <4C250F2C.2070709@audioboundary.com> Date: Fri, 25 Jun 2010 21:18:52 +0100 From: Tom Molesworth User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.9) Gecko/20100423 Thunderbird/3.0.4 MIME-Version: 1.0 To: pgsql-performance@postgresql.org, mallah.rajesh@gmail.com Subject: Re: Re: sudden spurt in swap utilization (was:cpu bound postgresql setup.) References: <4C2492F9.2020706@gmail.com> <4C2477A20200002500032B03@gw.wicourts.gov> <4C24DBA9.7030303@audioboundary.com> In-Reply-To: Content-Type: multipart/mixed; boundary="------------090204070905000607010605" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.8 tagged_above=-10 required=5 tests=BAYES_50=0.8 X-Spam-Level: X-Archive-Number: 201006/484 X-Sequence-Number: 39340 This is a multi-part message in MIME format. --------------090204070905000607010605 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 25/06/10 20:00, Rajesh Kumar Mallah wrote: > Dear tom, we have autocommit off in dbi. Any commit or rollback from > the persistent modperl process immediately issues begin work; if the > modperl process is waiting for request the database backend remains in > idle in transaction state. Unless we modify data in a http request we > neighter issue a commit nor rollback. > The backend shouldn't go to 'idle in transaction' state until there is some activity within the transaction. I've attached an example script to demonstrate this - note that even SELECT queries will leave the handle as 'IDLE in transaction' unless you've changed the transaction isolation level from the default. Any queries that are idle in transaction will block connection pooling and cause old versions of table rows to hang around, as described in other replies. Note that this is nothing to do with mod_perl, it's purely due to the way transactions are handled - a one-off script would also have this issue, but on exit issues an implicit rollback and disconnects. Typically your database wrapper would handle this (I think DBIx::Class should take care of this automatically, although I haven't used it myself). Tom --------------090204070905000607010605 Content-Type: application/x-perl; name="dbiPgConnectionHandling.pl" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="dbiPgConnectionHandling.pl" #!/usr/bin/perl use strict; use warnings FATAL => 'all', NONFATAL => 'redefine'; =pod Demonstrate transaction status at various points in the query lifecycle. Sets up two database handles, one to run some transactions and the other to select from the pg_stat_activity table to get information on current query status. Easier to read if you create a separate database to run this test, and connect the pg_stat_activity handle ($monitorDBH) to any existing database. Expected output: Before create table: After create table: in transaction After commit: Before prepare: After prepare: After execute: in transaction After rollback: =cut use DBI; my $dbname = 'test'; my $dbh = DBI->connect('dbi:Pg:db=' . $dbname, undef, undef, { RaiseError => 1, AutoCommit => 0 }) or die DBI->errstr; my $monitorDBH = DBI->connect('dbi:Pg:db=tom', undef, undef, { RaiseError => 1, AutoCommit => 1 }) or die DBI->errstr; # Make a table so we have something 'transactional' print "Before create table: " . getTransactionStatus() . "\n"; eval { $dbh->do(q{create table tst ( id int )}); } or warn "$@"; print "After create table: " . getTransactionStatus() . "\n"; $dbh->commit; print "After commit: " . getTransactionStatus() . "\n"; print "Before prepare: " . getTransactionStatus() . "\n"; my $sth = $dbh->prepare(q{select * from tst}); print "After prepare: " . getTransactionStatus() . "\n"; $sth->execute; print "After execute: " . getTransactionStatus() . "\n"; $dbh->rollback; print "After rollback: " . getTransactionStatus() . "\n"; $dbh->do(q{drop table tst}); $dbh->commit; =head2 C Show current transaction status for the db in $dbname. =cut sub getTransactionStatus { my $rslt = $monitorDBH->selectall_arrayref(q{select current_query from pg_stat_activity where datname = ?}, undef, $dbname); return join(':', map { @$_ } @$rslt); } --------------090204070905000607010605--