pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter J. Holzer <hjp-pgsql@hjp.at>
To: pgsql-general@lists.postgresql.org
Subject: Re: Question: Multiple pg clusters on one server can be reached with the standard port.
Date: Tue, 20 Jun 2023 16:54:14 +0200
Message-ID: <20230620145414.qjkdzctlja3z53u4@hjp.at> (raw)
In-Reply-To: <47751285-8c76-c31f-97a4-5c6e2e7db72e@gmail.com>
References: <0c2dd853-08ec-dbed-00c3-a680ae711d89@proventis.net>
	<71943c04e05c3c13d5cd2b287909f351@weiller.eu>
	<d869925784c9af50aa4379d4ab5ffae2@weiller.eu>
	<c5f9c9b4-7a1f-8a35-406c-d7cc34285bcd@proventis.net>
	<20230619103335.xyagd43uhneducxz@hjp.at>
	<0f467ff1-8cdb-2460-75d6-0e0e4497890f@gmail.com>
	<20230619171536.2kcrdor36rqchicq@hjp.at>
	<47751285-8c76-c31f-97a4-5c6e2e7db72e@gmail.com>

On 2023-06-19 16:09:34 -0500, Ron wrote:
> On 6/19/23 12:15, Peter J. Holzer wrote:
>     On 2023-06-19 07:49:49 -0500, Ron wrote:
>         On 6/19/23 05:33, Peter J. Holzer wrote:
>             So (again, as Francisco already wrote) the best way is probably to write
>             a simple proxy which uses the database (not DNS) name for routing. I
>             seem to remember that nginx has a plugin architecture for protocols so
>             it might make sense to write that as an nginx plugin instead of a
>             standalone server, but that's really a judgement call the programmer has
>             to make. Another possibility would of course be to extend pgbouncer to
>             do what the OP needs.
> 
>         How would this work with JDBC clients?
> 
>     Same as with any other client, I guess. Any reason why it should be
>     different?
> 
> 
> That goes to my ultimate point: why would this work, when the point of a
> database client is to connect to a database instance on a specific port like
> 5432, not connect to a web server.

Consider this scenario:

You have several databases scattered across several hosts and ports:

db1  host1.example.com:5432
db2  host1.example.com:5433
db3  host2.example.com:5432
db4  host3.example.com:5432

Then you have your proxy/gateway/bouncer (whatever you want to call it)
listening on proxy.example.com:5432.

The clients all connect to proxy.example.com:5432.

The proxy does the TLS handshake (if necessary) and reads the first
packet. This contains the database name. The proxy then uses the
database name to look up where that database resides (e.g. for db3 it
gets host2.example.com:5432) opens a connection to that port (plus TLS
handshake, if necessary) and forwards the packet. After that it just has
to forward packets in both directions until the connection is closed.

The client never knows that the databases are actually on different
hosts and/or ports. As far as it is concerned, all the databases are on
proxy.example.com:5432.

There is one caveat: All the database names need to be unique.

Such a proxy should be straightforward to write. It only needs to
understand two requests of postgresql protocol (initiate TLS and
connect). It would be much simpler than e.g. pg_bouncer which has to
know about authentication, transactions, etc.[1]. Depending on the
expected number of parallel connections and throughput you might want to
consider what programming language and concurrency model
(multi-threaded, async, state machine, ...) to use.

        hp

[1] But of course, if pgbouncer already does what you want, don't
    reinvent the wheel.

-- 
   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp@hjp.at         |    -- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |       challenge!"

Attachments:

  signature.asc (application/pgp-signature, 832B) download

view thread (37+ messages)  latest in thread

Message-ID: <20230620145414.qjkdzctlja3z53u4@hjp.at>
Permalink:  ../20230620145414.qjkdzctlja3z53u4@hjp.at/
Also on:    postgresql.org/message-id/20230620145414.qjkdzctlja3z53u4@hjp.at

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: hjp-pgsql@hjp.at
  Subject: Re: Question: Multiple pg clusters on one server can be reached with the standard port.
  In-Reply-To: <20230620145414.qjkdzctlja3z53u4@hjp.at>
  References: <0c2dd853-08ec-dbed-00c3-a680ae711d89@proventis.net> <71943c04e05c3c13d5cd2b287909f351@weiller.eu> <d869925784c9af50aa4379d4ab5ffae2@weiller.eu> <c5f9c9b4-7a1f-8a35-406c-d7cc34285bcd@proventis.net> <20230619103335.xyagd43uhneducxz@hjp.at> <0f467ff1-8cdb-2460-75d6-0e0e4497890f@gmail.com> <20230619171536.2kcrdor36rqchicq@hjp.at> <47751285-8c76-c31f-97a4-5c6e2e7db72e@gmail.com> <20230620145414.qjkdzctlja3z53u4@hjp.at>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox