jackc/pgx · error

cannot scan NULL into *time.Interval

Error message

cannot scan NULL into *time.Interval

What it means

ScanInterval was given a NULL Interval while scanning into *time.Interval (a durationWrapper), which cannot represent NULL. Null guard in the interval-to-duration conversion; note the wrapper also flattens months/days into microseconds.

Solutions

  1. Use COALESCE with a zero interval default
  2. Scan into pgtype.Interval or *time.Duration to handle NULLs
  3. Ensure the column is NOT NULL
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgtype/builtin_wrappers.go:504 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/37d9314e9f3dd806. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/builtin_wrappers.go:504

	return nil
}

func (w timeWrapper) TimeValue() (Time, error) {
	t := time.Time(w)
	usec := int64(t.Hour())*microsecondsPerHour +
		int64(t.Minute())*microsecondsPerMinute +
		int64(t.Second())*microsecondsPerSecond +
		int64(t.Nanosecond())/1000
	return Time{Microseconds: usec, Valid: true}, nil
}

type durationWrapper time.Duration

func (w durationWrapper) SkipUnderlyingTypePlan() {}

func (w *durationWrapper) ScanInterval(v Interval) error {
	if !v.Valid {
		return fmt.Errorf("cannot scan NULL into *time.Interval")
	}

	us := int64(v.Months)*microsecondsPerMonth + int64(v.Days)*microsecondsPerDay + v.Microseconds
	*w = durationWrapper(time.Duration(us) * time.Microsecond)
	return nil
}

func (w durationWrapper) IntervalValue() (Interval, error) {
	return Interval{Microseconds: int64(w) / 1000, Valid: true}, nil
}

type netIPNetWrapper net.IPNet

func (w *netIPNetWrapper) ScanNetipPrefix(v netip.Prefix) error {
	if !v.IsValid() {
		return fmt.Errorf("cannot scan NULL into *net.IPNet")
	}

View on GitHub (pinned to ec1a0befd2)