thanks in advance for the help!
I would like to set up a Lisp program that does something when my Postgres database table is updated. I'm using the amazing Postmodern library, which has been absolutely terrific.
As discussed in this question (client side notification on table change in Common Lisp with Postmodern package) I don't want to poll the database (as the OP complains of the answer), but instead want the database to tell my program when to do something.
The person who answered points to the Postmodern function "wait-for-notifications": https://github.com/marijnh/Postmodern/blob/22aab0ec25d5f6affd390e690827a7515aeafd4f/cl-postgres/public.lisp#L358-L370
I am a little concerned about this function because 1. it is internal to Postmodern and therefore (let me know if I'm wrong here) not for public consumption and 2. it takes "database-connection" as an argument, which I can't track down.
So:
Thank you so much!
- it is internal to Postmodern and therefore (let me know if I'm wrong here) not for public consumption
There are different layers, wait-for-notification
is an exported symbol from cl-postgres
package, and defined in a file named public.lisp
, so this should be ok to use it directly. Postmodern is at a higher level of abstraction but depending on your needs you are allowed to call directly cl-postgres
functions.
- it takes "database-connection" as an argument, which I can't track down.
A connection is what you get when you call open-database
. Helper macros like with-connection
bind the special *database*
variable. Usually this is used invisibly by query
and other commands, which look at the current binding for *database*
. However if you want to understand a bit more how it is used, the documentation at https://marijnhaverbeke.nl/postmodern/cl-postgres.html seems quite good.
If you are going to wait for changes in your tables, chances are that you will want to use a dedicated thread. The documentation says that:
If your application is threaded, each thread should use its own connection. Connections are stateful and attempts to use the same connection for multiple threads will likely have problems.