Is there a way to limit CPU usage per user

Lists: pgsql-general
From: Luki Rustianto <lukirus(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Is there a way to limit CPU usage per user
Date: 2006-02-08 03:54:37
Message-ID: a87d9f3a0602071954p27bfb6fbt22b2368b0cfd3dad@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi All,

Is there a way to limit user's CPU resource specially on "SELECT" query ?

I hava a table with a lot of rows inside, if one sloopy DB users do a
"SELECT * FROM bigtable"
then CPU resource will go near 99% and this action will surely affect
the other database performance ...

Thanks.


From: Richard Huxton <dev(at)archonet(dot)com>
To: Luki Rustianto <lukirus(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Is there a way to limit CPU usage per user
Date: 2006-02-08 09:26:23
Message-ID: 43E9B93F.7080502@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Luki Rustianto wrote:
> Hi All,
>
> Is there a way to limit user's CPU resource specially on "SELECT" query ?
>
> I hava a table with a lot of rows inside, if one sloopy DB users do a
> "SELECT * FROM bigtable"
> then CPU resource will go near 99% and this action will surely affect
> the other database performance ...

Not really - this is something for the operating-system, but most don't
provide sophisticated ways of managing processes running under one user
(postgres in this case).

If there are 5 queries going on, each should end up getting about 20% of
the resources anyway, if your OS is balancing things properly.

If a query needs a lot of CPU and you restrict it, then that query will
presumably run for longer and so affect more queries from other users.

--
Richard Huxton
Archonet Ltd


From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Luki Rustianto <lukirus(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Is there a way to limit CPU usage per user
Date: 2006-02-08 21:42:42
Message-ID: b42b73150602081342w6e1add1o1dfb8c32345e1a14@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> Is there a way to limit user's CPU resource specially on "SELECT" query ?
>
> I hava a table with a lot of rows inside, if one sloopy DB users do a
> "SELECT * FROM bigtable"
> then CPU resource will go near 99% and this action will surely affect
> the other database performance ...
>
Yes, the answer is not to write the query in the first place :-). you
can implement cursors, do client side browsing, or other techiniques
to handle this problem more elegantly.

try to follow rule of thumb to return minimal amount of data necessary
to the client.

Merlin


From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: Luki Rustianto <lukirus(at)gmail(dot)com>, pgsql general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Is there a way to limit CPU usage per user
Date: 2006-02-09 16:43:06
Message-ID: 1139503386.22740.110.camel@state.g2switchworks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Wed, 2006-02-08 at 15:42, Merlin Moncure wrote:
> > Is there a way to limit user's CPU resource specially on "SELECT" query ?
> >
> > I hava a table with a lot of rows inside, if one sloopy DB users do a
> > "SELECT * FROM bigtable"
> > then CPU resource will go near 99% and this action will surely affect
> > the other database performance ...
> >
> Yes, the answer is not to write the query in the first place :-). you
> can implement cursors, do client side browsing, or other techiniques
> to handle this problem more elegantly.
>
> try to follow rule of thumb to return minimal amount of data necessary
> to the client.

Note that another useful tip here is to use slony to produce as many
replicants as needed to handle that kind of thing.

We have our production pgsql machines in a slony setup, with pg01 being
accessible by the application that inserts and updates the data, and all
reporting apps hit pg02 and up to do selects and such.


From: Luki Rustianto <lukirus(at)gmail(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Is there a way to limit CPU usage per user
Date: 2006-02-10 04:30:04
Message-ID: a87d9f3a0602092030q7784f4eai39b169f18538047d@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

So how can we terminate such a long running query ?

The idea is to make a crontab to periodicaly do a job to search a
typical "SELECT * FROM bigtable" query who has run for some hours then
to terminate them...

On 2/9/06, Merlin Moncure <mmoncure(at)gmail(dot)com> wrote:
> > Is there a way to limit user's CPU resource specially on "SELECT" query ?
> >
> > I hava a table with a lot of rows inside, if one sloopy DB users do a
> > "SELECT * FROM bigtable"
> > then CPU resource will go near 99% and this action will surely affect
> > the other database performance ...
> >
> Yes, the answer is not to write the query in the first place :-). you
> can implement cursors, do client side browsing, or other techiniques
> to handle this problem more elegantly.
>
> try to follow rule of thumb to return minimal amount of data necessary
> to the client.
>
> Merlin
>


From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Luki Rustianto <lukirus(at)gmail(dot)com>
Cc: Merlin Moncure <mmoncure(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Is there a way to limit CPU usage per user
Date: 2006-02-10 04:40:34
Message-ID: 20060210044034.GA64526@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, Feb 10, 2006 at 11:30:04AM +0700, Luki Rustianto wrote:
> So how can we terminate such a long running query ?
>
> The idea is to make a crontab to periodicaly do a job to search a
> typical "SELECT * FROM bigtable" query who has run for some hours then
> to terminate them...

Are you familiar with the statement_timeout setting?

test=> SET statement_timeout TO 1000; -- milliseconds
SET
test=> SELECT <some long-running query>;
ERROR: canceling statement due to statement timeout

If that won't work then please explain in general terms what problem
you're trying to solve, not how you're trying to solve it.

--
Michael Fuhr


From: Tony Wasson <ajwasson(at)gmail(dot)com>
To: Michael Fuhr <mike(at)fuhr(dot)org>
Cc: Luki Rustianto <lukirus(at)gmail(dot)com>, Merlin Moncure <mmoncure(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Is there a way to limit CPU usage per user
Date: 2006-02-10 15:31:07
Message-ID: 6d8daee30602100731j775cdabek43b6c4e337e176d3@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 2/9/06, Michael Fuhr <mike(at)fuhr(dot)org> wrote:
> On Fri, Feb 10, 2006 at 11:30:04AM +0700, Luki Rustianto wrote:
> > So how can we terminate such a long running query ?
> >
> > The idea is to make a crontab to periodicaly do a job to search a
> > typical "SELECT * FROM bigtable" query who has run for some hours then
> > to terminate them...
>
> Are you familiar with the statement_timeout setting?
>
> test=> SET statement_timeout TO 1000; -- milliseconds
> SET
> test=> SELECT <some long-running query>;
> ERROR: canceling statement due to statement timeout
>
> If that won't work then please explain in general terms what problem
> you're trying to solve, not how you're trying to solve it.

I am also interested in being able to terminate *certain* long running
queries. I didn't want to use statement_timeout because there are some
queries that must run for a long time - in our case some queries that
create summary tables. Other long running queries should be killed. I
was able to get more granular by creating a "kill_pid" function in an
untrusted language and selectively kill ad-hoc queries. I'd suggest
having your non-killable queries run as one user.

That way you can do something like

SELECT * FROM pg_stat_activity
WHERE usename !='some_special_user'
AND query_start < NOW()-INTERVAL '30 minutes';

And then if you were very brave - you could kill those queries off.

I may get flamed for this, but this is how I have killed errant
processes. I suspect you should pause for a long time before try to
install plperlu.

CREATE FUNCTION kill_pid(INTEGER) RETURNS TEXT AS
$BODY$
my ($pid) = @_;
my $out=system("kill -TERM $pid");
return $out;
$BODY$ language plperlu;

REVOKE ALL ON FUNCTION kill_pid(INTEGER) FROM public;


From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: Tony Wasson <ajwasson(at)gmail(dot)com>
Cc: Michael Fuhr <mike(at)fuhr(dot)org>, Luki Rustianto <lukirus(at)gmail(dot)com>, Merlin Moncure <mmoncure(at)gmail(dot)com>, pgsql general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Is there a way to limit CPU usage per user
Date: 2006-02-10 16:01:44
Message-ID: 1139587304.22740.124.camel@state.g2switchworks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, 2006-02-10 at 09:31, Tony Wasson wrote:
> On 2/9/06, Michael Fuhr <mike(at)fuhr(dot)org> wrote:
> > On Fri, Feb 10, 2006 at 11:30:04AM +0700, Luki Rustianto wrote:
> > > So how can we terminate such a long running query ?
> > >
> > > The idea is to make a crontab to periodicaly do a job to search a
> > > typical "SELECT * FROM bigtable" query who has run for some hours then
> > > to terminate them...
> >
> > Are you familiar with the statement_timeout setting?
> >
> > test=> SET statement_timeout TO 1000; -- milliseconds
> > SET
> > test=> SELECT <some long-running query>;
> > ERROR: canceling statement due to statement timeout
> >
> > If that won't work then please explain in general terms what problem
> > you're trying to solve, not how you're trying to solve it.
>
> I am also interested in being able to terminate *certain* long running
> queries. I didn't want to use statement_timeout because there are some
> queries that must run for a long time - in our case some queries that
> create summary tables. Other long running queries should be killed. I
> was able to get more granular by creating a "kill_pid" function in an
> untrusted language and selectively kill ad-hoc queries. I'd suggest
> having your non-killable queries run as one user.

Could setting a global statement_timeout of say 30000 milliseconds and
then having known long running queries set a different statement_timeout
on their own connections before running their query work?


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Tony Wasson <ajwasson(at)gmail(dot)com>
Cc: Michael Fuhr <mike(at)fuhr(dot)org>, Luki Rustianto <lukirus(at)gmail(dot)com>, Merlin Moncure <mmoncure(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Is there a way to limit CPU usage per user
Date: 2006-02-10 16:04:52
Message-ID: 19899.1139587492@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Tony Wasson <ajwasson(at)gmail(dot)com> writes:
> I am also interested in being able to terminate *certain* long running
> queries. I didn't want to use statement_timeout because there are some
> queries that must run for a long time - in our case some queries that
> create summary tables. Other long running queries should be killed.

You do know that statement_timeout can be changed freely via SET,
right? One way to attack this would be for the clients that are
issuing known long-running queries to do "SET statement_timeout"
to boost up their allowed runtime.

regards, tom lane


From: Csaba Nagy <nagy(at)ecircle-ag(dot)com>
To: Postgres general mailing list <pgsql-general(at)postgresql(dot)org>
Cc: Tony Wasson <ajwasson(at)gmail(dot)com>, Michael Fuhr <mike(at)fuhr(dot)org>, Luki Rustianto <lukirus(at)gmail(dot)com>, Merlin Moncure <mmoncure(at)gmail(dot)com>
Subject: Re: Is there a way to limit CPU usage per user
Date: 2006-02-10 16:10:59
Message-ID: 1139587859.24321.427.camel@coppola.muc.ecircle.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

> You do know that statement_timeout can be changed freely via SET,
> right? One way to attack this would be for the clients that are
> issuing known long-running queries to do "SET statement_timeout"
> to boost up their allowed runtime.

How does this apply to autovacuum's long running vacuum commands ? Cause
I have one table where the last vacuum took 15 hours :-)

It's true that now after migration to 8.1 I do the vacuuming manually
for that one and disabled it for autovacuum, but I still could have
other tables which would vacuum in more than 5 minutes (that would be my
statement_timeout for ordinary processes).

Cheers,
Csaba.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Csaba Nagy <nagy(at)ecircle-ag(dot)com>
Cc: Postgres general mailing list <pgsql-general(at)postgresql(dot)org>, Tony Wasson <ajwasson(at)gmail(dot)com>, Michael Fuhr <mike(at)fuhr(dot)org>, Luki Rustianto <lukirus(at)gmail(dot)com>, Merlin Moncure <mmoncure(at)gmail(dot)com>
Subject: Re: Is there a way to limit CPU usage per user
Date: 2006-02-10 16:37:43
Message-ID: 20159.1139589463@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Csaba Nagy <nagy(at)ecircle-ag(dot)com> writes:
> How does this apply to autovacuum's long running vacuum commands ? Cause
> I have one table where the last vacuum took 15 hours :-)

[ Checks code... ] No, statement_timeout only applies to commands
arriving from an interactive client. This *would* be a hazard for
the contrib version of autovacuum, but not for the 8.1 integrated
version.

(If you're using contrib autovacuum, it'd be a good idea to do
"ALTER USER SET statement_timeout = 0" for the user it runs as.)

regards, tom lane