Re: [GENERAL] How to detect Postgres deadlocks?

Lists: pgsql-adminpgsql-general
From: andre <andre(at)google(dot)com>
To: pgsql-admin(at)postgresql(dot)org, pgsql-general(at)postgresql(dot)org
Subject: How to detect Postgres deadlocks?
Date: 2006-08-23 19:45:56
Message-ID: 6c3954020608231245l3035ffbby41cfdbfac2d37326@mail.google.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-general

Hi,

We are using Postgres 7.4.5, and I'm trying to find a way to detect and
gather deadlock information.

1) Which sql queries should I use to detect deadlocks while they are
happening? I see the deadlock info on the log file, but I'd like to query
the database to see them as they happen...

2) Which fields on which pg catalogs indicate a deadlock condition?

3) How can I get more info about the processes involved on deadlocks?

4) How can I get the sql statements associated with the processes involved
on deadlocks?

On the logs I see the procpids of the processes involved on past deadlocks,
but I 'd like to know how to get those procpids, and how to get the
"current_query" associated with them...

Thank you in advance,

Andre Philippi


From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: andre <andre(at)google(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org
Subject: Re: [GENERAL] How to detect Postgres deadlocks?
Date: 2006-08-23 20:39:22
Message-ID: 200608232239.22827.peter_e@gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-general

andre wrote:
> 1) Which sql queries should I use to detect deadlocks while they are
> happening? I see the deadlock info on the log file, but I'd like to
> query the database to see them as they happen...

Since deadlocks are broken up within one second, it will be hard to
actually see them. You could crank up the deadlock timeout and then
query pg_locks and infer cycles from there. From there you can also
move over to the statistics tables to see what the processes are
currently doing. But the system won't actually tell you that there is
a deadlock until the timeout is over. If you have special requirements
like that, you need to patch the source.

The other option is to decorate your server log lines with pids and such
that you can link from the deadlock error message to the logged
statements.

--
Peter Eisentraut
http://developer.postgresql.org/~petere/


From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: andre <andre(at)google(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org, pgsql general <pgsql-general(at)postgresql(dot)org>
Subject: Re: [GENERAL] How to detect Postgres deadlocks?
Date: 2006-08-23 20:40:22
Message-ID: 1156365622.7223.62.camel@state.g2switchworks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-general

On Wed, 2006-08-23 at 14:45, andre wrote:
> Hi,
>
> We are using Postgres 7.4.5, and I'm trying to find a way to detect
> and gather deadlock information.
>
> 1) Which sql queries should I use to detect deadlocks while they are
> happening? I see the deadlock info on the log file, but I'd like to
> query the database to see them as they happen...
>
> 2) Which fields on which pg catalogs indicate a deadlock condition?
>
> 3) How can I get more info about the processes involved on deadlocks?
>
> 4) How can I get the sql statements associated with the processes
> involved on deadlocks?
>
> On the logs I see the procpids of the processes involved on past
> deadlocks, but I 'd like to know how to get those procpids, and how to
> get the "current_query" associated with them...

I'm not sure what you're really looking for. When PostgreSQL detects a
deadlock, it aborts one of the queries to stop the deadlock right away.

test=> update l set b=22 where i=2;
ERROR: deadlock detected
DETAIL: Process 25854 waits for ShareLock on transaction 11654043;
blocked by process 24918.
Process 24918 waits for ShareLock on transaction 11654047; blocked by
process 25854.
test=>

That deadlock detection took about 1 second and rolled back my
transaction immediately. I'm not sure you can detect them in real time
any better than pgsql can.

Or are you experiencing some kind of deadly embrace problem???

Tell us what problem you're having and maybe we can come up with some
better advice.