Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hniCy-0003CF-64 for pgsql-general@arkaria.postgresql.org; Wed, 17 Jul 2019 11:34:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hniCv-00023U-VA for pgsql-general@arkaria.postgresql.org; Wed, 17 Jul 2019 11:34:49 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hniCv-00023F-8k for pgsql-general@lists.postgresql.org; Wed, 17 Jul 2019 11:34:49 +0000 Received: from [2a01:7e00::f03c:91ff:fe18:393b] (helo=campbell-lange.net) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1hniCr-0003tF-AK for pgsql-general@lists.postgresql.org; Wed, 17 Jul 2019 11:34:47 +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:Cc:To:From:Date:Sender:Reply-To: 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=SPBuIZcDTEs5keC9coV9c5xyOdVbngZWQVG80K5+cn0=; b=ACBOo6Nxc8ALIgXL+1+wtjYP6 VKEZzSffy9ElRFvZVMboJ6tfqmWy4FU2YyOprAYk6msljFn554ZfMZZxj7uIItu0gJdTJ1R9Z99pM mqlc0rWh+L2aLfi2q9J7MiEyb6odipKI1epFMzW/XjJTYNMt1XfPEHREADOlOUisaXI98=; Received: from [217.138.52.158] (helo=rory-t450s) by campbell-lange.net with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1hniCn-0001WJ-K9; Wed, 17 Jul 2019 11:34:41 +0000 Received: from rory by rory-t450s with local (Exim 4.92) (envelope-from ) id 1hniCn-00048v-On; Wed, 17 Jul 2019 12:34:41 +0100 Date: Wed, 17 Jul 2019 12:34:41 +0100 From: Rory Campbell-Lange To: Luca Ferrari Cc: Dirk Mika , "pgsql-general@lists.postgresql.org" Subject: Re: How to run a task continuously in the background Message-ID: <20190717113441.GB15220@campbell-lange.net> References: <7932668C-EA48-436B-AA15-DB7C8446D57F@mikatiming.de> <14555705-8010-4A73-8501-EA07B285E1BA@mikatiming.de> <8D19E041-DFA4-40C0-9CCA-36D65548E842@mikatiming.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a01:7e00::f03c:91ff:fe18:393b (failed) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 17/07/19, Luca Ferrari (fluca1978@gmail.com) wrote: > On Wed, Jul 17, 2019 at 9:38 AM Dirk Mika wrote: > > That is basically still my main question. How do I start a > > background job (e.g. a function) which waits by polling or LISTEN / > > NOTIFY for records in a table to be processed. > > You will have a trigger that, once new tuples are created (or older > update and so on) issues a NOTIFY. > Somewhere (within PostgreSQL or outside it) there will be a process > that issued a LISTEN and is locked until a notify comes in. Then it > does process whatever you need to do. We make extensive use of postgresql 'contacting' an external process, but our use case involves many databases in a cluster rather than many schemas. Also we don't have to deal with cancelling the external process. We chose this architecture to avoid many postgresql connections for LISTEN/NOTIFY. We use a pgmemcache interface trigger to update memcached with a notification of the database holding items to be flushed. A python looping process running under supervisord checks memcache for items to be dealt with, and does so in a serial manner. For the use case mentioned a per-schema process may be required or a sub-process/thread created, which could check perhaps for memcache to signal cancellation of processing for the schema. I guess one might then have thread locking/cancellation issues to resolve. Rory