Re: force defaults

From: Sim Zacks <sim(at)compulab(dot)co(dot)il>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: force defaults
Date: 2012-09-12 08:55:17
Message-ID: 50504DF5.1070203@compulab.co.il
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

<html style="direction: ltr;">
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<style type="text/css">body p { margin-bottom: 0cm; margin-top: 0pt; } </style>
</head>
<body style="direction: ltr;"
bidimailui-detected-decoding-type="latin-charset" text="#000000"
bgcolor="#FFFFFF">
<div class="moz-cite-prefix">I haven't checked more recent versions,
but in 8.2 using <br>
&nbsp;&nbsp;&nbsp; case when new.val is null then 'U' else new.val end <br>
worked a hell of a lot faster then coalesce.<br>
However, just going into the trigger is significant overhead.<br>
<br>
Alban's suggestion of using a rule would work as well. <br>
If you don't need the value in real time, you can either have a
cron job update all the wrong values on a regular basis or using
listen/notify you can get it close to real time.<br>
<br>
Sim<br>
<br>
<br>
On 09/12/2012 09:49 AM, Willy-Bas Loos wrote:<br>
</div>
<blockquote
cite="mid:CAHnozTjpO3Yn9VfcAdZYj6A1tPkAOC26HFnsvL2mosHi6189sw(at)mail(dot)gmail(dot)com"
type="cite">Hi,<br>
<br>
I want to force deafults, and wonder about the performance.<br>
The trigger i use (below) makes the query (also below) take 45%
more time.<br>
The result is the same now, but i do have a use for using the
trigger (see "background info").<br>
<br>
Isn't there a more efficient way to force the defaults (in the
database) when the application layer explicitly inserts a NULL?<br>
<br>
Cheers,<br>
<br>
WBL<br>
<br>
<br>
&lt;backgorund info&gt;<br>
&nbsp;I'm building an application where users can upload data to a
database, with a website as a gui.<br>
&nbsp;<br>
&nbsp;In the legacy database that we're "pimping", some attributes are
optional, but there are also codes that explicitly mark the
attribute as 'Unknown' in the same field.<br>
&nbsp;That would mean that there are 2 things that mean the same thing:
NULL and 'U' for unknown.<br>
&nbsp;I don't want to bother our customer with the NULL issues in
queries, so i would like to make those fields NOT NULL.<br>
<br>
&nbsp;The users will use an Excel or CSV form to upload the data and
they can just leave a blank for the optional fields if they like.<br>
&nbsp;We'll use php to insert the data in a table, from which we'll
check if the input satisfies our demands before inserting into the
actual tables that matter.<br>
<br>
&nbsp;When the users leave a blank, php is bound to insert a NULL (or
even an empty string) into the upload table.<br>
&nbsp;I want to use a default, even if php explicitly inserts a NULL.<br>
&lt;/backgorund info&gt;<br>
<br>
<br>
--the TRIGGER<br>
create or replace function force_defaults () returns trigger as $$<br>
begin<br>
&nbsp;&nbsp;&nbsp; new.val:=coalesce(new.val, 'U');<br>
return new;<br>
end;<br>
$$ language plpgsql;<br>
<br>
--the QUERIES (on my laptop, no postgres config, pg 9.1):<br>
create table accounts (like pgbench_accounts including all);<br>
--(1)<br>
alter table accounts add column val text default 'U';<br>
insert into accounts(aid, bid, abalance, filler) select * from
pgbench_accounts;<br>
&nbsp;INSERT 0 50000000<br>
&nbsp;Time: 538760.542 ms<br>
<br>
--(2)<br>
alter table accounts alter column val set default null;<br>
create trigger bla before insert or update on accounts for each
row ...etc<br>
vacuum accounts;<br>
insert into accounts(aid, bid, abalance, filler) select * from
pgbench_accounts;<br>
&nbsp;INSERT 0 50000000<br>
&nbsp;Time: 780421.041 ms
<div class="yj6qo ajU">
<div id=":1hv" class="ajR" tabindex="0"><img
moz-do-not-send="true" class="ajT"
src="https://mail.google.com/mail/u/0/images/cleardot.gif"></div>
</div>
<br clear="all">
<br>
-- <br>
"Quality comes from focus and clarity of purpose" -- Mark
Shuttleworth<br>
<br>
</blockquote>
<br>
</body>
</html>

Attachment Content-Type Size
unknown_filename text/html 4.2 KB

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Albe Laurenz 2012-09-12 08:58:00 Re: CentOS initd Script
Previous Message Albe Laurenz 2012-09-12 08:51:40 Re: Postgresql replication assistance