Postgres notification - ghdrako/doc_snipets GitHub Wiki
The PostgreSQL protocol includes a streaming protocol with COPYand also implements asynchronous messages and notifications. This means that as soon as a connection is established with PostgreSQL, the server can send messages to the client even when the client is idle.
PostgreSQL Notifications LISTEN/NOTIFY
sql# listen channel;
LISTEN
sql# notify channel, 'foo';
NOTIFY
Asynchronous notification "channel" with payload "foo" ⏎
received from server process with PID 40430.
Note that the message could be sent from another connection, so try it and see with several psql instances. The payload from the message can be any text, up to 8kB in length. This allows for rich messages to flow, such as JSON encoded values.
yesql# listen channel;
LISTEN
yesql# notify channel, 'foo';
NOTIFY Programming
Server-side Prepared Statements
In console:
prepare foo as
select date, shares, trades, dollars
from factbook
where date >= $1::date
and date < $1::date + interval '1 month'
order by date;
execute foo('2010-02-01');