BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News High Performance Functions in Rust on RDS PostgreSQL

High Performance Functions in Rust on RDS PostgreSQL

AWS announced the general availability of the Rust procedural language handler, PL/Rust, for Amazon Relational Database Service (RDS) instances running versions 13 and 14 of PostgreSQL. This builds on the previous release in May 2023 that enabled the functionality only for instances running PostgreSQL version 15.

Amazon RDS is a managed database service provided by Amazon Web Services. It includes support for popular database engines such as MariaDB, MySQL, Microsoft SQL Server, Oracle and PostgreSQL.

PostgreSQL is an open source relational database system developed in C. It includes a system for the creation of user-defined functions in languages other than SQL and C as Procedural Languages (PLs). The execution of a PL requires a handler able to interpret, and in some cases execute, its source text. A PL is normally referred to by the language its handler enables, prefixed with a 'PL/'. The core PostgreSQL distribution includes four PLs as standard: PL/pgsql, PL/TCL, PL/Perl and PL/Python. PL/Rust is a new non-core handler within the PostgreSQL extension ecosystem.

With its first production release in April 2023, PL/Rust was created to provide comparable performance to PostgreSQL functions written in C. With the handler, PostgreSQL functions defined in Rust are compiled to native machine code and benefit from Rust’s memory safety guarantees. An example function defined in Rust is shown below:

psql> CREATE FUNCTION add_two_numbers(a NUMERIC, b NUMERIC) RETURNS NUMERIC STRICT LANGUAGE plrust AS $$
    Ok(Some(a + b))
$$;

psql> SELECT add_two_numbers(2, 2);
add_two_numbers 
-----------------
               4

Enabling the PL/Rust extension is done by adding 'plrust' to the 'shared_preload_libraries' parameter in the RDS DB parameter group and running a create extension command within the instance. PL/Rust executes within Amazon RDS as a trusted language, which denies function authors from using any Rust features that would access file system operations, external modules or PostgreSQL server processes via the operating system. While the PL/Rust guide includes instructions for installing the extension in an untrusted mode, Amazon RDS does not permit this.

A performance test of PL/Rust on a large sample of random vectors showed significant reductions in runtime. For the same dataset, PL/pgsql and PL/v8 had run times of 14 and 54 seconds while PL/Rust took 3 seconds.

A major limitation of PL/Rust in the Amazon RDS ecosystem is that it is currently not available for users of Amazon Aurora, the fully-managed offering on top of Amazon RDS. In addition, users on Amazon RDS must be granted access to PL/Rust by a holder of the RDS Superuser privilege, despite it being a trusted language extension.

Finally PL/Rust is available in all AWS regions and further information on its use can be found on the Amazon RDS User Guide.

About the Author

Rate this Article

Adoption
Style

BT