Re: [EXTERNAL] Support load balancing in libpq

2023-03-30 Thread Daniel Gustafsson
> On 30 Mar 2023, at 10:21, Daniel Gustafsson wrote: > >> On 30 Mar 2023, at 10:00, Julien Rouhaud wrote: >> >> On Thu, Mar 30, 2023 at 3:03 PM Daniel Gustafsson wrote: >>> On 30 Mar 2023, at 03:48, Hayato Kuroda (Fujitsu) wrote: >>> While checking the buildfarm, I found a f

Re: [EXTERNAL] Support load balancing in libpq

2023-03-30 Thread Daniel Gustafsson
> On 30 Mar 2023, at 10:00, Julien Rouhaud wrote: > > On Thu, Mar 30, 2023 at 3:03 PM Daniel Gustafsson wrote: >> >>> On 30 Mar 2023, at 03:48, Hayato Kuroda (Fujitsu) >>> wrote: >> >>> While checking the buildfarm, I found a failure on NetBSD caused by the >>> added code[1]: >> >> Thanks

Re: [EXTERNAL] Support load balancing in libpq

2023-03-30 Thread Julien Rouhaud
On Thu, Mar 30, 2023 at 3:03 PM Daniel Gustafsson wrote: > > > On 30 Mar 2023, at 03:48, Hayato Kuroda (Fujitsu) > > wrote: > > > While checking the buildfarm, I found a failure on NetBSD caused by the > > added code[1]: > > Thanks for reporting, I see that lapwing which runs Linux (Debian 7, g

Re: [EXTERNAL] Support load balancing in libpq

2023-03-30 Thread Daniel Gustafsson
> On 30 Mar 2023, at 03:48, Hayato Kuroda (Fujitsu) > wrote: > While checking the buildfarm, I found a failure on NetBSD caused by the added > code[1]: Thanks for reporting, I see that lapwing which runs Linux (Debian 7, gcc 4.7.2) has the same error. I'll look into it today to get a fix comm

RE: [EXTERNAL] Support load balancing in libpq

2023-03-29 Thread Hayato Kuroda (Fujitsu)
Dear Daniel, Jelte Thank you for creating a good feature! While checking the buildfarm, I found a failure on NetBSD caused by the added code[1]: ``` fe-connect.c: In function 'libpq_prng_init': fe-connect.c:1048:11: error: cast from pointer to integer of different size [-Werror=pointer-to-int-c

Re: [EXTERNAL] Support load balancing in libpq

2023-03-29 Thread Daniel Gustafsson
I took another couple of looks at this and pushed it after a few small tweaks to the docs. -- Daniel Gustafsson

Re: [EXTERNAL] Support load balancing in libpq

2023-03-28 Thread Tatsuo Ishii
> I think it's fine to remove it. It originated from postmaster.c, where > I copied the original implementation of libpq_prng_init from. I agree to remove unlikely macro here. Best reagards, -- Tatsuo Ishii SRA OSS LLC English: http://www.sraoss.co.jp/index_en/ Japanese:http://www.sraoss.co.jp

Re: [EXTERNAL] Support load balancing in libpq

2023-03-28 Thread Tatsuo Ishii
>> "unlikely" macro is used in libpq_prng_init() in the patch. I wonder >> if the place is really 'hot' to use "unlikely" macro. > > I don't think it is, I was thinking to rewrite as the below sketch: > > { > if (pg_prng_strong_seed(&conn->prng_state))) > return; > > /* fallback

Re: [EXTERNAL] Support load balancing in libpq

2023-03-28 Thread Jelte Fennema
I think it's fine to remove it. It originated from postmaster.c, where I copied the original implementation of libpq_prng_init from. On Tue, 28 Mar 2023 at 09:22, Daniel Gustafsson wrote: > > > On 28 Mar 2023, at 09:16, Tatsuo Ishii wrote: > > > "unlikely" macro is used in libpq_prng_init() in t

Re: [EXTERNAL] Support load balancing in libpq

2023-03-28 Thread Daniel Gustafsson
> On 28 Mar 2023, at 09:16, Tatsuo Ishii wrote: > "unlikely" macro is used in libpq_prng_init() in the patch. I wonder > if the place is really 'hot' to use "unlikely" macro. I don't think it is, I was thinking to rewrite as the below sketch: { if (pg_prng_strong_seed(&conn->prng_state)))

Re: [EXTERNAL] Support load balancing in libpq

2023-03-28 Thread Tatsuo Ishii
Hi, "unlikely" macro is used in libpq_prng_init() in the patch. I wonder if the place is really 'hot' to use "unlikely" macro. Best reagards, -- Tatsuo Ishii SRA OSS LLC English: http://www.sraoss.co.jp/index_en/ Japanese:http://www.sraoss.co.jp

Re: [EXTERNAL] Support load balancing in libpq

2023-03-27 Thread Aleksander Alekseev
Hi, > I guess you didn't set up the hostnames in /etc/hosts as described in > 004_load_balance_dns.pl. Then it's expected that the loop body isn't > covered. As discussed upthread, running this test manually is much > more cumbersome than is desirable, but it's still better than not > having the t

Re: [EXTERNAL] Support load balancing in libpq

2023-03-27 Thread Jelte Fennema
> > ``` > > if (conn->addr == NULL && conn->naddr != 0) > > ``` Afaict this is not necessary, since getaddrinfo already returns an error if the host could not be resolved to any addresses. A quick test gives me this error: error: could not translate host name "doesnotexist" to address: Name or ser

Re: [EXTERNAL] Support load balancing in libpq

2023-03-27 Thread Aleksander Alekseev
Hi, > ``` > +ret = store_conn_addrinfo(conn, addrlist); > +pg_freeaddrinfo_all(hint.ai_family, addrlist); > +if (ret) > +goto error_return;/* message already logged */ > ``` > The goto path is not test-covered. D'oh, this one is fine since store_conn_addrin

Re: [EXTERNAL] Support load balancing in libpq

2023-03-27 Thread Aleksander Alekseev
Hi, > So I think it should be: > > ``` > if (conn->addr == NULL && conn->naddr != 0) > ``` > > [...] > > I will take a look at v16 now. The code coverage could be slightly better. In v16-0001: ``` +ret = store_conn_addrinfo(conn, addrlist); +pg_freeaddrinfo_all(hint.ai_family, a

Re: [EXTERNAL] Support load balancing in libpq

2023-03-27 Thread Aleksander Alekseev
Hi, > > ▶ 6/6 - received at least one connection on node1 OK > > ▶ 6/6 - received at least one connection on node2 OK > > ▶ 6/6 - received at least one connection on node3 OK > > ▶ 6/6 - received 50 connections across all nodesOK > > Good point. > > > Finally, I changed a

Re: [EXTERNAL] Support load balancing in libpq

2023-03-27 Thread Daniel Gustafsson
> On 27 Mar 2023, at 13:50, Jelte Fennema wrote: > > Looks good overall. I attached a new version with a few small changes: > >> * Changed store_conn_addrinfo to return int like how all the functions >>dealing with addrinfo does. Also moved the error reporting to inside >> there >>whe

Re: [EXTERNAL] Support load balancing in libpq

2023-03-27 Thread Jelte Fennema
Looks good overall. I attached a new version with a few small changes: > * Changed store_conn_addrinfo to return int like how all the functions > dealing with addrinfo does. Also moved the error reporting to inside > there > where the error happened. I don't feel strong about the int

Re: [EXTERNAL] Support load balancing in libpq

2023-03-27 Thread Daniel Gustafsson
> On 17 Mar 2023, at 09:50, Jelte Fennema wrote: > >> The documentation lists the modes disabled and random, but I wonder if it's >> worth expanding the docs to mention that "disabled" is pretty much a round >> robin load balancing scheme? It reads a bit odd to present load balancing >> without

Re: [EXTERNAL] Support load balancing in libpq

2023-03-22 Thread Jelte Fennema
Rebased patch after conflicts with bfc9497ece01c7c45437bc36387cb1ebe346f4d2 v14-0001-libpq-Run-pgindent-after-a9e9a9f32b3.patch Description: Binary data v14-0002-Refactor-libpq-to-store-addrinfo-in-a-libpq-owne.patch Description: Binary data v14-0003-Support-load-balancing-in-libpq.patch Desc

Re: [EXTERNAL] Support load balancing in libpq

2023-03-17 Thread Jelte Fennema
> The documentation lists the modes disabled and random, but I wonder if it's > worth expanding the docs to mention that "disabled" is pretty much a round > robin load balancing scheme? It reads a bit odd to present load balancing > without a mention of round robin balancing given how common it is

Re: [EXTERNAL] Support load balancing in libpq

2023-03-16 Thread Daniel Gustafsson
In general I think this feature makes sense (which has been echoed many times in the thread), and the implementation strikes a good balance of robustness and simplicity. Reading this I think it's very close to being committable, but I have a few comments on the patch series: +sent to the

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-03-15 Thread Jelte Fennema
Rebased On Tue, 14 Mar 2023 at 19:05, Gregory Stark (as CFM) wrote: > > The pgindent run in b6dfee28f is causing this patch to need a rebase > for the cfbot to apply it. v12-0002-Refactor-libpq-to-store-addrinfo-in-a-libpq-owne.patch Description: Binary data v12-0003-Support-load-balancing-in

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-03-14 Thread Gregory Stark (as CFM)
The pgindent run in b6dfee28f is causing this patch to need a rebase for the cfbot to apply it.

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-03-06 Thread Jelte Fennema
Small update. Improved some wording in the docs. On Fri, 3 Mar 2023 at 15:37, Jelte Fennema wrote: > > > I want to note that the Fisher-Yates algorithm is implemented in a > > difficult to understand manner. > > +if (j < i) /* avoid fetching undefined data if j=i */ > > This stuff does not make s

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-03-03 Thread Jelte Fennema
> I want to note that the Fisher-Yates algorithm is implemented in a > difficult to understand manner. > +if (j < i) /* avoid fetching undefined data if j=i */ > This stuff does not make sense in case of shuffling arrays inplace. It > is important only for making a new copy of an array and only in

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-03-02 Thread Andrey Borodin
On Wed, Mar 1, 2023 at 12:03 PM Jelte Fennema wrote: > > done and updated cf entry > Hi Jelte! I've looked into the patch. Although so many improvements can be suggested, It definitely makes sense as-is too. These improvements might be, for example, sorting hosts according to ping latency or som

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-03-01 Thread Jelte Fennema
done and updated cf entry On Wed, 1 Mar 2023 at 20:13, Greg S wrote: > > This patch seems to need a rebase. > > I'll update the status to Waiting on Author for now. After rebasing > please update it to either Needs Review or Ready for Committer > depending on how simple the rebase was and whether

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-03-01 Thread Greg S
This patch seems to need a rebase. I'll update the status to Waiting on Author for now. After rebasing please update it to either Needs Review or Ready for Committer depending on how simple the rebase was and whether there are open questions to finish it.

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-01-26 Thread Jelte Fennema
After discussing this patch privately with Andres here's a new version of this patch. The major differences are: 1. Use the pointer value of the connection as a randomness source 2. Use more precise time as randomness source 3. Move addrinfo changes into a separate commit. This is both to make the

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-01-18 Thread Jelte Fennema
As far as I can tell this is ready for committer feedback now btw. I'd really like to get this into PG16. > It hadn't been my intention to block the patch on it, sorry. Just > registering a preference. No problem. I hadn't looked into the shared PRNG solution closely enough to determine if I thou

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-01-17 Thread Jacob Champion
On Fri, Jan 13, 2023 at 10:44 AM Jacob Champion wrote: > And my thought was that the one-time > initialization could be moved to a place that doesn't need to know the > connection options at all, to make it easier to reason about the > architecture. Say, next to the WSAStartup machinery. (And aft

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-01-13 Thread Jacob Champion
On Fri, Jan 13, 2023 at 9:10 AM Jelte Fennema wrote: > > > Just a quick single-issue review, but I agree with Maxim that having > > one PRNG, seeded once, would be simpler > > I don't agree that it's simpler. Because now there's a mutex you have > to manage, and honestly cross-platform threading i

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-01-13 Thread Jelte Fennema
> Just a quick single-issue review, but I agree with Maxim that having > one PRNG, seeded once, would be simpler I don't agree that it's simpler. Because now there's a mutex you have to manage, and honestly cross-platform threading in C is not simple. However, I attached two additional patches tha

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-01-12 Thread Jacob Champion
On Wed, Sep 14, 2022 at 7:54 AM Maxim Orlov wrote: > For the patch itself, I think it is better to use a more precise time > function in libpq_prng_init or call it only once. > Thought it is a special corner case, imagine all the connection attempts at > first second will be seeded with the save

Re: [EXTERNAL] Re: Support load balancing in libpq

2023-01-09 Thread Jelte Fennema
Attached an updated patch which should address your feedback and I updated the commit message. > I wonder whether making the parameter a boolean will paint us into a > corner I made it a string option, just like target_session_attrs. I'm pretty sure a round-robin load balancing policy could be i

Re: Support load balancing in libpq

2023-01-06 Thread Michael Banck
6 +0200 > Subject: [PATCH v5] Support load balancing in libpq > > Load balancing connections across multiple read replicas is a pretty > common way of scaling out read queries. There are two main ways of doing > so, both with their own advantages and disadvantages: > 1. Load balanc

Re: Support load balancing in libpq

2022-11-29 Thread Jelte Fennema
Attached is a new version with the tests cleaned up a bit (more comments mostly). @Michael, did you have a chance to look at the last version? Because I feel that the patch is pretty much ready for a committer to look at, at this point. v5-0001-Support-load-balancing-in-libpq.patch Description

Re: Support load balancing in libpq

2022-10-03 Thread Jelte Fennema
I attached a new patch which does the following: 1. adds tap tests 2. adds random_seed parameter to libpq (required for tap tests) 3. frees conn->loadbalance in freePGConn 4. add more expansive docs on the feature its behaviour Apart from bike shedding on the name of the option I think it's pretty

Re: [EXTERNAL] Re: Support load balancing in libpq

2022-09-17 Thread Michael Banck
Hi, On Mon, Sep 12, 2022 at 02:16:56PM +, Jelte Fennema wrote: > Attached is an updated patch with the following changes: > 1. rebased (including solved merge conflict) > 2. fixed failing tests in CI > 3. changed the commit message a little bit > 4. addressed the two remarks from Micheal > 5.

Re: [EXTERNAL] Re: Support load balancing in libpq

2022-09-17 Thread Michael Banck
Hi, On Wed, Sep 14, 2022 at 05:53:48PM +0300, Maxim Orlov wrote: > > Also, IMO, the solution must have a fallback mechanism if the > > standby/chosen host isn't reachable. > > Yeah, I think it should. I'm not insisting on a particular name of options > here, but in my view, the overall idea may b

Re: [EXTERNAL] Re: Support load balancing in libpq

2022-09-14 Thread Maxim Orlov
+1 for overall idea of load balancing via random host selection. For the patch itself, I think it is better to use a more precise time function in libpq_prng_init or call it only once. Thought it is a special corner case, imagine all the connection attempts at first second will be seeded with the

Re: [EXTERNAL] Re: Support load balancing in libpq

2022-09-12 Thread Jelte Fennema
Attached is an updated patch with the following changes: 1. rebased (including solved merge conflict) 2. fixed failing tests in CI 3. changed the commit message a little bit 4. addressed the two remarks from Micheal 5. changed the prng_state from a global to a connection level value for thread-saf

Re: Support load balancing in libpq

2022-09-10 Thread Michael Banck
Hi, the patch no longer applies cleanly, please rebase (it's trivial). I don't like the provided commit message very much, I think the discussion about pgJDBC having had load balancing for a while belongs elsewhere. On Wed, Jun 22, 2022 at 07:54:19AM +, Jelte Fennema wrote: > I tried to stay

RE: Support load balancing in libpq

2022-07-27 Thread kuroda.hay...@fujitsu.com
Dear Jelte, > With plain Postgres this assumption is probably correct. But the main reason > I'm interested in this patch was because I would like to be able to load > balance across the workers in a Citus cluster. These workers are all > primaries. > Similar usage would likely be possible with B

Re: Support load balancing in libpq

2022-07-15 Thread Jelte Fennema
> we can assume that one of members is a primary and others are secondary. With plain Postgres this assumption is probably correct. But the main reason I'm interested in this patch was because I would like to be able to load balance across the workers in a Citus cluster. These workers are all prim

RE: Support load balancing in libpq

2022-07-14 Thread kuroda.hay...@fujitsu.com
Dear Jelte, I like your idea. But do we have to sort randomly even if target_session_attr is set to 'primary' or 'read-write'? I think this parameter can be used when all listed servers have same data, and we can assume that one of members is a primary and others are secondary. In this case use

Re: [EXTERNAL] Re: Support load balancing in libpq

2022-07-05 Thread Jelte Fennema
> I'm quoting a previous attempt by Satyanarayana Narlapuram on this > topic [1], it also has a patch set. Thanks for sharing that. It's indeed a different approach to solve the same problem. I think my approach is much simpler, since it only requires minimal changes to the libpq client and none

Re: Support load balancing in libpq

2022-07-05 Thread Bharath Rupireddy
On Fri, Jun 10, 2022 at 10:01 PM Jelte Fennema wrote: > > Load balancing connections across multiple read replicas is a pretty > common way of scaling out read queries. There are two main ways of doing > so, both with their own advantages and disadvantages: > 1. Load balancing at the client level

Re: Support load balancing in libpq

2022-06-22 Thread Jelte Fennema
I tried to stay in line with the naming of this same option in JDBC and Npgsql, where it's called "loadBalanceHosts" and "Load Balance Hosts" respectively. So, actually to be more in line it should be the option for libpq should be called "load_balance_hosts" (not "loadbalance" like in the previo

Re: Support load balancing in libpq

2022-06-21 Thread Aleksander Alekseev
Hi Jelte, > Load balancing connections across multiple read replicas is a pretty > common way of scaling out read queries. There are two main ways of doing > so, both with their own advantages and disadvantages: > 1. Load balancing at the client level > 2. Load balancing by connecting to an interm

Support load balancing in libpq

2022-06-10 Thread Jelte Fennema
Load balancing connections across multiple read replicas is a pretty common way of scaling out read queries. There are two main ways of doing so, both with their own advantages and disadvantages: 1. Load balancing at the client level 2. Load balancing by connecting to an intermediary load balancer