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

Memory usage on subselect

From: "Alexander M(dot) Pravking" <fduch(at)antar(dot)bryansk(dot)ru>
To: pgsql-sql(at)postgresql(dot)org
Subject: Memory usage on subselect
Date: 2004-05-22 17:54:19
Message-ID: 20040522175419.GB54296@dyatel.antar.bryansk.ru (view raw or flat)
Thread:
Lists: pgsql-sql
Hello, PostgreSQL users and developers.
I've got a memory usage problem when I try to do a subselect on the same
table as the main select (PostgreSQL 7.3.4 on FreeBSD). Here's my query:

SELECT  sreq(s1.id, 'ipacct_ip', now()), s1.*
FROM    services s1
WHERE   EXISTS (
                SELECT  1
                FROM    services s2
                WHERE   s2.id != s1.id
                AND     sreq(s2.id, 'ipacct_ip', now()) = sreq(s1.id, 'ipacct_ip', now())
                AND     s2.sdate < now() AND s2.edate > now()
        )
AND     s1.sdate < now() AND s1.edate > now();

I.e. I want to find all records from services which have equal values of
sreq(...) for them (additionally filtering only those which are actual
now).

The "services" table is indexed only on "id" column and has about a
thousand tuples. sreq(integer, text, timestamptz) is a strict immutable
function written in SQL.
EXPLAIN says the following:

 Seq Scan on services s1  (cost=0.00..38628.80 rows=38 width=55)
   Filter: ((sdate < now()) AND (edate > now()) AND (subplan))
   SubPlan
     ->  Seq Scan on services s2  (cost=0.00..56.08 rows=1 width=0)
           Filter: ((id <> $0) AND (sreq(id, 'ipacct_ip'::text, now()) = sreq($0, 'ipacct_ip'::text, now())) AND (sdate < now()) AND (edate > now()))

I see no evil here (of course, the query is going to be slow), but the
postgres process begins to consume a lot of memory (I cancelled a query
after ~500M).

Am I doing something wrong or is it expected behavour?
I never seen this before, so I'd think it's me who mistaken,
but I can't find anything wrong for a few hours :)

Here's subquerie's EXPLAIN ANALYZE for a sample (existing) s1.id:

EXPLAIN ANALYZE
                SELECT  1
                FROM    services s2
                WHERE   s2.id != 561
                AND     sreq(s2.id, 'ipacct_ip', now()) = sreq(561, 'ipacct_ip', now())
                AND     s2.sdate < now() AND s2.edate > now();

 Seq Scan on services s2  (cost=0.00..56.08 rows=1 width=0) (actual time=177.01..177.01 rows=0 loops=1)
   Filter: ((id <> 561) AND (sreq(id, 'ipacct_ip'::text, now()) = sreq(561, 'ipacct_ip'::text, now())) AND (sdate < now()) AND (edate > now()))
 Total runtime: 177.05 msec

I can provide other details, if needed. Thanks in advance.

-- 
Fduch M. Pravking

Responses

pgsql-sql by date

Next:From: Tom LaneDate: 2004-05-23 16:28:18
Subject: Re: Memory usage on subselect
Previous:From: Tom LaneDate: 2004-05-21 15:28:52
Subject: Re: Preventing Deletions with triggers

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