Re: Plperlu and sending emails, is it safe?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Philippe Lang" <philippe(dot)lang(at)attiksystem(dot)ch>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Plperlu and sending emails, is it safe?
Date: 2004-06-28 14:16:27
Message-ID: 26374.1088432187@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Philippe Lang" <philippe(dot)lang(at)attiksystem(dot)ch> writes:
> I need to send emails from Postgresql triggers.

> I was able to do it with a 'plperlu' function, that calls the 'system'
> perl function, that itself calls the 'mail' shell function.

> Is is safe to do things like this? Is there a possible concurrent access
> that could arise?

The real issue here is transactional semantics. What happens if the
transaction that fired the trigger rolls back due to a later error?
The transaction effectively never happened, but you can't unsend the mail.

> Another solution would be to use cron every 5 minutes, and read the
> content of a table.

This would probably be better because the cron job could only see the
results of committed transactions. The failure mode in this case is
that the same mail could be sent more than once (if the cron job fails
between sending the mail and committing its update that deletes the
entry in the pending-mails table). But you'd not be wondering why you
got mail that seems not to be related to anything visible in the
database.

As noted elsewhere, you could use NOTIFY of a persistent daemon process
instead of a cron job. This might be a win if you would like the mail
sent more quickly than ~5 minutes. The point is to put the actual
sending in a separate transaction ...

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message UMPA Development 2004-06-28 15:15:48 Group by - Case insensitivity
Previous Message Tom Lane 2004-06-28 13:48:54 Re: Performance problem on RH7.1