AIFeed

Minipg:将 PostgreSQL 变为单一可执行文件

Translation(model translation, not the source's own text)

文章标题:Minipg: PostgreSQL as a Single Executable

正文: [ about | demo | using it | how it works | code | warning! | links ]

minipg: PostgreSQL as a Single Executable

Jeff Lindsay, h.d.a.

About

minipg 是 PostgreSQL 的单可执行文件版本,开箱即用,无需配置服务器。一个 .pgdb 目录就是数据库:它的数据、配置、凭据和 Postgres 版本全都存放在里面,minipg app.pgdb 会在其上打开一个 psql shell。当某条命令需要时服务器会启动,空闲时它会自行停止。

minipg 内嵌 Docker Hub 的 postgres:18-alpine 镜像,并用 h9 运行它,h9 以 Zig 库的形式使用。h9 的 Linux personality 直接在宿主机的 hypervisor 上运行 Alpine 自己的 Postgres 二进制文件,由 h9 的 Plan 9 内核响应它们的系统调用。没有 Linux 内核需要启动,没有 VM 镜像,也没有 Docker。

Demo

该演示下载约 35 MB。它需要 JavaScript Promise Integration,Chrome 和 Edge 从版本 137 起支持;其他浏览器正在跟进。它也可以在独立页面中打开。

该演示运行的是 minipg 的 Linux 构建,它运行在 Postgres 镜像本身内部。它以普通进程的方式启动 Postgres,并通过 unix socket 提供服务,因为页面没有 TCP。新数据库来自构建时创建的模板集群,因为在 v86 上 initdb 需要半分钟。

Using It


        minipg app.pgdb                     # psql shell (creates app.pgdb if missing)
minipg app.pgdb "select 1"          # one statement
minipg url app.pgdb                 # postgres://postgres:[email protected]:54321/app
minipg serve app.pgdb               # an explicit foreground server
minipg run app.pgdb -- npm start    # your app, with DATABASE_URL set
minipg dump app.pgdb > dump.sql     # and minipg restore app.pgdb dump.sql
      

一个 .pgdb 包含 minipg.toml(设置以及生成的超级用户密码)和 pgdata/,即 Postgres 的数据目录,以普通文件形式存放。停止后的 .pgdb 可以像任何目录一样复制。新集群使用 Postgres 内置的 C.UTF-8 locale,它在每个平台上的排序都相同。

在 CI 中,Linux 二进制文件是静态的,不需要特权或设备:没有 /dev/kvm 时,h9 在其原生后端上运行 Postgres。一个测试任务可以小到:


        minipg init test.pgdb
minipg run test.pgdb -- go test ./...
      

README 列出了每条命令,手册则是完整指南。

How It Works

每个 .pgdb 只有一个进程运行 Postgres,由锁强制执行。它持有该服务器的 h9 机器,其 Linux 根目录是解包后的镜像,其可写工作目录是 .pgdb。镜像的 docker-entrypoint.sh 在首次启动时初始化集群。命令本身从不托管 Postgres。如果没有服务器在运行,它们会在后台启动一个并使用它。psql、pg_dump 和其他工具各自在同一个根之上、各自短暂的 h9 机器中运行(启动约需 90 ms)。

Linux x86_64KVM,或没有 KVM 时使用 h9 的原生后端(Linux 5.11 或更高版本)
Linux arm64KVM
macOS (Apple Silicon)Hypervisor.framework
The browserh9 的 web 宿主、v86,以及 minipg 的 Linux 构建

Source Code

源代码在 GitHub 上,采用 MIT 许可证。构建需要 Zig 0.16,还需要 curl、jq、tar 和 xz 来获取 root:


        git clone --recursive https://github.com/progrium/minipg
cd minipg
make build         # pulls the root for this host's arch, builds ./minipg
make root ARCH=386
make web           # this page's demo, in .zig-out/web
      

Warning!

minipg 尚处于早期阶段,h9 的 Linux personality 也很年轻。在 h9 的原生后端上,单客户端时 Postgres 比原生慢约 2 倍,负载下更慢。在你能够重建数据之前,不要放心地把数据交给它。

请通过 GitHub issues 报告 bug 和发送问题。

Related Links

  • h9,它在宿主机 hypervisor 上运行原生 9front 可执行文件和 Linux 可执行文件
  • PostgreSQL 和 Docker Hub 的 postgres 镜像
  • v86,演示所运行的 x86 模拟器
  • xterm.js,演示的终端

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Hacker News
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗