DuckDB Extension
WebMacro
Load DuckDB SQL macros from an HTTP URL. Share a macro definition across sessions and pin its source to a reviewed revision.
On this page
Share SQL helpers through a URL
A DuckDB macro packages a reusable SQL expression or query. WebMacro loads a macro definition from an HTTP URL and registers it in the current session, allowing several notebooks or applications to use the same definition.
Use it for shared SQL helpers that you maintain in a repository or another HTTP host. Pin the URL to a reviewed revision when the definition needs to stay stable. The loaded content becomes executable SQL in your session, so choose a source you control or trust.
Example
Load a macro from a URL and use it
Load a macro definition from a URL, then call it like any other DuckDB macro.
Run this example in the DuckDB CLI or a native client.
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://duckdb-extensions.query.farm';
INSTALL webmacro FROM query_farm;
LOAD webmacro FROM query_farm;
-- Pull a macro definition from a URL you control
SELECT load_macro_from_url(
'https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/search_posts.sql'
) AS res;
-- Use it like any other DuckDB macro
SELECT * FROM search_posts('alice.example.com', text := 'quack');
SET allow_extension_repositories = 'allowed';
CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
WITH PREFIX 'https://haybarn-extensions.query.farm';
INSTALL webmacro FROM query_farm;
LOAD webmacro FROM query_farm;
-- Pull a macro definition from a URL you control
SELECT load_macro_from_url(
'https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/search_posts.sql'
) AS res;
-- Use it like any other DuckDB macro
SELECT * FROM search_posts('alice.example.com', text := 'quack');
INSTALL webmacro FROM community;
LOAD webmacro;
-- Pull a macro definition from a URL you control
SELECT load_macro_from_url(
'https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/search_posts.sql'
) AS res;
-- Use it like any other DuckDB macro
SELECT * FROM search_posts('alice.example.com', text := 'quack');
The downloaded text must be a single CREATE MACRO statement, and it runs in your session. Use a source you control or trust and pin a reviewed revision.
Common tasks
Deep Dive
Technical Details
These examples use DuckDB 2.0 or later. Complete the repository setup and installation first.
What you can do with one query
Define a macro in a Git repo, point load_macro_from_url at the raw URL, and use it like any built-in DuckDB function:
SELECT load_macro_from_url( 'https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/search_posts.sql');-- 'Successfully loaded macro: search_posts'
SELECT *FROM search_posts('alice.example.com', text := 'quack');The whole extension is one function. One URL is one macro. The interesting part isn’t the protocol — it’s that the URL becomes the unit of distribution: pull requests review the SQL, tags pin versions, and every machine in your team gets the same macro from the same place.
load_macro_from_url fetches arbitrary text from a URL and executes it as SQL in your DuckDB session. Treat every URL you load with the same posture you’d use for curl ... | bash:
- Only load from URLs you control or fully trust. Your own Git repos, your team’s internal artifact host, your own gists — not arbitrary URLs from chat messages, blog posts, or Stack Overflow snippets.
- Checking the statement is not a sandbox. The extension parses the response and accepts only one
CREATE MACROstatement, so a file can’t run anything else when it loads. But a macro’s body can do whatever your session can, such as reading files or URLs, and it runs whenever you call the macro. - Pin URLs to immutable revisions. Prefer
https://raw.githubusercontent.com/org/repo/<commit-sha>/macros/foo.sqlover.../main/macros/foo.sqlso a rebase or branch compromise doesn’t silently change what you load. - Keep HTTPS. Plain
http://URLs are vulnerable to in-flight tampering. On DuckDB 2.0 the extension refuses them unless youSET webmacro_allow_http = true, and it refuses to fetch anything whenenable_external_accessis false.
In a multi-tenant or hosted DuckDB environment, consider not loading the WebMacro extension at all, or restricting which roles can use it. See OWASP — Code Injection for the threat model.
The single function
The whole API surface is one scalar function: load_macro_from_url(url VARCHAR) → VARCHAR — accepts a URL, returns the loaded macro’s name (or an error).
load_macro_from_url does exactly three things, in order:
GETthe URL. On DuckDB 2.0 this uses DuckDB’s HTTP stack (httpfs is autoloaded forhttps://), sohttp_timeout,http_retries,http_proxyand HTTP secrets apply; redirects are followed and every hop is checked again; responses overwebmacro_max_response_sizebytes are refused. The Haybarn 1.5 build uses cpp-httplib with a 10 s read timeout.- Parse the response with DuckDB’s parser and require exactly one
CREATE MACROorCREATE OR REPLACE MACROstatement. Nothing runs if the response holds anything else, including a macro followed by another statement. - Create the macro and return
Successfully loaded macro: <name>. On DuckDB 2.0 the macro is created in your own transaction and search path, like typing theCREATE MACROyourself:TEMPmacros work and aROLLBACKremoves the macro. The Haybarn 1.5 build creates it on a separate connection and rejectsTEMPmacros.
The macro is then callable by name, like any hand-typed CREATE MACRO. There’s no separate registry to query and no unload — drop the macro with DROP MACRO when you no longer need it.
Settings (DuckDB 2.0)
| Setting | Default | Meaning |
|---|---|---|
webmacro_allow_http |
false |
Allow plain http:// URLs. The response runs as SQL, so anyone on the network path could swap the macro; use only for localhost or lab networks. |
webmacro_max_response_size |
1048576 |
Largest response, in bytes, that load_macro_from_url accepts. |
DuckDB’s own http_timeout (default 30 s), http_retries and http_proxy settings apply too. load_macro_from_url is VOLATILE, so EXPLAIN or constant folding never fetches a URL or creates a macro.
Hosting the macro
WebMacro doesn’t care where the URL lives, only that an HTTP GET against it returns a body that parses as a DuckDB macro. Common patterns:
- Git raw URL (recommended):
https://raw.githubusercontent.com/org/repo/<sha>/macros/foo.sql. Pinning to a commit SHA makes the macro deterministic forever. - Gist raw URL:
https://gist.githubusercontent.com/<user>/<id>/raw/foo.sql. Convenient for one-off sharing; harder to review than a Git PR. - Internal artifact server / S3 + presigned URL: any HTTPS endpoint that returns the SQL body works. TLS certificates are verified.
- Self-hosted “macro registry”: a tiny static site at
macros.your-org.internal/mapping path → SQL. The whole thing can be a directory of.sqlfiles behind any static webserver.
Loading at session start
A typical bootstrap pattern: pull a known set of macros at the top of a notebook or script, fail loudly if any URL is unreachable.
INSTALL webmacro FROM query_farm;LOAD webmacro FROM query_farm;
SELECT url, load_macro_from_url(url) AS statusFROM (VALUES ('https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/search_posts.sql'), ('https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/normalize_handle.sql'), ('https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/active_users.sql')) AS macros(url);If any URL fails the fetch or the statement check, the query errors out — so a missing macro shows up immediately rather than as a “function not found” later.
Compared to alternatives
- Hand-rolled wrapper around
http_client+query. Possible —http_clientreturns the URL body, then youEXECUTEit. WebMacro collapses that to one statement and adds the (best-effort) safety filter. For everything beyond loading macros — calling REST APIs, posting form data, handling JSON responses — reach forhttp_clientdirectly. - Copy-pasting
CREATE MACROinto every notebook. The thing this extension exists to replace. Drift between notebooks is the failure mode. - Baking macros into a startup
.duckdbrc/ init script. Works for a single machine, doesn’t scale to a team or to ephemeral workers. WebMacro lets the URL be the artifact instead.
Caveats
- No caching. Every call hits the network. If you load the same macro 100 times in a session, that’s 100 HTTP requests.
- No dependency graph. If macro
Acalls macroB, you must loadBfirst. WebMacro doesn’t parse the SQL to discover dependencies. - No version negotiation. The URL is the version. If you want immutability, pin to a commit SHA or a content-addressed URL.
- Status is experimental. The function surface is small and stable in shape, but pin a known-good extension version in production setups.
Reference
Extension Contents
Quick reference to all available functions and settings organized by category.
| Name | Type | Description |
|---|---|---|
|
Configuration
|
||
| webmacro_allow_http | Object type: Setting | Allow load_macro_from_url to fetch plain http:// URLs. |
| webmacro_max_response_size | Object type: Setting | Largest response, in bytes, that load_macro_from_url will accept |
|
Loading
Fetch a SQL macro definition from an HTTP(S) URL and register it in the current DuckDB session. The whole extension is one function — point it at a URL that returns |
||
| load_macro_from_url() | Object type: Scalar function |
Fetch the URL, check that the response body is a macro definition, create the macro, and return Successfully loaded macro: <name>.
|
No extension contents match that search.
API Reference
Function Reference
Configuration
Settings
Configure the webmacro extension behavior using these settings.
webmacro_allow_http
Allow load_macro_from_url to fetch plain http:// URLs. Off by default: the response is executed as SQL, so anyone on the network path could replace the macro
false
webmacro_max_response_size
Largest response, in bytes, that load_macro_from_url will accept
1048576
Practical Examples
Cookbook
Real-world recipes and patterns for common use cases.
These examples use DuckDB 2.0 or later. Complete the repository setup and installation first.
Install and load
INSTALL webmacro FROM query_farm;LOAD webmacro FROM query_farm;The extension registers a single function: load_macro_from_url. Everything below is a recipe around it.
Load a macro from a Git repo
The recommended pattern — host the SQL in a Git repo, load by raw URL:
SELECT load_macro_from_url( 'https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/search_posts.sql') AS res;-- 'Successfully loaded macro: search_posts'
-- Now use it like any DuckDB macroSELECT *FROM search_posts('alice.example.com', text := 'quack');The body of the URL must be exactly one CREATE MACRO statement (scalar or table macro). See load_macro_from_url for the validation rules.
Pin to an immutable commit
Use a commit SHA in the URL so a branch rebase or upstream change can’t silently swap the macro out from under you:
SELECT load_macro_from_url( 'https://raw.githubusercontent.com/Query-farm/sql-macros/' || 'a6a929d6d157bf10293dd04bffe606f8fabeda0d' || '/macros/search_posts.sql');The commit SHA pins the SQL forever — the same URL today and a year from now returns the same bytes. This is the closest thing to a versioned macro release.
Load several macros at once
A common bootstrap pattern at the top of a notebook or script:
SELECT url, load_macro_from_url(url) AS statusFROM (VALUES ('https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/search_posts.sql'), ('https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/normalize_handle.sql'), ('https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/active_users.sql')) AS macros(url);Any URL that fails the fetch or the statement check raises an error — the bootstrap halts, you find out at session start instead of at first use.
Load from a private host with auth
On DuckDB 2.0, load_macro_from_url goes through DuckDB’s HTTP stack, so an HTTP secret scoped to the host adds its bearer token or headers to the request — handy for private repos:
CREATE SECRET macro_host ( TYPE http, BEARER_TOKEN getenv('GITHUB_TOKEN'), SCOPE 'https://raw.githubusercontent.com/your-org/');
SELECT load_macro_from_url('https://raw.githubusercontent.com/your-org/private-macros/<sha>/macros/foo.sql');On the Haybarn 1.5 build the request is a plain unauthenticated GET. There, the simplest pattern is a presigned URL — generate a short-lived signed URL upstream (S3, GCS, etc.) and load that:
SET VARIABLE macro_url = getenv('MACRO_PRESIGNED_URL');
SELECT load_macro_from_url(getvariable('macro_url'));For an internally-routable host where TLS or network ACLs are the access control (e.g. https://macros.your-org.internal/foo.sql), a plain URL is fine.
Use scalar and table macros
Either macro form loads the same way — the difference is how you call the result.
A scalar macro definition at normalize_handle.sql:
CREATE OR REPLACE MACRO normalize_handle(h)AS lower(trim(trim(h), '@'));A table macro at active_users.sql, which carries its own sample rows:
CREATE OR REPLACE MACRO active_users(since_date) AS TABLESELECT user_id, last_seenFROM (VALUES (1, DATE '2025-11-02'), (2, DATE '2026-02-15'), (3, DATE '2026-03-01')) AS users(user_id, last_seen)WHERE last_seen >= since_dateORDER BY user_id;After loading both, scalar macros go in the SELECT list; table macros go in the FROM clause:
SELECT load_macro_from_url('https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/normalize_handle.sql');SELECT load_macro_from_url('https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/active_users.sql');
SELECT normalize_handle('@Alice ') AS handle, u.last_seenFROM active_users(DATE '2026-01-01') u;Replace a macro mid-session
CREATE OR REPLACE MACRO is the supported way to update a macro you’ve already loaded. If the URL’s body uses CREATE OR REPLACE MACRO, re-loading swaps the definition in place:
-- Edit the SQL upstream, then re-run:SELECT load_macro_from_url( 'https://raw.githubusercontent.com/Query-farm/sql-macros/main/macros/search_posts.sql');-- 'Successfully loaded macro: search_posts' -- now the new definitionIf the macro was created without OR REPLACE, you’ll need to DROP MACRO it first.
Pair with http_client for richer pipelines
WebMacro is a one-trick extension on purpose — it loads macros and nothing else. For HTTP request patterns it doesn’t cover (POST bodies, custom headers, reading non-macro JSON), pair with the http_client extension, which exposes general-purpose http_get / http_post for SQL-level HTTP.
-- Fetch a macro index, then load each entryINSTALL http_client FROM query_farm;LOAD http_client FROM query_farm;
WITH index AS ( SELECT (http_get('https://raw.githubusercontent.com/Query-farm/sql-macros/main/index.json')->>'body')::JSON AS j)SELECT load_macro_from_url(unnest(j->>'$.macros[*].url'))FROM index;Drop a loaded macro
A loaded macro is just a regular DuckDB macro — drop it like any other:
DROP MACRO search_posts;DROP MACRO IF EXISTS active_users;See DROP MACRO for the upstream syntax.
Platform Support
Compatibility
Extension availability may vary by platform and DuckDB version. Check below to ensure this extension supports your environment before installation.
Quick Facts
Platforms
DuckDB 2.0.0
- Linux x86_64aarch64
- Linux (musl) Not available
- macOS Apple Silicon
- Windows x86_64
- WASM Not available
Compiled binary sizes
| Platform | Architecture | Size |
|---|---|---|
| Linux | x86_64 | 17.66 MB |
| Linux | aarch64 | 15.62 MB |
| macOS | Apple Silicon | 13.54 MB |
| Windows | x86_64 | 12.17 MB |
Compressed download sizes for the release shown above.
DuckDB & Haybarn
Release calendar- DuckDB v2.0.0 Query.Farm repository Supported
Browser examples use Haybarn-Wasm 2.0.0-rc12.
Checked .
Learn and explore
Tutorials and articles
Writing about WebMacro: how it works, what changed, and why.
-
Webmacro for DuckDB 2.0: load one macro, in your transaction
Webmacro now validates one CREATE MACRO statement, uses verified HTTPS, limits response size, and creates macros in the caller’s DuckDB transaction.