Can I notify and listen inside PostgreSQL procedures (functions)?
Asked Answered
C

1

8

I have checked the documentation (for my version 9.3):

http://www.postgresql.org/docs/9.3/static/sql-notify.html

http://www.postgresql.org/docs/9.3/static/sql-listen.html

I have read multiple discussions and blogs about notify-listen in postgres. They all use a listening process / interface, which is not implemented inside "classic" procedure (which is function in postgres anyway). They implement it in different language and/or environment, external to the postgres server (e.g. perl, C#).

My question: Is it possible to implement listen(ing) inside postgres function (language plpgsql) ? If not (what I assume from not being to able to find such topic / example), can someone explain a bit, why it can't be done, or maybe why it does not make sense to do it that way ?

Cloninger answered 23/2, 2015 at 16:4 Comment(2)
Maybe its helps #5412974Croton
That is a strange thing to ask from a database because that is a client-ish thing to do, but something like that could help me right now, I can't find a solution either. I suppose I could create a plperl function that would connect back to the database using DBI, would LISTEN using select() until the first NOTIFY and then return that but that would be such a huge thing to do for something so small.Escalator
I
0

It is a classic use case for Trigger Function in case you depend on a single table: https://www.postgresql.org/docs/current/plpgsql-trigger.html

Ideally answered 14/7, 2022 at 12:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.