Thread: Best way to use trigger to email a report ?

Best way to use trigger to email a report ?

From
David Gauthier
Date:
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. 

Thanks for any ideas :-)



Re: Best way to use trigger to email a report ?

From
Tim Clarke
Date:

Personally I'd have your trigger put the necessary data into a queue to run the report then have some other process take that data off asynchronously. Either pop it in a table and do it yourself or use a robust distributed broker protocol platform like RabbitMQ or Apache ActiveMQ  or Kafka.

Tim Clarke MBCS
IT Director
Direct: +44 (0)1376 504510 | Mobile: +44 (0)7887 563420

On 08/05/2020 17:26, David Gauthier 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. 

Thanks for any ideas :-)



 

Telephone: Witham: +44(0)1376 503500 | London: +44 (0)20 3009 0853 | Frankfurt: +49 (0)69 7191 6000 | Hong Kong: +852 5803 1687 | Toronto: +1 647 503 2848
Web: https://www.manifest.co.uk/

 

Minerva Analytics Ltd - A Solactive Company
9 Freebournes Court | Newland Street | Witham | Essex | CM8 2BL | United Kingdom


Copyright: This e-mail may contain confidential or legally privileged information. If you are not the named addressee you must not use or disclose such information, instead please report it to admin@minerva.info
Legal: Minerva Analytics is the trading name of: Minerva Analytics Ltd: Registered in England Number 11260966 & The Manifest Voting Agency Ltd: Registered in England Number 2920820 Registered Office at above address. Please Click Here https://www.manifest.co.uk/legal/ for further information.

Re: Best way to use trigger to email a report ?

From
Alan Hodgson
Date:
On Fri, 2020-05-08 at 12:26 -0400, David Gauthier 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).  


plperlu can access resources outside PostgreSQL.

I'd suggest using Net::Mail and Net::SMTP to compose and send the mail to localhost:25. This avoids any shell escaping issues and still lets your local MTA accept the mail immediately and deliver it when it can.

Re: Best way to use trigger to email a report ?

From
Christophe Pettus
Date:

> On May 8, 2020, at 09:26, 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)

You probably *don't* want to actually send an email within a trigger; doing an operation that can block on an external
service(DNS, SMTP) within a trigger is asking for hard-to-diagnose trouble.  You probably don't even want to create the
HTML;that's adding a lot of time to the operation that fires the trigger. 

I'd probably set up the trigger to store the minimal data required to produce the HTML into a separate table, and then
havea background job query the table, create the HTML, and mail out the report.  If you don't want to periodically poll
thetable, you can use NOTIFY within the trigger to wake up a process that is waiting on NOTIFY. 

--
-- Christophe Pettus
   xof@thebuild.com




Re: Best way to use trigger to email a report ?

From
"David G. Johnston"
Date:
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.

David J.

Re: Best way to use trigger to email a report ?

From
"David G. Johnston"
Date:
On Fri, May 8, 2020 at 10:19 AM Christophe Pettus <xof@thebuild.com> wrote:
If you don't want to periodically poll the table, you can use NOTIFY within the trigger to wake up a process that is waiting on NOTIFY.

Kinda.  

"With the libpq library, the application issues LISTEN as an ordinary SQL command, and then must periodically call the function PQnotifies to find out whether any notification events have been received.".

IOW, the interface for the client is still a polling interface its just that with LISTEN the event is transient and in-memory only (on the server) and thus has less overhead.

David J.

Re: Best way to use trigger to email a report ?

From
David Gauthier
Date:
Got it. 

On Fri, May 8, 2020 at 2:05 PM David G. Johnston <david.g.johnston@gmail.com> wrote:
On Fri, May 8, 2020 at 10:19 AM Christophe Pettus <xof@thebuild.com> wrote:
If you don't want to periodically poll the table, you can use NOTIFY within the trigger to wake up a process that is waiting on NOTIFY.

Kinda.  

"With the libpq library, the application issues LISTEN as an ordinary SQL command, and then must periodically call the function PQnotifies to find out whether any notification events have been received.".

IOW, the interface for the client is still a polling interface its just that with LISTEN the event is transient and in-memory only (on the server) and thus has less overhead.

David J.

Re: Best way to use trigger to email a report ?

From
Magnus Hagander
Date:


On Fri, May 8, 2020 at 8:05 PM David G. Johnston <david.g.johnston@gmail.com> wrote:
On Fri, May 8, 2020 at 10:19 AM Christophe Pettus <xof@thebuild.com> wrote:
If you don't want to periodically poll the table, you can use NOTIFY within the trigger to wake up a process that is waiting on NOTIFY.

Kinda.  

"With the libpq library, the application issues LISTEN as an ordinary SQL command, and then must periodically call the function PQnotifies to find out whether any notification events have been received.".

IOW, the interface for the client is still a polling interface its just that with LISTEN the event is transient and in-memory only (on the server) and thus has less overhead.


Doesn't have to be. You can use PQsocket() to get the socket back, and then select() or poll() on that socket, and you only have to call PQnotifies() once that call has indicated "something happened".

Similar applies to writing such daemons using for example the python or perl interfaces. You block your process or thread on select() and take action when that one returns.

--

Re: Best way to use trigger to email a report ?

From
Tim Cross
Date:
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



Re: Best way to use trigger to email a report ?

From
AC Gomez
Date:
We're posting a flag to a table. The table has an "event" field. When we post the value "email" into that field, a sweeper app that runs on a schedule looks for this flag, then takes the value in the message field and sends that out as an email.



On Sat, May 9, 2020, 12:31 PM Tim Clarke <tim.clarke@minerva.info> wrote:

Personally I'd have your trigger put the necessary data into a queue to run the report then have some other process take that data off asynchronously. Either pop it in a table and do it yourself or use a robust distributed broker protocol platform like RabbitMQ or Apache ActiveMQ  or Kafka.

Tim Clarke MBCS
IT Director
Direct: +44 (0)1376 504510 | Mobile: +44 (0)7887 563420

On 08/05/2020 17:26, David Gauthier 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. 

Thanks for any ideas :-)



 

Telephone: Witham: +44(0)1376 503500 | London: +44 (0)20 3009 0853 | Frankfurt: +49 (0)69 7191 6000 | Hong Kong: +852 5803 1687 | Toronto: +1 647 503 2848
Web: https://www.manifest.co.uk/

 

Minerva Analytics Ltd - A Solactive Company
9 Freebournes Court | Newland Street | Witham | Essex | CM8 2BL | United Kingdom


Copyright: This e-mail may contain confidential or legally privileged information. If you are not the named addressee you must not use or disclose such information, instead please report it to admin@minerva.info
Legal: Minerva Analytics is the trading name of: Minerva Analytics Ltd: Registered in England Number 11260966 & The Manifest Voting Agency Ltd: Registered in England Number 2920820 Registered Office at above address. Please Click Here https://www.manifest.co.uk/legal/ for further information.

Re: Best way to use trigger to email a report ?

From
Rory Campbell-Lange
Date:
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