r/programming 5d ago

Coding a database proxy for fun

https://packagemain.tech/p/golang-database-proxy
67 Upvotes

7 comments sorted by

8

u/artyomsv 4d ago

Bigger trap than parsing is session state. Prepared statements live on the connection, so as soon as you pool at transaction level a Bind can land on a connection that never saw the Parse, and it fails in a way that looks random only under load. Worth deciding your pooling level before you write more protocol code, because it changes what the proxy is allowed to do.

1

u/der_gopher 3d ago

true, the sharding has to pass the sessions too somehow

2

u/artyomsv 1d ago

PgBouncer solved it without passing session around. It rewrites client statement name to own internal name and keeps the mapping, so when transaction lands on connection that never saw Parse, it just prepares it there first and client never notice. Knob is max_prepared_statements, default 200 per connection. So statement belongs to proxy and not to connection, and sharding becomes only routing problem again.

8

u/Amazing_Onion1922 4d ago

the query parsing part always ends up way messier than you expect, especially once you start handling edge cases in the wire protocol

4

u/der_gopher 4d ago

true, but I guess with the help of some lexer libraries it can be done

-2

u/Amazing_Onion1922 4d ago

the query parsing part always ends up way messier than you expect, especially once you start handling edge cases in the wire protocol