Skip to content
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

Is it safe to use the same connection to perform statements and LISTEN to notifications? #116

Open
tomwassing opened this issue May 16, 2022 · 3 comments

Comments

@tomwassing
Copy link

Hi,

I am trying to reduce the amount of connection to a Postgres database, as many libraries open a new connection when trying to LISTEN to a channel. I have the idea that pgconn supports concurrently executing statements and handling notifications by settings the OnNotification handler, however, I'm not entirely sure, am I seeing this correctly?

Thanks!

@tomwassing tomwassing changed the title Is it safe to use the same connection to perform statements and listen to notifications? Is it safe to use the same connection to perform statements and LISTEN to notifications? May 16, 2022
@jackc
Copy link
Owner

jackc commented May 16, 2022

It is safe -- though the handler must be written with care. It can be called during the execution of a query so it is unsafe to do anything with the connection.

If you moved up a layer to pgx then you could take advantage of it automatically buffering notifications received while executing a statement. i.e. You could alternate between executing statements and calling WaitForNotification.

@tomwassing
Copy link
Author

Cool! What would happen in case the connection is waiting for a notification and a statement is executed, would that interfere?

Do you have a suggestion for a good approach to alternate between WaitForNotification and executing a statement? One approach I was thinking about, is to cancel the context of the WaitForNotification (does feel a bit hacky)

@jackc
Copy link
Owner

jackc commented May 17, 2022

Cool! What would happen in case the connection is waiting for a notification and a statement is executed, would that interfere?

In pgx if a notification is received while a statement is executing it will be buffered and returned when WaitForNotification is next called. In pgconn the OnNotification handler is called while the statement is being executed (which is why it takes more care to use properly).

Do you have a suggestion for a good approach to alternate between WaitForNotification and executing a statement? One approach I was thinking about, is to cancel the context of the WaitForNotification (does feel a bit hacky)

Nothing wrong with that. In fact, WaitForNotification checks for buffered notifications before it checks if ctx is cancelled. So you can easily check for notifications that have already been received without waiting at all for new notifications by passing is an already cancelled context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants