pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
A question about rules
4+ messages / 2 participants
[nested] [flat]

* A question about rules
@ 2020-01-18 16:53 stan <stanb@panix.com>
  2020-01-18 16:56 ` Re: A question about rules Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 4+ messages in thread

From: stan @ 2020-01-18 16:53 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

So, I just discovered the rules system. As I understand it, it can be used
to rewrite queries before they are passed to the query processor.

I was wondering if I could use this to resolve a long standing frustration
of mine. I often need to declare a column as NON NULL, and create a
trigger/function to auto populate it with something more complex than a
simple sequence. This need arises fairly often for me, and I would like to
come up with a good way to implement this.

Sorry for such a newbie question.

-- 
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
						-- Benjamin Franklin





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: A question about rules
  2020-01-18 16:53 A question about rules stan <stanb@panix.com>
@ 2020-01-18 16:56 ` Adrian Klaver <adrian.klaver@aklaver.com>
  2020-01-19 16:52   ` Re: A question about rules Adrian Klaver <adrian.klaver@aklaver.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Adrian Klaver @ 2020-01-18 16:56 UTC (permalink / raw)
  To: stan <stanb@panix.com>; pgsql-general@lists.postgresql.org

On 1/18/20 8:53 AM, stan wrote:
> So, I just discovered the rules system. As I understand it, it can be used
> to rewrite queries before they are passed to the query processor.
> 
> I was wondering if I could use this to resolve a long standing frustration
> of mine. I often need to declare a column as NON NULL, and create a
> trigger/function to auto populate it with something more complex than a
> simple sequence. This need arises fairly often for me, and I would like to
> come up with a good way to implement this.

Stay away from rules, they will only drive you to distraction. Stick 
with triggers they are a lot easier to understand and implement.

> 
> Sorry for such a newbie question.
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: A question about rules
  2020-01-18 16:53 A question about rules stan <stanb@panix.com>
  2020-01-18 16:56 ` Re: A question about rules Adrian Klaver <adrian.klaver@aklaver.com>
@ 2020-01-19 16:52   ` Adrian Klaver <adrian.klaver@aklaver.com>
  2020-01-20 16:05     ` Re: A question about rules stan <stanb@panix.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Adrian Klaver @ 2020-01-19 16:52 UTC (permalink / raw)
  To: stan <stanb@panix.com>; +Cc: pgsql-general

On 1/19/20 2:32 AM, stan wrote:
Please post to list also.
Ccing list.

> On Sat, Jan 18, 2020 at 08:56:06AM -0800, Adrian Klaver wrote:
>> On 1/18/20 8:53 AM, stan wrote:
>>> So, I just discovered the rules system. As I understand it, it can be used
>>> to rewrite queries before they are passed to the query processor.
>>>
>>> I was wondering if I could use this to resolve a long standing frustration
>>> of mine. I often need to declare a column as NON NULL, and create a
>>> trigger/function to auto populate it with something more complex than a
>>> simple sequence. This need arises fairly often for me, and I would like to
>>> come up with a good way to implement this.
>>
>> Stay away from rules, they will only drive you to distraction. Stick with
>> triggers they are a lot easier to understand and implement.
>>
> 
> I use triggers quite a bit. My issue is that if I have a column defined as
> NOT NULL, and assign a trigger that fires a function to auto populate this
> column if the user (input form) does not. The NOT NULL constraint is
> checked BEFORE the trigger can fire, which means that I cannot properly
> provide data integrity.
> 

I don't know that this changes with rules. In other words I believe 
constraints are checked first with either rules/triggers. Someone else 
will need to confirm this.

A work around is to supply a dummy DEFAULT for the the NOT NULL column 
e.g. for a varchar column the string 'NOT NULL'. Test for this in the 
trigger and take the appropriate action. Or accept the reality which is, 
you are allowing NULL input to the column and take away the NOT NULL 
constraint and look for NULL or empty string input and take the 
appropriate action.


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: A question about rules
  2020-01-18 16:53 A question about rules stan <stanb@panix.com>
  2020-01-18 16:56 ` Re: A question about rules Adrian Klaver <adrian.klaver@aklaver.com>
  2020-01-19 16:52   ` Re: A question about rules Adrian Klaver <adrian.klaver@aklaver.com>
@ 2020-01-20 16:05     ` stan <stanb@panix.com>
  0 siblings, 0 replies; 4+ messages in thread

From: stan @ 2020-01-20 16:05 UTC (permalink / raw)
  To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-general@lists.postgresql.org

On Sun, Jan 19, 2020 at 08:52:36AM -0800, Adrian Klaver wrote:
> On 1/19/20 2:32 AM, stan wrote:
> Please post to list also.
> Ccing list.
> 
> > On Sat, Jan 18, 2020 at 08:56:06AM -0800, Adrian Klaver wrote:
> > > On 1/18/20 8:53 AM, stan wrote:
> > > > So, I just discovered the rules system. As I understand it, it can be used
> > > > to rewrite queries before they are passed to the query processor.
> > > > 
> > > > I was wondering if I could use this to resolve a long standing frustration
> > > > of mine. I often need to declare a column as NON NULL, and create a
> > > > trigger/function to auto populate it with something more complex than a
> > > > simple sequence. This need arises fairly often for me, and I would like to
> > > > come up with a good way to implement this.
> > > 
> > > Stay away from rules, they will only drive you to distraction. Stick with
> > > triggers they are a lot easier to understand and implement.
> > > 
> > 
> > I use triggers quite a bit. My issue is that if I have a column defined as
> > NOT NULL, and assign a trigger that fires a function to auto populate this
> > column if the user (input form) does not. The NOT NULL constraint is
> > checked BEFORE the trigger can fire, which means that I cannot properly
> > provide data integrity.
> > 
> 
> I don't know that this changes with rules. In other words I believe
> constraints are checked first with either rules/triggers. Someone else will
> need to confirm this.
> 
> A work around is to supply a dummy DEFAULT for the the NOT NULL column e.g.
> for a varchar column the string 'NOT NULL'. Test for this in the trigger and
> take the appropriate action. Or accept the reality which is, you are
> allowing NULL input to the column and take away the NOT NULL constraint and
> look for NULL or empty string input and take the appropriate action.
> 

Thanks, the dea of a default is an interesting one. I will consider that
solution.
-- 
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
						-- Benjamin Franklin





^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2020-01-20 16:05 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-01-18 16:53 A question about rules stan <stanb@panix.com>
2020-01-18 16:56 ` Adrian Klaver <adrian.klaver@aklaver.com>
2020-01-19 16:52   ` Adrian Klaver <adrian.klaver@aklaver.com>
2020-01-20 16:05     ` stan <stanb@panix.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox