jackc/pgx · error

invalid value length

Error message

invalid value length %d at offset %d

What it means

Reader.Value read a value length that is negative but not the -1 NULL sentinel. Any length other than -1 must be non-negative; this indicates a malformed binary value field in the message.

Solutions

  1. Inspect the length field at the recorded offset in the raw message
  2. Verify the encoder emits -1 for NULL and non-negative lengths otherwise
  3. Treat as protocol corruption and drop the connection
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at internal/pgio/read.go:189 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04). Data as JSON: /api/errors/9f413fd29f626c36. Report an issue: GitHub.

Appendix: source

Thrown at internal/pgio/read.go:189

	}
	return count
}

// Value reads an int32 length followed by that many bytes — the standard
// PostgreSQL binary representation of a value. A length of -1 means NULL and
// returns (nil, true). Any other negative length is an error. The returned
// slice aliases the source; null is only meaningful if Err returns nil.
func (r *Reader) Value() (data []byte, null bool) {
	offset := r.rp
	length := r.Int32()
	if r.err != nil {
		return nil, false
	}
	if length == -1 {
		return nil, true
	}
	if length < 0 {
		r.fail(fmt.Errorf("invalid value length %d at offset %d", length, offset))
		return nil, false
	}
	return r.Bytes(int(length)), false
}

// Finish returns the first error encountered, or an error if unread bytes
// remain. Decoders that must consume the entire source should end with Finish.
func (r *Reader) Finish() error {
	if r.err != nil {
		return r.err
	}
	if r.rp != len(r.s) {
		return r.errTrailing()
	}
	return nil
}

func (r *Reader) errTrailing() error {

View on GitHub (pinned to ec1a0befd2)