Re: [SQL] Elegant way to monitor for changes in a trigger and migrate

2004-05-22 Thread Mark Gibson
David B wrote:
Folks,
Perhaps you can helphell I'm sure you can!
I want to monitor for changes in a table and migrate the OLD. record to
audit table.
Is there an elegant or generic way to do this so I can use across multiple
tables with little change.
 

You can use a rule to do this:
CREATE RULE cust_audit AS ON UPDATE OR DELETE TO cust DO
 INSERT INTO cust_hist SELECT OLD.*;
cust_hist should be identical to cust without a primary key or any 
constraints/foreign keys etc.

I'm currently working on an auditing system at present, and will be 
releasing it
soon if anyone is interested. It needs some cleaning up first, when I 
have time.

--
Mark Gibson 
Web Developer & Database Admin
Cromwell Tools Ltd.
Leicester, England.
---(end of broadcast)---
TIP 4: Don't 'kill -9' the postmaster


[SQL] Memory usage on subselect

2004-05-22 Thread Alexander M. Pravking
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.*
FROMservices s1
WHERE   EXISTS (
SELECT  1
FROMservices 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
FROMservices 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

---(end of broadcast)---
TIP 3: if posting/reading through Usenet, please send an appropriate
  subscribe-nomail command to [EMAIL PROTECTED] so that your
  message can get through to the mailing list cleanly