sunng87 / pgwire

PostgreSQL wire protocol implemented as a rust library.
Apache License 2.0
512 stars 36 forks source link
database postgresql rust

pgwire

CI Docs

Build Postgres compatible access layer for your data service.

This library implements PostgreSQL Wire Protocol, and provide essential APIs to write PostgreSQL compatible servers and clients. If you are interested in related topic, you can check project ideas to build on top of this library.

Status

About Postgres Wire Protocol

Postgres Wire Protocol is a relatively general-purpose Layer-7 protocol. There are 6 parts of the protocol:

Also note that Postgres Wire Protocol has no semantics about SQL, so literally you can use any query language, data formats or even natural language to interact with the backend.

The response are always encoded as data row format. And there is a field description as header of the data to describe its name, type and format.

Jelte Fennema-Nio's on talk on PgConf.dev 2024 has a great coverage of how the wire protocol works: https://www.youtube.com/watch?v=nh62VgNj6hY

Usage

Server/Backend

To use pgwire in your server application, you will need to implement two key components: startup processor and query processor. For query processing, there are two kinds of queries: simple and extended. By adding SimpleQueryHandler to your application, you will get psql command-line tool compatibility. And for more language drivers and additional prepared statement, binary encoding support, ExtendedQueryHandler is required.

Examples are provided to demo the very basic usage of pgwire on server side:

Client/Frontend

I think in most case you do not need pgwire to build a postgresql client, existing postgresql client like rust-postgres should fit your scenarios. Please rise an issue if there is a scenario.

Projects using pgwire

License

This library is released under MIT/Apache dual license.