jackc/pgx · error

cannot scan to netip.Addr

Error message

cannot scan %v to netip.Addr

What it means

Returned by netipAddrWrapper.ScanNetipPrefix when the prefix's Bits() is smaller than the address bit length. Converting to a bare netip.Addr would silently discard the prefix length, so the scan is rejected.

Solutions

  1. Scan into netip.Prefix to keep the prefix length
  2. Use host() in SQL to mask the address to its prefix before scanning
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgtype/builtin_wrappers.go:593 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/1f423f11ad674e44. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/builtin_wrappers.go:593

func (w *netipPrefixWrapper) ScanNetipPrefix(v netip.Prefix) error {
	*w = netipPrefixWrapper(v)
	return nil
}

func (w netipPrefixWrapper) NetipPrefixValue() (netip.Prefix, error) {
	return netip.Prefix(w), nil
}

type netipAddrWrapper netip.Addr

func (w *netipAddrWrapper) ScanNetipPrefix(v netip.Prefix) error {
	if !v.IsValid() {
		*w = netipAddrWrapper(netip.Addr{})
		return nil
	}

	if v.Addr().BitLen() != v.Bits() {
		return fmt.Errorf("cannot scan %v to netip.Addr", v)
	}

	*w = netipAddrWrapper(v.Addr())

	return nil
}

func (w netipAddrWrapper) NetipPrefixValue() (netip.Prefix, error) {
	addr := (netip.Addr)(w)
	if !addr.IsValid() {
		return netip.Prefix{}, nil
	}

	return netip.PrefixFrom(addr, addr.BitLen()), nil
}

type mapStringToPointerStringWrapper map[string]*string

View on GitHub (pinned to ec1a0befd2)