From 11ce5ab087b68fc11f1e1e6f0f4eab8f0d6d62ba Mon Sep 17 00:00:00 2001
From: Garet Halliday <me@garet.holiday>
Date: Wed, 16 Aug 2023 19:25:23 -0500
Subject: [PATCH] better but not great

---
 lib/middleware/middlewares/ps/sync.go | 14 +++++++-------
 pgbouncer.ini                         |  2 +-
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/lib/middleware/middlewares/ps/sync.go b/lib/middleware/middlewares/ps/sync.go
index e4547aec..c6686af0 100644
--- a/lib/middleware/middlewares/ps/sync.go
+++ b/lib/middleware/middlewares/ps/sync.go
@@ -9,11 +9,10 @@ import (
 )
 
 func sync(tracking []strutil.CIString, clientPackets *zap.Packets, c *Client, server zap.ReadWriter, s *Server, name strutil.CIString) {
-	value := c.parameters[name]
-	expected := s.parameters[name]
+	value, hasValue := c.parameters[name]
+	expected, hasExpected := s.parameters[name]
 
 	if value == expected {
-		// TODO(garet) this will send twice if both server and client have it
 		if !c.synced {
 			pkt := zap.NewPacket()
 			packets.WriteParameterStatus(pkt, name.String(), expected)
@@ -22,22 +21,20 @@ func sync(tracking []strutil.CIString, clientPackets *zap.Packets, c *Client, se
 		return
 	}
 
-	if slices.Contains(tracking, name) {
+	if hasValue && slices.Contains(tracking, name) {
 		if err := backends.SetParameter(&backends.Context{}, server, name, value); err != nil {
 			panic(err) // TODO(garet)
 		}
 		if s.parameters == nil {
 			s.parameters = make(map[strutil.CIString]string)
 		}
-		s.parameters[name] = value
-	} else {
+	} else if hasExpected {
 		pkt := zap.NewPacket()
 		packets.WriteParameterStatus(pkt, name.String(), expected)
 		clientPackets.Append(pkt)
 		if c.parameters == nil {
 			c.parameters = make(map[strutil.CIString]string)
 		}
-		c.parameters[name] = value
 	}
 }
 
@@ -50,6 +47,9 @@ func Sync(tracking []strutil.CIString, client zap.ReadWriter, c *Client, server
 	}
 
 	for name := range s.parameters {
+		if _, ok := c.parameters[name]; ok {
+			continue
+		}
 		sync(tracking, pkts, c, server, s, name)
 	}
 
diff --git a/pgbouncer.ini b/pgbouncer.ini
index 118c7214..6dc48900 100644
--- a/pgbouncer.ini
+++ b/pgbouncer.ini
@@ -1,5 +1,5 @@
 [pgbouncer]
-pool_mode = session
+pool_mode = transaction
 auth_file = userlist.txt
 listen_addr = *
 
-- 
GitLab