Enable dynamically loaded postgres extensions #115
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi all,
We at Lantern wanted to enable dyunamically loading our lantern ectension into a wasm postgres instance. We were happy to find pglite where you had done the heavy lifting of bringing postgres to browser, so built our demo on top of pglite.
These two PRs implement the changes we needed to enable dynamic extension loading and we think these would be useful upstream as well.
postgres-wasm PR: electric-sql/postgres-wasm#13
Our blog post and the demo can be found here
MAIN_MODULE
linker flag, which will emit necessary code for dynamically loading side moudles. It will also export all symbols from postgres, so loaded extensions can access postgres symbols.EMULATE_FUNCTION_POINTER_CASTS
flag (see postgres-wasm PR)ASYNCIFY
flag which will wrap code in async runtime, allowing dlopen to work in browsers via resolving wasm files withfetch
. ChangedExecProtocolMsg
function call withccall
to properlyawait
the response.pathOrUrl
param for extensions. If it is passed, on startup we will try to fetch the extension control file, then fetch sql file corresponding the extension version which also should be located under the same path or url.module_pathname
in control file should be full path or url to wasm file.The control file should exist under the path or url like this example, the SQL script for corresponding version should exist under the same url like this. And control file should contain full URL to WASM file which will be fetched when we run
CREATE EXTENSION
command.locateFile
function to check if url starts withhttp:/
orhttps:/
it will append the second slash and return the url. This is because ondlopenInternal
function here when url passes throughPATH.normalize
function thehttp://
becomeshttp:/
addExtension(name: string, extension: Extension)
function which can also be used after db initialization to register extension control and sql files and later useCREATE EXTENSION
to load the extension.We have compiled and tested the following extensions: