-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow schema cache reloading with NOTIFY #1542
Conversation
Fixes PostgREST#1512 Helps on environments where you can't send unix signals(Windows, managed containers). Also provides better UX for schema reloads - NOTIFY can be send from pg clients(psql, pgadmin). By default, `NOTIFY pgrst` - with no payload - should be done to reload the schema cache. Notifications with a payload will be ignored. The channel name can be configured with `db-channel`. There's limitation for now, if the LISTEN connection is lost the connection won't be retried.
Made this restriction because it might be useful to later add |
Also make CircleCI nix-test job depend on nix-build
How to disable this feature? |
Hey @dwagin. Right now I'm including it by default with no option to disable it. |
@dwagin I've added the |
This is very good, because LISTEN/NOTIFY doesn't work with PgBouncer and additional extra connection is not needed in this situation. |
If this is something that'll potentially cause trouble, like with PgBouncer, wouldn't it make sense to have db-channel-enabled=false as default? |
@dwagin Could you share how are you using PgBouncer? On SQL feature map for pooling modes, they mention that LISTEN/NOTIFY does work with Session pooling(not transaction pooling). And #1011 left me with the impression that transaction pooling is not working with PostgREST now(only session pooling works), or did you managed to make that work? |
@nerfpops It was tempting to enable it by default and just tell users to run |
@steve-chavez Session pooling doesn't make sense, we use transaction pooling (with small hasql patch). |
An update on the implementation. The LISTEN connection is now retried if lost. Without the debounce, doing this in bash: for i in {1..20}; do psql postgrest_test -c "NOTIFY pgrst"; done Results in 20 schema reloads. The debounce reduces that to one. Weirdly, I'm not able to reproduce the bad 20 schema reloads with just DO $$ BEGIN FOR counter IN 1..20 LOOP NOTIFY pgrst; END LOOP; END$$; That always results in one NOTIFY sent, not sure what could be wrong there. But to be on the safe sade, I'll add the integration tests in bash. |
putMVar was locking the thread. Changed it to tryPutMVar, should be fine since the connectionWorker is the only producer.
Adding the bash integration tests to CI is proving to be a lot of work. The On the introduction of SIGHUP(#869) we also did manual bash tests. I have a general idea on how to automate this, but I'll open another issue for that. So I think this is good to merge now 🚀 |
I'm pretty sure this is because all those
If this was guaranteed (the wording doesn't seem like it is), we wouldn't need the debounce, because a schema migration could just be run in a single transaction. I guess in most cases it would be, anyways. |
@wolfgangwalther Nice catch! I missed that one. (For reference: https://www.postgresql.org/docs/current/sql-notify.html. ctrl +f Since the debounce was easy to set up - it didn't require an extra lib and just a few lines of code - I guess we can keep it to be on the safe side. But the pg doc reference could be noted as a comment on the code. |
Fixes PostgREST#1512 Helps on environments where you can't send unix signals(Windows, managed containers). Also provides better UX for schema reloads - NOTIFY can be sent from pg clients(psql, pgadmin). `NOTIFY pgrst` - with no payload - should be done to reload the schema cache. Notifications with a payload will be ignored. The channel can be enabled with `db-channel-enabled`(false by default) and its name can be configured with `db-channel`. The LISTEN thread uses a dedicated pg connection. This connection is recovered if it fails. A debounce of 1ms is done in case too many NOTIFYs arrive.
Fixes #1512
Helps on environments where you can't send unix signals(Google Cloud managed
containers, Windows). Also provides better UX for schema reloads - NOTIFY
can be sent from pg clients(psql, pgadmin).
By default,
NOTIFY pgrst
- with no payload - should be done to reloadthe schema cache. Notifications with a payload will be ignored.
There's limitation for now, if the LISTEN connection is lost theconnection won't be retried.
Edits:
The LISTEN thread uses a dedicated pg connection. This connection is recovered if it fails. A debounce of 1ms is done in case too many NOTIFYs arrive.
The channel can be enabled with
db-channel-enabled
(false by default) and its name can be configured withdb-channel
("pgrst" by default).