From: Tim Cross <theophilusx@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Best way to use trigger to email a report ?
Date: Sat, 09 May 2020 09:14:47 +1000
Message-ID: <871rnu589l.fsf@gmail.com> (raw)
In-Reply-To: <CAKFQuwYfcubMMxgD4QJnBkdCZMUB3=1MZM6_xM5pXaMUi5kFfQ@mail.gmail.com>
References: <CAMBRECCgPueR=XqaGw42h_Or=sDg65AUYpqFwGK52SHktjzoog@mail.gmail.com>
<CAKFQuwYfcubMMxgD4QJnBkdCZMUB3=1MZM6_xM5pXaMUi5kFfQ@mail.gmail.com>
David G. Johnston <david.g.johnston@gmail.com> writes:
> On Fri, May 8, 2020 at 9:26 AM David Gauthier <davegauthierpg@gmail.com>
> wrote:
>
>> psql (9.6.0, server 11.3) on linux
>>
>> Looking for ideas. I want a trigger to...
>> 1) compose an html report based on DB content
>> 2) email the report to a dist list (dl = value of a table column)
>>
>> If this will involve hybrid coding, I prefer PL/perl. The linux env has
>> both "mail" and "mutt" (if this is of any help).
>>
>> 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.
>
Yep, exactly how I would do it as well. Personally, triggers are my last
choice. In some situations, they are the right choice and when you do
need them, keep them as small and simple as possible.
Others have mentioned the issues of using external calls inside a
trigger. In addition to the problem with waiting on external processes
to complete, you also have all the messy external work things to take
care of (like network down, server unavailable, etc).
The solution I've used in the past is to have procedures in the database
which generate the email report and insert that data into an email
table. I then have a shell level script (could be perl, javascript,
java, whatever) which looks in this table periodically and if it finds a
report which has not been sent, extract it, optionally format it and
send it. On successful completion, set a 'sent' flag on the report
record in the DB (or just delete it - I prefer to set a flag so that if
something failed unexpectedly, you still have the report).
There are ways you can trigger periodic activity in the database, but to
be honest, CRON is easy and reliable and avoids needing to add
additional extensions etc to the DB. Turning off the report, changing
the time etc, is a simple crontab edit.
--
Tim Cross
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: theophilusx@gmail.com
Subject: Re: Best way to use trigger to email a report ?
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>
* 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