Unix Technical Forum

reading pg_stat_activity view

This is a discussion on reading pg_stat_activity view within the pgsql Admins forums, part of the PostgreSQL category; --> Hi everybody, As I look at output from pg_stat_activity view, I see often <idle> under query column and on ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Admins

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 09:17 AM
Tena Sakai
 
Posts: n/a
Default reading pg_stat_activity view

Hi everybody,

As I look at output from pg_stat_activity view,
I see often <idle> under query column and on the
same row waiting column is always f. I gather
that this is someone sitting at prompt of psql.

My question: What does it mean when a query column
lists a sql statment and waiting column f? Would
this mean something is in a hung state?

Regards,

Tena Sakai
tsakai@gallo.ucsf.edu

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 09:17 AM
Scott Marlowe
 
Posts: n/a
Default Re: reading pg_stat_activity view

On Dec 13, 2007 1:33 PM, Tena Sakai <tsakai@gallo.ucsf.edu> wrote:
>
> Hi everybody,
>
> As I look at output from pg_stat_activity view,
> I see often <idle> under query column and on the
> same row waiting column is always f. I gather
> that this is someone sitting at prompt of psql.
>
> My question: What does it mean when a query column
> lists a sql statment and waiting column f? Would
> this mean something is in a hung state?


Not what that means at all.

It means that the query is not waiting on locks from another query to
be released. Here's an example you can do yourself to see it happen:

T1: # create table test (a int, b text);
T1: # insert into test values (1,'abc');
T1: # insert into test values (2,'def');
T1: # begin;
T1: # select * from test where a=1 for update;
a | b
---+-----
1 | abc
(1 row)
T2: # update test set b='xyz' where a=1;
(T2 enters a wait state waiting on T1 to commit or rollback)

In another psql session, select * from pg_stat_activity where datname='mydb';
16385 | mydb | 13379 | 16384 | myname | update test set b='xyz'
where a=1; | t

Make sense?

---------------------------(end of broadcast)---------------------------
TIP 3: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-10-2008, 09:17 AM
Tena Sakai
 
Posts: n/a
Default Re: reading pg_stat_activity view

Hi Scott,

Thank you for an enlightening example! I see
it and I believe it.

My next question:
What would be a good way to tell if some
query is hung?

Regards,

Tena


-----Original Message-----
From: Scott Marlowe [mailto:scott.marlowe@gmail.com]
Sent: Thu 12/13/2007 11:47 AM
To: Tena Sakai
Cc: pgsql-admin@postgresql.org
Subject: Re: [ADMIN] reading pg_stat_activity view

On Dec 13, 2007 1:33 PM, Tena Sakai <tsakai@gallo.ucsf.edu> wrote:
>
> Hi everybody,
>
> As I look at output from pg_stat_activity view,
> I see often <idle> under query column and on the
> same row waiting column is always f. I gather
> that this is someone sitting at prompt of psql.
>
> My question: What does it mean when a query column
> lists a sql statment and waiting column f? Would
> this mean something is in a hung state?


Not what that means at all.

It means that the query is not waiting on locks from another query to
be released. Here's an example you can do yourself to see it happen:

T1: # create table test (a int, b text);
T1: # insert into test values (1,'abc');
T1: # insert into test values (2,'def');
T1: # begin;
T1: # select * from test where a=1 for update;
a | b
---+-----
1 | abc
(1 row)
T2: # update test set b='xyz' where a=1;
(T2 enters a wait state waiting on T1 to commit or rollback)

In another psql session, select * from pg_stat_activity where datname='mydb';
16385 | mydb | 13379 | 16384 | myname | update test set b='xyz'
where a=1; | t

Make sense?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-10-2008, 09:17 AM
Scott Marlowe
 
Posts: n/a
Default Re: reading pg_stat_activity view

On Dec 13, 2007 3:43 PM, Tena Sakai <tsakai@gallo.ucsf.edu> wrote:
>
> My next question:
> What would be a good way to tell if some
> query is hung?


That would really depend on what you mean by hung. Just running a
really long time, or waiting for a lock that some other session is not
will to commit / rollback like above?

I take it you just mean long running queries. You can do a couple of
things. You can set the value for statement_timeout and any statement
that takes over that amount of time will generate a timeout and you
then log it in the logs.

You can use pg_stat_activity to see how long a query's been running.
Something like

select datname, usename, current_query, waiting, now() - query_start
from pg_stat_activity order by query_start;

can show you how long each query has been running.

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-10-2008, 09:17 AM
Tena Sakai
 
Posts: n/a
Default Re: reading pg_stat_activity view

Hi Scott,

Thanks for the sql statement with pg_stat_activity.

> You can set the value for statement_timeout and any statement
> that takes over that amount of time will generate a timeout and you
> then log it in the logs.


I like this idea, but according to what I read, it looks
like the query will be aborted if it goes beyond the
time specified, which is not what I want. There wouldn't
be any way to just log it and still keep the query alive,
would there?

Is a scenario like below possible/probable/likely? A user
starts a query, after a few minutes decides to abort it via
control-C, he/she gets a notion that it is aborted, but
as far as postgres is concerned the query keeps running for
days and days? Is there any way to detect such has indeed
taken place?

Regards,

Tena Sakai
tsakai@gallo.ucsf.edu





-----Original Message-----
From: pgsql-admin-owner@postgresql.org on behalf of Scott Marlowe
Sent: Thu 12/13/2007 1:57 PM
To: Tena Sakai
Cc: pgsql-admin@postgresql.org
Subject: Re: [ADMIN] reading pg_stat_activity view

On Dec 13, 2007 3:43 PM, Tena Sakai <tsakai@gallo.ucsf.edu> wrote:
>
> My next question:
> What would be a good way to tell if some
> query is hung?


That would really depend on what you mean by hung. Just running a
really long time, or waiting for a lock that some other session is not
will to commit / rollback like above?

I take it you just mean long running queries. You can do a couple of
things. You can set the value for statement_timeout and any statement
that takes over that amount of time will generate a timeout and you
then log it in the logs.

You can use pg_stat_activity to see how long a query's been running.
Something like

select datname, usename, current_query, waiting, now() - query_start
from pg_stat_activity order by query_start;

can show you how long each query has been running.

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-10-2008, 09:17 AM
Scott Marlowe
 
Posts: n/a
Default Re: reading pg_stat_activity view

On Dec 13, 2007 4:39 PM, Tena Sakai <tsakai@gallo.ucsf.edu> wrote:
>
> Hi Scott,
>
> Thanks for the sql statement with pg_stat_activity.
>
> > You can set the value for statement_timeout and any statement
> > that takes over that amount of time will generate a timeout and you
> > then log it in the logs.

>
> I like this idea, but according to what I read, it looks
> like the query will be aborted if it goes beyond the
> time specified, which is not what I want. There wouldn't
> be any way to just log it and still keep the query alive,
> would there?


Yeah, it's pretty much a brick wall setting. anything over it gets
stopped, period.

> Is a scenario like below possible/probable/likely? A user
> starts a query, after a few minutes decides to abort it via
> control-C, he/she gets a notion that it is aborted, but
> as far as postgres is concerned the query keeps running for
> days and days? Is there any way to detect such has indeed
> taken place?


That's not what will happen. If the user hits ctrl-C from psql, it
will send a cancel command to the backend, unlike a certain database a
lot of people use **COUGHmysqlCOUGH** which will kill the client side
app and leave the query running against the backend.

Now, should the user initiate a query through a we interface it is
quite possible for them to close the web page and for the query to
keep on running for quite some time.

There are scenarios where querys run for days and days, and it's a
good idea to keep track of the long running queries to make sure
they're not scramming your db server. Figure out what your absolutely
longest running query time should be, and set the timeout to that.
For me, on a reporting server at work, that's several hours. On an
OLTP machine it would likely be minutes.

Now, the cool thing is, you can set different max execution times per
database and per user. not just for the whole db cluster. Once
you've set something like 4 hours for the whole server, you can then
do something like:

alter database xyzdb set statement_timeout=300;
alter user stanley set statement_timeout=1200;

etc...

If you just want to see what statements have been running a long time,
you can use the pg_stat_activity view to see that. It's pretty easy
to write a simple bash script that tosses a query at the db and pulls
the numbers out and sends you an alert if it sees a query running too
long. Shout at me if you want an example.

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-10-2008, 09:17 AM
Usama Dar
 
Posts: n/a
Default Re: reading pg_stat_activity view

>
> There wouldn't
> be any way to just log it and still keep the query alive,
> would there?
>

I think it will be a useful feature to add to postgres, for the benefit of
DBAs, the ability to log slow queries, the queries which take more than x
amount of time, maybe in a separate slow query log. i am sure it sounds
familiar to a lot of people



--
Usama Munir Dar http://linkedin.com/in/usamadar
Consultant Architect
Cell:+92 321 5020666
Skype: usamadar

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-10-2008, 09:17 AM
Scott Marlowe
 
Posts: n/a
Default Re: reading pg_stat_activity view

On Dec 16, 2007 1:37 PM, Usama Dar <munir.usama@gmail.com> wrote:
>
> >
> >
> >
> > There wouldn't
> > be any way to just log it and still keep the query alive,
> > would there?
> >

> I think it will be a useful feature to add to postgres, for the benefit of
> DBAs, the ability to log slow queries, the queries which take more than x
> amount of time, maybe in a separate slow query log. i am sure it sounds
> familiar to a lot of people


You can already log slow queries, they just go into the pgsql logs.

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

http://archives.postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-10-2008, 09:17 AM
Tom Lane
 
Posts: n/a
Default Re: reading pg_stat_activity view

"Usama Dar" <munir.usama@gmail.com> writes:
> I think it will be a useful feature to add to postgres, for the benefit of
> DBAs, the ability to log slow queries, the queries which take more than x
> amount of time, maybe in a separate slow query log. i am sure it sounds
> familiar to a lot of people


See log_min_duration_statement

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:48 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com