liveBook · Manning

User-defined function

As explained in Server-Side WebAssembly

A user-defined function is a database function implemented by an application or extension rather than built into the database engine.

A user-defined function (UDF) is custom logic that extends a database engine. With WebAssembly, the function runs inside the database, keeping computation close to the data while providing a sandboxed execution environment. section 5.3

Wasm-based UDFs provide several practical advantages:

  • Reduce client-server data transfer by processing values where they reside. section 5.3
  • Run bespoke database operations without moving data into an external application. section 5.3
  • Implement functions in any language that compiles to Wasm rather than only traditional UDF languages such as C or SQL. section 5.3
  • Use a sandboxed runtime to extend database behavior while retaining secure and efficient execution. section 5.5

Registering encryption functions in libSQL

A libSQL workflow builds a Wasm module, converts its binary to a hexadecimal blob, and registers encryption and decryption functions in SQL. The SQL setup initializes the Wasm function table, drops existing definitions, and creates new functions with CREATE FUNCTION ... LANGUAGE wasm. section 5.3.2

cargo build --release --target wasm32-unknown-unknown

Build the database function as a release Wasm module targeting wasm32-unknown-unknown. section 5.3.2

wasm_hex_dump -i wasm_udf_example.wasm

Convert the compiled Wasm binary into hexadecimal text suitable for embedding in SQL. section 5.3.2

Listing 5.5 data/create_wasm_udfs.sql file
.init_wasm_func_table
DROP FUNCTION IF EXISTS encrypt;
DROP FUNCTION IF EXISTS decrypt;
CREATE FUNCTION encrypt LANGUAGE wasm AS X'0061736d01...';
CREATE FUNCTION decrypt LANGUAGE wasm AS X'0061736d01...';

Initialize libSQL's Wasm function table, remove conflicting definitions, and embed the function binaries as literal blobs. section 5.3.2

How Server-Side WebAssembly covers it

How deep it goes
Explains locality, language flexibility, sandboxing, binary embedding, and SQL registration, but does not detail function implementation code.
Examples
Uses libSQL encryption and decryption functions registered from embedded Wasm binaries.
What you can do after
Operate/configure libSQL UDFs by building Wasm, generating a hex dump, and issuing CREATE FUNCTION statements.
How it connects
Connects user-defined functions with WebAssembly, SQL, libSQL, sandboxing, and WebAssembly Text Format.

Also explained in

Compare how 2 books explain user-defined function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Server-Side WebAssembly. The section links point to the parts of the book it draws on.