Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Postgres without SQL: Natural language queries ...

Postgres without SQL: Natural language queries using GPT-3 & Rust | Citus Con: An Event for Postgres 2023 | Jelte Fennema

Slides from a conference talk about Generative AI and a new Postgres extension which allows you to use the power of GPT-3 right from your database. Presented by Jelte Fennema at Citus Con: An Event for Postgres 2023, this new PG extension makes it easy to optimize your database schema, query your data, and even distribute your Postgres tables using the open source Citus database extension. All of this by using normal human language, and without the need to know any SQL. Now you can finally jump on the NoSQL bandwagon while still using the much-loved PostgreSQL relational database.

This talk includes a section on how to write Postgres extensions using Rust (no Rust experience required.)

Citus Data

May 24, 2023
Tweet

More Decks by Citus Data

Other Decks in Technology

Transcript

  1. 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
  2. My not so secret problem • I don’t like doing

    boring things • Copy pasting schemas and queries around sounds pretty boring
  3. 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
  4. 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!
  5. 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
  6. 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
  7. But I never used Rust professionally • So, let’s change

    that! • How do I combine Postgres and Rust? And maybe even Citus?
  8. Setup is extremely easy • Install pgx, also installs 5

    postgres versions: • Create a new extension:
  9. 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
  10. So let’s make some small changes • What? Why does

    it take 7 seconds to compile this tiny extension?
  11. 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
  12. 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
  13. Sending requests to the GPT API • Use the Rust

    package manager to get an API client
  14. Sending requests to the GPT API • Tell it what

    you have • Tell it what you need • Tell it what you do not want
  15. Sending requests • Rust loves async and threads • Async

    support in pgx is non-existent • Postgres and pgx don’t like threads
  16. Returning arbitrary results • Another problem: Postgres requires functions to

    have known columns • Let’s go fully NoSQL and just return a single json column