Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f1mv9-0002Rf-Im for pgsql-general@arkaria.postgresql.org; Fri, 30 Mar 2018 05:49:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f1mv8-0002ef-4n for pgsql-general@arkaria.postgresql.org; Fri, 30 Mar 2018 05:49:50 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f1mv7-0002eU-Sc for pgsql-general@lists.postgresql.org; Fri, 30 Mar 2018 05:49:49 +0000 Received: from mx2.mailbox.org ([80.241.60.215]) by magus.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f1mv4-0002qt-DV for pgsql-general@postgresql.org; Fri, 30 Mar 2018 05:49:49 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [80.241.60.240]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx2.mailbox.org (Postfix) with ESMTPS id 9BC7E41D31; Fri, 30 Mar 2018 07:49:44 +0200 (CEST) X-Virus-Scanned: amavisd-new at heinlein-support.de Received: from smtp1.mailbox.org ([80.241.60.240]) by hefe.heinlein-support.de (hefe.heinlein-support.de [91.198.250.172]) (amavisd-new, port 10030) with ESMTP id V8frjwUGWgln; Fri, 30 Mar 2018 07:49:40 +0200 (CEST) Message-ID: <1522388978.2396.10.camel@cybertec.at> Subject: Re: Asynchronous Trigger? From: Laurenz Albe To: Cory Tucker , "pgsql-general@postgresql.org" Date: Fri, 30 Mar 2018 07:49:38 +0200 In-Reply-To: References: Organization: Cybertec Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Cory Tucker wrote: > Is it possible to have the execution of a trigger (or any function) not block the > completion of the statement they are associated with? Constraint triggers can be deferred to the end of the transaction, but I am certain that is not what you are looking for. > A pattern I had hoped to implement was to do a quick update of rows that signaled > they needed attention, and then an async per-row trigger would come and do the > maintenance (in this case, make an expensive materialized view). > > Any suggestions welcome. This seems like a really bad idea - even if it happens "in the background", would you want an expensive materialized view to be updated whenever a row is modified? If you modify three rows, that would happen three times in a row! One solution would be to set a flag somewhere that marks the materialized view dirty, and a regular cron-job that checks the flag and rebuilds the view. With the asynchronous solution you had in mind, the materialized view would not be up to date anyway. Other than that, if your materialized view definition is simple enough, you might be able to use a regular table and a trigger that updates the table to reflect the modifications caused by the insert, update or delete. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com