Patch for bind message regarding the number of parameters and result column format codes

First seen: 2026-05-21 01:12:34+00:00 · Messages: 2 · Participants: 1

Latest Update

2026-05-22 · claude-opus-4-6

Patch for Bind Message: Unsigned Integer Handling for Parameter and Result Column Format Codes

Core Technical Problem

The PostgreSQL frontend/backend protocol (v3) specifies that certain count fields in the Bind message are transmitted as unsigned 16-bit integers (Int16 in the protocol docs). However, the backend code in postgres.c reads these values using signed integer handling, creating a semantic mismatch between the protocol specification and the implementation.

The three affected fields are:

  1. Parameter format codes count (protocol line 4323, read at postgres.c:1725) — indicates how many format codes follow for parameter values
  2. Parameter values count (protocol line 4348, read at postgres.c:1734) — indicates the number of parameter values in the Bind message
  3. Result-column format codes count (protocol line 4396, read at postgres.c:2017) — indicates how many format codes follow for result columns

Why This Matters Architecturally

The Bind message is the fundamental mechanism by which prepared statements receive their parameter values in the extended query protocol. Every parameterized query flows through this code path. While in practice the signed/unsigned distinction for a 16-bit integer only matters when values exceed 32767 (which would mean more than 32767 parameters — an extreme case), the correctness of the implementation relative to the protocol specification matters for:

Proposed Solutions

Patch 1 (Dave Cramer)

The initial patch corrects the reading of these three specific count fields to use unsigned integer semantics, ensuring they match the protocol documentation.

Patch 2 (Austin Bonander, AI-assisted)

A more comprehensive patch that audits and annotates all signed/unsigned integer reads in the protocol message handling, not just the three identified in the Bind message. This takes a broader approach to ensuring protocol compliance across the board.

Key Technical Insights

The protocol specification uses Int16 for these count fields, which in the PostgreSQL protocol documentation denotes an unsigned 16-bit integer for count/length fields. The C code likely uses pq_getmsgint() which returns a signed integer type, and the receiving variable may be declared as int or int16. The fix would involve either:

This is a low-risk, high-correctness patch since the practical impact only manifests with extremely high parameter counts, but it aligns implementation with specification — a principle that matters for a database system's protocol layer.

Methodology Note

The second patch was explicitly noted as being created with AI assistance (Claude), which is notable as an emerging pattern in PostgreSQL development where AI tools are used to perform systematic audits of code for similar patterns across a codebase.

Current Status

The thread is in its early stages with only two messages. No committer feedback has been provided yet. The patches await review from the community, particularly from developers familiar with the protocol layer (src/backend/tcop/postgres.c and libpq protocol handling).