Slide 1

Slide 1 text

Postgres without SQL: Natural language queries using GPT-3 & Rust Jelte Fennema Senior Software Engineer @ Microsoft Developing Citus, PostgreSQL, & PgBouncer @JelteF | @citusdata | @AzureCosmosDB 2023-04-18

Slide 2

Slide 2 text

My secret problem I find it very difficult to write working Postgres queries

Slide 3

Slide 3 text

Solution I asked this question at: https://chat.openai.com/

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

My not so secret problem • I don’t like doing boring things • Copy pasting schemas and queries around sounds pretty boring

Slide 6

Slide 6 text

How to avoid boring stuff • Postgres has all this information • Can I tell Postgres talk to ChatGPT directly? • I would need to add some functionality to Postgres

Slide 7

Slide 7 text

Postgres extension to the rescue • Allows adding new functionality to Postgres such as types or functions • Part 1: A shared library that postgres loads (.so file) • Part 2: Some SQL definitions, e.g. function definitions • So, let’s write an extension!

Slide 8

Slide 8 text

I like Rust • It feels modern: everything is an expression + pattern matching • Its type system is fancy: Like Haskell but I can read it • Secure: No memory leaks, no double frees, no data races • It’s as fast as C/C++ • It has a package manager Rust logo by Rust Foundation, CC BY 4.0, https://commons.wikimedia.org/w/index.php?curid=40715219

Slide 9

Slide 9 text

I have been playing with Rust for a long time • I have a popular Rust library: derive_more • It automates writing boring boilerplate • 7 years old

Slide 10

Slide 10 text

But I never used Rust professionally • So, let’s change that! • How do I combine Postgres and Rust? And maybe even Citus?

Slide 11

Slide 11 text

Let’s go write a Postgres extension in Rust

Slide 12

Slide 12 text

Setup is extremely easy • Install pgx, also installs 5 postgres versions: • Create a new extension:

Slide 13

Slide 13 text

Oh no, we need a name for this extension • pg_gpt is the obvious choice but is already taken  https://github.com/cloudquery/pg_gpt • gptpg: palyndromes are awesome but is hard to pronounce • Final choice: pg_human

Slide 14

Slide 14 text

Just double checking

Slide 15

Slide 15 text

Setup continued • Create the new extension

Slide 16

Slide 16 text

Has built in Hello World

Slide 17

Slide 17 text

So let’s make some small changes • What? Why does it take 7 seconds to compile this tiny extension?

Slide 18

Slide 18 text

GNU linker (gold) is really slow

Slide 19

Slide 19 text

But mold is fast

Slide 20

Slide 20 text

Okay now can actually start. What do we need? • Getting the schema definition • Sending requests to the OpenAI GPT API • Executing SQL • Returning arbitrary SQL results

Slide 21

Slide 21 text

Getting the schema definition • Ugh, why doesn’t Postgres have a built-in function for this? • Fine I’ll fetch stuff from the catalog tables myself and concatenate some strings

Slide 22

Slide 22 text

Sending requests to the GPT API • Use the Rust package manager to get an API client

Slide 23

Slide 23 text

Sending requests to the GPT API • Give the AI some confidence

Slide 24

Slide 24 text

Sending requests to the GPT API • Tell it what you have • Tell it what you need • Tell it what you do not want

Slide 25

Slide 25 text

Sending requests • Rust loves async and threads • Async support in pgx is non-existent • Postgres and pgx don’t like threads

Slide 26

Slide 26 text

Executing SQL • Postgres has SPI (Server Programming Interface) • pgx has nice Rust bindings for SPI

Slide 27

Slide 27 text

Returning arbitrary results • Another problem: Postgres requires functions to have known columns • Let’s go fully NoSQL and just return a single json column

Slide 28

Slide 28 text

Demo time

Slide 29

Slide 29 text

Where to find this amazing Postgres extension https://github.com/JelteF/pg_human (USE AT YOUR OWN RISK)

Slide 30

Slide 30 text

Future ideas • Explaining explain plans • Index suggestions • Citus distribution column suggestions

Slide 31

Slide 31 text

Any questions?

Slide 32

Slide 32 text

Resources