Re: Need psql send email

From: Chris Travers <chris(dot)travers(at)gmail(dot)com>
To: Martin Gainty <mgainty(at)hotmail(dot)com>
Cc: martin(dot)french(at)romaxtech(dot)com, depesz(at)depesz(dot)com, pavithra(dot)ibt(at)gmail(dot)com, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>, pgsql-general-owner(at)postgresql(dot)org
Subject: Re: Need psql send email
Date: 2012-09-20 13:53:19
Message-ID: CAKt_Zfsm8tf81M3iD7rG3AUgiftFqkRMy3iZK=4SbQUDrNsJhg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all;

A couple points here.

First, you probably don't want to send email directly from a database
function. This gives significant problems for which there is no good
solution. Consider:

1) You sent your email and now the transaction rolls back. You *cannot*
roll back the sent email.

2) Your email fails to send. Do you abort the transaction?

IMO it is always better to send email from a second process that can be
notified on db commit. This avoids these issues and kicks them to a
post-transaction handler.

As luck would have it, I recently set a project up on Google Code to help
address this (and other application integration) issues. See
http://code.google.com/p/pg-message-queue/

There isn't a lot of overlap with something like pgq. This is
listen/notify/queue tables based. May not ever be big and professional but
it should work once the bugs are ironed out. Even before then it may give
a good idea of how to implement a notification-based queue on PostgreSQL.

The idea here is that you can essentially send a message to a channel on a
db event (say, from a trigger) and then have another app that either
periodically checks the queue (say, from a cron job) or listens on a
channel for notifications.

The whole thing was confirmed working before I made some changes. If folks
are interested in helping I am sure it will be well tested and working in
no time. Please read the docs first though. I wouldn't say it is
production-ready yet, but it may provide an overview of how to go about
implementing something like this in production.

Also for more info on how to do this with a LISTEN/NOTIFY approach outside
of the above, see
http://ledgersmbdev.blogspot.com/2012/09/objectrelational-interlude-messaging-in.html

In general I think mixing transactional and non-transactional side-effects
is just asking for trouble. Don't do it any more than you have to.

Best Wishes,
Chris Travers

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2012-09-20 14:47:48 Re: pg_upgrade: out of memory
Previous Message Edson Richter 2012-09-20 13:35:54 Re: Need psql send email