jackc/pgx · error

invalid tsvector format: unexpected end after backslash

Error message

invalid tsvector format: unexpected end after backslash

What it means

consumeLexeme hit end-of-input immediately after a backslash: a backslash escape must be followed by the escaped character, so the input ends mid-escape.

Solutions

  1. Complete the backslash escape or remove the trailing backslash
  2. Escape backslashes themselves (\\) when they are literal
  3. Fix programmatically generated tsvector strings
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgtype/tsvector.go:389 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/81455065689bb807. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/tsvector.go:389

		ch, end := p.consume()
		if end {
			return "", fmt.Errorf("invalid tsvector format: unterminated quoted lexeme")
		}

		switch ch {
		case '\'':
			// Escaped quote ('') — write a literal single quote
			if !p.atEnd() && p.peek() == '\'' {
				p.consume()
				buf.WriteByte('\'')
			} else {
				// Closing quote — lexeme is complete
				return buf.String(), nil
			}
		case '\\':
			next, end := p.consume()
			if end {
				return "", fmt.Errorf("invalid tsvector format: unexpected end after backslash")
			}
			buf.WriteByte(next)
		default:
			buf.WriteByte(ch)
		}
	}
}

// consumePositions consumes a comma-separated list of position[weight] values.
func (p *tsvectorParser) consumePositions() ([]TSVectorPosition, error) {
	var positions []TSVectorPosition

	for {
		pos, err := p.consumePosition()
		if err != nil {
			return nil, err
		}
		positions = append(positions, pos)

View on GitHub (pinned to ec1a0befd2)