jackc/pgx · error

invalid tsvector format: unterminated quoted lexeme

Error message

invalid tsvector format: unterminated quoted lexeme

What it means

consumeLexeme reached the end of input while inside a single-quoted lexeme: the closing quote is missing, so the quoted lexeme is unterminated.

Solutions

  1. Add the closing single quote to the lexeme
  2. Escape embedded quotes by doubling them ('')
  3. Validate tsvector text before scanning
Defensive patterns

Strategy: validation

When it happens

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

Appendix: source

Thrown at pgtype/tsvector.go:373

func (p *tsvectorParser) consumeSpaces() {
	for !p.atEnd() && p.peek() == ' ' {
		p.consume()
	}
}

// consumeLexeme consumes a single-quoted lexeme, handling single quotes and backslash escapes.
func (p *tsvectorParser) consumeLexeme() (string, error) {
	ch, end := p.consume()
	if end || ch != '\'' {
		return "", fmt.Errorf("invalid tsvector format: lexeme must start with a single quote")
	}

	var buf strings.Builder
	for {
		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)

View on GitHub (pinned to ec1a0befd2)