pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexander M. Pravking <fduch@antar.bryansk.ru>
To: pgsql-sql@postgresql.org
Subject: Memory usage on subselect
Date: Sat, 22 May 2004 21:54:19 +0400
Message-ID: <20040522175419.GB54296@dyatel.antar.bryansk.ru> (raw)

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



view thread (7+ messages)  latest in thread

Message-ID: <20040522175419.GB54296@dyatel.antar.bryansk.ru>
Permalink:  ../20040522175419.GB54296@dyatel.antar.bryansk.ru/
Also on:    postgresql.org/message-id/20040522175419.GB54296@dyatel.antar.bryansk.ru

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: fduch@antar.bryansk.ru
  Subject: Re: Memory usage on subselect
  In-Reply-To: <20040522175419.GB54296@dyatel.antar.bryansk.ru>
  References: <20040522175419.GB54296@dyatel.antar.bryansk.ru>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox