Proposal to allow setting cursor options on Portals
Proposal to allow setting cursor options on Portals
DECLARE liahona CURSOR WITH HOLD FOR SELECT * FROM films;
Re: Proposal to allow setting cursor options on Portals
Hi, I did not look into this patch in detail yet, but I am +1 for being able to create cursors at the protocol level. I think this should be allowed for regular cursors as well. One big use-case I see is allowing postgres_fdw to create and fetch from cursors at the protocol level rather than SQL (DECLARE CURSOR, FETCH, etc.) -- Sami Imseih Amazon Web Services (AWS)
Re: Proposal to allow setting cursor options on Portals
On Sun, 7 Dec 2025 at 15:38, Dave Cramer wrote: > My main driver here is to allow the creation of Holdable portals at the protocol level for drivers. Overall seems like a sensible feature to want. A somewhat random collection of thoughts: 1. We still have fairly limited experience with protocol options, so afaik not everyone agrees what we should use a version bump for vs a protocol extension. 2. I think I like the idea of optional fields that a client can add to the existing messages. That way "implementing" the new protocol version is a no-op for clients. 3. I think we should mark optional fields more clearly in the docs somehow. e.g. Make the docs say Optional Int32 and explain what Optional means in the "Message Data Types" section. 4. I think the server should be strict that it only receives this optional field for the expected protocol version. 5. Do we really need to add the CURSOR_BINARY flag? Seems confusing with our other way of indicating binary support, i.e. what does it mean to say text as the format code but then specify CURSOR_BINARY. 6. What is the benefit of PQsendQueryPreparedWithCursorOptions? I understand the use case for PQsendBindWithCursorOptions, but not for PQsendQueryPreparedWithCursorOptions. 7. The server should check that no unknown flags are passed 8. Docs need to be added for the new libpq function(s) I have one question about your intended usage: I expect you intend to make using this opt-in for the users of pgjdbc? (i.e. by using some flag/different method to use this HOLD behaviour)
Re: Proposal to allow setting cursor options on Portals
On Sun, 7 Dec 2025 at 15:38, Dave Cramer <davecramer@gmail.com> wrote:
> My main driver here is to allow the creation of Holdable portals at the protocol level for drivers.
Overall seems like a sensible feature to want. A somewhat random
collection of thoughts:
1. We still have fairly limited experience with protocol options, so
afaik not everyone agrees what we should use a version bump for vs a
protocol extension.
2. I think I like the idea of optional fields that a client can add to
the existing messages. That way "implementing" the new protocol
version is a no-op for clients.
3. I think we should mark optional fields more clearly in the docs
somehow. e.g. Make the docs say <term>Optional Int32</term> and
explain what Optional means in the "Message Data Types" section.
4. I think the server should be strict that it only receives this
optional field for the expected protocol version.
5. Do we really need to add the CURSOR_BINARY flag? Seems confusing
with our other way of indicating binary support, i.e. what does it
mean to say text as the format code but then specify CURSOR_BINARY.
6. What is the benefit of PQsendQueryPreparedWithCursorOptions? I
understand the use case for PQsendBindWithCursorOptions, but not for
PQsendQueryPreparedWithCursorOptions.
7. The server should check that no unknown flags are passed
8. Docs need to be added for the new libpq function(s)
I have one question about your intended usage: I expect you intend to
make using this opt-in for the users of pgjdbc? (i.e. by using some
flag/different method to use this HOLD behaviour)
Re: Proposal to allow setting cursor options on Portals
On Mon, 8 Dec 2025 at 23:08, Dave Cramer wrote: > Thx for the comments. One more comment: It would be good to enable tracing[1][2] for your test, especially because I think you still need to update the tracing logic in libpq for your new packet type. [1]: https://github.com/postgres/postgres/blob/f00484c170f56199c3eeacc82bd72f8c1e3baf6b/src/test/modules/libpq_pipeline/README#L29-L34 [2]: https://github.com/postgres/postgres/blob/f00484c170f56199c3eeacc82bd72f8c1e3baf6b/src/test/modules/libpq_pipeline/t/001_libpq_pipeline.pl#L39-L42
Re: Proposal to allow setting cursor options on Portals
On Mon, Dec 8, 2025 at 1:43 PM Jelte Fennema-Nio wrote: > 1. We still have fairly limited experience with protocol options, so > afaik not everyone agrees what we should use a version bump for vs a > protocol extension. I think it'd be helpful for proposals to describe why a minor version bump was chosen over a protocol extension parameter (or vice versa), so that we can begin to develop some consensus. To me, the conversation on the wire for this feature seems perfect for an extension parameter: "Hello server, do you support this optional thing in this one message type? If not, let me know." Especially since the optional thing is itself an extensible bitmap! With the minor-version strategy, if we added new bits in 3.6, clients who just wanted those new bits would then have to implement support for every feature in versions 3.4, 3.5, and 3.6 just to improve that one use case, and that incentive mismatch leads to more ossification IMO. = Soapbox Follows = I've talked about it face-to-face with people, but to go on the public record: I don't think this is a wise use of a minor version upgrade strategy. I prefer protocol architectures that introduce separate extensions first, then periodically bundle the critical and highly-used extensions into a new minor version once they're sure that _everyone_ should support those things. I know that 3.2 didn't do that. My view of 3.2 is that it was a big compromise to get some things unstuck, so overall I'm glad we have it -- but now that we have it, I'd rather that 3.next be more intentional. Plus I think it's unwise to introduce a 3.3 before we're confident that 3.2 can be widely deployed, and I'm trying to put effort into the latter for 19, so that I'm not just sitting here gatekeeping. IETF has a bunch of related case studies [1,2,3] that might be useful reading, even if we decide that their experience differs heavily from ours. --Jacob [1] https://www.rfc-editor.org/rfc/rfc5218 [2] https://www.rfc-editor.org/rfc/rfc8170 [3] https://www.rfc-editor.org/rfc/rfc9170
Re: Proposal to allow setting cursor options on Portals
On Wed, 10 Dec 2025 at 18:41, Jacob Champion wrote: > I think it'd be helpful for proposals to describe why a minor version > bump was chosen over a protocol extension parameter (or vice versa), > so that we can begin to develop some consensus. Agreed. > With the > minor-version strategy, if we added new bits in 3.6, clients who just > wanted those new bits would then have to implement support for every > feature in versions 3.4, 3.5, and 3.6 just to improve that one use > case, and that incentive mismatch leads to more ossification IMO. I think in this optional bitmap field case, there's no work for the client to "implement" it. It can simply request 3.3, but not send the bitmap field. Similarly for my proposed GoAway message, a client can simply ignore that message completely when it receives it. If we keep the features that are bundled with a protocol version bump of the kind where a client, either has to do nothing to implement it, or at worst has to ignore the contents of a new message/field. Then implementing support becomes so trivial for clients that I don't think it'd be a hurdle for client authors to implement support for 3.3, 3.4, 3.5 and if they only wanted a feature from the 3.6 protocol.^1 I'll call these things "no-op implementations" from now on. > I've talked about it face-to-face with people, but to go on the public > record: I don't think this is a wise use of a minor version upgrade > strategy. I prefer protocol architectures that introduce separate > extensions first, then periodically bundle the critical and > highly-used extensions into a new minor version once they're sure that > _everyone_ should support those things. I think we disagree on this. I think the downside of using protocol extensions for everything is that we then end up with N*N different combinations of features in the wild that servers and clients need to deal with. We have to start to define what happens when features interact, but either of them is not enabled. With incrementing versions you don't have that problem, which results in simpler logic in the spec, servers and clients. Finally, because we don't have any protocol extensions yet. All clients still need to build infrastructure for them, including libpq. So I'd argue that if we make such "no-op implementation" features use protocol extensions, then it'd be more work for everyone. > I know that 3.2 didn't do that. My view of 3.2 is that it was a big > compromise to get some things unstuck, so overall I'm glad we have it > -- but now that we have it, I'd rather that 3.next be more > intentional. > Plus I think it's unwise to introduce a 3.3 before we're > confident that 3.2 can be widely deployed, and I'm trying to put > effort into the latter for 19, so that I'm not just sitting here > gatekeeping. I'm not sure what you mean with this. People use libpq18 and PG18, and I've heard no complaints about protocol problems. So I think it was a success. Do you mean widely deployed by default? Why exactly does that matter for 3.3? Anything that stands default deployment in the way for 3.2, will continue to stand default deployment in the way for 3.3. Personally, if we flip the default in e.g. 5 years from now. I'd much rather have it be flipped to a very nice 3.6 protocol, than still only having the single new feature that was added in 3.2. > IETF has a bunch of related case studies [1,2,3] that might be useful > reading, even if we decide that their experience differs heavily from > ours. I gave them a skim and they seem like a good read (which I'll do later). But I'm not sure part of them you thought was actionable for the discussion about version bumps vs protocol extensions. (I did see useful stuff for the grease thread, but that seems better to discuss there) ^1: You and I only talked about clients above, but obviously there's also proxies and other servers that implement the protocol to consider. If a feature that is "no-op implementation" on the client is a complicated implementation on the proxy/server then maybe a protocol extension is indeed the better choice. I think for GoAway it's trivial to "no-op implement" too on the proxy/server. For this cursor option proposal it's less clear cut imo. Proxies can probably simply forward the message to the server, although maybe PgBouncer would want to throw an error when a client uses a hold cursor (but it also doesn't do that for SQL level hold cursors, so that seems like an optional enhancement). Other servers might not even support hold cursors, but then they could simply throw a clear error (like pgbouncer would do). If throwing an error is an acceptable server implementation, then I think a "no-op implementation" is again trivial.
Re: Proposal to allow setting cursor options on Portals
[I considered splitting this off into a new thread, but I think Dave has to wait for it to be resolved before much can happen with the patch. Sorry Dave.] On Wed, Dec 10, 2025 at 3:01 PM Jelte Fennema-Nio wrote: > If we keep the features that are bundled with a protocol version bump > of the kind where a client, either has to do nothing to implement it, > or at worst has to ignore the contents of a new message/field. Then > implementing support becomes so trivial for clients that I don't think > it'd be a hurdle for client authors to implement support for 3.3, 3.4, > 3.5 and if they only wanted a feature from the 3.6 protocol.^1 I'll > call these things "no-op implementations" from now on. It's too late for that, isn't it? 3.2's only feature doesn't work that way (and couldn't have been designed that way, as far as I can tell). So I don't have any confidence that all future features will fall in line with this new rule. NegotiateProtocolVersion is the only in-band tool we have to ratchet the protocol forward. Why go through all this pain of getting NPV packets working, only to immediately limit its power to the most trivial cases? > I think we disagree on this. I think the downside of using protocol > extensions for everything is that we then end up with N*N different > combinations of features in the wild that servers and clients need to > deal with. We have to start to define what happens when features > interact, but either of them is not enabled. In the worst case? Yes. (That worst case doesn't really bother me. Many other protocols regularly navigate extension combinations.) But! The two extension proposals in flight at the moment -- GoAway and cursor options -- are completely orthogonal, no? Both to each other, and to the functionality in 3.2. There are no combinatorics yet. So it seems strange to optimize for combinatorics out of the gate, by burning through a client-mandatory minor version every year. > With incrementing > versions you don't have that problem, You still have N*M. Implementers have to test each feature of their 3.10 client against server versions 3.0-9, rather than testing against a single server that turns individual extension support on and off. I prefer the latter (but maybe that's just because it's what I'm used to). Middleboxes increase the matrix further, as you point out below. Paradoxically, if all N features happen to be orthogonal, the testing burden for the extension strategy collapses to... N. Minor-version-per-year is worse for that case. > which results in simpler logic > in the spec, servers and clients. I don't want to dissuade a proof of concept for this, because simpler logic everywhere sounds amazing. But it sounds like magical thinking to me. A bit like telling Christoph that the dpkg dependency graph is too complicated, so it should be a straight line instead -- if that worked, presumably everyone would have done it that way, right? Convince me that you're not just ignoring necessary complexity in an attempt to stamp out unnecessary complexity. An example of an established network protocol that follows this same strategy would be helpful. How do their clients deal with the minor-version treadmill? > Finally, because we don't have any protocol extensions yet. All > clients still need to build infrastructure for them, including libpq. For clients still on 3.0 (the vast majority of them), they'd have to add infrastructure for sliding minor version ranges, too. > So I'd argue that if we make such "no-op implementation" features use > protocol extensions, then it'd be more work for everyone. Why advertise a protocol extension if you plan to ignore it? Don't advertise it. Do nothing. That's even less work than retrofitting packet parsers to correctly ignore a byte range when minorversion > X. > > Plus I think it's unwise to introduce a 3.3 before we're > > confident that 3.2 can be widely deployed, and I'm trying to put > > effort into the latter for 19, so that I'm not just sitting here > > gatekeeping. > > I'm not sure what you mean with this. People use libpq18 and PG18, and > I've heard no complaints about protocol problems. So I think it was a > success. Do you mean widely deployed by default? Yes. Or even just "deployed". GitHub shows zero hits outside of the Postgres fork graph. Google's results show that an organization called "cardo" tried max_protocol_version=latest. They had to revert it. :( Time for grease. > Why exactly does that > matter for 3.3? Anything that stands default deployment in the way for > 3.2, will continue to stand default deployment in the way for 3.3. Exactly. Don't you want to make sure that clients in the ecosystem are able to use this _before_ we rev the version again, and again? We don't ever get these numbers back. Like, I'm arguing as hard as I can against the very existence of the treadmill. But if I'm outvoted on that, *please* don't start the treadmill before other people can climb on -- otherwise, they won't be able to give us any feedback at all! > Personally, if we flip the default in e.g. 5 years from now. I'd much > rather have it be flipped to a very nice 3.6 protocol, than still only > having the single new feature that was added in 3.2. Those are not the only two choices. I'd rather we get a bunch of nice features without any flipping at all, if that's possible. It looks possible to me. > > IETF has a bunch of related case studies [1,2,3] that might be useful > > reading, even if we decide that their experience differs heavily from > > ours. > > I gave them a skim and they seem like a good read (which I'll do > later). But I'm not sure part of them you thought was actionable for > the discussion about version bumps vs protocol extensions. (I did see > useful stuff for the grease thread, but that seems better to discuss > there) For this conversation, I'm focused on RFC 8170. Specifically the concepts of incremental transitions and incentive alignment (cost/benefit to individual community members). I view minor-version-per-year as violating both of those principles. It instead focuses on the ease of the people who are most plugged into this mailing list, and who have the most power to change things on a whim. > ^1: You and I only talked about clients above, but obviously there's > also proxies and other servers that implement the protocol to > consider. If a feature that is "no-op implementation" on the client is > a complicated implementation on the proxy/server then maybe a protocol > extension is indeed the better choice. I think for GoAway it's trivial > to "no-op implement" too on the proxy/server. For this cursor option > proposal it's less clear cut imo. Proxies can probably simply forward > the message to the server, although maybe PgBouncer would want to > throw an error when a client uses a hold cursor (but it also doesn't > do that for SQL level hold cursors, so that seems like an optional > enhancement). I think proposals should attempt to answer those questions as a prerequisite to commit, personally. Or at least, we should be moving in that direction, if that's too harsh on the first authors who are trying to get things moving inside the protocol. More generally, it bothers me that we still don't have a clear mental model of middlebox extensibility. We're just retreading the discussions from [1] instead of starting from where we stopped, and that's exhausting for me. (As a reminder: 3.2 broke my testing rig, which relied on implicit assumptions around minor-version extensibility for middleboxes. I didn't speak up until very late, because it was just a testing rig, and I could change it. I should have spoken up immediately, because IIRC, pgpool then broke as well.) > Other servers might not even support hold cursors, but > then they could simply throw a clear error (like pgbouncer would do). > If throwing an error is an acceptable server implementation, then I > think a "no-op implementation" is again trivial. A server is always free to decide at the _application_ layer that it will error out for a particular packet that it can parse at the _network_ layer. But it seems a lot more user-friendly to just decline the protocol bit, if it's directly tied to an application-level feature that isn't implemented. I think we should encourage that when possible; otherwise we've traded protocol fragmentation for application fragmentation. --Jacob [1] https://postgr.es/m/CAGECzQR5PMud4q8Atyz0gOoJ1xNH33g7g-MLXFML1_Vrhbzs6Q%40mail.gmail.com
Re: Proposal to allow setting cursor options on Portals
Let me start with this: I agree with you that both HOLD and GoAway would work well as protocol extensions. And if that's what is needed to get stuff to continue moving in the protocol space, then fine that's what I'll do... But I have some reasons to prefer a protocol version bump at least for GoAway The primary reason I have is that protocol extensions are currently enormously underspecified. Especially in regards to what their values can be and also in regards to their own versioning and compatibility. e.g. if we add _pq_.goaway=true. And later we want to add a field to it, how do we do that? I can see a few options: 1. _pq_.goaway=v2,on (first acceptable value is used, this would need immediate and constant grease) 2. _pq_.goaway=true _pq_.goaway_v2=true (have clients specify both a new and old one and specify how the server should behave it gets both of these) I feel like this requires significant discussion, design and implementation work. I tried to do that in patch 7 and 8 here[2], but those parts of the patchset got very little review and/or feedback. I think a big reason was that the proposal went in a too complex direction, by trying to handle too many of the possible usecases for the protocol extensions. I think as long as we limit the discussion to protocol extensions that don't need to be changed on an active connection using something like SetProtocolParameter and those protocol extensions only have an "on/off" style value, then I think we can make some incremental progress here. This would apply to both GoAway (middleware should just not forward GoAway messages to clients) and Hold (server does not send anything different for this feature). Still this seems like quite a bit more work than "simply" including "no-op implementation" features in a protocol bump. Especially because I think the benefit of protocol parameters for these features is negligible, or even negative because of the secondary reason: The secondary reason is that I'd really like clients to actually support the longer cancel token feature in 3.2. It's not that hard to implement for client authors, but I don't think many users care about it (because the primary beneficiary are server implementers, but those only benefit if there's enough clients that implement it, so chicken and egg). By giving people some extra goodies in 3.3 my hope is that clients will actually implement it. So basically I agree that protocol versions do require some additional work on client author side, but I (selfishly) think that would be a good thing in this case. Because it resolves this chicken and egg problem. To take advice from RFC 8170, I'd like to align incentives better, by having protocol 3.3 contain features that are beneficial for both client and server authors. [2]: https://www.postgresql.org/message-id/CAGECzQRbAGqJnnJJxTdKewTsNOovUt4bsx3NFfofz3m2j-t7tA%40mail.gmail.com -- detailed response below (things I did not respond to I agree with) -- On Thu, 11 Dec 2025 at 20:21, Jacob Champion wrote: > NegotiateProtocolVersion is the only in-band tool we have to ratchet > the protocol forward. Why go through all this pain of getting NPV > packets working, only to immediately limit its power to the most > trivial cases? I think it's a fairly easy test to uphold. To be clear, I'm not saying we should indefinitely limit that power. Eventually we'd probably want to add things that are more difficult to implement for clients (possibly after evaluating them as a protocol extension), but that discussion can be punted to when we get there imo. > So it > seems strange to optimize for combinatorics out of the gate, by > burning through a client-mandatory minor version every year. To me 2 protocol extensions a year is strictly more complexity added than 1 minor version a year. i.e. IF the changes are "no-op implementable", why not group them together in a single identifier. > You still have N*M. Implementers have to test each feature of their > 3.10 client against server versions 3.0-9, rather than testing against > a single server that turns individual extension support on and off. I don't understand this argument. If you can have a single server version that turns protocol extensions on and off, then why couldn't you have a single server version that can turn different protocol versions on and off. > An example of an established network protocol that follows this same > strategy would be helpful. How do their clients deal with the > minor-version treadmill? I agree that it would be helpful, but I'm not sure there's such a network protocol. All protocols I know have infrequent version bumps, which then often results in ossification. So frequent version bumps seem like a good way to avoid that from happening. Using protocol extension for everything might mean we ossify the protocol version (again). > > Finally, because we don't have any protocol extensions yet. All > > clients still need to build infrastructure for them, including libpq. > > For clients still on 3.0 (the vast majority of them), they'd have to > add infrastructure for sliding minor version ranges, too. Yes, but adding infrastructure for both protocol versions (which we already have now) and protocol extensions is even more work. libpq still has no support for protocol parameters. > Yes. Or even just "deployed". GitHub shows zero hits outside of the > Postgres fork graph. Yeah, that's sad, but unsurprising. Almost no-one cares about security and that's the only end-user feature of 3.2. > Google's results show that an organization called "cardo" tried > max_protocol_version=latest. They had to revert it. :( Time for > grease. While I totally agree that we need grease, this case actually involved people that did not update their PgBouncer version to a new enough version that supports NegotiateProtocolVersion. [3] [3]: https://www.cardogis.com/AenderungenIwan7#oktober-2025 > > Why exactly does that > > matter for 3.3? Anything that stands default deployment in the way for > > 3.2, will continue to stand default deployment in the way for 3.3. > > Exactly. Don't you want to make sure that clients in the ecosystem are > able to use this _before_ we rev the version again, and again? We > don't ever get these numbers back. To me not every protocol version needs to be implemented by every client. If 3.2 is never used by anyone in the wild, then half of the world immediately switches to 3.3, and then the other half implements 3.4, then I'll be extremely happy. > I'd rather we get a bunch of nice > features without any flipping at all, if that's possible. It looks > possible to me. Me too, but I don't understand how that would work. Sending protocol extensions is just as much of a breaking change for this ungreased middleware as a protocol version bump. So having libpq request _pq_.bindhold=true by default would also need some flip. > I think proposals should attempt to answer those questions as a > prerequisite to commit, personally. Or at least, we should be moving > in that direction, if that's too harsh on the first authors who are > trying to get things moving inside the protocol. Agreed, but I don't think that has to come from the author necessarily. I'm happy to provide that input on proposals and explain if and why it would be hard for something like pgbouncer or other servers. > More generally, it bothers me that we still don't have a clear mental > model of middlebox extensibility. We're just retreading the > discussions from [1] instead of starting from where we stopped, and > that's exhausting for me. I'm still of the opinion that the requirements for [1] are good enough for middleboxes to handle extensibility. I think those requirements could be extended to allow GoAway too, by adding possibility 3 with "The new message sent by the server can be dropped completely by the middleware to imitate the lower protocol version". Remembering and re-reading the thread and this email, it's unclear to me what your thoughts on this are. > (As a reminder: 3.2 broke my testing rig, which relied on implicit > assumptions around minor-version extensibility for middleboxes. I > didn't speak up until very late, because it was just a testing rig, > and I could change it. I should have spoken up immediately, because > IIRC, pgpool then broke as well.) I'm not sure what exactly you're talking about here. You mean libpq complaining about not receiving a BackendKeyData? If so, I agree that wasn't a great situation. But I don't think it was related to the current protocol being under specified, more than the new feature. > A server is always free to decide at the _application_ layer that it > will error out for a particular packet that it can parse at the > _network_ layer. But it seems a lot more user-friendly to just decline > the protocol bit, if it's directly tied to an application-level > feature that isn't implemented. I think we should encourage that when > possible; otherwise we've traded protocol fragmentation for > application fragmentation. I agree in principle, but does it really matter in practice in the case of Hold in practice? If the network layer does not support it, then really all that the user's application can do is throw an error. Whether that error is thrown by the database/middleware or by the client doesn't matter much in the end I think. The main reason where it would matter is if the client could fall back to something else, but in the case of HOLD that something else would probably be send HOLD with SQL. And any server that would throw an error for protocol based HOLD probably (should) also throw one for application level HOLD. > [1] https://postgr.es/m/CAGECzQR5PMud4q8Atyz0gOoJ1xNH33g7g-MLXFML1_Vrhbzs6Q%40mail.gmail.com
Re: Proposal to allow setting cursor options on Portals
On Mon, Dec 8, 2025 at 1:43 PM Jelte Fennema-Nio <postgres@jeltef.nl> wrote:
> 1. We still have fairly limited experience with protocol options, so
> afaik not everyone agrees what we should use a version bump for vs a
> protocol extension.
I think it'd be helpful for proposals to describe why a minor version
bump was chosen over a protocol extension parameter (or vice versa),
so that we can begin to develop some consensus.
To me, the conversation on the wire for this feature seems perfect for
an extension parameter: "Hello server, do you support this optional
thing in this one message type? If not, let me know." Especially since
the optional thing is itself an extensible bitmap! With the
minor-version strategy, if we added new bits in 3.6, clients who just
wanted those new bits would then have to implement support for every
feature in versions 3.4, 3.5, and 3.6 just to improve that one use
case, and that incentive mismatch leads to more ossification IMO.
= Soapbox Follows =
I've talked about it face-to-face with people, but to go on the public
record: I don't think this is a wise use of a minor version upgrade
strategy. I prefer protocol architectures that introduce separate
extensions first, then periodically bundle the critical and
highly-used extensions into a new minor version once they're sure that
_everyone_ should support those things.
I know that 3.2 didn't do that. My view of 3.2 is that it was a big
compromise to get some things unstuck, so overall I'm glad we have it
-- but now that we have it, I'd rather that 3.next be more
intentional. Plus I think it's unwise to introduce a 3.3 before we're
confident that 3.2 can be widely deployed, and I'm trying to put
effort into the latter for 19, so that I'm not just sitting here
gatekeeping.
IETF has a bunch of related case studies [1,2,3] that might be useful
reading, even if we decide that their experience differs heavily from
ours.
Re: Proposal to allow setting cursor options on Portals
[I considered splitting this off into a new thread, but I think Dave
has to wait for it to be resolved before much can happen with the
patch. Sorry Dave.]
On Wed, Dec 10, 2025 at 3:01 PM Jelte Fennema-Nio <postgres@jeltef.nl> wrote:
> If we keep the features that are bundled with a protocol version bump
> of the kind where a client, either has to do nothing to implement it,
> or at worst has to ignore the contents of a new message/field. Then
> implementing support becomes so trivial for clients that I don't think
> it'd be a hurdle for client authors to implement support for 3.3, 3.4,
> 3.5 and if they only wanted a feature from the 3.6 protocol.^1 I'll
> call these things "no-op implementations" from now on.
It's too late for that, isn't it? 3.2's only feature doesn't work that
way (and couldn't have been designed that way, as far as I can tell).
So I don't have any confidence that all future features will fall in
line with this new rule.
NegotiateProtocolVersion is the only in-band tool we have to ratchet
the protocol forward. Why go through all this pain of getting NPV
packets working, only to immediately limit its power to the most
trivial cases?
> I think we disagree on this. I think the downside of using protocol
> extensions for everything is that we then end up with N*N different
> combinations of features in the wild that servers and clients need to
> deal with. We have to start to define what happens when features
> interact, but either of them is not enabled.
In the worst case? Yes. (That worst case doesn't really bother me.
Many other protocols regularly navigate extension combinations.)
But! The two extension proposals in flight at the moment -- GoAway and
cursor options -- are completely orthogonal, no? Both to each other,
and to the functionality in 3.2. There are no combinatorics yet. So it
seems strange to optimize for combinatorics out of the gate, by
burning through a client-mandatory minor version every year.
> With incrementing
> versions you don't have that problem,
You still have N*M. Implementers have to test each feature of their
3.10 client against server versions 3.0-9, rather than testing against
a single server that turns individual extension support on and off. I
prefer the latter (but maybe that's just because it's what I'm used
to). Middleboxes increase the matrix further, as you point out below.
Paradoxically, if all N features happen to be orthogonal, the testing
burden for the extension strategy collapses to... N.
Minor-version-per-year is worse for that case.
> which results in simpler logic
> in the spec, servers and clients.
I don't want to dissuade a proof of concept for this, because simpler
logic everywhere sounds amazing. But it sounds like magical thinking
to me. A bit like telling Christoph that the dpkg dependency graph is
too complicated, so it should be a straight line instead -- if that
worked, presumably everyone would have done it that way, right?
Convince me that you're not just ignoring necessary complexity in an
attempt to stamp out unnecessary complexity.
An example of an established network protocol that follows this same
strategy would be helpful. How do their clients deal with the
minor-version treadmill?
> Finally, because we don't have any protocol extensions yet. All
> clients still need to build infrastructure for them, including libpq.
For clients still on 3.0 (the vast majority of them), they'd have to
add infrastructure for sliding minor version ranges, too.
> So I'd argue that if we make such "no-op implementation" features use
> protocol extensions, then it'd be more work for everyone.
Why advertise a protocol extension if you plan to ignore it? Don't
advertise it. Do nothing. That's even less work than retrofitting
packet parsers to correctly ignore a byte range when minorversion > X.
> > Plus I think it's unwise to introduce a 3.3 before we're
> > confident that 3.2 can be widely deployed, and I'm trying to put
> > effort into the latter for 19, so that I'm not just sitting here
> > gatekeeping.
>
> I'm not sure what you mean with this. People use libpq18 and PG18, and
> I've heard no complaints about protocol problems. So I think it was a
> success. Do you mean widely deployed by default?
Yes. Or even just "deployed". GitHub shows zero hits outside of the
Postgres fork graph.
Google's results show that an organization called "cardo" tried
max_protocol_version=latest. They had to revert it. :( Time for
grease.
> Why exactly does that
> matter for 3.3? Anything that stands default deployment in the way for
> 3.2, will continue to stand default deployment in the way for 3.3.
Exactly. Don't you want to make sure that clients in the ecosystem are
able to use this _before_ we rev the version again, and again? We
don't ever get these numbers back.
Like, I'm arguing as hard as I can against the very existence of the
treadmill. But if I'm outvoted on that, *please* don't start the
treadmill before other people can climb on -- otherwise, they won't be
able to give us any feedback at all!
> Personally, if we flip the default in e.g. 5 years from now. I'd much
> rather have it be flipped to a very nice 3.6 protocol, than still only
> having the single new feature that was added in 3.2.
Those are not the only two choices. I'd rather we get a bunch of nice
features without any flipping at all, if that's possible. It looks
possible to me.
> > IETF has a bunch of related case studies [1,2,3] that might be useful
> > reading, even if we decide that their experience differs heavily from
> > ours.
>
> I gave them a skim and they seem like a good read (which I'll do
> later). But I'm not sure part of them you thought was actionable for
> the discussion about version bumps vs protocol extensions. (I did see
> useful stuff for the grease thread, but that seems better to discuss
> there)
For this conversation, I'm focused on RFC 8170. Specifically the
concepts of incremental transitions and incentive alignment
(cost/benefit to individual community members).
I view minor-version-per-year as violating both of those principles.
It instead focuses on the ease of the people who are most plugged into
this mailing list, and who have the most power to change things on a
whim.
> ^1: You and I only talked about clients above, but obviously there's
> also proxies and other servers that implement the protocol to
> consider. If a feature that is "no-op implementation" on the client is
> a complicated implementation on the proxy/server then maybe a protocol
> extension is indeed the better choice. I think for GoAway it's trivial
> to "no-op implement" too on the proxy/server. For this cursor option
> proposal it's less clear cut imo. Proxies can probably simply forward
> the message to the server, although maybe PgBouncer would want to
> throw an error when a client uses a hold cursor (but it also doesn't
> do that for SQL level hold cursors, so that seems like an optional
> enhancement).
I think proposals should attempt to answer those questions as a
prerequisite to commit, personally. Or at least, we should be moving
in that direction, if that's too harsh on the first authors who are
trying to get things moving inside the protocol.
More generally, it bothers me that we still don't have a clear mental
model of middlebox extensibility. We're just retreading the
discussions from [1] instead of starting from where we stopped, and
that's exhausting for me.
(As a reminder: 3.2 broke my testing rig, which relied on implicit
assumptions around minor-version extensibility for middleboxes. I
didn't speak up until very late, because it was just a testing rig,
and I could change it. I should have spoken up immediately, because
IIRC, pgpool then broke as well.)
> Other servers might not even support hold cursors, but
> then they could simply throw a clear error (like pgbouncer would do).
> If throwing an error is an acceptable server implementation, then I
> think a "no-op implementation" is again trivial.
A server is always free to decide at the _application_ layer that it
will error out for a particular packet that it can parse at the
_network_ layer. But it seems a lot more user-friendly to just decline
the protocol bit, if it's directly tied to an application-level
feature that isn't implemented. I think we should encourage that when
possible; otherwise we've traded protocol fragmentation for
application fragmentation.
Re: Proposal to allow setting cursor options on Portals
On Sun, 14 Dec 2025 at 13:31, Dave Cramer wrote: >> Exactly. Don't you want to make sure that clients in the ecosystem are >> able to use this _before_ we rev the version again, and again? We >> don't ever get these numbers back. > > Well there are 97 of them. 1 per year is a long time. I don't think Jacob was concerned about the actual numbers running out, but in case he was: it's actually 9997 versions that we still have (9996 after we'd commit the grease proposal[1]). [1]: https://commitfest.postgresql.org/patch/6157/ > FWIW, HOLDABLE cursors are not the only option this enables. It enables all of the other cursor options. As mentioned upthread, I'm not sure BINARY makes sense. For any other options, the protocol docs should specify which ones are allowed and what their bits are. Looking at the DECLARE docs[2]. 1. I think supporting ASENSITVE/INSENSITIVE/SENSITIVE bits is unnecessary, since postgres cursors are always INSENSITIVE. 2. For SCROLL vs NO SCROLL, it would be nice if we could get rid of the intermediate mode where if neither SCROLL or NO SCROLL is specified, it's still SCROLL sometimes. I'm not sure backwards compatibility would allow that, i.e. can you currently sometimes do a BACKWARD scan on a portal created with Bind. I guess we could make it so that if you specify the portal flags, then you have to be explicit abuot specifying SCROLL or NO SCROLL 3. All the flags with no SQL variant probably shouldn't be configurable through the protocol too (e.g. CURSOR_OPT_FAST_PLAN) [2]: https://www.postgresql.org/docs/18/sql-declare.html > Are we concerned with servers that are not compatible with Postgres ? I think there's enough re-implementations of the postgres protocol by other databases that it would be a shame if we didn't even try to consider them, but I wouldn't consider it critical to get it right. Since they can always throw application errors for features they don't support, just like they do now for SQL that they don't support. They can always contribute changes to clients to make using unsupported features opt-in in the rare case where they are not.
Re: Proposal to allow setting cursor options on Portals
On Sun, 14 Dec 2025 at 13:31, Dave Cramer <davecramer@gmail.com> wrote:
>> Exactly. Don't you want to make sure that clients in the ecosystem are
>> able to use this _before_ we rev the version again, and again? We
>> don't ever get these numbers back.
>
> Well there are 97 of them. 1 per year is a long time.
I don't think Jacob was concerned about the actual numbers running
out, but in case he was: it's actually 9997 versions that we still
have (9996 after we'd commit the grease proposal[1]).
[1]: https://commitfest.postgresql.org/patch/6157/
> FWIW, HOLDABLE cursors are not the only option this enables. It enables all of the other cursor options.
As mentioned upthread, I'm not sure BINARY makes sense. For any other
options, the protocol docs should specify which ones are allowed and
what their bits are. Looking at the DECLARE docs[2].
1. I think supporting ASENSITVE/INSENSITIVE/SENSITIVE bits is
unnecessary, since postgres cursors are always INSENSITIVE.
2. For SCROLL vs NO SCROLL, it would be nice if we could get rid of
the intermediate mode where if neither SCROLL or NO SCROLL is
specified, it's still SCROLL sometimes. I'm not sure backwards
compatibility would allow that, i.e. can you currently sometimes do a
BACKWARD scan on a portal created with Bind. I guess we could make it
so that if you specify the portal flags, then you have to be explicit
abuot specifying SCROLL or NO SCROLL
3. All the flags with no SQL variant probably shouldn't be
configurable through the protocol too (e.g. CURSOR_OPT_FAST_PLAN)
[2]: https://www.postgresql.org/docs/18/sql-declare.html
> Are we concerned with servers that are not compatible with Postgres ?
I think there's enough re-implementations of the postgres protocol by
other databases that it would be a shame if we didn't even try to
consider them, but I wouldn't consider it critical to get it right.
Since they can always throw application errors for features they don't
support, just like they do now for SQL that they don't support. They
can always contribute changes to clients to make using unsupported
features opt-in in the rare case where they are not.
Re: Proposal to allow setting cursor options on Portals
On Sun, 14 Dec 2025 at 14:49, Dave Cramer wrote: > Here I was thinking that binary was the one that did make sense. The pgjdbc driver would like the results back in binary, I believe others would as well. I agree drivers would like binary results back, but it's unclear to me how CURSOR_OPT_BINARY is different from setting the result column format codes to an array of a single 1? That should also change all columns to be binary right? > Fair, but from my POV, we are only concerned with Postgres. I would say it's up to the other implementations to deal with incompatibilities. I get what you mean, but I feel like we should at least be concerned with popular ecosystem tools like, pgbouncer and pgpool. But then it quickly becomes an exercise in where we draw the line, what about postgres forks like Yugabyte? Or things very similar like cockroachdb. Both of those are distributed, and probably don't use our LSNs. So as a concrete example, if we add LSNs to the protocol, it would be nice to work with their version too if it's not too much effort. e.g. by specifing a length for the commit id in the protocol instead of forcing it at the protocol level to always be a 64bit integer.
Re: Proposal to allow setting cursor options on Portals
On Sun, 14 Dec 2025 at 14:49, Dave Cramer <davecramer@gmail.com> wrote:
> Here I was thinking that binary was the one that did make sense. The pgjdbc driver would like the results back in binary, I believe others would as well.
I agree drivers would like binary results back, but it's unclear to me
how CURSOR_OPT_BINARY is different from setting the result column
format codes to an array of a single 1? That should also change all
columns to be binary right?
> Fair, but from my POV, we are only concerned with Postgres. I would say it's up to the other implementations to deal with incompatibilities.
I get what you mean, but I feel like we should at least be concerned
with popular ecosystem tools like, pgbouncer and pgpool. But then it
quickly becomes an exercise in where we draw the line, what about
postgres forks like Yugabyte? Or things very similar like cockroachdb.
Both of those are distributed, and probably don't use our LSNs. So as
a concrete example, if we add LSNs to the protocol, it would be nice
to work with their version too if it's not too much effort. e.g. by
specifing a length for the commit id in the protocol instead of
forcing it at the protocol level to always be a 64bit integer.
Re: Proposal to allow setting cursor options on Portals
On Sun, 14 Dec 2025 at 09:04, Jelte Fennema-Nio <postgres@jeltef.nl> wrote:On Sun, 14 Dec 2025 at 14:49, Dave Cramer <davecramer@gmail.com> wrote:
> Here I was thinking that binary was the one that did make sense. The pgjdbc driver would like the results back in binary, I believe others would as well.
I agree drivers would like binary results back, but it's unclear to me
how CURSOR_OPT_BINARY is different from setting the result column
format codes to an array of a single 1? That should also change all
columns to be binary right?Fair point.
> Fair, but from my POV, we are only concerned with Postgres. I would say it's up to the other implementations to deal with incompatibilities.
I get what you mean, but I feel like we should at least be concerned
with popular ecosystem tools like, pgbouncer and pgpool. But then it
quickly becomes an exercise in where we draw the line, what about
postgres forks like Yugabyte? Or things very similar like cockroachdb.
Both of those are distributed, and probably don't use our LSNs. So as
a concrete example, if we add LSNs to the protocol, it would be nice
to work with their version too if it's not too much effort. e.g. by
specifing a length for the commit id in the protocol instead of
forcing it at the protocol level to always be a 64bit integer.It would make sense to be forward looking here in the event that Postgres ever has wider LSN's agreed.
Re: Proposal to allow setting cursor options on Portals
On Tue, Jan 6, 2026 at 4:14 AM Dave Cramer wrote: > Any progress on this ? Well, I'd hoped that you and Jelte would maybe hash out your differences in opinion a bit before I jumped back in. You think extensions are orthogonal -- seemingly negating the primary advantage cited for regular minor bumps? -- but Jelte is optimizing for interrelated features. --Jacob
Re: Proposal to allow setting cursor options on Portals
On Tue, 6 Jan 2026 at 17:17, Jacob Champion wrote: > Well, I'd hoped that you and Jelte would maybe hash out your > differences in opinion a bit before I jumped back in. You think > extensions are orthogonal -- seemingly negating the primary advantage > cited for regular minor bumps? -- but Jelte is optimizing for > interrelated features. I had a quick discord chat with Dave. And we don't disagree much with each other: We both would like to use a version bump for these kinds of very simple to implement features. For an important part because we hope to do multiple of such small changes in a single PG release, so the protocol can actually move forward at a decent speed. Having a single version is only 1 option, while having N protocol extensions a year gives at least N different configurations (if they're all orthogonal, and at worst N*N). In your first email you (Jacob) wrote this: > I prefer protocol architectures that introduce separate > extensions first, then periodically bundle the critical and > highly-used extensions into a new minor version once they're sure that > _everyone_ should support those things. Dave and I both agree that if we create a protocol extension for every tiny feature and then in 3 years include some of them in a protocol bump, that's just a lot more complexity that every client author will have to deal with in the long run.
Re: Proposal to allow setting cursor options on Portals
On Wed, Jan 7, 2026 at 6:48 AM Jelte Fennema-Nio wrote: > I had a quick discord chat with Dave. And we don't disagree much with > each other: We both would like to use a version bump for these kinds > of very simple to implement features. I asked because I'm worried that the strongest technical argument for this strategy is "it's simpler (for us)", outweighing all consideration of downstream consequences. I'm not really on board with that. Dave seems not to be particularly worried about our compatibility with third parties. You seem to be hoping to _force_ clients to update, even if they disagree with you that they need the new features. I think I'm on record as saying these are both bad starting points when making changes to a widely implemented protocol. (If not, now I am.) That combination will burn hard-earned trust and goodwill. > Having a single version is only 1 option, Seems like clients must support 3.0 up to 3.N in practice, and test all of those. If you want a feature in 3.6 and the server says it only supports 3.4, you're speaking 3.4 now. That's still N options. You're saying "well hopefully clients don't actually have to support all of them," but I don't think you gave a reason why that would be okay for a production implementation. Is there an unstated assumption here, that we'll eventually drop support for 3.0 at some point relatively soon? (And then 3.2, and then...) If so, I'd prefer to focus the conversation on that assumption. Because that seems like a complete nonstarter to me, personally. --Jacob
Re: Proposal to allow setting cursor options on Portals
Jacob Champion writes:
> You're saying "well hopefully clients don't actually have to support
> all of them," but I don't think you gave a reason why that would be
> okay for a production implementation. Is there an unstated assumption
> here, that we'll eventually drop support for 3.0 at some point
> relatively soon? (And then 3.2, and then...) If so, I'd prefer to
> focus the conversation on that assumption. Because that seems like a
> complete nonstarter to me, personally.
After we introduced protocol version 3.0, it took us 18 years to drop
support for version 2 (2003..2021). We introduced 3.2 in 2025, so on
the same schedule we won't drop 3.0 support before 2043. If anyone's
thinking that it can happen significantly faster this time around,
I'm here to disabuse you of that notion. Considering how much larger
the Postgres ecosystem is now than it was in 2003, it will probably
take *longer* for everything to be sufficiently on-board for another
hard break.
I'm pretty bemused by this entire discussion. We have a perfectly
good design for handling new protocol features without any hard
protocol break, so I don't understand why people are insisting on
doing things incompatibly when they could be doing them compatibly.
I quote from Robert's commit ae65f6066 (the same one that invented
NegotiateProtocolVersion):
In addition, if the startup packet includes name/value pairs where
the name starts with "_pq_.", assume that those are protocol options,
not GUCs. Include those we don't support (i.e. all of them, at
present) in the NegotiateProtocolVersion message so that the client
knows they were not understood. This makes it possible for the
client to request previously-unsupported features without bumping
the protocol version at all; the client can tell from the server's
response whether the option was understood.
I think that the right way forward is that the protocol version
stays at 3.2 for several decades more, and we implement requests for
individual protocol-level features through the "_pq_." mechanism.
I would not have any use for 3.2 as such at all, except that
asking for that is needed to ensure that the server knows about
NegotiateProtocolVersion.
regards, tom lane
Re: Proposal to allow setting cursor options on Portals
On Thu, 8 Jan 2026 at 01:39, Jacob Champion wrote: > > Having a single version is only 1 option, > > Seems like clients must support 3.0 up to 3.N in practice, and test > all of those. If you want a feature in 3.6 and the server says it only > supports 3.4, you're speaking 3.4 now. That's still N options. I meant 1 per yearly set of protocol features. So if there's M years with protocol features and on average N features per each of those years the amount of different options to consider over time are: For only version bumps: M For completely orthogonal protocol extensions: M*N For completely non-orthogonal options: (M*N)^2 > You're saying "well hopefully clients don't actually have to support > all of them," but I don't think you gave a reason why that would be > okay for a production implementation. Clients don't have to care about every postgres version that ever existed, or every possible proxy in existence. In 5 years, a client author might just say: well I only care about my client being able to connect to supported postgres servers, so if a server downgrades to protocol 3.0 I close the connection. This is a decision for the client author to make and for the client author only. And they don't need to care about any of the other clients in existence at all. Only the servers their client should be able to connect to. > Is there an unstated assumption > here, that we'll eventually drop support for 3.0 at some point > relatively soon? (And then 3.2, and then...) If so, I'd prefer to > focus the conversation on that assumption. Because that seems like a > complete nonstarter to me, personally. No, that's not what I meant. Because postgres has to care about the clients it wants to support. And that's all of them. There will probably be clients supporting only 3.0 for a very long time. So the postgres server will need to be supporting 3.0 for a very long time.
Re: Proposal to allow setting cursor options on Portals
On Thu, 8 Jan 2026 at 03:51, Tom Lane wrote:
> I'm pretty bemused by this entire discussion. We have a perfectly
> good design for handling new protocol features without any hard
> protocol break, so I don't understand why people are insisting on
> doing things incompatibly when they could be doing them compatibly.
The whole point of minor protocol versions is that they don't do a
hard protocol break. 3.2 will not be a hard protocol break, 3.3 won't
be and neither will 3.4
> I quote from Robert's commit ae65f6066 (the same one that invented
> NegotiateProtocolVersion):
The section before is just as worth quoting, because it makes clear
that minor protocol versions are not a hard protocol break.
Previously, any attempt to request a 3.x protocol version other than
3.0 would lead to a hard connection failure, which made the minor
protocol version really no different from the major protocol version
and precluded gentle protocol version breaks. Instead, when the
client requests a 3.x protocol version where x is greater than 0, send
the new NegotiateProtocolVersion message to convey that we support
only 3.0. This makes it possible to introduce new minor protocol
versions without requiring a connection retry when the server is
older.
> I think that the right way forward is that the protocol version
> stays at 3.2 for several decades more, and we implement requests for
> individual protocol-level features through the "_pq_." mechanism.
Is the misunderstanding of 3.3 being a hard protocol break the only
reason you think that? Or do you have some more reasons?
Re: Proposal to allow setting cursor options on Portals
On Thu, 8 Jan 2026 at 01:39, Jacob Champion wrote: > Dave seems not to be particularly worried about our compatibility with > third parties. You seem to be hoping to _force_ clients to update, > even if they disagree with you that they need the new features. I > think I'm on record as saying these are both bad starting points when > making changes to a widely implemented protocol. (If not, now I am.) > That combination will burn hard-earned trust and goodwill. tl;dr I give up, let's do protocol extensions for everything. I've updated my GoAway patch do so[1]. I don't think I can convince you that slightly more forceful push forward that I'm suggesting is worth the gained simplicity (both for us, users and client authors). And I'm starting to get pretty sick of discussing the same points over and over again, without making any progress. So instead of continuing to do so, I'll just agree to disagree with you. If in 5 years, when we have 15 protocol extensions with completely distinct support across clients and proxies instead, and no-one knows what features they can rely on in practice. While we could have had 5 new protocol versions. I'll just think (and probably tell you) "I told you so". But you might just be right, and that won't happen, or even if it does it will somehow be trivial to compare all the compatibility matrices. [1]: https://www.postgresql.org/message-id/flat/DDPQ1RV5FE9U.I2WW34NGRD8Z@jeltef.nl
Re: Proposal to allow setting cursor options on Portals
On Thu, Jan 8, 2026 at 4:46 AM Jelte Fennema-Nio wrote: > tl;dr I give up, let's do protocol extensions for everything. I've > updated my GoAway patch do so[1]. > > I don't think I can convince you that slightly more forceful push > forward that I'm suggesting is worth the gained simplicity (both for > us, users and client authors). And I'm starting to get pretty sick of > discussing the same points over and over again, without making any > progress. So instead of continuing to do so, I'll just agree to > disagree with you. That sounds like the right approach to me. Note that I have also previously expressed my disagreement with the idea of bumping the protocol version regularly. I'm not entirely comfortable with the idea of using protocol extensions for everything, because I really imagined that they would be used for larger features that made a cluster of related changes rather than solitary changes, and that there wouldn't be many of them. If we have lots of them and use them for solitary changes like GoAway, we will eventually end up with a very long list of protocol extensions. I agree with you that this is not great. On the other hand, I also do not think the alternative of bumping the protocol version every year is viable. And even if I did, working in this community means that you sometimes have to defer to a consensus with which you do not personally agree in the interest of getting stuff done. I find that when 2 or 3 people all disagree with the same decision that I've made, it is usually a sign that I am wrong, regardless of how sure I am that I am not wrong. > If in 5 years, when we have 15 protocol extensions with completely > distinct support across clients and proxies instead, and no-one knows > what features they can rely on in practice. While we could have had 5 > new protocol versions. I'll just think (and probably tell you) "I told > you so". But you might just be right, and that won't happen, or even > if it does it will somehow be trivial to compare all the compatibility > matrices. IMHO, one thing upon which this greatly depends is the degree to which all the changes are interdependent. If we end up with an extension for a GoAway message, an extension for column encryption, and an extension for trace IDs, I don't see why the compatibility matrix should be a huge issue. Like, yes, some 3rd-party implementations will support all of those and some will support only some of them, but that's sort of the point. Our own code should work with any combination -- I hope -- because the code should live in pretty separate places. If we end up with extensions for column encryption, column use-of-binary format, and column encoding, well then it's probably going to be quite a mess to keep our stuff working with all combinations. In that case, we need to either bump the version and mandate that you have to support all of them, or have extremely good testing frameworks, or maybe decide not to add all those features. -- Robert Haas EDB: http://www.enterprisedb.com
Re: Proposal to allow setting cursor options on Portals
Robert Haas writes: > That sounds like the right approach to me. Note that I have also > previously expressed my disagreement with the idea of bumping the > protocol version regularly. I'm not entirely comfortable with the idea > of using protocol extensions for everything, because I really imagined > that they would be used for larger features that made a cluster of > related changes rather than solitary changes, and that there wouldn't > be many of them. I kind of doubt that there will ever be many of them, but if we start to feel like there's a lot, we could invent abbreviations: single feature names that clients can ask for that are defined to represent a particular set of older features. But I'd argue that those sets should be groups of related functions, not "whatever random stuff exists as of Postgres 27". I think it'll be highly useful for clients to declare which features they want, rather than leave people wondering exactly which features this client intends to support. regards, tom lane
Re: Proposal to allow setting cursor options on Portals
Robert Haas <robertmhaas@gmail.com> writes:
> That sounds like the right approach to me. Note that I have also
> previously expressed my disagreement with the idea of bumping the
> protocol version regularly. I'm not entirely comfortable with the idea
> of using protocol extensions for everything, because I really imagined
> that they would be used for larger features that made a cluster of
> related changes rather than solitary changes, and that there wouldn't
> be many of them.
I kind of doubt that there will ever be many of them, but if we start
to feel like there's a lot, we could invent abbreviations: single
feature names that clients can ask for that are defined to represent
a particular set of older features. But I'd argue that those sets
should be groups of related functions, not "whatever random stuff
exists as of Postgres 27". I think it'll be highly useful for clients
to declare which features they want, rather than leave people
wondering exactly which features this client intends to support.
regards, tom lane
Re: Proposal to allow setting cursor options on Portals
On Fri, Jan 9, 2026 at 1:20 PM Dave Cramer wrote: > For the particular case of adding the ability to create holdable cursors at the protocol level which is what my patch is concerned with I don't think it's even necessary to bump the protocol or create a protocol extension. > The change in the message is backward compatible and clients only need to know that after version 18 they can use the message to create a holdable cursor. I would argue that this patch is just rectifying an oversight in the original protocol. I'm not sure what I think about the patch itself, but I find myself somewhat in agreement with this logic. If the server is supposed to start sending something different to the client, or the client must send something different to the server, that's clearly got to be negotiated. But I wonder whether we should just consider slipping things like this into the protocol without bumping the version at all. That makes me a bit nervous because it kind of makes a mockery of the idea of a version number, but it's also not entirely without precedent. For example, COPY BOTH mode didn't use to exist, and now it does, and the protocol version number didn't change in the process. -- Robert Haas EDB: http://www.enterprisedb.com
Re: Proposal to allow setting cursor options on Portals
On Wed, 14 Jan 2026 at 20:24, Robert Haas wrote: > But I wonder whether we should just consider slipping > things like this into the protocol without bumping the version at all. I feel like I've said this many times already, but I really do not understand why there's such a hesitation on bumping the minor protocol version. Bumping the minor protocol version has zero downsides to me. IMO we could bump it every PG release even if we don't make any changes to the protocol. Nothing would break. The only thing that breaks things is requesting anything other than 3.0, because most proxies haven't implemented NegotiateProtocolVersion. But hopefully that will be a concern of the past once Jacob merges the grease stuff[1]. After that it doesn't matter what minor version is requested, if it's 3.2, 3.3, or 3.456. Requesting any of those will not cause breakage. > That makes me a bit nervous because it kind of makes a mockery of the > idea of a version number, but it's also not entirely without > precedent. For example, COPY BOTH mode didn't use to exist, and now it > does, and the protocol version number didn't change in the process. I think CopyBoth is very special, because it's only used in logical replication. In a sense it's practically a protocol extension, because it is only sent by the server if the client explicitly requested it through a keyword in the StartupMessage (replication=database). As a maintainer of a proxy, I'd be pretty annoyed if clients start sending fields or messages that I don't expect, without *something* in the StartupMessage advertising that that will happen. This specific patch would be fine for pgbouncer, because it only parses the first few fields of the Bind message. But e.g pgcat seems to parse the whole Bind message[2] [1]: https://www.postgresql.org/message-id/flat/DDPR5BPWH1RJ.1LWAK6QAURVAY@jeltef.nl [2]: https://github.com/postgresml/pgcat/blob/5b038813eb14f181434ab7b5509e74d9b1fe123b/src/messages.rs#L1019-L1064 P.S. This *really* is my final on-list message on the topic of bumping protocol version numbers . Off-list I'm happy to explain to people and maybe I'll reconsider re-opening the discussion in a few years. But the current discussion is only going in circles, and I'd rather spend my energy on something that is useful.
Re: Proposal to allow setting cursor options on Portals
Jelte Fennema-Nio writes: > I feel like I've said this many times already, but I really do not > understand why there's such a hesitation on bumping the minor protocol > version. Bumping the minor protocol version has zero downsides to me. I think you have that backwards. The right way to think about it is that bumping the minor version has zero upside. What we actually want is for the client and server to agree on what specific optional features they will use, and we have a design that allows doing that in a fine-grained, extensible way. We don't need to change the protocol version number ever again, as long as we use protocol options correctly. Having said that, I share Robert's distaste for "silent" protocol bumps that change the behavior without any negotiation. regards, tom lane
Re: Proposal to allow setting cursor options on Portals
On Wed, Jan 14, 2026 at 3:12 PM Jelte Fennema-Nio wrote: > As a maintainer of a proxy, I'd be pretty annoyed if clients start > sending fields or messages that I don't expect, without *something* in > the StartupMessage advertising that that will happen. This specific > patch would be fine for pgbouncer, because it only parses the first > few fields of the Bind message. But e.g pgcat seems to parse the whole > Bind message[2] Yeah... I'm pretty skeptical of the phrase "backwards compatible" in the way you've used it, Dave. I think that assumes either that everyone's packet parsers are incredibly lenient, or else that `ERROR: invalid message format` should be considered a compatible response. (This ties into my upthread concern that we don't have a good shared mental model for middlebox compatibility in the protocol.) --Jacob
Re: Proposal to allow setting cursor options on Portals
Jelte Fennema-Nio <postgres@jeltef.nl> writes:
> I feel like I've said this many times already, but I really do not
> understand why there's such a hesitation on bumping the minor protocol
> version. Bumping the minor protocol version has zero downsides to me.
I think you have that backwards. The right way to think about it
is that bumping the minor version has zero upside. What we actually
want is for the client and server to agree on what specific optional
features they will use, and we have a design that allows doing that
in a fine-grained, extensible way. We don't need to change the
protocol version number ever again, as long as we use protocol
options correctly.
Having said that, I share Robert's distaste for "silent" protocol
bumps that change the behavior without any negotiation.
Re: Proposal to allow setting cursor options on Portals
On Thu, Jan 15, 2026 at 5:33 AM Dave Cramer wrote: > I would argue in the case of "cursor with hold" this should have been in the original protocol. > This is not an added feature this just enables an existing feature in the server. This is not unlike widening the cancel key. > Something like encryption would be a feature that I could see using the extension mechanism I agree that encryption is a better fit for the extension mechanism. But I don't really agree that this isn't a new feature. Exposing existing functionality in new ways counts as a new feature in my book. >> Having said that, I share Robert's distaste for "silent" protocol >> bumps that change the behavior without any negotiation. > > My understanding reading his message he was in favour of it Well, my feelings are mixed on that point, honestly. If the server needs to know whether the client supports something, you pretty much have to have a protocol negotiation of some kind, whether that's a version bump or an extension. Otherwise, the server simply has no way to find out what the client supports. In the opposite direction, it's more fuzzy: the client can see the server version number, and so can draw some conclusions on that basis. We have used this for protocol changes in the past. However, something like changing the cancel key or adding cursor with hold affects a lot more clients than adding CopyBoth that is only triggered by a very specific command in a special mode. And it's not that theoretically appealing to conflate the *protocol* version number with the *server* version number. Sometimes things that are not theoretically appealing are nonetheless pragmatically appealing. But it is not really clear to me whether this is one of those cases, and it sounds like Tom and Jacob think it isn't. I could probably be persuaded either way on the best thing to do here in a vacuum, but I'm strongly disinclined to argue against two committers who have a firm position on the topic already. I think what I like least about this proposal is the feeling that we're about to embark on a long slippery slope of changing the protocol a little bit in every new PG version. The cancel key thing is a small change, look here's another. If we just keep doing that, we'll end up with either a lot of minor version bumps or a lot of extensions. I don't foresee a good outcome either way. This is a widely used, widely adopted protocol. The idea that we can just start tweaking it a little bit every year and have nothing bad happened seems wild, regardless of how we do the tweaking. > As for proxies or "middleboxes" I will concede that not advertising that we are going to change that message is a non-starter Yeah. -- Robert Haas EDB: http://www.enterprisedb.com
Re: Proposal to allow setting cursor options on Portals
On Thu, Jan 15, 2026 at 5:33 AM Dave Cramer <davecramer@gmail.com> wrote:
> I would argue in the case of "cursor with hold" this should have been in the original protocol.
> This is not an added feature this just enables an existing feature in the server. This is not unlike widening the cancel key.
> Something like encryption would be a feature that I could see using the extension mechanism
I agree that encryption is a better fit for the extension mechanism.
But I don't really agree that this isn't a new feature. Exposing
existing functionality in new ways counts as a new feature in my book.
>> Having said that, I share Robert's distaste for "silent" protocol
>> bumps that change the behavior without any negotiation.
>
> My understanding reading his message he was in favour of it
Well, my feelings are mixed on that point, honestly. If the server
needs to know whether the client supports something, you pretty much
have to have a protocol negotiation of some kind, whether that's a
version bump or an extension. Otherwise, the server simply has no way
to find out what the client supports. In the opposite direction, it's
more fuzzy: the client can see the server version number, and so can
draw some conclusions on that basis. We have used this for protocol
changes in the past. However, something like changing the cancel key
or adding cursor with hold affects a lot more clients than adding
CopyBoth that is only triggered by a very specific command in a
special mode. And it's not that theoretically appealing to conflate
the *protocol* version number with the *server* version number.
Sometimes things that are not theoretically appealing are nonetheless
pragmatically appealing. But it is not really clear to me whether this
is one of those cases, and it sounds like Tom and Jacob think it
isn't. I could probably be persuaded either way on the best thing to
do here in a vacuum, but I'm strongly disinclined to argue against two
committers who have a firm position on the topic already.
I think what I like least about this proposal is the feeling that
we're about to embark on a long slippery slope of changing the
protocol a little bit in every new PG version. The cancel key thing is
a small change, look here's another. If we just keep doing that, we'll
end up with either a lot of minor version bumps or a lot of
extensions. I don't foresee a good outcome either way. This is a
widely used, widely adopted protocol. The idea that we can just start
tweaking it a little bit every year and have nothing bad happened
seems wild, regardless of how we do the tweaking.
Re: Proposal to allow setting cursor options on Portals
First, let me say that I very much support getting this into the wire protocol. As for ways to extend the protocol, I have been myself working on another patch + extension where one can return extra info in ReadyForQuery message The first things to add are * CommitLSN so we can make use of ability to WAIT FOR LSN on replica and two connection-pooling helpers * a flag telling that there are HOLD CURSORS * a flag telling that there are temp tables This extra feedback is enabled by setting a flag, so no flag -- nothing to confuse the client. The extras themselves are uniform (length, tag, data) so client can ignore any tag they do not recognize. On Thu, Jan 15, 2026 at 8:11 PM Dave Cramer wrote: > > > On Thu, 15 Jan 2026 at 14:00, Robert Haas wrote: >> ... >> I think what I like least about this proposal is the feeling that >> we're about to embark on a long slippery slope of changing the >> protocol a little bit in every new PG version. The cancel key thing is >> a small change, look here's another. If we just keep doing that, we'll >> end up with either a lot of minor version bumps or a lot of >> extensions. I don't foresee a good outcome either way. This is a >> widely used, widely adopted protocol. The idea that we can just start >> tweaking it a little bit every year and have nothing bad happened >> seems wild, regardless of how we do the tweaking. I think "tweaking ait little bit" and only whhere needed is exactly the right approach, if it can be cleanly isolated. My approach to protocol extension modulation *is* the ability to enable different parts of the protocol individually. For example the protocol extension to allow cursor/portal flags could be implemented this way Client has to set a flag to PROTOCOL_PORTAL_OPTIONS=true to tell the server that new protocol messages are coming - If flag setting fails, client will not send the new protocol messages - If flag setting succeeds, then it is ok to send the new messages corresponding to the flag. This way the extra packets are disconnected from protocol version and can be enabled/disabled individually and per connection And it also lets one cleanly backport the change as needed without affecting anything else. > This leaves us with an all or nothing solution, which practically means we do nothing, since we have to wait until we have a sufficient backlog of > changes or features to change the protocol. I see that as untenable, unless you are now advocating for using extensions for everything ? > > Dave
Re: Proposal to allow setting cursor options on Portals
First, let me say that I very much support getting this into the wire protocol.
As for ways to extend the protocol, I have been myself working on
another patch + extension where one can return extra info in
ReadyForQuery message
The first things to add are
* CommitLSN so we can make use of ability to WAIT FOR LSN on replica
and two connection-pooling helpers
* a flag telling that there are HOLD CURSORS
* a flag telling that there are temp tables
This extra feedback is enabled by setting a flag, so no flag --
nothing to confuse the client.
The extras themselves are uniform (length, tag, data) so client can
ignore any tag they do not recognize.
On Thu, Jan 15, 2026 at 8:11 PM Dave Cramer <davecramer@gmail.com> wrote:
>
>
> On Thu, 15 Jan 2026 at 14:00, Robert Haas <robertmhaas@gmail.com> wrote:
>>
...
>> I think what I like least about this proposal is the feeling that
>> we're about to embark on a long slippery slope of changing the
>> protocol a little bit in every new PG version. The cancel key thing is
>> a small change, look here's another. If we just keep doing that, we'll
>> end up with either a lot of minor version bumps or a lot of
>> extensions. I don't foresee a good outcome either way. This is a
>> widely used, widely adopted protocol. The idea that we can just start
>> tweaking it a little bit every year and have nothing bad happened
>> seems wild, regardless of how we do the tweaking.
I think "tweaking ait little bit" and only whhere needed is exactly
the right approach, if it can be cleanly isolated.
My approach to protocol extension modulation *is* the ability to
enable different parts of the protocol individually.
For example the protocol extension to allow cursor/portal flags could
be implemented this way
Client has to set a flag to PROTOCOL_PORTAL_OPTIONS=true to tell the
server that new protocol messages are coming
- If flag setting fails, client will not send the new protocol messages
- If flag setting succeeds, then it is ok to send the new messages
corresponding to the flag.
This way the extra packets are disconnected from protocol version and
can be enabled/disabled individually and per connection
And it also lets one cleanly backport the change as needed without
affecting anything else.
> This leaves us with an all or nothing solution, which practically means we do nothing, since we have to wait until we have a sufficient backlog of
> changes or features to change the protocol. I see that as untenable, unless you are now advocating for using extensions for everything ?
>
> Dave
Re: Proposal to allow setting cursor options on Portals
On Thu, 15 Jan 2026 at 16:06, Hannu Krosing <hannuk@google.com> wrote:First, let me say that I very much support getting this into the wire protocol.
As for ways to extend the protocol, I have been myself working on
another patch + extension where one can return extra info in
ReadyForQuery message
The first things to add are
* CommitLSN so we can make use of ability to WAIT FOR LSN on replica
and two connection-pooling helpers
* a flag telling that there are HOLD CURSORS
* a flag telling that there are temp tables
This extra feedback is enabled by setting a flag, so no flag --
nothing to confuse the client.
The extras themselves are uniform (length, tag, data) so client can
ignore any tag they do not recognize.
On Thu, Jan 15, 2026 at 8:11 PM Dave Cramer <davecramer@gmail.com> wrote:
>
>
> On Thu, 15 Jan 2026 at 14:00, Robert Haas <robertmhaas@gmail.com> wrote:
>>
...
>> I think what I like least about this proposal is the feeling that
>> we're about to embark on a long slippery slope of changing the
>> protocol a little bit in every new PG version. The cancel key thing is
>> a small change, look here's another. If we just keep doing that, we'll
>> end up with either a lot of minor version bumps or a lot of
>> extensions. I don't foresee a good outcome either way. This is a
>> widely used, widely adopted protocol. The idea that we can just start
>> tweaking it a little bit every year and have nothing bad happened
>> seems wild, regardless of how we do the tweaking.
I think "tweaking ait little bit" and only whhere needed is exactly
the right approach, if it can be cleanly isolated.
My approach to protocol extension modulation *is* the ability to
enable different parts of the protocol individually.
For example the protocol extension to allow cursor/portal flags could
be implemented this way
Client has to set a flag to PROTOCOL_PORTAL_OPTIONS=true to tell the
server that new protocol messages are coming
- If flag setting fails, client will not send the new protocol messages
- If flag setting succeeds, then it is ok to send the new messages
corresponding to the flag.
This way the extra packets are disconnected from protocol version and
can be enabled/disabled individually and per connection
And it also lets one cleanly backport the change as needed without
affecting anything else.
> This leaves us with an all or nothing solution, which practically means we do nothing, since we have to wait until we have a sufficient backlog of
> changes or features to change the protocol. I see that as untenable, unless you are now advocating for using extensions for everything ?
>
> DaveI have modified the patch to use protocol options instead of protocol version
See new version attached
Re: Proposal to allow setting cursor options on Portals
Greetings,My main driver here is to allow the creation of Holdable portals at the protocol level for drivers. Currently the only way to create a holdable cursor is at the SQL level.DECLARE liahona CURSOR WITH HOLD FOR SELECT * FROM films;The JDBC driver has an option in the API to have result sets survive commits see https://docs.oracle.com/javase/8/docs/api/java/sql/Connection.html#createStatement-int-int-int-Doing this at the protocol level is the correct way to do this as modifying the SQL to create a cursor is very cumbersome and we already have existing code to create a portal. Adding the ability to specify optionsLooking for feedback.Dave Cramer
Re: Proposal to allow setting cursor options on Portals
On Wed, 4 Mar 2026 at 09:26, Dave Cramer <davecramer@gmail.com> wrote:On Thu, 15 Jan 2026 at 16:06, Hannu Krosing <hannuk@google.com> wrote:First, let me say that I very much support getting this into the wire protocol.
As for ways to extend the protocol, I have been myself working on
another patch + extension where one can return extra info in
ReadyForQuery message
The first things to add are
* CommitLSN so we can make use of ability to WAIT FOR LSN on replica
and two connection-pooling helpers
* a flag telling that there are HOLD CURSORS
* a flag telling that there are temp tables
This extra feedback is enabled by setting a flag, so no flag --
nothing to confuse the client.
The extras themselves are uniform (length, tag, data) so client can
ignore any tag they do not recognize.
On Thu, Jan 15, 2026 at 8:11 PM Dave Cramer <davecramer@gmail.com> wrote:
>
>
> On Thu, 15 Jan 2026 at 14:00, Robert Haas <robertmhaas@gmail.com> wrote:
>>
...
>> I think what I like least about this proposal is the feeling that
>> we're about to embark on a long slippery slope of changing the
>> protocol a little bit in every new PG version. The cancel key thing is
>> a small change, look here's another. If we just keep doing that, we'll
>> end up with either a lot of minor version bumps or a lot of
>> extensions. I don't foresee a good outcome either way. This is a
>> widely used, widely adopted protocol. The idea that we can just start
>> tweaking it a little bit every year and have nothing bad happened
>> seems wild, regardless of how we do the tweaking.
I think "tweaking ait little bit" and only whhere needed is exactly
the right approach, if it can be cleanly isolated.
My approach to protocol extension modulation *is* the ability to
enable different parts of the protocol individually.
For example the protocol extension to allow cursor/portal flags could
be implemented this way
Client has to set a flag to PROTOCOL_PORTAL_OPTIONS=true to tell the
server that new protocol messages are coming
- If flag setting fails, client will not send the new protocol messages
- If flag setting succeeds, then it is ok to send the new messages
corresponding to the flag.
This way the extra packets are disconnected from protocol version and
can be enabled/disabled individually and per connection
And it also lets one cleanly backport the change as needed without
affecting anything else.
> This leaves us with an all or nothing solution, which practically means we do nothing, since we have to wait until we have a sufficient backlog of
> changes or features to change the protocol. I see that as untenable, unless you are now advocating for using extensions for everything ?
>
> DaveI have modified the patch to use protocol options instead of protocol versionresending with a different patch name as I think the commitfest app won't pick it up with the same nameSee new version attached
Re: Proposal to allow setting cursor options on Portals
Patch rebased, now ready for reviewDave CramerOn Wed, 4 Mar 2026 at 10:35, Dave Cramer <davecramer@gmail.com> wrote:On Wed, 4 Mar 2026 at 09:26, Dave Cramer <davecramer@gmail.com> wrote:On Thu, 15 Jan 2026 at 16:06, Hannu Krosing <hannuk@google.com> wrote:First, let me say that I very much support getting this into the wire protocol.
As for ways to extend the protocol, I have been myself working on
another patch + extension where one can return extra info in
ReadyForQuery message
The first things to add are
* CommitLSN so we can make use of ability to WAIT FOR LSN on replica
and two connection-pooling helpers
* a flag telling that there are HOLD CURSORS
* a flag telling that there are temp tables
This extra feedback is enabled by setting a flag, so no flag --
nothing to confuse the client.
The extras themselves are uniform (length, tag, data) so client can
ignore any tag they do not recognize.
On Thu, Jan 15, 2026 at 8:11 PM Dave Cramer <davecramer@gmail.com> wrote:
>
>
> On Thu, 15 Jan 2026 at 14:00, Robert Haas <robertmhaas@gmail.com> wrote:
>>
...
>> I think what I like least about this proposal is the feeling that
>> we're about to embark on a long slippery slope of changing the
>> protocol a little bit in every new PG version. The cancel key thing is
>> a small change, look here's another. If we just keep doing that, we'll
>> end up with either a lot of minor version bumps or a lot of
>> extensions. I don't foresee a good outcome either way. This is a
>> widely used, widely adopted protocol. The idea that we can just start
>> tweaking it a little bit every year and have nothing bad happened
>> seems wild, regardless of how we do the tweaking.
I think "tweaking ait little bit" and only whhere needed is exactly
the right approach, if it can be cleanly isolated.
My approach to protocol extension modulation *is* the ability to
enable different parts of the protocol individually.
For example the protocol extension to allow cursor/portal flags could
be implemented this way
Client has to set a flag to PROTOCOL_PORTAL_OPTIONS=true to tell the
server that new protocol messages are coming
- If flag setting fails, client will not send the new protocol messages
- If flag setting succeeds, then it is ok to send the new messages
corresponding to the flag.
This way the extra packets are disconnected from protocol version and
can be enabled/disabled individually and per connection
And it also lets one cleanly backport the change as needed without
affecting anything else.
> This leaves us with an all or nothing solution, which practically means we do nothing, since we have to wait until we have a sufficient backlog of
> changes or features to change the protocol. I see that as untenable, unless you are now advocating for using extensions for everything ?
>
> DaveI have modified the patch to use protocol options instead of protocol versionresending with a different patch name as I think the commitfest app won't pick it up with the same name
Re: Proposal to allow setting cursor options on Portals
Hi,
> Apparently that patch does not apply. Checked this one against master
I spent some time today reviewing the discussion and testing the patch. I have
some comments:
# 1. Remove protocol 3.3 version bump, since we will be using the protocol
extension mechanism. Right?
+ * Test holdable cursors using protocol 3.3 cursor options in Bind message.
+ */
+static void
+test_holdable_cursor(PGconn *conn)
+{
+ PGresult *res;
+
+ fprintf(stderr, "holdable cursor... ");
+
+ /* Verify protocol 3.3 */
+ if (PQfullProtocolVersion(conn) < 30003)
+ pg_fatal("protocol 3.3 required, got %d",
PQfullProtocolVersion(conn));
+
+ if (strcmp(value, "3.3") == 0)
+ {
+ *result = PG_PROTOCOL(3, 3);
+ return true;
+ }
# 2. Handle case where _pq_.holdable_portal is rejected in
pqGetNegotiateProtocolVersion3
I was testing on a patched client and unpatched server with a
connection string of:
"host=localhost dbname=postgres holdable_portal=1"
I received this error:
```
failed: received invalid protocol negotiation message: server reported
an unsupported parameter that was not requested
("_pq_.holdable_portal")
```
The patch only checks _pq_.test_protocol_negotiation (the grease test
parameter), but we also need to deal
with the rejected extension parameters, rather than fall through to the error.
@@ -1544,6 +1547,16 @@ pqGetNegotiateProtocolVersion3(PGconn *conn)
strcmp(conn->workBuffer.data,
"_pq_.test_protocol_negotiation") == 0)
{
found_test_protocol_negotiation = true;
+ continue;
+ }
+
+ /*
+ * Handle rejected protocol extensions we requested. Disable the
+ * corresponding feature so the client doesn't try to use it.
+ */
+ if (strcmp(conn->workBuffer.data, "_pq_.holdable_portal") == 0)
+ {
+ conn->holdable_portal_enabled = false;
}
else
{
The above is the change I tested with.
This will be the pattern for other protocol extensions that will be
added in the future as well.
Also, should there also be an API such `PQholdablePortalEnabled` which
takes a connection and
returns if conn->holdable_portal_enabled is enabled? This lets the
client detect at
runtime whether the extension was enabled, or disabled because the
server rejected it.
# 3. Remove PQsendQueryPreparedWithCursorOptions
This was also asked earlier [1], but I don't see an answer. Why do we need this?
Wouldn't PQsendBindWithCursorOption() be sufficient?
# 4. PQsendBindWithCursorOptions
a. Looking at other PQSend patterns, it looking like
PQsendBindWithCursorOptions/PQsendQueryPreparedWithCursorOptions are
missing
`pqAppendCmdQueueEntry(conn, entry)` before returning.
b. The last message type sent in `PQsendBindWithCursorOptions` is a
describe, so the query class should be PGQUERY_DESCRIBE, right?
c. Require a named portal (not just for the HOLD case). An unnamed
portal will not be executed in this API, so
subsequent calls to this API with an unnamed portal will just
overwrite the last one.
# 5. Replace hex with constants defined in libpq-fe.h which mirror parsenodes.h
for example 0x0020 → PQ_CURSOR_OPT_HOLD, etc.
Although I am not sure how these could remain in sync with core.
# 6. In the test_holdable_cursor, can we use PQsendClosePortal() instead of
"CLOSE"? Not for this patch, but it will be good to also have an API
for FETCH.
[1] [https://www.postgresql.org/message-id/CADK3HH%2B9V58vJzCkgvMwk2fyaCtYwr-Dv5em7rXzgUiVrnpuFA%40mail.gmail.com]
--
Sami Imseih
Amazon Web Services (AWS)
Re: Proposal to allow setting cursor options on Portals
Hi, One thing that just occurred to me is why must we limit the new protocol extension to holdable cursors? PQsendBindWithCursorOptions() will work with any cursor option, Maybe it should be called "_pq_.named_portal" or perhaps better "_pq_.cursor"? -- Sami Imseih Amazon Web Services (AWS)
Re: Proposal to allow setting cursor options on Portals
Hi,
One thing that just occurred to me is why must we limit
the new protocol extension to holdable cursors?
PQsendBindWithCursorOptions() will work with any cursor option,
Maybe it should be called "_pq_.named_portal" or perhaps better "_pq_.cursor"?
Re: Proposal to allow setting cursor options on Portals
> Well there are currently ways to name a portal with V3 so named portal doesn't make sense. > > _pq_.cursor would be fine. I spent some time today on this, and implemented this as a new protocol extension called _pq_.protocol_cursor, and the libpq option will also have the same name. Maybe someone else has a better name, but I like this as it differentiates from a sql-level cursor. Maybe someone has a better suggestion. All the cursor options can be passed, though CURSOR_OPT_BINARY is irrelevant in the extended query protocol as noted here [1]. Binary output is controlled by the result format codes on the FETCH instead. So, CURSOR_OPT_BINARY can be passed as a cursor option, but will be silently ignored. I added a new test module libpq_protocol_cursor that expands the tests to cover all options, except for cursor sensitivity. Also, there are tests for multiple flags, enabling/disabling the extension and unnamed portal being used ( which should not be allowed ). A few other comments to earlier points: > Also, should there also be an API such `PQholdablePortalEnabled` which > takes a connection and I did not implement this and instead rely on if (cursorOptions != 0 && !conn->protocol_cursor_enabled) to reject the usage of the API. So, if the API is called with cursor options but protocol_cursor is not enabled, we reject the attempt. So, I am not sure this Enabled() API has any value after thinking about it a bit more. > # 5. Replace hex with constants defined in libpq-fe.h which mirror parsenodes.h > for example 0x0020 → PQ_CURSOR_OPT_HOLD, etc. Also, did not implement this since as mentioned earlier, I am not sure how to keep these in sync with core. The docs do mention that parsenodes.h is to be referenced for the bitmasks that are defined as CURSOR_OPT_*. That may be good enough. See v3 attached. [1] https://www.postgresql.org/docs/18/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
Re: Proposal to allow setting cursor options on Portals
On Tue, 24 Mar 2026 at 21:54, Sami Imseih wrote: > All the cursor options can be passed, though CURSOR_OPT_BINARY is > irrelevant in the extended query protocol as noted here [1]. Binary output is > controlled by the result format codes on the FETCH instead. So, > CURSOR_OPT_BINARY can be passed as a cursor option, but will be > silently ignored. This is what this patch originally did in one of the earlier versions. And if I understand correctly it was changed after this feedback from me: On Sun, 14 Dec 2025 at 14:41, Jelte Fennema-Nio wrote: > As mentioned upthread, I'm not sure BINARY makes sense. For any other > options, the protocol docs should specify which ones are allowed and > what their bits are. Looking at the DECLARE docs[2]. > 1. I think supporting ASENSITVE/INSENSITIVE/SENSITIVE bits is > unnecessary, since postgres cursors are always INSENSITIVE. > 2. For SCROLL vs NO SCROLL, it would be nice if we could get rid of > the intermediate mode where if neither SCROLL or NO SCROLL is > specified, it's still SCROLL sometimes. I'm not sure backwards > compatibility would allow that, i.e. can you currently sometimes do a > BACKWARD scan on a portal created with Bind. I guess we could make it > so that if you specify the portal flags, then you have to be explicit > abuot specifying SCROLL or NO SCROLL > 3. All the flags with no SQL variant probably shouldn't be > configurable through the protocol too (e.g. CURSOR_OPT_FAST_PLAN)
Re: Proposal to allow setting cursor options on Portals
On Tue, 24 Mar 2026 at 11:01, Dave Cramer wrote: > _pq_.cursor would be fine. I think that makes sense as a name for the option. I think adding flag support for SCROLL and NO SCROLL would make sense in that case. Some notes on the patch (but I didn't look look at the client side libpq code in detail): For the protocol definition I'd like a few changes: 1. I'd like the new field in the bind message that you add to be described as an extension bitmap, not specifically for cursor options, so that future extensions could add bits too it too. 2. Related to that, I think the used bits should not align with the internal bits. Having the only valid flag bit be 0x0020 is kinda weird. Let's just make that 0x0001. We could update the internal ones to match if desired, but I think it's fine for the protocol bits to differ from the bits in the postgres server. Docs still mention CURSOR_OPT_BINARY, but support for that has been removed from the code afaict (which I think is indeed what should happen) There's a bunch of protocol version 3.3 code still around, which should be removed now that the protocol option is added. PQsendBindWithCursorOptions and PQsendQueryPreparedWithCursorOptions should error out if conn->holdable_portal_enabled is false. Right now it silently skips the cursor options if the connection does not support the protocol extension. There should be a libpq function to inspect whether the connection supports cursor options, so some kind of graceful fallback logic can be implemented by the application when it's not supported. libpq docs are missing
Re: Proposal to allow setting cursor options on Portals
On Tue, 24 Mar 2026 at 11:01, Dave Cramer <davecramer@gmail.com> wrote:
> _pq_.cursor would be fine.
I think that makes sense as a name for the option. I think adding flag
support for SCROLL and NO SCROLL would make sense in that case.
Some notes on the patch (but I didn't look look at the client side
libpq code in detail):
For the protocol definition I'd like a few changes:
1. I'd like the new field in the bind message that you add to be
described as an extension bitmap, not specifically for cursor options,
so that future extensions could add bits too it too.
2. Related to that, I think the used bits should not align with the
internal bits. Having the only valid flag bit be 0x0020 is kinda
weird. Let's just make that 0x0001. We could update the internal ones
to match if desired, but I think it's fine for the protocol bits to
differ from the bits in the postgres server.
Docs still mention CURSOR_OPT_BINARY, but support for that has been
removed from the code afaict (which I think is indeed what should
happen)
There's a bunch of protocol version 3.3 code still around, which
should be removed now that the protocol option is added.
PQsendBindWithCursorOptions and PQsendQueryPreparedWithCursorOptions
should error out if conn->holdable_portal_enabled is false. Right now
it silently skips the cursor options if the connection does not
support the protocol extension.
There should be a libpq function to inspect whether the connection
supports cursor options, so some kind of graceful fallback logic can
be implemented by the application when it's not supported.
libpq docs are missing
Re: Proposal to allow setting cursor options on Portals
On Wed, 25 Mar 2026 at 15:34, Dave Cramer wrote:
> Attached is v4 of the patch
> Co-Authored by Sami Imseih
>
> Adds docs and test module
It's looking much more finished
> The portal can
> later be operated on with cursor commands such as FETCH, MOVE,
> and CLOSE.
This made me realize that, adding the Bind side of cursors is only
half of the equation. The "Execute" message should also gain new
behaviour to support all the same functionality as FETCH and MOVE. I
think we can do that fairly easily by adding similar flags to Execute.
I think we'd need three flags:
1. MOVE
2. BACKWARD
3. ABSOLUTE
I do realize the scope creep of this, but it feels that without
addressing Execute we have a half-finished feature. That could be
fine, but then I don't think we should call the option
_pq_.protocol_cursor. Because that sounds like it solves the whole
half-baked protocol-level cursor implemention that we currently have.
Maybe _pq_.cursor_bind instead.
I think the "protocol_" part in _pq_.protocol_cursor is duplicative.
The _pq_ part already indicates that it's a protocol option, so I'd
leave that out.
> PQ_BIND_CURSOR_SCROLL (scroll),
> PQ_BIND_CURSOR_NO_SCROLL (no scroll), and
> PQ_BIND_CURSOR_HOLD (hold).
> These are defined in libpq-fe.h.
and
>
> Bitmap set by protocol extensions.
>
I think the Message Formats page should list the actual flag values
that are valid. The protocol docs should not require you to look at
the postgres source code.
/*
* Only override the default cursorOptions when the client has
* explicitly set flags. A value of 0 means no cursor options were
* requested, so keep the CreatePortal defaults.
*/
What is the difference between setting cursorOptions = 0 and the
CreatePortal defaults?
> {"protocol_cursor", NULL, "0", NULL,
> "Protocol-Cursor", "", 1,
> offsetof(struct pg_conn, protocol_cursor)},
In my GoAway patchset I linked enabling the protocol extension to the
user requesting protocol v3.2 (or higher).
> /* Reject any bits we don't recognize */
> if (bind_ext_flags & ~0x0007)
Let's use PQ_BIND_CURSOR_VALID_FLAGS here too instead of this magic number.
> src/test/modules/libpq_protocol_cursor/libpq_protocol_cursor.c
I think it'd be better to put these tests in the libpq_pipeline test
file. Then we can keep all the libpq tests together so they can share
the helper logic.
Re: Proposal to allow setting cursor options on Portals
On Thu, Sep 10, 2026 at 5:50 PM Sehrope Sarkuni wrote:
> Attached are two patches on top of v5. The first fixes the above and
> the second adds libpq_pipeline tests for each case.
I tested v5 as cfbot built it (16c3391d331, v5 on master d2b65863f04)
with 0004 and 0005 applied, once with assertions and once without, using
raw protocol messages and small libpq programs. I'll send what I found
about the wire format and the rest of v5 separately.
1. Problems in 0004
1a. SCROLL with FOR UPDATE is still accepted when the query has a
data-modifying WITH
The FOR UPDATE check runs only for PORTAL_ONE_SELECT, and a query with a
data-modifying WITH gets PORTAL_ONE_MOD_WITH:
with i as (insert into n1_t values (99) returning id)
select id from n1_t where id < 10 order by id limit 4 for update
Bind SCROLL, FORWARD 3, BACKWARD 2: 1, 2, 3, then 2, 1
DECLARE d SCROLL CURSOR FOR ...:
ERROR 0A000 DECLARE CURSOR must not contain data-modifying
statements in WITH
The rows themselves are correct, because a portal with a data-modifying
WITH runs to completion into its tuplestore and a backward fetch reads
stored rows. The problem is the documented contract. 0004 adds this
sentence to protocol.sgml: "Neither SCROLL nor WITH HOLD is permitted
with FOR UPDATE or FOR SHARE". That sentence does not hold for this
query. DECLARE refuses the query, and whether the Bind is refused
depends on a portal strategy the client cannot see. Either test rowMarks
whenever the portal has a single CMD_SELECT PlannedStmt, or narrow the
documentation to the case the check covers.
1b. SCROLL overrides plan_cache_mode = force_generic_plan
The replan passes the Bind parameters to pg_plan_queries, so they are
folded in as constants and the portal gets a custom plan. A statement
whose error depends on the parameter then behaves differently with and
without SCROLL:
create table agg as
select g % 10 + 1 as k from generate_series(1, 100000) g;
set plan_cache_mode = force_generic_plan;
prepare: select k, case when k > 100 then 1/$1 end from agg group by k
bind $1 = 0
cursor options 0: 10 rows (8, NULL), (10, NULL), ..., SELECT 10
cursor options SCROLL: ERROR 22012 division by zero, at Bind
Replanning with boundParams = NULL when the cached plan was the generic
one would give the same result with and without SCROLL.
1c. The FOR UPDATE check is stricter than DECLARE
It looks at PlannedStmt->rowMarks, which also holds row marks from
subqueries, CTEs, and views. DECLARE checks only the top-level query, so
DECLARE ... SCROLL and DECLARE ... WITH HOLD accept these four shapes,
while a Bind with the same cursor options fails with 0A000:
subquery: select * from (select id from t order by id
limit 2 for update) s
flattened subquery: a subquery the planner pulls up
CTE: with c as materialized (select id from t for
update) select * from c
view: a view defined with FOR UPDATE
Refusing them may well be the more correct behavior, since DECLARE WITH
HOLD over such a view is accepted without the error "Holdable cursors
must be READ ONLY". But the documentation says "as for DECLARE", so
either the check or that sentence should change.
1d. Smaller points
- For every SCROLL Bind whose cached plan cannot scan backwards, the
server plans the query again and does not cache the new plan.
pg_prepared_statements then reports the discarded generic plans and
no custom ones: with three plain and three SCROLL Binds of one
statement it showed generic_plans 6, custom_plans 0. This behavior is
worth a sentence in the docs or the commit message.
- The replan does not pass CURSOR_OPT_FAST_PLAN, while DECLARE passes
it, so the plan can differ from the one DECLARE SCROLL gets. The rows
are the same.
- The amended "DO NOT put any code that could possibly throw" comment
mentions only the checks above it, saying they release the plan
first. pg_plan_queries and copyObject can throw there too, and they
are safe only because cplan was already released.
2. The tests in 0005
I ran 17 mutations of 0004 against libpq_pipeline; 11 were caught, and
one of the six survivors changes nothing observable.
- Removing ReleaseCachedPlan before the "only allowed for a SELECT
statement" error is not caught, and it leaks the CachedPlan context
for the life of the session. A new test catches it: prepare
"insert ... returning /* marker */", send three HOLD Binds that are
rejected, DEALLOCATE, then count the pg_backend_memory_contexts rows
whose name is 'CachedPlan' and whose ident is that statement's text.
The expected count is 0; with the mutation it is 1.
- Both expect_bind_rejected and the INT64_MIN check accept any
PGRES_FATAL_ERROR. The mutation that raises the FOR UPDATE error as
XX000 is not caught, and a rejection for an unrelated reason would not
be caught either. Checking PG_DIAG_SQLSTATE (0A000, 22003) would catch
both.
- Values just inside the new limits are not tested as accepted: the
suite still passes if the server check is changed to also reject
INT64_MIN + 1, or the libpq check is changed to reject
nParams = 65535.
- The FOR UPDATE tests have no FOR SHARE case and no case with a
data-modifying WITH, which is the case in 1a. No test binds a
parameter under force_generic_plan, which is the case in 1b.
- check_scroll_plan takes the expected first row from the portal's own
forward fetch and compares two later fetches with it. For the
backward fetch after MOVE ALL, and for the fetches after COMMIT in the
HOLD case, it checks only the row counts. I assume that is because a
hash join does not fix the row order, but DECLARE SCROLL of the same
query, or an ORDER BY, would give literal expected rows. Without 0004,
on a build without assertions, the test fails for the right reason
("SELECT 1: expected 1 row from backward fetch, got 0"). It stops at
the first shape, though, and none of the shapes is a HashAggregate.
With a HashAggregate plan, v5 returned wrong rows rather than no rows.
- On a mismatch, expect_bind_rejected reports the line number inside
the helper, not its "what" argument, so the cases
"HOLD with FOR UPDATE" and "HOLD | SCROLL with FOR UPDATE" print the
same failure. The two "expected rejection of too many parameters"
checks share a message and print neither the return value nor
PQerrorMessage.
- Several scenarios share a test: test_cursor_bind_for_update checks
three rejections and then an accepted NO SCROLL fetch;
test_cursor_bind_scroll_plans covers four plan shapes and HOLD across
COMMIT; the server-side INT64_MIN check is appended to
test_cursor_execute_validation, which otherwise tests client-side
validation. Each libpq_pipeline test is its own entry, so splitting
these tests costs little, and the name of a failed test then
identifies the case.
- The comment "Test client-side validation of cursor bind options."
now sits above expect_bind_rejected, because the new functions are
inserted between it and test_cursor_bind_validation.
Regards,
Vladimir
Re: Proposal to allow setting cursor options on Portals
On Wed, Sep 9, 2026 at 6:04 PM Dave Cramer wrote:
> Attached is v5, rebased onto master and split into three patches
Hi Dave,
Before more work goes into the details, I'd like to discuss the wire
format. Once _pq_.cursor is negotiated, the format of every Bind and
every Execute on the connection changes, and that is hard on connection
poolers. I think holdability can be sent in a message of its own, which
would leave Bind and Execute unchanged and make 0002 optional. Many of
the remaining problems depend on which format is chosen, so I've listed
them last.
The PgBouncer results below come from v5 without 0004; 0004 does not
change the behavior they show. The rest was tested on v5 with 0004 and
0005 applied (my comments on those are in reply to Sehrope).
1. What a pooler sees
The changed format also applies to Bind and Execute messages sent by
code that never uses a cursor option. For a pooler, that has two
consequences:
- It cannot share a server connection between clients that negotiated
the option and clients that did not; it needs separate pools or has
to rewrite messages.
- If a middlebox drops _pq_.cursor without sending
NegotiateProtocolVersion, libpq assumes the option was accepted, and
every extended query fails. With PgBouncer 1.22.1 and
ignore_startup_parameters = _pq_.cursor, psql's
"select $1 \bind 42 \g" fails with "invalid message format".
(PgBouncer 1.23 and later send NegotiateProtocolVersion themselves and
decline the option. They are safe today, but the feature is not
available through them.)
2. Which statements need HOLD in the protocol
The strongest case for holding portals in the protocol is the result of
INSERT, UPDATE, DELETE, or MERGE ... RETURNING, or of a data-modifying
WITH, because SQL cannot keep it open across COMMIT at all. With 0004,
HOLD is limited to a single SELECT, and for a SELECT drivers can already
get a holdable cursor from DECLARE ... WITH HOLD. Portals for RETURNING,
for a data-modifying WITH, and for row-returning utility statements
already run to completion into a tuplestore, so holding them after
their first Execute looks tractable. What COMMIT should do with such a
portal that was never executed is an open question. I'd see holding
these portals as a follow-up rather than something for this patch.
3. HOLD in a message of its own
CURSOR_OPT_HOLD is read only by PreCommit_Portals and pg_cursors, not by
PortalStart or the planner, so holdability does not have to arrive with
Bind. A separate message would leave Bind and Execute unchanged. For
example:
PortalHold (F) String portal name
PortalHoldComplete (B)
The client would send PortalHold after the Bind of a named portal and
before the transaction ends. The server would accept it only if the
option was negotiated.
The separate message changes three things for a pooler. It can enable
the option on every server connection. A dropped startup parameter
breaks only the new message. And the request to hold is a message of its
own, rather than the last four bytes of a Bind whose tail PgBouncer
streams without buffering. It would also leave libpq with a single Bind
encoder. With v5 applied, libpq has two, and the two PQsendBindGuts
bugs in section 5 are in the second one.
Supporting HOLD itself in transaction pooling costs the same with either
format: the pooler has to keep the server connection for the client
while a held portal exists, because the portal and its name live on that
connection. Section 4 asks how a pooler would know when that ends.
SCROLL has to be known at PortalStart, so if SCROLL stays in the patch,
a variant of the Bind message would be the natural place for it, again
without changing Bind itself. SQL FETCH and MOVE can already read, by
name and in any direction, a portal that 0004 makes scrollable. Are the
Execute fields in 0002 needed in this patch at all?
4. A question on session state
A held portal outlives its transaction. I ran a PgBouncer build patched
to forward the option in transaction pooling mode, and three things
happened. The client that created the portal lost it at the client's
next transaction. Another client could fetch from the portal. And the
portal collided with the next client's ordinary portal of the same name
(42P03, cursor "C_1" already exists; pgjdbc names portals C_1, C_2, ...
on every connection).
To keep the server connection for that client while the portal exists,
a pooler has to detect when the portal is dropped, and it cannot detect
every way that happens: Close, SQL CLOSE, DISCARD ALL, or a failed
transaction. Hannu mentioned a ReadyForQuery flag for held cursors
(https://postgr.es/m/CAMT0RQRFa43CAf773LaDUvBYmZtwcNfdQSAzpBUtdJ+EiHH=kQ@mail.gmail.com).
I'm not sure a generic "the session has state" signal would be useful
instead, because changed GUCs, prepared statements, temp tables,
advisory locks, and LISTEN are session state too, and poolers already
track some of them. Is a portal-specific signal worth adding? Or is it
enough for now to say in protocol.sgml that poolers in transaction
pooling mode cannot support HOLD?
5. Other problems in v5 that 0004 does not fix
The problems below depend on the format. None of them remains if Bind
and Execute stay unchanged or 0002 is dropped:
- A directional Execute runs a PORTAL_UTIL_SELECT portal with
isTopLevel = false. For a procedure that has an INOUT parameter and
commits, "call p(null)" returns 42 through a plain Execute and fails
with 2D000 "invalid transaction termination" through an Execute with
FORWARD ALL.
- The server validates the direction field after the portal lookup, and
after log_statement has logged the Execute: an Execute with direction
5 on a missing portal fails with 34000 rather than 08P01.
- protocol.sgml says the server never sends PortalSuspended in response
to an Execute under the extension. With all-zero fetch fields and a
nonzero row limit the server still sends it, as the 0002 commit
message says. The code is right and protocol.sgml is wrong.
- 0002 without 0003 breaks every PQexecParams on a connection with
protocol_cursor=1 ("insufficient data left in message"), so the series
cannot be bisected at 0002.
- A MOVE through PQsendExecutePortal returns PGRES_TUPLES_OK after a
Describe, where SQL MOVE returns PGRES_COMMAND_OK.
- libpq's PQsendBindGuts has two bugs:
* with paramLengths NULL it calls strlen() on a binary parameter;
AddressSanitizer reports a heap-buffer-overflow (READ of size 5 on a
4-byte value), where PQsendQueryPrepared refuses the call with
"length must be given for binary parameter".
* it uses paramLengths for text parameters. For
"select $1::text || '|' || length($1::text)" with "3" and length 0,
PQsendBindAndExecutePortal returns "|0", while PQexecPrepared with
the same arrays returns "3|1".
The problems below do not depend on the format:
- libpq accepts a NegotiateProtocolVersion that names _pq_.cursor even
when the client did not request it, while any other unrequested name
fails the connection.
- Outside a transaction block, PQsendBindWithCursorOptions without HOLD
returns PGRES_COMMAND_OK, but the server has already dropped the
portal at the Sync that call sent, so the next PQsendExecutePortal
gets 34000.
- PQprotocolCursorEnabled returns 1 for a connection still waiting for
the startup reply, and for a connection that failed after startup
(database does not exist).
- libpq reads only the first character of protocol_cursor, so "true",
"on", and "yes" silently leave the extension off.
- conn->protocol_cursor is never freed (2 bytes per PGconn and per
PQcancelCreate).
Regards,
Vladimir
Re: Proposal to allow setting cursor options on Portals
Hi Vladimir and Dave et al.
Sorry to invade you thread, apologies if this is inapproriate. ChatGPT pointed me to this discussion and suggested to respond with below contribution. Would like to add a perspective from an existing Oracle OCI application that we are considering migrating to PostgreSQL.
First, I appreciate the effort going into this proposal. I can see that there are quite a few difficult details to get right, particularly around protocol compatibility, holdable cursors, scrollability and connection poolers.
However, I wonder whether you might be trying to solve too many problems at once, potentially delaying functionality that would already be extremely useful to a significant class of applications.
Our requirements are actually rather modest compared with the full scope being discussed.
Each application process maintains a collection of roughly 100–200 prepared SQL statements, which are reused many times with different bind values. Usually, an execution is performed and consumed before moving on to the next one. Performance is critical.
However, in some parts of the application, we have 2–10 SELECT cursors active simultaneously. A single application thread decides which cursor to consume next. Each cursor maintains its own execution state, and fetching is done in bounded batches (similar to OCI array fetching).
We need:
- Prepare each SQL statement once per connection.
- Execute it repeatedly with different binary bind values.
- Create multiple independent, named execution portals from the same prepared statement.
- Fetch from whichever portal the application chooses, with a configurable maximum number of rows per fetch.
- Resume a portal later, without losing its execution state.
- Close each portal independently.
- Avoid materializing complete result sets in client memory.
We only need forward-only, transaction-scoped cursors. We don't need scrollability, holdability across transactions, or concurrent execution of queries on the server. Sequential execution of portal operations on a connection is perfectly acceptable, provided the application controls which portal is executed next.
We have approximately 40 application processes, each using 2–3 pthreads. Using one database connection per application thread would be perfectly reasonable for us. Connection pooling and multiplexing are not the issue.
This is a perfectly natural programming model with Oracle OCI, and the absence of an equivalent basic API is likely to become an obstacle to migrating this application to PostgreSQL. We probably are not be the only potential PostgreSQL adopters in this situation.
Perhaps it would be worth considering a smaller initial API that supports the essential forward-only portal operations, leaving scrollability, holdability and other more elaborate functionality for subsequent extensions.
Could you maybe first provide the basic building blocks that allow applications to use PostgreSQL portals as independently managed cursors, without requiring every possible cursor feature and use case to be addressed at the same time?
I realize that a clean and extensible API is important, and I don't want to underestimate the protocol and implementation complexities involved, just wondering whether the pursuit of a fully comprehensive solution might inadvertently keep a useful, relatively basic capability out of reach for applications that need it.
Note : I consulted Claude, Gemini and ChatGPT. Gemini suggested to use the "solution" below :
<<
// 2. Prepare statement once per connection
res = PQprepare(
conn,
"user_query",
"SELECT * FROM users WHERE status = $1 AND region = $2",
2,
NULL
);
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "Prepare failed: %s\n", PQerrorMessage(conn));
}
PQclear(res);
// 3. Declare cursor using PQexecParams with bind values
const char *paramValues[2] = {"1", "US-WEST"};
res = PQexecParams(
conn,
"DECLARE cur_1 CURSOR FOR EXECUTE user_query($1, $2)",
2, NULL, paramValues, NULL, NULL, 0
);
PQclear(res);
>>
And this looked nice but ChatGPT clarified this is not supported :
<<
PostgreSQL does not allow EXECUTE (the SQL prepared-statement command) as the query expression of DECLARE CURSOR.
>>
Both Claude and ChatGPT explained that the protocol supports what I want, but libpq does not provide an API, and this lead to your discussion.
Am just exploring feasibility of migration to postgresql, so still far from evaluating performance, and so will drop the requirement to parse the SQL only once, using a binary version of the sample code below :
<<
PQexec(conn, "BEGIN");
const char *params1[] = {"1", "US-WEST"};
const char *params2[] = {"1", "EU-WEST"};
PGresult *res;
res = PQexecParams(conn,
"DECLARE cur_1 NO SCROLL CURSOR FOR "
"SELECT * FROM users WHERE status = $1 AND region = $2",
2, NULL, params1, NULL, NULL, 0);
>>
Yet, if an upcoming version of libpq would handle the above this would be most welcome, even if it would come with many limitations (which, I believe, would not matter for our use-case).
My prompt was :
Large (2M lines) Ada application uses Oracle OCI via binding that supports creating multiple selecting cursors on a single connection using (binary) bind variables, it can then set bind values to the variables and execute several cursors in parallel, and consume rows from the individual cursors while the OCI layer keeps all fetched data available in buffer memory until rows are consumed. I want the sql parsed once and executed multiple times (so repeatedly set new bind values and execute without providing the sql each time). The total amount of data read can be huge so I do not want to buffer the entire result in client memory, I believe this implies the use of server-side cursors. The Oracle OCI implementation is based on array-fetch, buffering a max nr of rows for each OCI call to fetch the next batch. Real parallelism on the server is not essential (application rarely uses oracle parallel query execution). The application must be able to choose which cursor to consume when. Many clients execute this application in parallel, so each constructs the same sql statements, Oracle uses a cursor cache on the server (SQL area) to avoid repeated identical work. The clients cannot coordinate to allocate some wrapper functions that need to have a unique name in the database. All info is supposed to stream from the DB server to the client, never storing the entire result. How do I map this to postgresql libpq ? I understand libpq will always manage itself the buffer memory and copy info read from the connection to its own buffers. I want the application copying this data directly from the libpq buffers, i.e. I don't want a second layer in the binding to copy from libpq and then later copy to the application when it requests a row from a specific cursor. Not sure the libpq PREPARE/DECLARE can accomplish the above. It looks like pipeline mode lets the server decide for which cursor/select to send rows when, not sure if the application can access these rows from libpq when it is ready to do so. Especially PQGetResult (Conn) looks like a non-starter as it does not let the application associate one result with one specific select. Using one connection per cursor is likely inefficient, will start a process for each connection (so for each cursor), and for each application, ending up consuming a lot of process/memory resources.
No need to respond to my message but I remain available in case you have questions.
Thanks again for working on this, I am personally a big fan of open-source software.
All the best,
Stef
Re: Proposal to allow setting cursor options on Portals
On Sun, 8 Nov 2026 at 22:29, Stef Van Vlierberghe
wrote:
>
> Hi Vladimir and Dave et al.
>
> Sorry to invade you thread, apologies if this is inapproriate. ChatGPT pointed me to this discussion and suggested to respond with below contribution. Would like to add a perspective from an existing Oracle OCI application that we are considering migrating to PostgreSQL.
>
> First, I appreciate the effort going into this proposal. I can see that there are quite a few difficult details to get right, particularly around protocol compatibility, holdable cursors, scrollability and connection poolers.
>
> However, I wonder whether you might be trying to solve too many problems at once, potentially delaying functionality that would already be extremely useful to a significant class of applications.
>
> Our requirements are actually rather modest compared with the full scope being discussed.
>
> Each application process maintains a collection of roughly 100–200 prepared SQL statements, which are reused many times with different bind values. Usually, an execution is performed and consumed before moving on to the next one. Performance is critical.
>
> However, in some parts of the application, we have 2–10 SELECT cursors active simultaneously. A single application thread decides which cursor to consume next. Each cursor maintains its own execution state, and fetching is done in bounded batches (similar to OCI array fetching).
>
> We need:
>
> Prepare each SQL statement once per connection.
> Execute it repeatedly with different binary bind values.
> Create multiple independent, named execution portals from the same prepared statement.
> Fetch from whichever portal the application chooses, with a configurable maximum number of rows per fetch.
> Resume a portal later, without losing its execution state.
> Close each portal independently.
> Avoid materializing complete result sets in client memory.
>
> We only need forward-only, transaction-scoped cursors. We don't need scrollability, holdability across transactions, or concurrent execution of queries on the server. Sequential execution of portal operations on a connection is perfectly acceptable, provided the application controls which portal is executed next.
>
> We have approximately 40 application processes, each using 2–3 pthreads. Using one database connection per application thread would be perfectly reasonable for us. Connection pooling and multiplexing are not the issue.
>
> This is a perfectly natural programming model with Oracle OCI, and the absence of an equivalent basic API is likely to become an obstacle to migrating this application to PostgreSQL. We probably are not be the only potential PostgreSQL adopters in this situation.
>
> Perhaps it would be worth considering a smaller initial API that supports the essential forward-only portal operations, leaving scrollability, holdability and other more elaborate functionality for subsequent extensions.
>
> Could you maybe first provide the basic building blocks that allow applications to use PostgreSQL portals as independently managed cursors, without requiring every possible cursor feature and use case to be addressed at the same time?
>
> I realize that a clean and extensible API is important, and I don't want to underestimate the protocol and implementation complexities involved, just wondering whether the pursuit of a fully comprehensive solution might inadvertently keep a useful, relatively basic capability out of reach for applications that need it.
>
> Note : I consulted Claude, Gemini and ChatGPT. Gemini suggested to use the "solution" below :
>
> <<
>
> // 2. Prepare statement once per connection
> res = PQprepare(
> conn,
> "user_query",
> "SELECT * FROM users WHERE status = $1 AND region = $2",
> 2,
> NULL
> );
> if (PQresultStatus(res) != PGRES_COMMAND_OK) {
> fprintf(stderr, "Prepare failed: %s\n", PQerrorMessage(conn));
> }
> PQclear(res);
>
> // 3. Declare cursor using PQexecParams with bind values
> const char *paramValues[2] = {"1", "US-WEST"};
> res = PQexecParams(
> conn,
> "DECLARE cur_1 CURSOR FOR EXECUTE user_query($1, $2)",
> 2, NULL, paramValues, NULL, NULL, 0
> );
> PQclear(res);
>
> >>
>
> And this looked nice but ChatGPT clarified this is not supported :
>
> <<
> PostgreSQL does not allow EXECUTE (the SQL prepared-statement command) as the query expression of DECLARE CURSOR.
> >>
>
> Both Claude and ChatGPT explained that the protocol supports what I want, but libpq does not provide an API, and this lead to your discussion.
>
> Am just exploring feasibility of migration to postgresql, so still far from evaluating performance, and so will drop the requirement to parse the SQL only once, using a binary version of the sample code below :
>
> <<
> PQexec(conn, "BEGIN");
>
> const char *params1[] = {"1", "US-WEST"};
> const char *params2[] = {"1", "EU-WEST"};
>
> PGresult *res;
>
> res = PQexecParams(conn,
> "DECLARE cur_1 NO SCROLL CURSOR FOR "
> "SELECT * FROM users WHERE status = $1 AND region = $2",
> 2, NULL, params1, NULL, NULL, 0);
>
> >>
>
> Yet, if an upcoming version of libpq would handle the above this would be most welcome, even if it would come with many limitations (which, I believe, would not matter for our use-case).
>
>
> My prompt was :
>
> Large (2M lines) Ada application uses Oracle OCI via binding that supports creating multiple selecting cursors on a single connection using (binary) bind variables, it can then set bind values to the variables and execute several cursors in parallel, and consume rows from the individual cursors while the OCI layer keeps all fetched data available in buffer memory until rows are consumed. I want the sql parsed once and executed multiple times (so repeatedly set new bind values and execute without providing the sql each time). The total amount of data read can be huge so I do not want to buffer the entire result in client memory, I believe this implies the use of server-side cursors. The Oracle OCI implementation is based on array-fetch, buffering a max nr of rows for each OCI call to fetch the next batch. Real parallelism on the server is not essential (application rarely uses oracle parallel query execution). The application must be able to choose which cursor to consume when. Many clients execute this application in parallel, so each constructs the same sql statements, Oracle uses a cursor cache on the server (SQL area) to avoid repeated identical work. The clients cannot coordinate to allocate some wrapper functions that need to have a unique name in the database. All info is supposed to stream from the DB server to the client, never storing the entire result. How do I map this to postgresql libpq ? I understand libpq will always manage itself the buffer memory and copy info read from the connection to its own buffers. I want the application copying this data directly from the libpq buffers, i.e. I don't want a second layer in the binding to copy from libpq and then later copy to the application when it requests a row from a specific cursor. Not sure the libpq PREPARE/DECLARE can accomplish the above. It looks like pipeline mode lets the server decide for which cursor/select to send rows when, not sure if the application can access these rows from libpq when it is ready to do so. Especially PQGetResult (Conn) looks like a non-starter as it does not let the application associate one result with one specific select. Using one connection per cursor is likely inefficient, will start a process for each connection (so for each cursor), and for each application, ending up consuming a lot of process/memory resources.
>
> No need to respond to my message but I remain available in case you have questions.
>
> Thanks again for working on this, I am personally a big fan of open-source software.
>
> All the best,
>
> Stef
HI Stef,
So my interest in changing the protocol is to make it easier for
drivers such as the JDBC driver to implement cursors with hold and
scrollable cursors. I see that your interest is to add new
functionality to the cursor API to be more compatible with Oracle.
Today everything I am looking to implement is possible using SQL.
Thanks,
Dave