Tuesday, July 1, 2008

Re: [SQL] column default dependant on another columns value

> -----Mensaje original-----
> De: Richard Broersma [mailto:richard.broersma@gmail.com]
>
> It is possible to do this with a trigger or a rule. A
> trigger would be more robust.
>
> > Is this correct? Is there another (better/simpler) way to
> achieve this?
>
> Well I might work, but it is a bad practice to get into since
> what you are trying to do violates the rules of database
> normalization.
>
> Wouldn't it be better to calculate the minutes with you query
> your table?
>
> SELECT *, seconds / 60 AS minutes
> FROM yourtable;

Actually I only used this as an example.
The real table is queried lots of times for millions of rows and the server
is showing some high-level user cpu consumption. There are a couple
calculated columns on the table so I am trying to reduce cpu usage by
pre-calculating the more cpu intensive data once on insert. Enhancing the
application is currently not possible.

Anyway, the rule didn't work. Got "an infinite recursion error" when
inserting on the table.
Can't figure out where the recursion is as supposedly the rule kicks in when
the "where minutes is null" condition is satisfied. The DO INSTEAD part runs
an insert were minutes is NOT null so the rule should be ignored.
Where is the recursion then? I am on postgres 8.2.9.

Thanks for your hindsight Richard. I Will look into the trigger solution.
Still, I'd like to understand this recursion error.

Regards,
Fernando.

--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql

No comments: