Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

    View Slide

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

    View Slide

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

    View Slide

  4. View Slide

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

    View Slide

  6. 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

    View Slide

  7. 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!

    View Slide

  8. 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

    View Slide

  9. 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

    View Slide

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

    View Slide

  11. Let’s go write a Postgres extension in Rust

    View Slide

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

    View Slide

  13. 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

    View Slide

  14. Just double checking

    View Slide

  15. Setup continued
    • Create the new extension

    View Slide

  16. Has built in Hello World

    View Slide

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

    View Slide

  18. GNU linker (gold) is really slow

    View Slide

  19. But mold is fast

    View Slide

  20. 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

    View Slide

  21. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  28. Demo time

    View Slide

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

    View Slide

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

    View Slide

  31. Any questions?

    View Slide

  32. Resources

    View Slide