PG_LISTEN(n)
π PG_LISTEN β or, how to make your app listen for drama from Postgres π
ok so real talk, this page lives in the PostgreSQL Tcl Interface Documentation and it's the whole guide to pg_listen, which is basically the group-chat-notifications feature of pgtcl. stay with me bestie, it's actually so good β¨
β¨ SYNOPSIS β¨
pg_listen conn notifyName [callbackCommand]
three little words and a maybe-fourth, that's the whole vibe π¦
π DESCRIPTION π
okay so pg_listen creates, changes, or cancels a request to listen for asynchronous notification messages from the PostgreSQL server. that's the entire job description and honestly? iconic.
here's the tea on the two modes:
- pass a
callbackCommandβ the request is established, or, if a request already existed, its command string gets replaced. glow-up, not a new entry π - pass no
callbackCommandβ any prior request is canceled. it's giving "we're not doing this anymore" π
once a pg_listen request is established, that specified command string gets executed whenever a notification message bearing the given name arrives from the server. and when does that happen? when any PostgreSQL client application issues a NOTIFY command referencing that name. so someone else's app can be the one pressing send, and your app gets the ping. teamwork, fr fr π«Ά
now the technical bit, and it matters: the command string is executed from the Tcl idle loop. that's the normal idle state of an application written with Tk. in non-Tk Tcl shells, you can execute update or vwait to cause the idle loop to be entered. ngl if your non-Tk shell seems like it's ignoring you, that's why β no idle loop action, no callback. no cap β¨
π¨ important bestie warning π¨ you should NOT invoke the SQL statementsLISTENorUNLISTENdirectly when usingpg_listen. pgtcl takes care of issuing those statements for you. it's handled! don't do it manually, that's double-texting in SQL form π
but β and this is the fun part β if you want to send a notification message yourself, invoke the SQL NOTIFY statement using pg_exec. that's the approved slay route π
πΈ ARGUMENTS πΈ
connβ the handle of the connection on which to listen for notifications. this is your β¨main characterβ¨ connection.notifyNameβ the name of the notification condition to start or stop listening to. the name is the whole personality here, ok πcallbackCommandβ if present, provides the command string to execute when a matching notification arrives. present = "do this when it lands." absent = "we're done here." π
β RETURN VALUE β
None. that's it. that's the return value. it does the thing, it doesn't hand you a value back. lowkey refreshing tbh π
π¦ reminder:callbackCommandin = established/replaced,callbackCommandout = canceled.+vs-energy, iykyk.
made with π from the PostgreSQL Tcl Interface Documentation, dated 17 September 2020 β that's the source, the version, and the date all wrapped up with a bow π go forth and let your notifications reach you, bestie. periodt. π₯ππ
π¦ where this page comes from
- alpine 3.22: pgtcl-doc 3.1.0-r0 Β· BSD-3-Clause
- alpine 3.23: pgtcl-doc 3.1.1-r0 Β· BSD-3-Clause
- alpine 3.24: pgtcl-doc 3.1.1-r0 Β· BSD-3-Clause
- nixpkgs unstable: pgtcl 3.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms