X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 0BB42DB7DA for ; Wed, 23 Nov 2005 08:23:00 -0400 (AST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 68289-10 for ; Wed, 23 Nov 2005 12:23:01 +0000 (GMT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id 4AAEADB723 for ; Wed, 23 Nov 2005 08:22:57 -0400 (AST) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 70E4D40C0BC; Wed, 23 Nov 2005 12:22:52 +0000 (GMT) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 0A5C315EA4; Wed, 23 Nov 2005 12:23:00 +0000 (GMT) Message-ID: <43845F23.60204@archonet.com> Date: Wed, 23 Nov 2005 12:22:59 +0000 From: Richard Huxton User-Agent: Mozilla Thunderbird 1.0.7 (X11/20051013) X-Accept-Language: en-us, en MIME-Version: 1.0 To: lucas@presserv.org Cc: pgsql-sql@postgresql.org Subject: Re: Default on update References: <20051123084353.8ga6nrpx43gg8408@www.presserv.org> In-Reply-To: <20051123084353.8ga6nrpx43gg8408@www.presserv.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0 required=5 tests=[AWL=0.000] X-Spam-Score: 0 X-Spam-Level: X-Archive-Number: 200511/219 X-Sequence-Number: 23470 lucas@presserv.org wrote: > Hi. > Is there a way to create "default" constraint on UPDATE query. > It's becouse I have a bool field that may NOT support NULL value, but the > Front-End calls null for FALSE values. Sounds like your frontend is broken. > I was thinking something like: > create table table1 ( > id serial primary key, > bv bool default false not null > ); > I would want to replace "bv" values with FALSE when insert/update NULL value for > this field. You could do this by having the application insert to a view with a rule that replaces null bv values before redirecting to the base table. > Or need I create a TRIGGER that check it and replace the itens??? > CREATE or REPLACE function TG_table1_check RETURNS trigger as ' > BEGIN > IF nullvalue(NEW.bv) THEN IF NEW.bv IS NULL THEN > NEW.bv=FALSE; > END IF; > END; > ' language 'plpgsql'; CREATE TRIGGER TG_table1_check BEFORE UPDATE on table1 for > each row execute procedure tg_table1_check(); To make the trigger work you'll have to relax the "NOT NULL" on column "bv" otherwise PG's type-checks will raise an error. Oh, and then make sure the trigger is called before INSERT too. > Other question: I have a lot of triggers in my db system, I have table that has > 5/6 triggers, many triggers are simple (like the tg_table1_check), any are > complex... Is it a problem??? My tests are with few regs and run fine. Where > can I read more about triggers and performance? Triggers behave exactly as you'd expect. For every row (or statement) the function gets executed. Difficult to say what effect they'll have on performance without testing with your actual setup. -- Richard Huxton Archonet Ltd