Skip to content

Reduce Postgis shared library size to <8mb#62

Draft
larsmennen wants to merge 1 commit intoelectric-sql:tdrz/backend-trypostgis-2from
cogna-public:larsmennen/reduce-postgis-size
Draft

Reduce Postgis shared library size to <8mb#62
larsmennen wants to merge 1 commit intoelectric-sql:tdrz/backend-trypostgis-2from
cogna-public:larsmennen/reduce-postgis-size

Conversation

@larsmennen
Copy link

This aims to resolve the outstanding issue ( electric-sql/pglite#807 (comment) ) on electric-sql/pglite#807 that the Postgis library is over 8mb and Chrome has a hard 8mb limit for WASM modules.

This is achieved by:

  • Moving stdc++, sqlite3, geos libs out of --whole-archive (saves ~250k bytes)
  • Adding -flto for link-time optimisation (saves ~11k bytes)
  • Adding -Oz (saves ~45k bytes)
  • Adding a wasm-opt step (saves ~30k bytes)

Brings it down in total to 8,370,885 bytes (8,388,608 limit).

This works locally for me in Chrome and seems to pass the test suite used on the above PR.

Performance-wise, the effects of -Oz seem neglible. I have not ran a extensive benchmark, but on a table with 20k points and 200 regions (w/ index) running a couple queries like ST_DWithin, ST_Intersects, etc. 5 times on the optimized and non-optimized versions, the differences are within 1-2% either direction.

@tdrz
Copy link
Collaborator

tdrz commented Feb 22, 2026

@larsmennen Cool stuff. Not to take anything away from your efforts, but I was hoping we could get the preload plugins working instead:

"
Dynamic libraries (.so): The files are precompiled and instantiated using WebAssembly.instantiate. This is useful for browsers, such as Chrome, that require compiling large WebAssembly modules asynchronously, if you then want to load the module synchronously using dlopen later. (Set Module.noWasmDecoding to true to disable).
"

Should have mentioned it in that PR.

@tdrz
Copy link
Collaborator

tdrz commented Feb 22, 2026

This works locally for me in Chrome and seems to pass the test suite used on the above PR.

Did you also try Postgis' own test suite? There are some comments in that PR documenting how to do it.

@larsmennen
Copy link
Author

larsmennen commented Feb 22, 2026

@tdrz makes sense -- I'll try out getting the preloading working in the next couple days and get a PR up. Do you want to keep the size optimisations in then as well, or take those out?

@tdrz
Copy link
Collaborator

tdrz commented Feb 23, 2026

Do you want to keep the size optimisations in then as well, or take those out?

@larsmennen Please leave this PR as it is, we just might use it if we can't get the preload plugins to work for the >8MB .so.

@tdrz tdrz marked this pull request as draft February 26, 2026 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants