Skip site navigation (1) Skip section navigation (2)

Re: Query hanging/not finishing inconsistently

From: "Craig A(dot) James" <cjames(at)modgraph-usa(dot)com>
To: meetesh(dot)karia(at)alumni(dot)duke(dot)edu
Cc: pgsql-performance(at)postgresql(dot)org, Jean-David Dahan <jd(dot)dahan(at)gmail(dot)com>
Subject: Re: Query hanging/not finishing inconsistently
Date: 2006-05-22 18:49:39
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-performance
Meetesh Karia wrote:
> Hi all,
> We've recently started having a problem where a query that normally 
> executes in ~15ms starts to take upwards of 20s to complete.  When the 
> connection that ran query is returned to the connection pool, it appears 
> as though a transaction is still in progress so the connection pool 
> tries to cancel the transaction and close the connection.  This fails 
> and the connection is removed from the connection pool.  At this point, 
> the situation rapidly degrades and we run out of connections to the 
> postgres server.
> An inspection of the pg_stat_activity table shows that practically every 
> connection is running the above-mentioned query and some of those 
> queries have been active for many minutes!  We've looked at the pg_locks 
> table as well and the only exclusive locks are on transactions that are 
> open.  All other locks are AccessShareLocks.  Also, as far as we can 
> tell (from looking at the Hibernate stats), every db session that is 
> opened is closed.
> When this happens, if I kill one of the running postgres processes (just 
> by picking the last process returned from "ps -ef | grep postgres"), the 
> other queries will immediately finish and the system will respond.  
> However, within 15 minutes, we'll be back in the same state as before.  
> At that point, I've cycled Apache, Tomcat and Postgres and the system 
> then seems to come back.

This sounds suspiciously like a question I asked a few weeks ago, on April 4.  I have a process that just gets stuck.  After some questions from various of the experts in this forum, I used gdb(1) to attach to one of the frozen Postgress backend processes, and here's what I found:

On 5/12/2006, I wrote:
> Thanks, good advice.  You're absolutely right, it's stuck on a
> mutex.  After doing what you suggest, I discovered that the query
> in progress is a user-written function (mine).  When I log in as
> root, and use "gdb -p <pid>" to attach to the process, here's
> what I find.  Notice the second function in the stack, a mutex
> lock:
> (gdb) bt
> #0  0x0087f7a2 in _dl_sysinfo_int80 () from /lib/
> #1  0x0096cbfe in __lll_mutex_lock_wait () from /lib/tls/
> #2  0x008ff67b in _L_mutex_lock_3220 () from /lib/tls/
> #3  0x4f5fc1b4 in ?? ()
> #4  0x00dc5e64 in std::string::_Rep::_S_empty_rep_storage () from /usr/local/pgsql/lib/
> #5  0x009ffcf0 in ?? () from /usr/lib/
> #6  0xbfe71c04 in ?? ()
> #7  0xbfe71e50 in ?? ()
> #8  0xbfe71b78 in ?? ()
> #9  0x009f7019 in zcfree () from /usr/lib/
> #10 0x009f7019 in zcfree () from /usr/lib/
> #11 0x009f8b7c in inflateEnd () from /usr/lib/
> #12 0x00c670a2 in ~basic_unzip_streambuf (this=0xbfe71be0) at zipstreamimpl.h:332
> #13 0x00c60b61 in OpenBabel::OBConversion::Read (this=0x1, pOb=0xbfd923b8, pin=0xffffffea) at istream:115
> #14 0x00c60fd8 in OpenBabel::OBConversion::ReadString (this=0x8672b50, pOb=0xbfd923b8) at obconversion.cpp:780
> #15 0x00c19d69 in chmoogle_ichem_mol_alloc () at stl_construct.h:120
> #16 0x00c1a203 in chmoogle_ichem_normalize_parent () at stl_construct.h:120
> #17 0x00c1b172 in chmoogle_normalize_parent_sdf () at vector.tcc:243
> #18 0x0810ae4d in ExecMakeFunctionResult ()
> #19 0x0810de2e in ExecProject ()
> #20 0x08115972 in ExecResult ()
> #21 0x08109e01 in ExecProcNode ()
> #22 0x00000020 in ?? ()
> #23 0xbed4b340 in ?? ()
> #24 0xbf92d9a0 in ?? ()
> #25 0xbed4b0c0 in ?? ()
> #26 0x00000000 in ?? ()
> It looks to me like my code is trying to read the input parameter
> (a fairly long string, maybe 2K) from a buffer that was gzip'ed
> by Postgres for the trip between the client and server... somewhere
> along the way, a mutex gets set, and then ... it's stuck forever.
> ps(1) shows that this thread had been running for about 7 hours,
> and the job status showed that this function had been
> successfully called about 1 million times, before this mutex lock
> occurred.

This is not an issue that's been resolved.  Nobody had ever seen this before.  Tom Lane suggested it might be a libc/c++ bug, but unfortunately in my case this lockup occurs so rarely (every few days) that it will be very difficult to know if we've fixed the problem.

If gdb(1) reveals that your process is stuck in a mutex, then you might have a better chance testing this hypothesis, since your problem happens within 15 minutes or so.

Did this start recently, perhaps right after a kernel update?


In response to


pgsql-performance by date

Next:From: Meetesh KariaDate: 2006-05-22 20:40:11
Subject: Re: Query hanging/not finishing inconsistently
Previous:From: Meetesh KariaDate: 2006-05-22 17:20:09
Subject: Query hanging/not finishing inconsistently

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group