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()
ANDs2.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()))Totalruntime: 177.05 msec
I can provide other details, if needed. Thanks in advance.
--
Fduch M. Pravking