Skip to content

pgconn: add configurable max message body length#2558

Open
carter-ya wants to merge 1 commit into
jackc:masterfrom
carter-ya:codex/configurable-max-body-len
Open

pgconn: add configurable max message body length#2558
carter-ya wants to merge 1 commit into
jackc:masterfrom
carter-ya:codex/configurable-max-body-len

Conversation

@carter-ya
Copy link
Copy Markdown

@carter-ya carter-ya commented May 15, 2026

Summary

  • Add pgconn.Config.MaxBodyLen and apply it to pgproto3.Frontend during ConnectConfig and Construct.
  • Add regression coverage for configured body length limits.

Rationale

pgproto3.Frontend already supports SetMaxBodyLen, but pgconn users currently need to reach through to the protocol frontend to configure it. Exposing the setting on pgconn.Config makes it possible to reject unreasonable message bodies before chunkReader allocates the buffer.

The default remains 0, so no maximum is enforced unless callers opt in. This preserves the existing behavior while allowing applications to configure a defensive limit.

Testing

  • go test ./pgproto3 ./pgconn -run 'TestFrontendReceiveExceededMaxBodyLen|TestBackendReceiveExceededMaxBodyLen|TestConnectConfigMaxBodyLen|TestConfigCopyReturnsEqualConfig'

@carter-ya carter-ya changed the title pgconn: expose frontend max body length pgconn: add configurable max message body length May 15, 2026
@carter-ya carter-ya force-pushed the codex/configurable-max-body-len branch from c870425 to f8fca46 Compare May 15, 2026 12:08
@jackc
Copy link
Copy Markdown
Owner

jackc commented May 16, 2026

Is MaxBodyLen the right name for this at the pgconn.Config layer? It has a clear meaning at the pgproto3.Frontend layer. But at the connection / config layer I'm not sure I would immediately understand what it meant. 🤷

@carter-ya
Copy link
Copy Markdown
Author

Is MaxBodyLen the right name for this at the pgconn.Config layer? It has a clear meaning at the pgproto3.Frontend layer. But at the connection / config layer I'm not sure I would immediately understand what it meant. 🤷

Good point. MaxBodyLen mirrors the pgproto3.Frontend API, but I agree it may be too protocol-internal for pgconn.Config.

Would you prefer one of these directions?

  1. Keep it as a top-level pgconn.Config field, but use a more explicit name, e.g. MaxProtocolMessageBodyLen.
  2. Add a small protocol namespace, e.g. Config.Protocol.MaxMessageBodyLen, so future protocol-level frontend options have a clearer home.

My current leaning is option 1 as the smallest API change, but option 2 may be cleaner if you expect more protocol-level knobs to be exposed later.

@jackc
Copy link
Copy Markdown
Owner

jackc commented May 30, 2026

One additional bit of information: this already can be configured on connection with Config.BuildFrontend. This feature doesn't enable new functionality, it makes it a bit easier to use. Depending on how frequently this is used, it may or may not be necessary to add this helper. That said, I'm willing to accept it.

I would go for the top-level pgconn.Config field. That matches what we already have with MinProtocolVersion and MaxProtocolVersion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants