pgwire icon indicating copy to clipboard operation
pgwire copied to clipboard

Example of using pgwire as a postgres proxy

Open osawyerr opened this issue 1 year ago • 8 comments

Was looking at the examples and there isn't one that actually connects to a real postgres instance in the backend. For example to use pgwire as a proxy to a 'real' postgres server. i.e. maintaining different client sessions and forwarding them to a real postgres instance.

osawyerr avatar Apr 18 '23 16:04 osawyerr

hi @osawyerr , it's possible to write such a proxy but we need to define the purpose of it so we can choose at which layer we will proxy the traffic. For example, we can write a Layer-4 proxy by simply forward any tcp traffic to backend, or a Layer-7 one that understands postgresql's protocol or even sql statements.

If I'm creating a basic Layer-7 example, I will let the proxy to finish the startup and forward further query traffic to backend. I will find time for that.

sunng87 avatar Apr 19 '23 02:04 sunng87

My vote is for Layer-7. Personally, I'd like to be able to fundamentally understand the request, execute the query, apply transforms to the result rows, then hand them back out.

lucasyvas avatar Oct 28 '23 16:10 lucasyvas

I'll 3rd the request for a l7 proxy example, @sunng87 . My use case is an auth n/z aware proxy that can make authorization decisions based on the query. Even something as simple as how to connect to a backend postgres and forward/reply messages back and forth.

batmilkyway avatar Dec 05 '23 02:12 batmilkyway

Let me do this in weekend if everything goes well.

sunng87 avatar Dec 05 '23 05:12 sunng87

Let me do this in weekend if everything goes well.

Thank you, looking forward to it!

batmilkyway avatar Dec 05 '23 20:12 batmilkyway

I have an unfinished proxy example that uses tokio_postgres in SimpleQueryHandler to forward requests to upstream. There is some residual work to convert data format from tokio_postgres to pgwire.

However, for a complete proxy solution, we will need to implement tokio layer for using pgwire data format as client for finer and better control over messages. That's beyond my time budget for this project. I welcome contributors to join the development if you are interested in this feature.

sunng87 avatar Dec 10 '23 12:12 sunng87

I'll +1 this request.

My use case is to intercept every message and validate incoming queries. I wrote a Python version of this that worked by starting two async tasks, one that listened to the server and pushed to the client and another that listened to the client and pushed to the server. Thus I didn't need any sort of state machine.

adriangb avatar Mar 06 '24 14:03 adriangb

We also need this for https://github.com/twentyhq/twenty

I've never tried Replit bounties but thought it could be an occasion to give it a shot... Let's see if it works! https://replit.com/bounties/@felix54/rust-postgres-proxy

FelixMalfait avatar Jul 10 '24 06:07 FelixMalfait