Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jXrS1-0000KD-Ep for pgsql-general@arkaria.postgresql.org; Sun, 10 May 2020 19:17:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jXrS0-0005Tc-Bf for pgsql-general@arkaria.postgresql.org; Sun, 10 May 2020 19:17:24 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jXrRz-0005Qs-PU for pgsql-general@lists.postgresql.org; Sun, 10 May 2020 19:17:24 +0000 Received: from campbell-lange.net ([178.79.140.51]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jXrRx-0001yx-Io for pgsql-general@lists.postgresql.org; Sun, 10 May 2020 19:17:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=campbell-lange.net; s=it; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:To:From:Date:Sender:Reply-To:Cc: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=Suu+Hjda5eNk2S8T8TBnIiyxnBW4YdaYQEMCllW1138=; b=pQP+pLnX4mafz/G9kQsgVgfJM vTVJ6/TwZ51aKdwJuxTEeob0QGXzgo5/lafohn1VWZbsLgYXPycdZ+Q0iEazsYd97Mp78PC86DKVq x/WLHoVbRdHAG//bOFWUKf5M664VRYUJsSkQhup604EEgqPucCY3/At6glEal50ShCg8k=; Received: from 195.164.198.146.dyn.plus.net ([146.198.164.195] helo=rory-t450s) by campbell-lange.net with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jXrRv-0001El-Hh for pgsql-general@lists.postgresql.org; Sun, 10 May 2020 19:17:19 +0000 Received: from rory by rory-t450s with local (Exim 4.92) (envelope-from ) id 1jXrRx-00034H-Ec for pgsql-general@lists.postgresql.org; Sun, 10 May 2020 20:17:21 +0100 Date: Sun, 10 May 2020 20:17:21 +0100 From: Rory Campbell-Lange To: pgsql-general@lists.postgresql.org Subject: Re: Best way to use trigger to email a report ? Message-ID: <20200510191721.GA11313@campbell-lange.net> References: <871rnu589l.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <871rnu589l.fsf@gmail.com> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 09/05/20, Tim Cross (theophilusx@gmail.com) wrote: > David G. Johnston writes: > > On Fri, May 8, 2020 at 9:26 AM David Gauthier > > 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