pgrust README

项目的 README,来自 github.com/malisper/pgrust。合并为一个 markdown 文件:README.md。

pgrust

A Postgres-compatible database, rewritten in Rust, built for fast analytics on your Postgres data.

Postgres 18.6 Version: v0.4-beta License: AGPL-3.0

Try pgrust in your browser →   |   Discord   |   Updates   |   Issues

New: Video of Michael's talk at PlanetScale on pgrust — and the slides.

How AI Changes the Economics of JIT Compilers

v0.4-beta is out: the release post and what's new.

pgrust is a drop-in replacement for Postgres built for fast analytics. It is wire compatible and SQL dialect compatible with Postgres 18, so your existing drivers, ORMs, psql and tools work unchanged. It is Postgres rebuilt the way it would be built in 2026.

Every line is Rust, written to match the behavior of the C implementation. Rust makes it easy to re-architect several core Postgres pieces. pgrust has:

and many other really awesome pieces. The scheduler and the memory manager go after two of the four horsemen behind thousands of Postgres outages.

Quickstart

In your browser: https://pgrust.com, a full pgrust server compiled to WebAssembly, no install.

On your machine: download a release tarball from https://pgrust.com/downloads/v0.4-beta/. Each one is self-contained: it carries pgrust's own bin/postgres, bin/initdb and bin/psql plus the share files they read, so you do not need a PostgreSQL install. The Linux binaries need glibc 2.34 or newer (Debian 12, Ubuntu 22.04, RHEL/Rocky 9 and later); the macOS binaries need macOS 11 or newer.

Tarball Runs on
pgrust-0.4-beta-linux-x86_64.tar.gz any x86-64 CPU
pgrust-0.4-beta-linux-aarch64.tar.gz any 64-bit Arm CPU
pgrust-0.4-beta-linux-x86_64-znver3.tar.gz tuned for AMD Zen 3 and newer
pgrust-0.4-beta-linux-aarch64-neoverse-v2.tar.gz tuned for Neoverse-V2 (AWS Graviton4, NVIDIA Grace)
pgrust-0.4-beta-macos-arm64.tar.gz macOS on Apple Silicon
pgrust-0.4-beta-macos-x86_64.tar.gz macOS on Intel
pgrust-0.4-beta-macos-universal.tar.gz macOS, either (both architectures in one binary)

The Linux builds are profile-guided optimized. The tuned builds use instructions only those CPUs (and newer ones) have; when in doubt, take the generic build. Every tarball has a .sha256 file next to it, and MANIFEST.txt lists them all.

Linux

# Pick your build: linux-x86_64, linux-aarch64, linux-x86_64-znver3 or
# linux-aarch64-neoverse-v2 (see the table above).
PLATFORM=linux-x86_64

# Download pgrust, verify the checksum and unpack it.
curl -LO "https://pgrust.com/downloads/v0.4-beta/pgrust-0.4-beta-$PLATFORM.tar.gz"
curl -LO "https://pgrust.com/downloads/v0.4-beta/pgrust-0.4-beta-$PLATFORM.tar.gz.sha256"
sha256sum -c "pgrust-0.4-beta-$PLATFORM.tar.gz.sha256"
tar xzf "pgrust-0.4-beta-$PLATFORM.tar.gz"
cd pgrust-0.4-beta-linux-*/

# Create a data directory with pgrust's initdb.
bin/initdb -D /tmp/pgrust-data --no-locale --encoding UTF8 -U postgres

# Start the server. It runs in the foreground and logs to this terminal.
ulimit -s 65520
RUST_MIN_STACK=33554432 bin/postgres \
  -D /tmp/pgrust-data \
  -k /tmp -p 5432 \
  -c listen_addresses= \
  -c io_method=sync \
  -c max_stack_depth=60000

Leave the server running and connect from a second terminal, in the same directory:

bin/psql -h /tmp -p 5432 -U postgres -c "select version()"

You should see PostgreSQL 18.6 (pgrust 0.4-beta, build <commit>) on ..., where <commit> is the short commit the binary was built from. bin/postgres --version prints pgrust 0.4-beta (PostgreSQL 18.6 compatible).

Docker

The prebuilt image on Docker Hub, malisper/pgrust, is a drop-in replacement for the official postgres image: same env vars (POSTGRES_PASSWORD, POSTGRES_USER, POSTGRES_DB, PGDATA, ...), same /docker-entrypoint-initdb.d init scripts, same data volume at /var/lib/postgresql/data. It carries the same postgres and initdb binaries as the Linux release tarballs above, and initializes the cluster with pgrust's own initdb; the only C PostgreSQL component in the image is psql for the init scripts. malisper/pgrust:v0.4-beta is multi-arch (amd64 + arm64); v0.4-beta-amd64 and v0.4-beta-arm64 pin one architecture.

docker run -d --name pgrust -e POSTGRES_PASSWORD=secret -p 5432:5432 malisper/pgrust:v0.4-beta

Then connect, with your own psql or the one in the image:

psql -h localhost -p 5432 -U postgres
docker exec -it pgrust psql -U postgres

malisper/pgrust:latest still points at v0.3; use the v0.4-beta tag to get this release. A data volume written by v0.3 holding columnar tables needs the dump and restore described under Stopping, restarting, cleaning up.

macOS

# Pick your build: macos-arm64 (Apple Silicon), macos-x86_64 (Intel) or
# macos-universal (both).
PLATFORM=macos-arm64

# Download pgrust, verify the checksum and unpack it. Download with curl: curl
# does not set the quarantine flag a browser download gets, so Gatekeeper stays
# out of the way (see the note below).
curl -LO "https://pgrust.com/downloads/v0.4-beta/pgrust-0.4-beta-$PLATFORM.tar.gz"
curl -LO "https://pgrust.com/downloads/v0.4-beta/pgrust-0.4-beta-$PLATFORM.tar.gz.sha256"
shasum -a 256 -c "pgrust-0.4-beta-$PLATFORM.tar.gz.sha256"
tar xzf "pgrust-0.4-beta-$PLATFORM.tar.gz"
cd "pgrust-0.4-beta-$PLATFORM"

# Create a data directory with pgrust's initdb.
bin/initdb -D /tmp/pgrust-data --no-locale --encoding UTF8 -U postgres

# Start the server. It runs in the foreground and logs to this terminal.
ulimit -s 65520
RUST_MIN_STACK=33554432 bin/postgres \
  -D /tmp/pgrust-data \
  -k /tmp -p 5432 \
  -c listen_addresses= \
  -c io_method=sync \
  -c max_stack_depth=60000

Leave the server running and connect from a second terminal, in the same directory:

bin/psql -h /tmp -p 5432 -U postgres -c "select version()"

A note on Gatekeeper: the macOS binaries are signed ad hoc, not notarized by Apple. Downloaded with curl as above, they are never quarantined and none of this comes up. If you download with a browser instead, macOS refuses to run them with "Apple could not verify ... is free of malware". Clear the flag with xattr -dr com.apple.quarantine pgrust-0.4-beta-$PLATFORM, or approve each binary under System Settings > Privacy & Security > "Open Anyway".

Columnar tables

Columnar storage (pgrcolumnar) is a table access method. Register it once in each database, then create tables with USING pgrcolumnar:

CREATE ACCESS METHOD pgrcolumnar TYPE TABLE HANDLER heap_tableam_handler;

CREATE TABLE events (id bigint, kind text, val double precision, at timestamp)
  USING pgrcolumnar;

Stopping, restarting, cleaning up

When to use pgrust

Status

We're working with a few early design partners and are targeting production readiness in Q1 2027. Testing and reliability are our top priority; see Testing for how we verify pgrust against Postgres. We run pgrust ourselves internally today.

Extensions

pgrust ships the most-used Postgres extensions built in: pgvector, pg_cron, pg_stat_statements, pg_trgm, hstore, pgcrypto, postgres_fdw and most of contrib. Install them with CREATE EXTENSION as usual.

pgrust doesn't load C extension binaries compiled for Postgres, but porting an existing extension to pgrust is straightforward, and we add new ones regularly. If you need an extension that isn't listed, open an issue and we'll prioritize it. PL/Python, PL/Perl and PL/Tcl are not supported yet.

Performance

Measured on c8g.4xlarge (AWS Graviton4), pgrust with pgrcolumnar, its builtin columnar layout, runs ClickBench's analytical queries up to 1,600x faster than PostgreSQL (per query, cold runs, against PostgreSQL's published results on the same instance type).

For a code-level walkthrough of one part of that speedup, read how batching, operator fusion, and SIMD speed up the query engine.

Writing

Testing

The most common question we get about pgrust is: how can anyone trust it? Passing a test suite doesn't mean our code is correct. Getting to the point where people can trust pgrust is our number one priority today.

To achieve that goal, we're taking several different approaches:

  1. Of the 3,400 user facing Postgres functions, we've formally verified that 1,397 have identical behavior in pgrust with Kani; 466 more are tested against Postgres. The ledger is proofs/USER_FACING_FUNCTIONS.tsv.
  2. We're engaging with Antithesis to do simulation testing of pgrust to battle test pgrust.
  3. We're doing aggressive differential fuzz testing to ensure pgrust's behavior is identical to Postgres's.

Unsafe Code

pgrust uses unsafe code, but only for the specific things that need it. Postgres represents every internal value as a Datum, an untyped 8 byte value, and Rust has no safe equivalent, so the code that packs and unpacks internal values is unsafe. The same goes for the places where pgrust must match Postgres' memory layouts byte for byte.

If there are particular unsafes that are not necessary and removing them would not have an impact on performance, let us know, as we would love to remove more unsafes.

Building from source

You need the Rust toolchain (any rustup install works; rust-toolchain.toml pins the build to Rust 1.96.0 and rustup fetches it automatically) and the RE2 regex library:

cargo build --release --locked -p main_main -p initdb

select version() names the build: a build in a git checkout prints its short commit (PostgreSQL 18.6 (pgrust 0.4-beta, build 1a2b3c4) on ...); set PGRUST_BUILD_ID at build time to choose the id, or build outside git for none. postgres --version prints pgrust 0.4-beta (PostgreSQL 18.6 compatible); postgres -V prints postgres (PostgreSQL) 18.6, the line PostgreSQL's own pg_ctl, initdb and pg_upgrade check.

The server binary lands at target/release/postgres, pgrust's initdb beside it, and the share tree it needs (postgres.bki, the setup SQL, timezone, timezonesets, tsearch data, extension files) is staged at target/release/share/. No C PostgreSQL is involved in creating or running a cluster:

target/release/initdb -D /tmp/pgrust-data --no-locale --encoding UTF8 -U postgres
ulimit -s 65520
RUST_MIN_STACK=33554432 target/release/postgres -D /tmp/pgrust-data \
  -k /tmp -p 5432 -c listen_addresses= -c io_method=sync -c max_stack_depth=60000

The time zone database is PostgreSQL 18.6's own compiled tzdata, read from share/timezone beside the binary (never the host zoneinfo, like C built without --with-system-tzdata). To use the system zoneinfo instead, build with PGRUST_SYSTEM_TZDATA=/usr/share/zoneinfo (C's --with-system-tzdata); PGRUST_TZDIR and PGRUST_PGSHAREDIR remain runtime overrides, not requirements. A data directory made by stock PostgreSQL 18 initdb still works too (same on-disk format). Release builds refuse to compile without RE2 on purpose: a build with only the fallback regex engine is drastically slower on regex heavy workloads. The binaries we publish additionally use --profile dist, which turns on fat LTO; it produces a faster binary but takes much longer to compile.

What's new in v0.4-beta

v0.4-beta is a beta: it shows what is coming in v0.4 while we finish stabilizing the rest.

Developing in the open

Up to v0.3 we developed pgrust privately and published it in large monthly releases. Starting with v0.4-beta, development happens in this repository: features, optimizations and bug fixes are pushed here as soon as they are ready, instead of being held back for the next release.

Implementation

Executor

Parallelism

Storage

Other

Roadmap

Next we're working on testing and battle testing pgrust and making it so people can trust pgrust with their data.

Beyond that we want to add:

Contributing

We are not actively accepting pull requests right now. If you want to help out with the project, reach out on Discord.

Please do still open an issue if something breaks, if setup is confusing, or if there is a Postgres behavior you want matched.

License

pgrust is licensed under AGPL-3.0. Portions derived from PostgreSQL remain under the PostgreSQL License. The JIT's code generator (crates/tpde/) is ported from TPDE (Apache-2.0 WITH LLVM-exception) and Fadec and Disarm (BSD-3-Clause). See NOTICE.

Acknowledgements: the research and other systems whose ideas pgrust uses are credited in ACKNOWLEDGEMENTS.md.

Contact

pgrust (malisper/pgrust)

正在搜索数据库…

这是什么?pgrust 是用 Rust 从零重新实现的 PostgreSQL,目标是在线协议和 SQL 方言上与 Postgres 兼容,同时速度比 Postgres 和 ClickHouse 都快。
这是我现有 Postgres 数据库的直接替代品,还是需要迁移过去的东西?它与真正的 Postgres 在协议和 SQL 方言上兼容,并通过了 Postgres 自带的回归测试套件。已发布的 Docker 镜像可直接替代官方 postgres 镜像使用,沿用相同的环境变量、初始化脚本和数据卷路径。
我该如何试用 pgrust?下载 Linux(Debian/Ubuntu)版本的可执行文件并运行服务器,然后在第二个终端中用 psql 连接;数据会保存在数据目录中,重启后依然保留。也提供支持多架构的预构建 Docker 镜像,可直接替代官方 postgres 镜像使用。
pgrust 可以安全地用于真实数据吗?不可以。项目明确表示它尚未达到生产就绪,并警告不要在其中存放重要数据。由于目前还没有稳定的扩展 ABI,现有的 PostgreSQL 扩展暂时无法使用,PL/Python、PL/Perl 和 PL/Tcl 也尚未移植。
pgrust 要跑得快,是否需要特定的 ARM 硬件?pgrust 可以在其他平台上运行,但它的 JIT 编译器和性能调优专门针对 AWS Graviton4 ARM 实例;在其他 CPU 上可以运行,但无法达到已发布的基准测试数字。
pgrust 比 Postgres 和 ClickHouse 快多少?在 ClickBench 中,它比 ClickHouse 快 18.5%,并使用内置的 pgrcolumnar 列式布局,比 Postgres 快数百倍。在 300GB 规模的 sysbench-oltp 只读负载下,它的吞吐量比 PostgreSQL 18.3 高 30%。
团队如何确认 pgrust 的行为确实与 Postgres 一致?他们使用 Kani 对约 3000 个面向用户的 Postgres 函数中的 1000 个进行了形式化验证,确认行为完全一致(发现 12 处差异,其中 4 处是真实 Postgres 中的 bug);此外还使用差分模糊测试,并借助 Antithesis 进行模拟测试。
v0.2 版本有哪些变化?新增了基于向量化、推送模型的执行器,并配有 copy-and-patch JIT;采用基于线程的并发模型和工作窃取式查询调度器;引入了 pgrcolumnar 列式存储格式;还内置了 OOM 杀手,它只会终止一个工作进程,而不会让操作系统杀掉整个服务器。
有哪些类似的项目?kividb, pgx, walshadow, bbtree, timescaledb, HikariCP, neon, pgbot
作者
malisper
大小
666.1 MB
星标
6.2K
时长
7个月
提交 · 90 天