Develop Database Extensions Using PGRX
This document explains how to develop database extensions using Rust and the PGRX framework. PGRX is a Rust framework for developing extensions for Apache Cloudberry, offering a safe and efficient development experience.
For the core features of PGRX, see PGRX Core Features. For notes of PGRX, see Considerations and Best Practices for PGRX.
Requirements for Development Environment
- Make sure that your OS is one of Debian/Ubuntu and RHEL/CentOS.
- Make sure that your Apache Cloudberry cluster is compiled from source code, not installed using RPM package.
Basic Software Environment
Required software:
- Rust toolchain (
rustc,cargo, andrustfmt) - install via https://rustup.rs - Git
- libclang 11 or higher (for bindgen)
- GCC 7 or higher
PostgreSQL Dependencies
Install required PostgreSQL dependencies for your OS:
For Debian/Ubuntu:
sudo apt-get install build-essential libreadline-dev zlib1g-dev flex bison \
libxml2-dev libxslt-dev libssl-dev libxml2-utils xsltproc ccache pkg-config
For RHEL/CentOS:
sudo yum install -y bison-devel readline-devel zlib-devel openssl-devel wget ccache
sudo yum groupinstall -y 'Development Tools'
After installing the dependencies, you can start developing extensions.
Quick Start for PGRX
This section introduces the process of quickly developing extensions using PGRX, including:
- Setting up and installing PGRX
- Creating extension
- Installing and using extension
Set up and Install PGRX
PGRX is maintained by PgCentral Foundation, Inc., while we used here is one forked PGRX version with better compatibility within Cloudberry. It's contributed by the community members and customized for Cloudberry, but please note that it is not maintained as one official Cloudberry project.
-
Set the environment variable for Apache Cloudberry's
pg_configpath, where<pg_config_path>is the path in your Apache Cloudberry cluster (for example,/usr/local/cloudberry-db/bin/pg_config):export PGRX_PG_CONFIG_PATH=<pg_config_path> -
Build the PGRX framework:
-
Clone the Apache Cloudberry-compatible
pgrxrepository:git clone https://github.com/cloudberry-contrib/pgrxcd pgrx -
Build the code with
pg14andcbdbfeatures enabled:cargo build --features "pg14, cbdb"
-
-
Install the Apache Cloudberry-compatible
cargo-pgrxtool:cargo install --path cargo-pgrx/ -
Initialize the environment with your database kernel version:
cargo pgrx init --pg14=`which pg_config`
Create an Extension
-
Generate an extension template. This example creates an extension named
my_extension:cargo pgrx new my_extensioncd my_extensionThe created directory structure is as follows:
.├── Cargo.toml├── my_extension.control├── sql