pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rory Campbell-Lange <rory@campbell-lange.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: Best way to use trigger to email a report ?
Date: Sun, 10 May 2020 20:17:21 +0100
Message-ID: <20200510191721.GA11313@campbell-lange.net> (raw)
In-Reply-To: <871rnu589l.fsf@gmail.com>
References: <CAMBRECCgPueR=XqaGw42h_Or=sDg65AUYpqFwGK52SHktjzoog@mail.gmail.com>
	<CAKFQuwYfcubMMxgD4QJnBkdCZMUB3=1MZM6_xM5pXaMUi5kFfQ@mail.gmail.com>
	<871rnu589l.fsf@gmail.com>

On 09/05/20, Tim Cross (theophilusx@gmail.com) wrote:
> David G. Johnston <david.g.johnston@gmail.com> writes:
> > On Fri, May 8, 2020 at 9:26 AM David Gauthier <davegauthierpg@gmail.com>
> > wrote:

> >> The idea is to send a report to the list when all the data has been
> >> collected for a particular job and the final status of the job is updated
> >> as a col of a rec of a certain table.  Probably a post update trigger.
> >
> > I'd probably limit the trigger to checking for the completion of the data
> > collection and inserting a record into a "email job" table.  Then I'd have
> > cron on a linux machine periodically run a script that queries the "email
> > job" table for work, perform the work, and then either flag the work as
> > done or remove the job record.

We have gone for a solution using triggers with pgmemcached and
supervisord.

Supervisord loops, checking memcached for updates to any in our large
cluster of databases periodically, eg every 15 seconds, and then picks
up the messages to send from any databases with messages pending.

Here is an example trigger:

    CREATE OR REPLACE FUNCTION trigger_send_message() RETURNS TRIGGER AS $$
    BEGIN
        IF NEW.status = 'sendme' THEN
            PERFORM extensionscheme.memcache_set(current_database(), '1');
        END IF;
        RETURN NEW;
    END;
    $$ LANGUAGE plpgsql;

In our case, with many databases, we wished to remove the connection
overhead for many NOTIFYs. The above arrangement means the supervisord
process only makes a connection to the client database if messages are
pending.

Rory





view thread (11+ messages)

Message-ID: <20200510191721.GA11313@campbell-lange.net>
Permalink:  ../20200510191721.GA11313@campbell-lange.net/
Also on:    postgresql.org/message-id/20200510191721.GA11313@campbell-lange.net

 · 

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-general@postgresql.org
  Cc: rory@campbell-lange.net
  Subject: Re: Best way to use trigger to email a report ?
  In-Reply-To: <20200510191721.GA11313@campbell-lange.net>
  References: <CAMBRECCgPueR=XqaGw42h_Or=sDg65AUYpqFwGK52SHktjzoog@mail.gmail.com> <CAKFQuwYfcubMMxgD4QJnBkdCZMUB3=1MZM6_xM5pXaMUi5kFfQ@mail.gmail.com> <871rnu589l.fsf@gmail.com> <20200510191721.GA11313@campbell-lange.net>

* 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