Subject: [pgjdbc/pgjdbc] issue #3107: JDBC driver sends "SAVEPOINT" query without finishing extended query protocol cycle

From: "tatsuo-ishii (@tatsuo-ishii)" <[email-masked]>
Date: Thu, 01 Feb 2024 11:20:57 +0000

**Describe the issue**
If autosave=always option is specified, JDBC driver sends "SAVEPOINT PGJDBC_AUTOSAVE" in simple query protocol without finishing extended query protocol cycle.
This is not recommended. A an extended query protocol cycle should be ended before sending a simple protocol query.
See the discussion in pgsql-hackers: 
https://www.postgresql.org/message-id/20240130.082257.800729082723903188.t-ishii%40sranhm.sra.co.jp

**Driver Version?** 

Not known.

**Java Version?**

Not known.

**OS Version?**

Not known.

**PostgreSQL Version?**

Not known.

**To Reproduce**
Steps to reproduce the behaviour:

Use connection string autosave=always&cleanupSavepoints=true

**Expected behaviour**

The JDBC driver should end the extended query protocol cycle before sending "SAVEPOINT PGJDBC_AUTOSAVE".
I am not familiar with the internal of JDBC driver but in general you could do this by either:

- sending a sync message before sending "SAVEPOINT PGJDBC_AUTOSAVE".

- sending "SAVEPOINT PGJDBC_AUTOSAVE" using an extended query protocol messages.

Probably the latter is safer?

**Logs**

Not available.

---

From: "tatsuo-ishii (@tatsuo-ishii)" <[email-masked]>
Date: Wed, 21 Feb 2024 01:43:48 +0000

The driver version is  42.5.1.

---

From: "vlsi (@vlsi)" <[email-masked]>
Date: Wed, 09 Jul 2025 08:40:46 +0000

Here's the discussion thread on hackers:
* https://www.postgresql.org/message-id/20240130.082257.800729082723903188.t-ishii%40sranhm.sra.co.jp

Note that pgjdbc can't use extended protocol for `savepoint pgjdbc_autosave` since we can't allow "prepared statement doe snot exist" for autosave queries (it defeats the purpose of `autosave`): https://www.postgresql.org/message-id/CAB%3DJe-HqHHqryuebk8MZGLpginRATRXHbP5_XvGG1arKtCWe0g%40mail.gmail.com

At the same time, currently, the error message is pgpool2-specific only. PostgreSQL does not throw such errors yet: https://www.postgresql.org/message-id/CAB%3DJe-HQ0Zwy1QWo2OpRcu3d41XW_o35ZV%3D12cS94%3D4DP6fw1A%40mail.gmail.com

The fix could probably be sending extra sync message before sending a simple query, however:
1) The error is pgpool2-specific only
2) Probably we should inject `sync` messages if we send a simple query before sending an extended one: https://www.postgresql.org/message-id/20240228.210532.1955389974353647116.t-ishii%40sranhm.sra.co.jp
3) We probably need to account for transaction safety as it looks like extra `sync` might close the currently active transaction: https://www.postgresql.org/message-id/CAGECzQSEeZMR%2Bhc9%2BtM%2BiGeOxWEx53VB0WKLyx1EOjt_y%3DNAKg%40mail.gmail.com