pgzx: Postgres extensions with Zig

An open source framework to easily build PostgreSQL extensions in Zig, a systems programming language for maintainable and fast code.

View on GitHub
Full text search example

Access all Postgres APIs

Because Zig interacts so effortlessly with C code, it means that you can call any Postgres function.

Memory allocators

Make use of the Postgres memory contexts via Zig memory allocators. This means that you get memory safety and protection against leaks.

Logging and error handling

Pgzx provides a wrapper around standard Postgres error reporting functions making it easier to set up and use from Zig.

Development environment

Pgzx comes with its own development environment that downloads the Postgres source code and builds the extensions against it. It also supports regression and unit testing.

Server Programming Interface (SPI)

SPI is a way to run SQL from inside your extension. Pgzx provides access to it also from the Zig extensions.

Data structures

pgzx provides wrappers for several of the C data structures from the Postgres source code, like lists, linked lists, or hash tables.

char_count_zig

Adds a function that counts how many times a particular character shows up in a string. Shows how to register a function and how to interpret the parameters.

# SELECT char_count_zig('Postgres, the best database in the world', 's');

 char_count_zig
----------------
              4
(1 row)

pg_audit_zig

Inspired by the pgaudit C extension, this one registers callbacks to multiple hooks and uses more advanced error handling and memory allocation patterns.

{
  "operation": "CMD_SELECT",
  "relations": [
    {
      "relOid": 24596,
      "relname": "foo",
      "namespaceOid": 2200,
      "relnamespaceName": "public"
    },
    {
      "relOid": 24601,
      "relname": "bar",
      "namespaceOid": 2200,
      "relnamespaceName": "public"
    }
  ],
  "commandText": "select * from foo,bar;"
}

Explore more resources

pgzx

Introducing pgzx: create PostgreSQL extensions using Zig

By Tudor Golubenco, Steffen Siering

March 21, 2024

We are excited to introduce pgzx, a framework for creating Postgres extensions using the Zig programming language.

pgroll

How pgroll works under the hood

By Andrew Farries

November 30, 2023

Learn how pgroll implements zero-downtime schema changes by exploring an example.

pgroll

Introducing pgroll: zero-downtime, reversible, schema migrations for Postgres

By Carlos Pérez-Aradros Herce

October 3, 2023

We are excited to ship the first version of pgroll, a command line tool that offers safe and reversible schema migrations for PostgreSQL

Start free,
pay as you grow

Xata provides the best free plan in the industry. It is production ready by default and doesn't pause or cool-down. Take your time to build your business and upgrade when you're ready to scale.

Free plan includes
  • 10 database branches
  • High availability
  • 15 GB data storage
  • 15 GB search engine storage
  • 2 GB file attachments
  • 250 AI queries per month
Start freeExplore all plans
Free plan includes
  • 10 database branches
  • High availability
  • 15 GB data storage
  • 15 GB search engine storage
  • 2 GB file attachments
  • 250 AI queries per month
Add column to table

Copyright © 2024 Xatabase Inc.
All rights reserved.

Product

RoadmapFeature requestsPricingStatusAI solutionsFile attachments