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 1jXCCp-0007ea-2Z for pgsql-general@arkaria.postgresql.org; Fri, 08 May 2020 23:14:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jXCCn-0006CX-UF for pgsql-general@arkaria.postgresql.org; Fri, 08 May 2020 23:14:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jXCCn-0006CQ-K5 for pgsql-general@lists.postgresql.org; Fri, 08 May 2020 23:14:57 +0000 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jXCCl-00049U-DE for pgsql-general@lists.postgresql.org; Fri, 08 May 2020 23:14:57 +0000 Received: by mail-pf1-x42f.google.com with SMTP id w65so1732307pfc.12 for ; Fri, 08 May 2020 16:14:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:subject:in-reply-to:message-id:date :mime-version; bh=nq5AUHewL0M57TArNxyxWYcjyJp60u7dkO/ePbJ9+xQ=; b=TxbS3kC0b7PFLnYBwVLv/mx12TNVUajr9FgF0idwWm7idAj7hEEG525wdbPQ3utBqr rnVzM8VvR5Krfy2kBpAUn2HWNz7UGe5WotQ3SHjTpMh+59m9gBe6CkepOxc0oOGf+3cG 39CaXm6WJRTgCs3nlva68b2tpPcrgPoqDGFuFpXbIw0C66AWH8NmVeRa/tOPW2MWjkTm 3JyqKTzDbQWrnZ/dbNxOH9j3ITzxlVJHe6BrYE0GeOdekl4TiUHh1Zg9gLnqSxCFUZsd yvwR2+pdjZUUw0D6Cjk4QRDHPK49cnzn1Jj1L1zpD09TdLwy6xY2Bbb+48EXFusUL1ZV oJwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:subject :in-reply-to:message-id:date:mime-version; bh=nq5AUHewL0M57TArNxyxWYcjyJp60u7dkO/ePbJ9+xQ=; b=ktfFeg30Z6TH8Dj4WjBm+Vo5sjChzFjpEUErGLFDw95j95GWkHS4bwFWAvB/ZBHV/k IjLRRza5OT3n1i8uXHhKdM6AQsoxmNZ5vihSyJrrev1UExgExhiivLkyxN4iOos8CbGE vVw6lZU1VNFUVbvoxPN5sRWOcufIZz5KzWHo9Cleyi3cCVYvKQTZshegczJLRXK5e9ET QmTnwUl0OdOVh+9jZVJ2oykVWGKsj8JAnyC/LlQwNBRHQhuCc1ktLUowQ0L/ZgJ42VxQ n1rf7QCpiCjPQ4Znm8pjkOYNUUdONdZG6ZcKxlsTzGArqDzhPfV0nAlrZ/woL3NuJp6t I+ig== X-Gm-Message-State: AGi0PubLDaE3vGF3wcdThIQveiJ2V8xLFn5oY4ZxBJRtra7omG62Bm1v 55YIT46PEvbPDEMkVLEgRnbK8+d8 X-Google-Smtp-Source: APiQypJcDYgBhwORBcnsnQtCRE7nledbIPDGPXS2AgPCmKlGZnRLeMJqWUWR+s+bp9BigHOqOfYbcw== X-Received: by 2002:a62:19c7:: with SMTP id 190mr5464908pfz.27.1588979691786; Fri, 08 May 2020 16:14:51 -0700 (PDT) Received: from tim-desktop (203-173-28-107.dyn.iinet.net.au. [203.173.28.107]) by smtp.gmail.com with ESMTPSA id t76sm2115719pgb.33.2020.05.08.16.14.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 May 2020 16:14:51 -0700 (PDT) References: User-agent: mu4e 1.4.4; emacs 27.0.91 From: Tim Cross To: pgsql-general@lists.postgresql.org Subject: Re: Best way to use trigger to email a report ? In-reply-to: Message-ID: <871rnu589l.fsf@gmail.com> Date: Sat, 09 May 2020 09:14:47 +1000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk David G. Johnston writes: > On Fri, May 8, 2020 at 9:26 AM 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. >> > > 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