# Code Engine - Powered by Wasm

The Code Engine feature in SingleStore supports creating functions (UDFs, UDAFs, and TVFs) using code compiled to WebAssembly (Wasm). The Code Engine uses the `wasmtime` runtime to compile and run WebAssembly code. Refer to [Wasmtime](https://docs.wasmtime.dev/) for more information.&#x20;

This feature supports any language that can compile to the Wasm core specification, which allows you to create UDFs/UDAFs/TVFs in a language of your choice using existing code libraries and run them in a sandboxed environment for enhanced security. Each Wasm function instance runs in its own in-process sandbox. It uses a linear memory model and provides hard memory protection boundaries. By default, each Wasm function sandbox is allocated `16MB` of memory. You can use the `GROW TO` clause to modify the memory allocated to a Wasm function sandbox.

Once a Wasm function is added to SingleStore, it becomes a part of the database. Hence, it is also included in the database backup and restore operations.

## Why use Wasm-based Functions

SingleStore recommends using Wasm-based functions for integrating complex logic and data structures or using existing C/C++ or Rust code.

Key benefits to using Wasm:

* **Security**: Run your code in a sandboxed environment for enhanced security.
* **Code reusability**: Leverage existing code base without rewriting the logic in SQL.
* **Code integration**: Integrate complex logic and data structures that cannot be expressed using PSQL.

If your logic and data structures can be implemented in [Procedural SQL](https://docs.singlestore.com/db/v9.1/developer-resources/procedural-extensions.md) (PSQL) with reasonable effort in a way that performs well, use PSQL. Refer to [Choose Between PSQL, Wasm-based, and External Functions](https://docs.singlestore.com/db/v9.1/developer-resources/procedural-extensions/choose-between-psql-wasm-based-and-external-functions.md) for related information.

## Use Cases

The following is a list of common real-world applications that have been implemented using Wasm-based functions:

* [Sentiment Analysis](https://github.com/singlestore-labs/singlestoredb-extension-sentiment): Perform sentiment analysis using Valence Aware Dictionary and Sentiment Reasoner (VADER).
* [Bloom Filters](https://github.com/singlestore-labs/singlestoredb-extension-bloom-filters): Generate Bloom filters from a column of string values and then run the Bloom filter on another column.
* [JSON Flatten](https://github.com/singlestore-labs/singlestoredb-extension-json-flatten): Perform flattening operations on JSON objects.
* [HyperLogLog (HLL) Sketches](https://github.com/singlestore-labs/singlestoredb-extension-hll-sketch): Generate HLL sketches using different operations and algorithms and return diagnostic information.
* [Theta Sketches](https://github.com/singlestore-labs/singlestoredb-extension-theta-sketch): Generate Theta sketches using different operations and algorithms and return diagnostic information.

## Build Wasm-based Functions

You can build Wasm extensions for SingleStore on Mac, Windows, or Linux using [Docker](https://docs.docker.com/get-docker/) development containers. Refer to [SingleStore Wasm Toolkit](https://singlestore-labs.github.io/singlestore-wasm-toolkit/html/Tutorial-Overview.html) for a tutorial on creating Wasm functions, importing them in SingleStore, and evaluating them in queries.

For more examples, refer to [Create Wasm UDFs](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/create-wasm-udfs/#section-idm4543865861515233102331638184.md) or [Create Wasm TVFs](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/create-wasm-tvfs/#section-idm4623033066499233187115419357.md). Refer to [Create Wasm-based UDAFs](https://docs.singlestore.com/db/v9.1/reference/sql-reference/procedural-sql-reference/create-aggregate/#section-idm4573709425052833819733285792.md) for an example on creating Wasm-based user-defined aggregate functions (UDAFs).

## Memory Management

Wasm modules are serialized to the plancache. Refer to [Managing Plancache Memory and Disk Usage](https://docs.singlestore.com/db/v9.1/user-and-cluster-administration/maintain-your-cluster/managing-memory/managing-plancache-memory-and-disk-usage/#section-idm234457677444929.md) for information on how Wasm-modules are evicted from memory.

## Runtime Constraints

The Wasm UDF/TVF must not require runtime capabilities that the database does not provide out of the box. For example, a Wasm UDF/TVF can not make system calls, open files, open sockets, send network messages, create processes, or create threads. SingleStore does not support these operations out of the box to protect the security and integrity of its service process.

## Related Topics

| [Wasm Application Binary Interfaces (ABIs)](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/wasm-application-binary-interfaces-abis.md)                                 | Information on supported Wasm ABIs.SingleStoresupports both the Basic and Canonical ABIs.                                      |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| [Configure Wasm](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/configure-wasm.md)                                                                                     | Configure Wasm extensibility using engine variables.                                                                           |
| [Create Wasm UDFs](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/create-wasm-udfs.md)                                                                                 | Create Wasm-based user-defined functions (UDFs).                                                                               |
| [Create Wasm TVFs](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/create-wasm-tvfs.md)                                                                                 | Create Wasm-based user-defined table-valued functions (TVFs).                                                                  |
| [Create Wasm UDAFs](https://docs.singlestore.com/db/v9.1/reference/sql-reference/procedural-sql-reference/create-aggregate/#section-idm4508754721451233639501215694.md)                            | Create Wasm-based user-defined aggregate functions (UDAFs).                                                                    |
| [Choose Between PSQL, Wasm-based, and External Functions](https://docs.singlestore.com/db/v9.1/developer-resources/procedural-extensions/choose-between-psql-wasm-based-and-external-functions.md) | Understand when to use PSQL, Wasm-based, or external functions.                                                                |
| [Procedural Extensions](https://docs.singlestore.com/db/v9.1/developer-resources/procedural-extensions.md)                                                                                         | Information on creating Procedural SQL (PSQL) functions or procedures.                                                         |
| [Extensions](https://docs.singlestore.com/db/v9.1/reference/sql-reference/procedural-sql-reference/extensions.md)                                                                                  | Combine Wasm binaries and WIT interfaces into a packaged archive and create, manage, or deploy these resources using a script. |

## FAQs

* **Can I use a Wasm binary outside of the SingleStore database once it is imported into the database?**

  No. Once a Wasm binary is imported into a SingleStore database, you cannot use it for applications outside the database. However, you can make a copy of the binary and use it instead.
* **What happens if there is a mismatch between the WIT file and the generated code?**

  A mismatch may cause a Wasm trap or unexpected results. The exact behavior depends on how the Wasm program interprets the arguments.
* **Can I reverse engineer a Wasm binary once it is imported into the database?**

  No. Once a Wasm binary is imported into the database, it is not possible to see the binary data, it can only be executed. Hence, you can only reverse engineer the Wasm binary behaviorally and not by disassembling the binary.
* **What happens when a Wasm function crashes or runs out of memory?**

  If a Wasm function crashes or runs out of memory, it causes the query to fail and returns an error. If possible, the error message may include a stack into the Wasm function.

## In this section

* [Wasm Application Binary Interfaces (ABIs)](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/wasm-application-binary-interfaces-abis.md)
* [Configure Wasm](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/configure-wasm.md)
* [Wasm Data Type Coercions](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/wasm-data-type-coercions.md)
* [Create Wasm UDFs](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/create-wasm-udfs.md)
* [Create Wasm TVFs](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/create-wasm-tvfs.md)
* [Improve Wasm Function Performance](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/improve-wasm-function-performance.md)
* [Troubleshooting](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/troubleshooting.md)

***

Modified at: June 13, 2025

Source: [/db/v9.1/reference/code-engine-powered-by-wasm/](https://docs.singlestore.com/db/v9.1/reference/code-engine-powered-by-wasm/)

(An index of the documentation is available at /llms.txt)
