pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ramakrishnan Muralidharan <ramakrishnanm@pervasive-postgres.com>
To: weigelt@metux.de
To: pgsql-sql <pgsql-sql@postgresql.org>
Subject: Re: trigger/rule question
Date: Mon, 2 May 2005 10:19:17 +0530
Message-ID: <02767D4600E59A4487233B23AEF5C5992A4081@blrmail1.aus.pervasive.com> (raw)

Hi,
  
  I have written the following trigger assuming the application can pass NULL value on mtime and it can be handled on the server side by the trigger.

CREATE TABLE TEST3
( ID INT4 NOT NULL,
  DDATE TIMESTAMP 
)



CREATE OR REPLACE FUNCTION Updatemtime() 
RETURNS TRIGGER AS $Updatemtime$
DECLARE
  dDate timestamp;
BEGIN
  dDate = 'now';
  IF COALESCE(NEW.DDATE , dDate ) = dDate THEN
     NEW.DDATE = dDate;
  END IF;
  RETURN NEW;
END;
$Updatemtime$ LANGUAGE 'plpgsql';


CREATE TRIGGER Updatemtime BEFORE INSERT  ON TEST3
    FOR EACH ROW EXECUTE PROCEDURE Updatemtime();


INSERT INTO TEST3 VALUES( 1 , NULL );
INSERT INTO TEST3 VALUES( 2 , '2005-05-01');  

select * from TEST3

  I will continue work on this and let you know if I can find another better solution for this issue.

Regards,
R.Muralidharan
     
-----Original Message-----
From: pgsql-sql-owner@postgresql.org
[mailto:pgsql-sql-owner@postgresql.org]On Behalf Of Enrico Weigelt
Sent: Monday, May 02, 2005 9:02 AM
To: pgsql-sql
Subject: Re: [SQL] trigger/rule question


* Ramakrishnan Muralidharan <ramakrishnanm@pervasive-postgres.com> wrote:

Hi,

>     Going through you mail, I assume that you are updating the mtime 
>     only after inserting the record. 

An "normal" update (=done by an application or user) should also 
update the mtime. But there's an replication subsystem, which writes
should go through untouched. 

> It is always possible to check the mtime filed value of the inserted 
> record and take action based on it in the trigger. 

yeah, but how to detect whether the application has explicitly 
written it ?

The only chance I currently have in mind is to use some session 
dependent data, i.e. username or some persistant storage (could be 
easily done ie. w/ plphp) for this decision. The sync subsystem
has to do some "special" login (ie. separate user or setting the 
session wide variable) before doing its work.

I would be happier to let a rule do this, so there's not an extra 
function per written row. But all my experiments ran into infinite
recoursion trouble.

>     Is it possible to send me detail about the trigger?
The trigger isn't existing yet. I'm currently maintaining the mtime
updates within the application, but I wanna get away from that. It 
probably would be interesting, if a normal application couldn't 
touch the mtime at all.


cu
-- 
---------------------------------------------------------------------
 Enrico Weigelt    ==   metux IT service
  phone:     +49 36207 519931         www:       http://www.metux.de/
  fax:       +49 36207 519932         email:     contact@metux.de
---------------------------------------------------------------------
  Realtime Forex/Stock Exchange trading powered by postgresSQL :))
                                            http://www.fxignal.net/
---------------------------------------------------------------------

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

               http://archives.postgresql.org



view thread (10+ messages)  latest in thread

Message-ID: <02767D4600E59A4487233B23AEF5C5992A4081@blrmail1.aus.pervasive.com>
Permalink:  ../02767D4600E59A4487233B23AEF5C5992A4081@blrmail1.aus.pervasive.com/
Also on:    postgresql.org/message-id/02767D4600E59A4487233B23AEF5C5992A4081@blrmail1.aus.pervasive.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: ramakrishnanm@pervasive-postgres.com, weigelt@metux.de
  Subject: Re: trigger/rule question
  In-Reply-To: <02767D4600E59A4487233B23AEF5C5992A4081@blrmail1.aus.pervasive.com>
  References: <02767D4600E59A4487233B23AEF5C5992A4081@blrmail1.aus.pervasive.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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