Skip to content

Commit

Permalink
docs: Add documentation on PostgreSQL extension (#4124)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: eitsupi <50911393+eitsupi@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 25, 2024
1 parent 461741a commit cd95bc6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions web/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
- [VS Code](./project/integrations/vscode.md)
- [Rill](./project/integrations/rill.md)
- [Syntax highlighting](./project/integrations/syntax-highlighting.md)
- [PostgreSQL](./project/integrations/postgresql.md)

- [Contributing to PRQL](./project/contributing/README.md)

Expand Down
1 change: 1 addition & 0 deletions web/book/src/project/integrations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ PRQL is building integrations with lots of external tools, including:
- [Prefect](./prefect.md)
- [VS Code](./vscode.md)
- [Rill](./rill.md)
- [PostgreSQL](./postgresql.md)

We also have a CLI, [`prqlc`](./prqlc-cli.md)
49 changes: 49 additions & 0 deletions web/book/src/project/integrations/postgresql.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# PostgreSQL

PL/PRQL is a PostgreSQL extension that lets you write functions with PRQL.

PL/PRQL functions serve as intermediaries, compiling the user's PRQL code into SQL statements that PostgreSQL executes. The extension is based on the [pgrx](https://github.com/pgcentralfoundation/pgrx) for developing PostgreSQL extensions in Rust. This framework manages the interaction with PostgreSQL's internal APIs, type conversions, and other function hooks necessary to integrate PRQL with PostgreSQL.


## Examples
PL/PRQL functions are defined using the `plprql` language specifier:
```sql
create function match_stats(int) returns table(player text, kd_ratio float) as $$
from matches
filter match_id == $1
group player (
aggregate {
total_kills = sum kills,
total_deaths = sum deaths
}
)
filter total_deaths > 0
derive kd_ratio = total_kills / total_deaths
select { player, kd_ratio }
$$ language plprql;

select * from match_stats(1001)

player | kd_ratio
---------+----------
Player1 | 0.625
Player2 | 1.6
(2 rows)
```

You can also run PRQL directly with the `prql` function which is useful for custom SQL in ORMs:

```sql
select prql('from matches | filter player == ''Player1''', 'player1_cursor');

fetch 2 from player1_cursor;

id | match_id | round | player | kills | deaths
----+----------+-------+---------+-------+--------
1 | 1001 | 1 | Player1 | 4 | 1
3 | 1001 | 2 | Player1 | 1 | 7
(2 rows)
```

## Getting Started
For installation instructions and more information on the extension, see the [PL/PRQL repository](https://github.com/kaspermarstal/plprql).
4 changes: 4 additions & 0 deletions web/website/content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ integrations_section:
link: https://github.com/ywelsch/duckdb-prql
text: A DuckDB extension to execute PRQL

- label: "PostgreSQL"
link: https://github.com/kaspermarstal/plprql
text: Write PRQL functions in PostgreSQL

bindings_section:
enable: true
title: "Bindings"
Expand Down

0 comments on commit cd95bc6

Please sign in to comment.