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

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
  • Single team member
  • 10 database branches
  • High availability
  • 15 GB data storage
Start freeExplore all plans
Free plan includes
  • Single team member
  • 10 database branches
  • High availability
  • 15 GB data storage

Sign up to our newsletter

By subscribing, you agree with Xata’s Terms of Service and Privacy Policy.

Copyright © 2024 Xatabase Inc.
All rights reserved.

Product

Feature requestsPricingStatusAI solutions