From: Rory Campbell-Lange <rory@campbell-lange.net>
To: Luca Ferrari <fluca1978@gmail.com>
Cc: Dirk Mika <Dirk.Mika@mikatiming.de>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: How to run a task continuously in the background
Date: Wed, 17 Jul 2019 12:34:41 +0100
Message-ID: <20190717113441.GB15220@campbell-lange.net> (raw)
In-Reply-To: <CAKoxK+4XysF-aA3VvFMn6pQYXsgs1o92JZ5uGx8=18Fr8+q4xA@mail.gmail.com>
References: <7932668C-EA48-436B-AA15-DB7C8446D57F@mikatiming.de>
<E9FA92C2921F31408041863B74EE4C2001BD810545@CCPMAILDAG03.cantab.local>
<CAOzAquKb+O3iu0pVBonDDSS8SH=a4Wa4hmYFxAbTwUCi7Hxjhg@mail.gmail.com>
<D49B7AAD-5EC3-4D62-A263-E22E6633C807@mikatiming.de>
<CAKoxK+7x4wEA-AMocZ00b8pAcBe2xZMq_6s8xsKPTWq7tGPdig@mail.gmail.com>
<14555705-8010-4A73-8501-EA07B285E1BA@mikatiming.de>
<CAKoxK+6exRv9yeLOpZGdOi6uefPDDF1yBJOj_Ph8Sq8b3CmeaQ@mail.gmail.com>
<8D19E041-DFA4-40C0-9CCA-36D65548E842@mikatiming.de>
<CAKoxK+4XysF-aA3VvFMn6pQYXsgs1o92JZ5uGx8=18Fr8+q4xA@mail.gmail.com>
On 17/07/19, Luca Ferrari (fluca1978@gmail.com) wrote:
> On Wed, Jul 17, 2019 at 9:38 AM Dirk Mika <Dirk.Mika@mikatiming.de> 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: rory@campbell-lange.net, fluca1978@gmail.com, Dirk.Mika@mikatiming.de
Subject: Re: How to run a task continuously in the background
In-Reply-To: <20190717113441.GB15220@campbell-lange.net>
References: <7932668C-EA48-436B-AA15-DB7C8446D57F@mikatiming.de> <E9FA92C2921F31408041863B74EE4C2001BD810545@CCPMAILDAG03.cantab.local> <CAOzAquKb+O3iu0pVBonDDSS8SH=a4Wa4hmYFxAbTwUCi7Hxjhg@mail.gmail.com> <D49B7AAD-5EC3-4D62-A263-E22E6633C807@mikatiming.de> <CAKoxK+7x4wEA-AMocZ00b8pAcBe2xZMq_6s8xsKPTWq7tGPdig@mail.gmail.com> <14555705-8010-4A73-8501-EA07B285E1BA@mikatiming.de> <CAKoxK+6exRv9yeLOpZGdOi6uefPDDF1yBJOj_Ph8Sq8b3CmeaQ@mail.gmail.com> <8D19E041-DFA4-40C0-9CCA-36D65548E842@mikatiming.de> <CAKoxK+4XysF-aA3VvFMn6pQYXsgs1o92JZ5uGx8=18Fr8+q4xA@mail.gmail.com> <20190717113441.GB15220@campbell-lange.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox