apps/www/_blog/2024-08-16-postgres-foreign-data-wrappers-with-wasm.mdx
Foreign Data Wrappers (FDWs) allow Postgres to interact with externally hosted data. To operate a FDW, the user creates a foreign table. When queried, the foreign table reaches out to the 3rd party service, collects the requested data, and returns it to the query in the shape defined by the foreign table. This allows seamless querying and data manipulation across different tools as if they were local tables from within Postgres.
Wrappers is a Rust framework for creating Postgres Foreign Data Wrappers. Today we're releasing support for Wasm (WebAssembly) wrappers.
With this feature, anyone can create a Wasm wrapper to an external service and run it directly from e.g. GitHub:
-- An Example Google Sheets Wasm Wrapper:
create server google_sheets
foreign data wrapper wasm_wrapper
options (
-- Install from GitHub
fdw_package_url 'https://github.com/<ORG>/<REPO>/releases/download/v0.2.0/google_sheets_fdw.wasm',
fdw_package_name 'my-company:google-sheets-fdw',
fdw_package_version '0.2.0',
fdw_package_checksum '338674c4c983aa6dbc2b6e63659076fe86d847ca0da6d57a61372b44e0fe4ac9',
-- Provide custom options
base_url 'https://docs.google.com/spreadsheets/d'
);
This feature is available today in public alpha for all new projects.
<div className="video-container"> <iframe className="w-full" src="https://www.youtube-nocookie.com/embed/wCwEWR4k0no" title="Build Wasm foreign data wrapper with Wrappers and use it on Supabase" allow="accelerometer; autoplay; clipboard-write; encrypted-media; fullscreen; gyroscope; picture-in-picture; web-share" allowfullscreen /> </div>Foreign Data Wrappers (FDW) are a powerful feature of Postgres that allows you to connect to and query external data sources as if they were regular tables.
<div> </div>Wrappers is an open source project that simplifies the creation of Postgres Foreign Data Wrappers using Rust.
WebAssembly (Wasm) is a binary instruction format that enables secure and high-performance execution of code on the web. It is originally designed for web browsers, but now can also be used in server-side environments like Postgres.
Here's how the Wasm FDW benefits us:
To better understand how the Wasm FDW works, let's take a look at the architecture:
<div> </div>The above diagram illustrates the key components and how they interact:
Wasm FDWs are loaded dynamically when the first request is made. The interaction flow is:
SELECT statement is initiated.The Wasm FDW currently only supports data sources which have HTTP(s) based JSON API, other sources such like TCP/IP based DBMS or local files are not supported yet.
A major benefit of Wasm FDW is that you can build your own FDW and use it on Supabase. To get started, clone the Postgres Wasm FDW [Template]. Building your own Wasm FDWs opens up a world of possibilities for integrating diverse data sources into Postgres.
Visit Wrappers docs and guides to learn more about how to develop a Wasm FDW.
<Admonition>As the Wasm FDW can access external data sources, you should never install Wasm Wrappers from untrusted source. Always use official Supabase FDWs, or use sources which you have full visibility and control.
</Admonition>The Wasm FDW feature is available today on the Supabase platform. We have 2 new built-in Wasm FDWs: Snowflake and Paddle.
To get started, follow below steps:
Snowflake or Paddle wrapper, follow the instructions and create foreign tables.We can also use SQL. Let's try, using the Paddle FDW as an example.
Inside the SQL editor, enable the Wasm Wrapper feature:
-- install Wrappers extension
create extension if not exists wrappers with schema extensions;
-- create Wasm foreign data wrapper
create foreign data wrapper wasm_wrapper
handler wasm_fdw_handler
validator wasm_fdw_validator;
Sign up for a sandbox account and get API key with Paddle.
Create a Paddle server in Postgres using the Wasm FDW created above:
-- create Paddle foreign server
create server paddle_server
foreign data wrapper wasm_wrapper
options (
-- check all available versions at
-- https://fdw.dev/catalog/paddle/#available-versions
fdw_package_url 'https://github.com/supabase/wrappers/releases/download/wasm_paddle_fdw_v0.1.1/paddle_fdw.wasm',
fdw_package_name 'supabase:paddle-fdw',
fdw_package_version '0.1.1',
fdw_package_checksum 'c5ac70bb2eef33693787b7d4efce9a83cde8d4fa40889d2037403a51263ba657',
-- save your Paddle credentials
api_url 'https://sandbox-api.paddle.com',
api_key '<your Paddle sandbox API key>'
);
Create a table for Paddle data:
-- create dedicated schema for Paddle foreign tables
create schema if not exists paddle;
-- create foreign table
create foreign table paddle.customers (
id text,
name text,
email text,
status text,
custom_data jsonb,
created_at timestamp,
updated_at timestamp,
attrs jsonb
)
server paddle_server
options (
object 'customers',
rowid_column 'id'
);
Now let's query the foreign table and check the result:
select id, name, email, status
from paddle.customers;
That's it. Head over to the Supabase Wrappers documentation to find more detailed guides on setting up and using Wasm FDWs.
None of this innovation would have been possible without the relentless efforts and contributions of our vibrant community. We'd like to thank all the following developers for their contributions:
Aayushya Vajpayee, Romain Graux
Want to join the Supabase Wrappers community contributors? Check out our contribution docs.