• Stars
    star
    642
  • Rank 70,096 (Top 2 %)
  • Language
    C
  • License
    BSD 3-Clause "New...
  • Created over 9 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go library to parse and normalize SQL queries using the PostgreSQL query parser

pg_query_go GoDoc

Go version of https://github.com/pganalyze/pg_query

This Go library and its cgo extension use the actual PostgreSQL server source to parse SQL queries and return the internal PostgreSQL parse tree.

You can find further background to why a query's parse tree is useful here: https://pganalyze.com/blog/parse-postgresql-queries-in-ruby.html

Installation

go get github.com/pganalyze/pg_query_go/v4@latest

Due to compiling parts of PostgreSQL, the first time you build against this library it will take a bit longer.

Expect up to 3 minutes. You can use go build -x to see the progress.

Usage with Go modules

When integrating this library using Go modules, and using a vendor/ directory, you will need to explicitly copy over some of the C build files, since Go does not copy files in subfolders without .go files whilst vendoring.

The best way to do so is to use modvendor, and vendor your modules like this:

go mod vendor
go get -u github.com/goware/modvendor
modvendor -copy="**/*.c **/*.h **/*.proto" -v

Usage

Parsing a query into JSON

Put the following in a new Go package, after having installed pg_query as above:

package main

import (
	"fmt"

	pg_query "github.com/pganalyze/pg_query_go/v4"
)

func main() {
	tree, err := pg_query.ParseToJSON("SELECT 1")
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", tree)
}

Running will output the query's parse tree as JSON:

{"version":150001,"stmts":[{"stmt":{"SelectStmt":{"targetList":[{"ResTarget":{"val":{"A_Const":{"ival":{"ival":1},"location":7}},"location":7}}],"limitOption":"LIMIT_OPTION_DEFAULT","op":"SETOP_NONE"}}}]}

Parsing a query into Go structs

When working with the query information inside Go its recommended you use the Parse() method which returns Go structs:

package main

import (
	"fmt"

	pg_query "github.com/pganalyze/pg_query_go/v4"
)

func main() {
	result, err := pg_query.Parse("SELECT 42")
	if err != nil {
		panic(err)
	}

	// This will output "42"
	fmt.Printf("%d\n", result.Stmts[0].Stmt.GetSelectStmt().GetTargetList()[0].GetResTarget().GetVal().GetAConst().GetIval().Ival)
}

You can find all the node types in the pg_query.pb.go Protobuf definition.

Deparsing a parse tree back into a SQL statement

In order to go back from a parse tree to a SQL statement, you can use the deparsing functionality:

package main

import (
	"fmt"

	pg_query "github.com/pganalyze/pg_query_go/v4"
)

func main() {
	result, err := pg_query.Parse("SELECT 42")
	if err != nil {
		panic(err)
	}

	result.Stmts[0].Stmt.GetSelectStmt().GetTargetList()[0].GetResTarget().Val = pg_query.MakeAConstStrNode("Hello World", -1)

	stmt, err := pg_query.Deparse(result)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", stmt)
}

This will output the following:

SELECT 'Hello World'

Note that it is currently not recommended to pass unsanitized input to the deparser, as it may lead to crashes.

Parsing a PL/pgSQL function into JSON (Experimental)

Put the following in a new Go package, after having installed pg_query as above:

package main

import (
	"fmt"

	pg_query "github.com/pganalyze/pg_query_go/v4"
)

func main() {
	tree, err := pg_query.ParsePlPgSqlToJSON(
		`CREATE OR REPLACE FUNCTION cs_fmt_browser_version(v_name varchar, v_version varchar)
  			RETURNS varchar AS $$
  			BEGIN
  			    IF v_version IS NULL THEN
  			        RETURN v_name;
  			    END IF;
  			    RETURN v_name || '/' || v_version;
  			END;
  			$$ LANGUAGE plpgsql;`)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%s\n", tree)
}

Running will output the functions's parse tree as JSON:

$ go run main.go
[
{"PLpgSQL_function":{"datums":[{"PLpgSQL_var":{"refname":"v_name","datatype":{"PLpgSQL_type":{"typname":"UNKNOWN"}}}},{"PLpgSQL_var":{"refname":"v_version","datatype":{"PLpgSQL_type":{"typname":"UNKNOWN"}}}},{"PLpgSQL_var":{"refname":"found","datatype":{"PLpgSQL_type":{"typname":"UNKNOWN"}}}}],"action":{"PLpgSQL_stmt_block":{"lineno":2,"body":[{"PLpgSQL_stmt_if":{"lineno":3,"cond":{"PLpgSQL_expr":{"query":"v_version IS NULL"}},"then_body":[{"PLpgSQL_stmt_return":{"lineno":4,"expr":{"PLpgSQL_expr":{"query":"v_name"}}}}]}},{"PLpgSQL_stmt_return":{"lineno":6,"expr":{"PLpgSQL_expr":{"query":"v_name || '/' || v_version"}}}}]}}}}
]

Benchmarks

$ make benchmark
go build -a
go test -test.bench=. -test.run=XXX -test.benchtime 10s -test.benchmem -test.cpu=4
goos: darwin
goarch: arm64
pkg: github.com/pganalyze/pg_query_go/v4
BenchmarkParseSelect1-4                  	 3230398	      3656 ns/op	    1104 B/op	      20 allocs/op
BenchmarkParseSelect2-4                  	  927363	     12739 ns/op	    2896 B/op	      59 allocs/op
BenchmarkParseCreateTable-4              	  399819	     30080 ns/op	    8432 B/op	     151 allocs/op
BenchmarkParseSelect1Parallel-4          	10951803	      1094 ns/op	    1104 B/op	      20 allocs/op
BenchmarkParseSelect2Parallel-4          	 3255471	      3675 ns/op	    2896 B/op	      59 allocs/op
BenchmarkParseCreateTableParallel-4      	 1341716	      8919 ns/op	    8432 B/op	     151 allocs/op
BenchmarkRawParseSelect1-4               	 4275111	      2795 ns/op	     192 B/op	       5 allocs/op
BenchmarkRawParseSelect2-4               	 1252704	      9534 ns/op	     352 B/op	       5 allocs/op
BenchmarkRawParseCreateTable-4           	  503385	     23168 ns/op	    1120 B/op	       5 allocs/op
BenchmarkRawParseSelect1Parallel-4       	15446528	       780.1 ns/op	     192 B/op	       5 allocs/op
BenchmarkRawParseSelect2Parallel-4       	 4638837	      2595 ns/op	     352 B/op	       5 allocs/op
BenchmarkRawParseCreateTableParallel-4   	 1932316	      6197 ns/op	    1120 B/op	       5 allocs/op
BenchmarkFingerprintSelect1-4            	 6583965	      1795 ns/op	     112 B/op	       4 allocs/op
BenchmarkFingerprintSelect2-4            	 2962663	      4015 ns/op	     112 B/op	       4 allocs/op
BenchmarkFingerprintCreateTable-4        	 1796041	      6831 ns/op	     112 B/op	       4 allocs/op
BenchmarkNormalizeSelect1-4              	10073278	      1171 ns/op	      72 B/op	       4 allocs/op
BenchmarkNormalizeSelect2-4              	 6029834	      1932 ns/op	     104 B/op	       4 allocs/op
BenchmarkNormalizeCreateTable-4          	 4703816	      2490 ns/op	     184 B/op	       4 allocs/op
PASS
ok  	github.com/pganalyze/pg_query_go/v4	273.449s

Note that allocation counts exclude the cgo portion, so they are higher than shown here.

See benchmark_test.go for details on the benchmarks.

Authors

License

Copyright (c) 2015, Lukas Fittl [email protected]
pg_query_go is licensed under the 3-clause BSD license, see LICENSE file for details.

This project includes code derived from the PostgreSQL project, see LICENSE.POSTGRESQL for details.

More Repositories

1

libpg_query

C library for accessing the PostgreSQL parser outside of the server environment
C
1,173
star
2

pg_query

Ruby extension to parse, deparse and normalize SQL queries using the PostgreSQL query parser
C
778
star
3

collector

pganalyze statistics collector for gathering PostgreSQL metrics and log data
Go
333
star
4

pg_query.rs

Rust library to parse, deparse and normalize SQL queries using the PostgreSQL query parser
Rust
125
star
5

pg-query-emscripten

Emscripten Port of pg_query to easily play with it in the browser
C++
76
star
6

activerecord-copy

Supports binary COPY into PostgreSQL with activerecord
Ruby
41
star
7

queryparser

DEPRECATED - use libpg_query instead! https://github.com/lfittl/libpg_query
C
38
star
8

lint

Check for missing indexes during development using constraint programming
Rust
35
star
9

materialized-views-demo

Example for using Materialized Views in Rails
Ruby
18
star
10

sidekiq_server.rs

A Sidekiq server implemented in Rust
Rust
16
star
11

pg_simulator

PostgreSQL Schema Simulator - Load schema and statistics information into a database and run EXPLAIN on queries
Ruby
15
star
12

react-svg-example

Example of how to build UI components with React and SVG
TypeScript
14
star
13

subqueries-rails-example

Ruby
11
star
14

pgcon2023

Simple index selection model using constraint programming presented at PGCon 2023
Python
10
star
15

pganalyze-docs

Documentation for pganalyze - PostgreSQL Performance Monitoring
MDX
10
star
16

graphql-batch-example

Code example for "Efficient GraphQL queries in Ruby on Rails & Postgres" blog post
Ruby
9
star
17

cp-sat-python-example

Python
9
star
18

postgis_mock

Simplified version of the PostGIS extension functions, types and operators, to be used for parsing/planning queries that depend on PostGIS APIs, but without calling actual PostGIS code.
PLpgSQL
3
star
19

pgday-chicago-2024

Python
3
star
20

full-text-search-rails

Example for article "Full Text Search in Milliseconds with Rails and PostgreSQL"
Ruby
2
star
21

collector-snapshot

Protocol Buffers definition for the pganalyze collector
1
star
22

heroku-plugin-psql

Like heroku pg:psql but for any Postgres database
TypeScript
1
star
23

postgres-local-sync

Heroku helper app to synchronize your staging database to local development, via S3
Ruby
1
star
24

index-advisor-feedback

1
star