GitXplorerGitXplorer
s

pgwire

public
545 stars
47 forks
14 issues

Commits

List of commits on branch master.
Verified
0c36b860bfcf074f79c54abaa79af0ff7569ef24

feat(API):Upgrade server version format for compatibility improvement. (#233)

ssunng87 committed a day ago
Verified
b6aebbd969183b8241692f7fa1f53960eeca96e9

feat: first batch of client APIs (#231)

ssunng87 committed 5 days ago
Verified
525444e5c504013298b4001014b7f727b4dfda21

update gluesql to 0.16 (#230)

sserprex committed 10 days ago
Verified
5374828b0bbc8de959be5b3381b5b4a7d5c3d6f9

tls feature improvements (#228)

sserprex committed a month ago
Verified
b179036ab44d2e9ebc3da552740771681692cda2

chore: Release

ssunng87 committed a month ago
Verified
5516012292ca1b18354006e546019122fb8761ac

chore: changelog

ssunng87 committed a month ago

README

The README file for this repository.

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

  • [x] Message format
    • [x] Frontend-Backend protocol messages
    • [ ] Streaming replication protocol
    • [ ] Logical streaming replication protocol message
  • [x] Backend TCP/TLS server on Tokio
  • [x] Frontend-Backend interaction over TCP
    • [x] SSL Request and Response
      • [x] PostgreSQL 17 direct SSL negotiation
    • [x] Startup
      • [x] No authentication
      • [x] Clear-text password authentication
      • [x] Md5 Password authentication
      • [x] SASL SCRAM authentication (optional feature server-api-scram-ring or server-api-scram-aws-lc-rs)
        • [x] SCRAM-SHA-256
        • [x] SCRAM-SHA-256-PLUS
    • [x] Simple Query and Response
    • [x] Extended Query and Response
      • [x] Parse
      • [x] Bind
      • [x] Execute
      • [x] Describe
      • [x] Sync
    • [x] Termination
    • [x] Cancel
    • [x] Error and Notice
    • [x] Copy
    • [x] Notification
  • [ ] Streaming replication over TCP
  • [ ] Logical streaming replication over TCP
  • [x] Data types
    • [x] Text format
    • [x] Binary format, implemented in postgres-types
  • [ ] APIs
    • [x] Startup APIs
      • [x] AuthSource API, fetching and hashing passwords
      • [x] Server parameters API, ready but not very good
    • [x] Simple Query API
    • [x] Extended Query API
      • [x] QueryParser API, for transforming prepared statement
    • [x] ResultSet builder/encoder API
    • [ ] Query Cancellation API
    • [x] Error and Notice API
    • [x] Copy API
      • [x] Copy-in
      • [x] Copy-out
      • [x] Copy-both
    • [x] Transaction state
    • [ ] Streaming replication over TCP
    • [ ] Logical streaming replication server API

About Postgres Wire Protocol

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

  • Startup: client-server handshake and authentication.
  • Simple Query: The text-based query protocol of postgresql. Query are provided as string, and server is allowed to stream data in response.
  • Extended Query: A new sub-protocol for query which has ability to cache the query on server-side and reuse it with new parameters. The response part is identical to Simple Query.
  • Copy: the subprotocol to copy data from and to postgresql.
  • Replication
  • Logical Replication

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:

  • examples/sqlite.rs: uses an in-memory sqlite database at its core and serves it with postgresql protocol. This is a full example with both simple and extended query implementation. cargo run --features _sqlite_ --example sqlite
  • examples/duckdb.rs: similar to sqlite example but with duckdb backend. Note that not all data types are implemented in this example. cargo run --features _duckdb_ --example duckdb
  • examples/gluesql.rs: uses an in-memory gluesql at its core and serves it with postgresql protocol.
  • examples/server.rs: demos a server that always returns fixed results.
  • examples/secure_server.rs: demos a server with ssl support and always returns fixed results.
  • examples/scram.rs: demos how to configure more secure authentication mechanism: SCRAM
  • examples/transaction.rs: see how to control transaction state at wire protocol level.
  • examples/datafusion.rs: Now moved to datafusion-postgres

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

  • GreptimeDB: Cloud-native time-series database
  • risinglight: OLAP database system for educational purpose
  • PeerDB Postgres first ETL/ELT, enabling 10x faster data movement in and out of Postgres
  • CeresDB CeresDB is a high-performance, distributed, cloud native time-series database from AntGroup.
  • dozer a real-time data platform for building, deploying and maintaining data products.
  • restate Framework for building resilient workflow

License

This library is released under MIT/Apache dual license.