Re: How to run a task continuously in the background

From: "Weatherby,Gerard" <gweatherby(at)uchc(dot)edu>
To:
Cc: "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: How to run a task continuously in the background
Date: 2019-07-17 10:42:04
Message-ID: 04008F3C-CFA3-44D4-8A20-A5390A131AC9@uchc.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

We used a python process running continually on a linux client for the LISTEN piece.

Reading the documentation, it seems that a background worker ( https://www.postgresql.org/docs/11/bgworker.html) might be a solution to your requirements. I don’t have personal experience with them.
--
Gerard Weatherby | Application Architect
NMRbox | Department of Molecular Biology and Biophysics | UConn Health
263 Farmington Avenue, Farmington, CT 06030-6406
Phone: 860 679 8484
uchc.edu<http://uchc.edu>

On Jul 17, 2019, at 5:57 AM, Dirk Mika <Dirk(dot)Mika(at)mikatiming(dot)de<mailto:Dirk(dot)Mika(at)mikatiming(dot)de>> wrote:

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.
As an example your trigger function will be something like
<https://github.com/fluca1978/PostgreSQL-11-Quick-Start-Guide/blob/master/Chapter09/Chapter09_Listing05.sql<https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_fluca1978_PostgreSQL-2D11-2DQuick-2DStart-2DGuide_blob_master_Chapter09_Chapter09-5FListing05.sql&d=DwMGaQ&c=EZxp_D7cDnouwj5YEFHgXuSKoUq2zVQZ_7Fw9yfotck&r=f-X3XMK2s9o3QbCr8_n2sYw801ckLPRVRmiIoBpOKHg&m=7PED1sfWud-XLOBS3gbmOFen-nrNCi5WmVCAyeDV5wY&s=wyteTGQGbVOgGvJUo40G2IeljhHNo1ixRp4F6A3sQyU&e=>>
and your listening process will be something like
<https://github.com/fluca1978/PostgreSQL-11-Quick-Start-Guide/blob/master/Chapter09/Chapter09_Listing09.perl<https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_fluca1978_PostgreSQL-2D11-2DQuick-2DStart-2DGuide_blob_master_Chapter09_Chapter09-5FListing09.perl&d=DwMGaQ&c=EZxp_D7cDnouwj5YEFHgXuSKoUq2zVQZ_7Fw9yfotck&r=f-X3XMK2s9o3QbCr8_n2sYw801ckLPRVRmiIoBpOKHg&m=7PED1sfWud-XLOBS3gbmOFen-nrNCi5WmVCAyeDV5wY&s=b_NqUS_pRBF_UN20-PJJMb7xJEAfNDXnblDihtNQWpA&e=>>.

Thanks for the examples. I’ll look into them.

This makes your processing fully asynchronous, and with some tune
allows you to decide the start/stop/resume policy as you need/wish.

Besides, it is quite hard for me to get to the point where you need to
check for new data every second, and therefore why you are not
satisfied with pg_cron or stuff like that.

pg_cron doesn’t start the task instantly and queues subsequent runs, if the task is still running. I just need to start the task once and it should keep running until stopped / killed.

Maybe I'll have to rephrase it.
Suppose I have a procedure and want to start it without the client where I start the procedure waiting for it to finish. And I want the procedure to continue even if the client that started it quits.
And I want to be able to check if the procedure is still running.

Dirk

--
Dirk Mika
Software Developer

<image001_d0a5cf51-5dd3-4ff5-b428-1016a6d95a0f.png>

mika:timing GmbH
Strundepark - Kürtener Str. 11b
51465 Bergisch Gladbach
Germany

fon +49 2202 2401-1197
dirk(dot)mika(at)mikatiming(dot)de<mailto:dirk(dot)mika(at)mikatiming(dot)de>
www.mikatiming.de<http://www.mikatiming.de/>

AG Köln HRB 47509 * WEEE-Reg.-Nr. DE 90029884
Geschäftsführer: Harald Mika, Jörg Mika

<CCh2U_f27a5ce6-8556-4db1-8aa4-fd610d6e9efe.jpg><https://urldefense.proofpoint.com/v2/url?u=https-3A__youtu.be_qfOFXrpSKLQ&d=DwMGaQ&c=EZxp_D7cDnouwj5YEFHgXuSKoUq2zVQZ_7Fw9yfotck&r=f-X3XMK2s9o3QbCr8_n2sYw801ckLPRVRmiIoBpOKHg&m=7PED1sfWud-XLOBS3gbmOFen-nrNCi5WmVCAyeDV5wY&s=U1bLNWLn_W8f97ao7qwbGU19rJAGN34e1rLr4o4_r9Q&e=>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Luca Ferrari 2019-07-17 10:49:21 Re: Issue related with patitioned table:How can I quickly determine which child table my record is in,given a specific primary key value?
Previous Message Nicola Contu 2019-07-17 10:31:43 Re: Matview size - space increased on concurrently refresh