GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Why Go
  • Case Studies
  • Use Cases
  • Security
  • Learn
  • Effective Go
  • Go User Manual
  • Standard library
  • Release Notes
  • API
  • Packages
  • Recorded Talks
  • Meetups
  • Conferences

This view

  • Original page
  • New address

GeezerVison is showing pkg.go.dev — pgparser module - github.com/bytebase/pgparser - Go Packages

Details

  • checked Valid go.mod file

    The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.

  • checked Redistributable license

    Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.

  • checked Tagged version

    Modules with tagged versions give importers more predictable builds.

  • unchecked Stable version

    When a project reaches major version v1 it is considered stable.

  • Learn more about best practices

Repository

github.com/bytebase/pgparser

Links

  • Open Source Insights Logo Open Source Insights

README ¶

pgparser - Pure Go PostgreSQL Parser

pgparser is a pure Go implementation of the PostgreSQL SQL parser. It translates the original PostgreSQL grammar (gram.y) into Go using goyacc, ensuring 100% compatibility with the PostgreSQL parse tree structure.

Target PostgreSQL Version: 17.7 (REL_17_STABLE)

Features

  • 100% Native Go: No CGO, no external dependencies.
  • AST Compatibility: The generated Abstract Syntax Tree (AST) strictly matches PostgreSQL's internal Node structures (parsenodes.h), enabling seamless integration with other PG-compatible tools.
  • Battle-Tested: Verified against PostgreSQL's own massive regression test suite (~45,000 SQL statements) with a 99.6% pass rate.
  • Thread Safe: The parser is designed to be safe for concurrent use.

Installation

go get github.com/bytebase/pgparser

Quick Start

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/bytebase/pgparser/parser"
)

func main() {
	sql := "SELECT * FROM users WHERE id > 100"

	// Parse the SQL string
	stmts, err := parser.Parse(sql)
	if err != nil {
		log.Fatalf("Parse error: %v", err)
	}

	// Print the AST as JSON
	jsonBytes, _ := json.MarshalIndent(stmts, "", "  ")
	fmt.Println(string(jsonBytes))
}

Architecture

This project is not a hand-written parser. It is a port of the official PostgreSQL source code:

  1. Grammar: parser/gram.y is a translated version of PostgreSQL's src/backend/parser/gram.y.
  2. Lexer: parser/scan.l logic is reimplemented in Go in parser/lexer.go.
  3. Nodes: nodes/ contains Go struct definitions that map 1:1 to PostgreSQL's C structs.

Development

Requirements
  • Go 1.18+
  • goyacc (installed via go install golang.org/x/tools/cmd/goyacc@latest)
Build & Test
# Regenerate parser (if gram.y changed)
make generate-parser

# Run tests
go test ./...

# Run PostgreSQL regression tests compatibility check
go test ./parser/pgregress -run TestPGRegressStats -v

License

This project is licensed under the MIT License. Portions derived from PostgreSQL are subject to the PostgreSQL License.

Directories ¶

Path Synopsis
cmd
pgsema-gen command
Command pgsema-gen generates Go code from PostgreSQL source files.
Command pgsema-gen generates Go code from PostgreSQL source files.
nodes
Package nodes defines PostgreSQL parse tree node types.
Package nodes defines PostgreSQL parse tree node types.
parser
Package parser implements a PostgreSQL-compatible SQL lexer and parser.
Package parser implements a PostgreSQL-compatible SQL lexer and parser.
pgregress
Package pgregress provides tools for running PostgreSQL regression test SQL files against the pgparser parser to verify parse compatibility.
Package pgregress provides tools for running PostgreSQL regression test SQL files against the pgparser parser to verify parse compatibility.
tools
pg_parse_diff command
Click to show internal directories.
Click to hide internal directories.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.