electric-sql / pglite

Lightweight WASM Postgres with real-time, reactive bindings.
https://pglite.dev
Apache License 2.0
9.46k stars 204 forks source link

Add PGlite version to a VFS file and report with `select version()` #369

Open samwillis opened 1 month ago

samwillis commented 1 month ago

There are a few things that would be useful here:

  1. For the VFS to have a PGLITE_VERSION file containing the PGlite version number that created the database. This is useful to track which version of PGlite was used to run INITDB. It should probable not be updated when running the same datadir with a newer PGlite, it's purely a history of what version ran initdb (the PG_VERSION only contains the numeric major version of postgres that created the database and so we can't reuse this file)
  2. Report the PGlite version when a user calls SELECT VERSION()

Postgres has it's version compiled into it via a PG_VERSION env var, which is used to construct PG_VERSION_STR here:

https://github.com/postgres/postgres/blob/53b2c921a0f9b56465ab65165c1909f9616ffa98/configure#L19226

Both are added to confdefs.h before compiling.

PGlite has a split compilation process, the WASM is built first, then the JS/TS wrapper and packaging. Our CI caches the WASM for faster builds. Ideally we use the existing changesets tooling to bump a version file somewhere that will trigger the rebuild of the WASM, and we patch the build config to something like:

cat >>confdefs.h <<_ACEOF
#define PG_VERSION_STR "PostgreSQL $PG_VERSION (PGlite $PGLITE_VERSION) on $host, compiled by $cc_string, `expr $ac_cv_sizeof_void_p \* 8`-bit"
_ACEOF

so that the resulting string is something like:

PostgreSQL 16.4 (PGlite v0.2.11) on x86_64-pc-linux-gnu, compiled by emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.68-git (20697f11b8f0fcb4c190acbf605e2361e39829ab), 32-bit

We should then modify our initdb to add the PGLITE_VERSION' file - it should probably be the full output ofSELECT VERSION()but maybe prefixed withCreated by: ` to make it apparent its the version stat created the database.

I would prefer all this to be baked into the WASM (or native build with libpglite) rather than in the JS/TS code.

pmp-p commented 1 month ago

right after initdb we could issues a : COPY (SELECT CONCAT('Created by : ',version())) TO '/tmp/pglite/base/PGLITE_VERSION';