Skip to content

Commit bb27586

Browse files
authored
Reset instead of discard all (#549)
* Use reset all instead of discard all * Move 'X' handling to before admin handle * fix tests
1 parent 4f0f45b commit bb27586

File tree

3 files changed

+35
-26
lines changed

3 files changed

+35
-26
lines changed

src/client.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,14 @@ where
821821
message_result = read_message(&mut self.read) => message_result?
822822
};
823823

824+
if message[0] as char == 'X' {
825+
debug!("Client disconnecting");
826+
827+
self.stats.disconnect();
828+
829+
return Ok(());
830+
}
831+
824832
// Handle admin database queries.
825833
if self.admin {
826834
debug!("Handling admin command");
@@ -940,14 +948,6 @@ where
940948
continue;
941949
}
942950

943-
'X' => {
944-
debug!("Client disconnecting");
945-
946-
self.stats.disconnect();
947-
948-
return Ok(());
949-
}
950-
951951
// Close (F)
952952
'C' => {
953953
if prepared_statements_enabled {

src/server.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ impl StreamInner {
107107

108108
#[derive(Copy, Clone)]
109109
struct CleanupState {
110-
/// If server connection requires DISCARD ALL before checkin because of set statement
110+
/// If server connection requires RESET ALL before checkin because of set statement
111111
needs_cleanup_set: bool,
112112

113-
/// If server connection requires DISCARD ALL before checkin because of prepare statement
113+
/// If server connection requires DEALLOCATE ALL before checkin because of prepare statement
114114
needs_cleanup_prepare: bool,
115115
}
116116

@@ -296,7 +296,7 @@ pub struct Server {
296296
/// Is the server broken? We'll remote it from the pool if so.
297297
bad: bool,
298298

299-
/// If server connection requires DISCARD ALL before checkin
299+
/// If server connection requires reset statements before checkin
300300
cleanup_state: CleanupState,
301301

302302
/// Mapping of clients and servers used for query cancellation.
@@ -982,7 +982,7 @@ impl Server {
982982
// We don't detect set statements in transactions
983983
// No great way to differentiate between set and set local
984984
// As a result, we will miss cases when set statements are used in transactions
985-
// This will reduce amount of discard statements sent
985+
// This will reduce amount of reset statements sent
986986
if !self.in_transaction {
987987
debug!("Server connection marked for clean up");
988988
self.cleanup_state.needs_cleanup_set = true;
@@ -1304,12 +1304,21 @@ impl Server {
13041304
// Client disconnected but it performed session-altering operations such as
13051305
// SET statement_timeout to 1 or create a prepared statement. We clear that
13061306
// to avoid leaking state between clients. For performance reasons we only
1307-
// send `DISCARD ALL` if we think the session is altered instead of just sending
1307+
// send `RESET ALL` if we think the session is altered instead of just sending
13081308
// it before each checkin.
13091309
if self.cleanup_state.needs_cleanup() && self.cleanup_connections {
13101310
info!(target: "pgcat::server::cleanup", "Server returned with session state altered, discarding state ({}) for application {}", self.cleanup_state, self.application_name);
1311-
self.query("DISCARD ALL").await?;
1312-
self.query("RESET ROLE").await?;
1311+
let mut reset_string = String::from("RESET ROLE;");
1312+
1313+
if self.cleanup_state.needs_cleanup_set {
1314+
reset_string.push_str("RESET ALL;");
1315+
};
1316+
1317+
if self.cleanup_state.needs_cleanup_prepare {
1318+
reset_string.push_str("DEALLOCATE ALL;");
1319+
};
1320+
1321+
self.query(&reset_string).await?;
13131322
self.cleanup_state.reset();
13141323
}
13151324

@@ -1336,7 +1345,7 @@ impl Server {
13361345
self.last_activity
13371346
}
13381347

1339-
// Marks a connection as needing DISCARD ALL at checkin
1348+
// Marks a connection as needing cleanup at checkin
13401349
pub fn mark_dirty(&mut self) {
13411350
self.cleanup_state.set_true();
13421351
}

tests/ruby/misc_spec.rb

+10-10
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,15 @@
221221
conn.close
222222
end
223223

224-
it "Does not send DISCARD ALL unless necessary" do
224+
it "Does not send RESET ALL unless necessary" do
225225
10.times do
226226
conn = PG::connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
227227
conn.async_exec("SET SERVER ROLE to 'primary'")
228228
conn.async_exec("SELECT 1")
229229
conn.close
230230
end
231231

232-
expect(processes.primary.count_query("DISCARD ALL")).to eq(0)
232+
expect(processes.primary.count_query("RESET ALL")).to eq(0)
233233

234234
10.times do
235235
conn = PG::connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
@@ -239,7 +239,7 @@
239239
conn.close
240240
end
241241

242-
expect(processes.primary.count_query("DISCARD ALL")).to eq(10)
242+
expect(processes.primary.count_query("RESET ALL")).to eq(10)
243243
end
244244

245245
it "Resets server roles correctly" do
@@ -273,7 +273,7 @@
273273
end
274274
end
275275

276-
it "Does not send DISCARD ALL unless necessary" do
276+
it "Does not send RESET ALL unless necessary" do
277277
10.times do
278278
conn = PG::connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
279279
conn.async_exec("SET SERVER ROLE to 'primary'")
@@ -282,7 +282,7 @@
282282
conn.close
283283
end
284284

285-
expect(processes.primary.count_query("DISCARD ALL")).to eq(0)
285+
expect(processes.primary.count_query("RESET ALL")).to eq(0)
286286

287287
10.times do
288288
conn = PG::connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
@@ -292,7 +292,7 @@
292292
conn.close
293293
end
294294

295-
expect(processes.primary.count_query("DISCARD ALL")).to eq(10)
295+
expect(processes.primary.count_query("RESET ALL")).to eq(10)
296296
end
297297

298298
it "Respects tracked parameters on startup" do
@@ -331,7 +331,7 @@
331331
conn.async_exec("COMMIT")
332332
conn.close
333333
end
334-
expect(processes.primary.count_query("DISCARD ALL")).to eq(0)
334+
expect(processes.primary.count_query("RESET ALL")).to eq(0)
335335

336336
10.times do
337337
conn = PG::connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
@@ -341,7 +341,7 @@
341341
conn.async_exec("COMMIT")
342342
conn.close
343343
end
344-
expect(processes.primary.count_query("DISCARD ALL")).to eq(0)
344+
expect(processes.primary.count_query("RESET ALL")).to eq(0)
345345
end
346346
end
347347

@@ -355,7 +355,7 @@
355355
conn.close
356356

357357
puts processes.pgcat.logs
358-
expect(processes.primary.count_query("DISCARD ALL")).to eq(0)
358+
expect(processes.primary.count_query("RESET ALL")).to eq(0)
359359
end
360360

361361
it "will not clean up prepared statements" do
@@ -366,7 +366,7 @@
366366
conn.close
367367

368368
puts processes.pgcat.logs
369-
expect(processes.primary.count_query("DISCARD ALL")).to eq(0)
369+
expect(processes.primary.count_query("RESET ALL")).to eq(0)
370370
end
371371
end
372372
end

0 commit comments

Comments
 (0)