Skip to content

Add per connection support for OAuth 2.0 with PostgreSQL 18#700

Open
larskanis wants to merge 8 commits intoged:masterfrom
larskanis:oauth
Open

Add per connection support for OAuth 2.0 with PostgreSQL 18#700
larskanis wants to merge 8 commits intoged:masterfrom
larskanis:oauth

Conversation

@larskanis
Copy link
Collaborator

This is based on #693, but changed it to a hook which is set as a connection parameter.

I plan to make it based on st_table instead of handling in ruby.
That should make it Ractor compatible.
Let's see what looks better...

jeltz and others added 7 commits February 11, 2026 14:21
Since the buitt-in OAuth hooks in libpq can return timerfd and not jsut
a socket when you ask for the current file descriptor we are waiting on
we need to make sure to use the right Ruby class to wrap the file
descriptor, if it is not a valid socket we should use IO.
Async is not supported yet,
To avoid overly complicated or duplicated definitions.
This allows to pass the OAuth request to the related PG::Connection object.

The intention of this change is to prepare for a connection local hook.

Another option would be to wrap the PGconn address in the callback into a regular PG::Connection object.
But this is difficult to combine with the garbage collector.
In the end a wrapped object isn't needed, since the PG::Connection is already present after `PG::Connection.connect_start`.
This is before the hook is called.
So in the callback it's enough to compare the PGconn addresses.
The per connection hook works only in async mode, but sync API in ruby-pg is documented as for testing only and they have several flaws already.
So I don't think there is any need to support them with the OAuth hook.
Therefore on sync API the only supported option is non-hooked OAuth.

This moves common code to helpers and hooked OAuth to async specs.

Connecting with a OAuth hook within a Ractor is currently also not possible, but might be changed in future.
@larskanis larskanis force-pushed the oauth branch 2 times, most recently from b4ac6df to 260c964 Compare February 14, 2026 19:02
This doesn't expose the global function PQsetAuthDataHook to ruby, but only the one per connection.
The conversion of "PGconn *" -> "PG::Connection object" is no longer done per WeakMap, but per st_table.
That should make it easier to get Ractor compatible.
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

Successfully merging this pull request may close these issues.

2 participants