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)
- 100% Native Go: No CGO, no external dependencies.
- AST Compatibility: The generated Abstract Syntax Tree (AST) strictly matches PostgreSQL's internal
Nodestructures (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.
go get github.com/bytebase/pgparserpackage 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))
}This project is not a hand-written parser. It is a port of the official PostgreSQL source code:
- Grammar:
parser/gram.yis a translated version of PostgreSQL'ssrc/backend/parser/gram.y. - Lexer:
parser/scan.llogic is reimplemented in Go inparser/lexer.go. - Nodes:
nodes/contains Go struct definitions that map 1:1 to PostgreSQL's C structs.
- Go 1.18+
goyacc(installed viago install golang.org/x/tools/cmd/goyacc@latest)
# 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 -vThis project is licensed under the MIT License. Portions derived from PostgreSQL are subject to the PostgreSQL License.