jackc/pgx · error

unexpected escape in quoted string: found '%#v'

Error message

unexpected escape in quoted string: found '%#v'

What it means

Inside a double-quoted hstore string, a backslash escape was followed by a character other than '\\' or '"', which the escape rules do not allow. The faulting input is the invalid escape sequence's second byte, shown in the message.

Solutions

  1. Escape only backslash and double-quote inside hstore quoted strings
  2. Regenerate the literal via the hstore extension or a proper encoder
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgtype/hstore.go:378 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/b022434fdbc9661e. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/hstore.go:378

	p.pos = firstBackslash

	// copy bytes until the end, unescaping backslashes
quotedString:
	for {
		nextB, end := p.consume()
		switch {
		case end:
			return "", errEOSInQuoted
		case nextB == '"':
			break quotedString
		case nextB == '\\':
			// escape: skip the backslash and copy the char
			nextB, end = p.consume()
			if end {
				return "", errEOSInQuoted
			}
			if !(nextB == '\\' || nextB == '"') {
				return "", fmt.Errorf("unexpected escape in quoted string: found '%#v'", nextB)
			}
			builder.WriteByte(nextB)
		default:
			// normal byte: copy it
			builder.WriteByte(nextB)
		}
	}
	return builder.String(), nil
}

// consumePairSeparator consumes the Hstore pair separator ", " or returns an error.
func (p *hstoreParser) consumePairSeparator() error {
	return p.consumeExpected2(',', ' ')
}

// consumeKVSeparator consumes the Hstore key/value separator "=>" or returns an error.
func (p *hstoreParser) consumeKVSeparator() error {
	return p.consumeExpected2('=', '>')

View on GitHub (pinned to ec1a0befd2)