tlocke / pg8000

A Pure-Python PostgreSQL Driver
BSD 3-Clause "New" or "Revised" License
515 stars 46 forks source link

Allow setting arbitrary init parameters #161

Closed benesch closed 1 month ago

benesch commented 1 month ago

Introduce a new init_params option that allows setting startup parameters outside of the parameters that pg8000 has dedicated support for setting.

tlocke commented 1 month ago

Hi @benesch, thanks for your patch. Looking at the spec for the PostgreSQL protocol I think we cover all the init params as individual parameters, so what's the motivation behind this new init_params?

benesch commented 1 month ago

Hi @benesch, thanks for your patch. Looking at the spec for the PostgreSQL protocol I think we cover all the init params as individual parameters, so what's the motivation behind this new init_params?

Hey @tlocke, thanks for taking a look! The PostgreSQL protocol allows the StartupMessage to take arbitrary parameters—basically anything that can be SET on the session:

In addition to the above, other parameters may be listed. Parameter names beginning with pq. are reserved for use as protocol extensions, while others are treated as run-time parameters to be set at backend start time. Such settings will be applied during backend start (after parsing the command-line arguments if any) and will act as session defaults.

To be transparent, we're looking to use this new option at @MaterializeInc to do some testing of how our PostgreSQL-compatible server handles certain startup options. But I've been working with standard PostgreSQL as well for many years now and have found this functionality to be useful in the other PostgreSQL drivers I've worked with. Being able to set these parameters in the startup message is more convenient than having to issue a standalone SET command for each parameter, and more importantly reduces the number of network roundtrips required to establish a connection with a particular set of session parameters.

tlocke commented 1 month ago

That's a fair point, so I've made a few tweaks and accepted your patch at https://github.com/tlocke/pg8000/commit/9d7b1f46e79c9d2ee7557a1d4cd07feddfea87ea

benesch commented 1 month ago

Ah that's fantastic—thanks very much, @tlocke!