NpgsqlRest 4.1 Against 3.21, PostgREST and Hand-Written Code
Before you trust these numbers
- The author of NpgsqlRest runs this benchmark, and NpgsqlRest is one of the services in it.
- The analysis was written with AI assistance from the raw data, then reviewed and edited.
- Every test ran once, on one machine. Treat differences of a few percent as a tie.
- Everything is public so you can check it: the code of every service, the data, and everything that went wrong, and how to run it yourself. Corrections and pull requests are welcome.
This benchmark is run by the author of NpgsqlRest, which is one reason everything about it is public. Eight of the 31 services are NpgsqlRest: two versions (3.21.0 from July, and 4.1.0), two builds (native AOT and JIT) and two ways to define endpoints (PostgreSQL functions, and plain SQL files).
Between the two versions, the path a request takes was rebuilt: responses are written straight into the HTTP response as rows arrive, the request context and the routine go to PostgreSQL in one round trip, pooled connections are reset only when an endpoint needs it, and the runtime moved from .NET 10 to .NET 11 with Npgsql 11. The 3.21.0 services run the same images as in July, so they are also this round's controls.
This page answers four questions:
- Is version 4.1 better than 3.21?
- AOT or JIT build?
- Endpoints as functions or as SQL files?
- How does it compare with PostgREST, Hasura and PostGraphile, and with code written by hand?
The answers, in short
- 4.1 against 3.21: a request needs 20% to 32% less CPU and the service uses 18% to 30% less memory. Large responses are about twice as fast; other requests are about as fast as before.
- AOT or JIT: JIT is about 5% faster under constant load. AOT starts four times sooner and uses about 40% less memory.
- Functions or SQL files: the same speed. Choose by how you like to work.
- Against the others: about 2.7 times the requests of PostgREST, about 4 times those of Hasura and PostGraphile, and level with .NET code written by hand.
What is compared
Eight NpgsqlRest services, from three choices:
| Choice | The two options |
|---|---|
| Version | 3.21.0 (the same images as in July) and 4.1.0 |
| Build | AOT: a native executable, no .NET runtime needed. JIT: runs on the .NET runtime |
| Endpoints | PostgreSQL functions, or plain .sql files |
None of them has endpoint code: the service is a configuration file (appsettings.json), and the endpoints are the functions or the SQL files. Each has 4 CPU cores.
Question 1: is 4.1 better than 3.21?
Pick a test and compare. For each of the four variants, the grey bar is version 3.21 and the purple bar is version 4.1. The label on the right says, in plain words, how 4.1 did.
Requests per second — Large payload, 25 users, 100 KB
3.21 4.1 How many requests were answered per second. More is better. The label on the right says how 4.1 did against 3.21.
The code of this test:the script that sends the requests · the PostgreSQL function it calls · the same as a SQL file. Click 3.21 ↗ or 4.1 ↗ next to a bar to open that service's own code for this test.
Things worth trying in the chart above:
- Large payload (where the chart starts): the biggest change of the release. Then switch to "Memory".
- Data types, 100 users, "Requests per CPU core": the same throughput for much less processor.
- Table insert: the one kind of test where 4.1 is a little behind 3.21.
The averages
The change from 3.21 to 4.1 over all 44 tests. A plus is good for the first two columns; a minus is good for the last two.
| Requests/s | Requests/s per CPU core | Peak memory | p99 latency | |
|---|---|---|---|---|
| AOT, functions | +8.0% | +25% | −30% | −9% |
| JIT, functions | +6.5% | +47% | −23% | −8% |
| AOT, SQL files | +7.1% | +25% | −25% | −10% |
| JIT, SQL files | +7.0% | +46% | −18% | −7% |
- A request costs a quarter to a half less CPU. Requests per core are up +25% for the AOT builds and +47% for the JIT builds.
- Memory is down 18% to 30%.
- Throughput is up +7% to +8% on average, and that average hides the shape of it. Almost all of the gain is in large responses, where 4.1 is 1.7 to 2.2 times as fast, and in list pages for the JIT builds (+15%). Elsewhere the two versions are within a few percent, which is what happens when the limit is somewhere else (see what limited the tests).
- Inserts are a few percent slower on 4.1, most with the AOT builds (−7%). It is the one place where the new version is behind the old one by more than a tie.
- Tail latency is 7% to 10% lower.
The same numbers for each kind of test
Requests per second, 4.1 against 3.21
| Kind of test | AOT, functions | JIT, functions | AOT, SQL files | JIT, SQL files |
|---|---|---|---|---|
| Data types | +2% | +1% | +1% | +1% |
| Minimal request | −4% | +2% | −3% | +1% |
| POST body | +0% | +0% | −0% | +0% |
| Nested JSON | +1% | +1% | +1% | +1% |
| Large payload | +125% | +71% | +116% | +73% |
| Many parameters | −1% | +6% | −0% | +6% |
| Table lookup | −1% | +1% | −1% | +5% |
| Table list page | +2% | +15% | +1% | +14% |
| Table insert | −7% | −3% | −6% | −1% |
| All 44 tests | +8% | +7% | +7% | +7% |
Requests per CPU core, 4.1 against 3.21
| Kind of test | AOT, functions | JIT, functions | AOT, SQL files | JIT, SQL files |
|---|---|---|---|---|
| Data types | +33% | +62% | +31% | +60% |
| Minimal request | +2% | +13% | +5% | +13% |
| POST body | +18% | +44% | +19% | +44% |
| Nested JSON | +18% | +48% | +15% | +47% |
| Large payload | +114% | +99% | +119% | +107% |
| Many parameters | +0% | +16% | +4% | +14% |
| Table lookup | +4% | +16% | +4% | +13% |
| Table list page | +6% | +31% | +8% | +30% |
| Table insert | −0% | +11% | +2% | +14% |
| All 44 tests | +25% | +47% | +25% | +46% |
Memory, by kind of test
Peak memory of the container during the tests of each type, in MB: 3.21 → 4.1.
| Kind of test | AOT, functions | JIT, functions | AOT, SQL files | JIT, SQL files |
|---|---|---|---|---|
| Data types | 165 → 105 | 166 → 147 | 177 → 104 | 160 → 156 |
| Minimal request | 109 → 104 | 179 → 167 | 115 → 108 | 163 → 161 |
| POST body | 125 → 83 | 166 → 163 | 110 → 74 | 153 → 142 |
| Nested JSON | 101 → 65 | 140 → 129 | 96 → 82 | 149 → 130 |
| Large payload | 365 → 133 | 422 → 204 | 436 → 179 | 542 → 224 |
| Many parameters | 305 → 128 | 394 → 197 | 347 → 190 | 394 → 191 |
| Table lookup | 198 → 97 | 280 → 195 | 124 → 89 | 223 → 192 |
| Table list page | 143 → 88 | 280 → 152 | 126 → 90 | 208 → 166 |
| Table insert | 158 → 118 | 252 → 157 | 166 → 110 | 228 → 154 |
| Highest in any test | 365 → 133 | 422 → 204 | 436 → 190 | 542 → 224 |
Large responses
A 100 KB response at 25 users at the same time is the single largest difference between two versions of any service in this round: 2,574 requests per second on 3.21 (AOT, functions) and 8,371 on 4.1. Version 3.21 read the whole value from PostgreSQL, built the response in memory and then sent it. Version 4.1 forwards it as it arrives.
↑ longer bar is betterEach bar is how many requests that service answered per second in this test.
- 1NpgsqlRest 4.1 JIT · SQL files8,786req/s
- 2NpgsqlRest 4.1 AOT · SQL files8,617req/s
- 3Fastify 5.128,584req/s
- 4NpgsqlRest 4.1 JIT · functions8,528req/s
- 5NpgsqlRest 4.1 AOT · functions8,371req/s
- 6Go 1.27 · net/http8,251req/s
- 7Actix-web 4.20 (Rust)8,118req/s
- 8.NET 11 · plain Npgsql5,762req/s
- 9PostgREST 16.64,211req/s
- 10NpgsqlRest 3.21 JIT · functions3,289req/s
- 11NpgsqlRest 3.21 JIT · SQL files3,210req/s
- 12NpgsqlRest 3.21 AOT · functions2,574req/s
- 13NpgsqlRest 3.21 AOT · SQL files2,446req/s
Question 2: AOT or JIT?
NpgsqlRest ships as a native AOT executable and as a JIT build. The AOT build against the JIT build of the same 4.1.0, over all 44 tests:
| Requests/s | Requests/s per CPU core | Peak memory | p99 latency | |
|---|---|---|---|---|
| AOT against JIT, functions | −5.0% | −13% | −44% | +0% |
| AOT against JIT, SQL files | −5.9% | −14% | −41% | +6% |
| Service | First answer after ms | Idle memory MB | Peak memory MB | Image MB |
|---|---|---|---|---|
| NpgsqlRest 4.1 AOT · functions | 287 | 22.8 | 133 | 59 |
| NpgsqlRest 4.1 JIT · functions | 1,213 | 38.9 | 204 | 123 |
| NpgsqlRest 4.1 AOT · SQL files | 244 | 23.9 | 190 | 59 |
| NpgsqlRest 4.1 JIT · SQL files | 1,254 | 39.4 | 224 | 123 |
JIT is faster under sustained load, AOT is smaller and starts sooner. The JIT build has 5% more throughput on average, because the JIT compiler optimizes the hot path with what it learns while running. The AOT build answers its first request about 4 times sooner, idles on 23 MB instead of 39, peaks lower, and its image is half the size. For a service that is scaled up and down or started on demand, AOT; for a long-running one that is always busy, JIT.
Question 3: functions or SQL files?
An endpoint can be a PostgreSQL function, or a .sql file with the statement in it. SQL files against functions on 4.1.0:
| Requests/s | Requests/s per CPU core | Peak memory | p99 latency | |
|---|---|---|---|---|
| SQL files against functions, AOT | +2.7% | +1% | +6% | +1% |
| SQL files against functions, JIT | +3.6% | +1% | +1% | −4% |
They are the same within a few percent (+2.7% and +3.6% in throughput). Choose by how you want to work, not by speed. In this benchmark each SQL file holds the same statement as the body of the matching function, so this compares two ways to run the same SQL: through a function call, or directly.
Question 4: how does it compare with the others?
Against the other generated APIs
PostgREST, Hasura and PostGraphile also turn a PostgreSQL schema into an API without endpoint code. Pick a test to see all of them with the four NpgsqlRest 4.1 variants:
↑ longer bar is betterHow many requests each service answered per second, with 100 users sending requests at the same time.
- 1NpgsqlRest 4.1 JIT · SQL files16,948req/sk6 limit
- 2NpgsqlRest 4.1 JIT · functions15,940req/sk6 limit
- 3NpgsqlRest 4.1 AOT · SQL files15,861req/sk6 limit
- 4NpgsqlRest 4.1 AOT · functions15,681req/sk6 limit
- 5PostgREST 16.64,027req/s
- 6PostGraphile 5.2 (GraphQL)2,465req/sk6 limit
- 7Hasura 2.50 (GraphQL)1,767req/s
The code of this test:the script that sends the requests · the PostgreSQL function it calls · the same as a SQL file. Click a service's name (↗) to open its own code for this test.
How many times as many requests per second NpgsqlRest 4.1 (AOT, functions) answered, for each kind of test. "2.7×" means 2.7 times as many.
| Kind of test | PostgREST 16.6 | Hasura 2.50 | PostGraphile 5.2 |
|---|---|---|---|
| Data types | 2.0× | 2.8× | 5.3× |
| Minimal request | 7.0× | 13.9× | 4.5× |
| POST body | 2.3× | 4.5× | 3.5× |
| Nested JSON | 1.9× | 3.6× | 4.7× |
| Large payload | 1.5× | 1.4× | 1.3× |
| Many parameters | 9.2× | 13.7× | 4.2× |
| Table lookup | 6.7× | 14.6× | 4.6× |
| Table list page | 5.4× | 10.7× | 4.9× |
| Table insert | 5.4× | 13.6× | 4.3× |
| All 44 tests | 2.7× | 4.4× | 4.1× |
| Service | Position on the leaderboard (of 31) | Peak memory MB | Idle memory MB | First answer after ms | Image MB | Requests/s per CPU core |
|---|---|---|---|---|---|---|
| NpgsqlRest 4.1 AOT · functions | 14 | 133 | 22.8 | 287 | 59 | 3,539 |
| PostgREST 16.6 | 26 | 142 | 7.2 | 165 | 20 | 1,508 |
| Hasura 2.50 (GraphQL) | 28 | 2,571 | 159.1 | 2,365 | 206 | 942 |
| PostGraphile 5.2 (GraphQL) | 29 | 1,309 | 372.2 | 943 | 220 | 832 |
Two things to keep in mind when reading this:
- PostgREST is smaller at rest. Its image is a third of the size and it idles on 7 MB against 23. Under load the two use about the same memory (the most in any test: 142 MB for PostgREST, 133 MB for NpgsqlRest).
- PostgREST was run twice. In the first run it had 10 database connections instead of 100, by a mistake in this benchmark's setup. These are the results with 100. What happened.
- Hasura and PostGraphile speak GraphQL, which is a different protocol with different strengths: a client asks for exactly the fields it wants, in one request across several objects. In this benchmark every request is a
POSTwith a query document that the server has to parse and plan before it reaches PostgreSQL, and Hasura calls wrapper functions because it cannot expose a function that returnsTABLE(...). It is the same data from the same functions, asked for the way each product is used.
Against code written by hand
NpgsqlRest 4.1 (AOT, functions) against services where every endpoint is written by hand, over all 44 tests:
| NpgsqlRest 4.1 AOT against | Requests/s | Requests/s per CPU core | Peak memory | p99 latency |
|---|---|---|---|---|
| .NET 11, plain Npgsql | −0.6% | −1% | −38% | −6% |
| .NET 11, Dapper | +1.0% | +2% | −42% | −5% |
| .NET 11, EF Core | +57.1% | +101% | −54% | −29% |
| Go, net/http | −18.7% | −4% | +97% | +17% |
| Rust, Actix-web | −1.0% | −10% | +128% | +6% |
| Java, Spring Boot | +25.7% | +43% | −94% | −27% |
| Node.js, Fastify | +11.3% | +35% | −84% | +5% |
| Bun | −10.9% | −3% | −57% | +9% |
| Python, FastAPI | +216.7% | +352% | −71% | −58% |
- Level with hand-written .NET on plain Npgsql and Dapper, with about 40% less memory. The generated API costs nothing over writing it by hand on the same platform. More on the .NET page.
- Behind Go by 19% in throughput, with about twice the memory. Go is the leader of this round.
- Level with the Actix-web service in throughput (−1.0%), which uses 12% less CPU per request. The two Rust services in this benchmark build each SQL statement as text instead of preparing it, so a tuned Rust service would do better than these numbers.
The code
What somebody has to write for the nine endpoints of this benchmark, in non-blank lines:
| Service | Own code and configuration | SQL | Total | Position on the leaderboard (of 31) | Its code |
|---|---|---|---|---|---|
| NpgsqlRest 4.1 AOT · SQL files | 29 | 135 (SQL files) | 164 | 9 | appsettings.json |
| NpgsqlRest 4.1 AOT · functions | 21 | 205 (functions) | 226 | 14 | appsettings.json |
| PostgREST 16.6 | 16 | 205 (functions) | 221 | 26 | postgrest.conf |
| PostGraphile 5.2 (GraphQL) | 44 | 205 (functions) | 249 | 29 | graphile.config.mjs |
| Hasura 2.50 (GraphQL) | 86 | 277 (functions and wrappers) | 363 | 28 | databases.yaml |
| .NET 11 · EF Core | 116 | 205 (functions) | 321 | 24 | Program.cs |
| .NET 11 · Dapper | 155 | 205 (functions) | 360 | 16 | Program.cs |
| .NET 11 · plain Npgsql | 281 | 205 (functions) | 486 | 13 | Program.cs |
| Fastify 5.12 | 129 | 205 (functions) | 334 | 20 | app.js |
| Go 1.27 · net/http | 408 | 205 (functions) | 613 | 2 | main.go |
The whole NpgsqlRest service is a 21-line configuration file (appsettings.json) that says where the database is and which schema to expose. The endpoints are the functions themselves, with one comment each (HTTP GET /api/perf-test). With SQL files there is not even a function: each file is a statement with a comment on top. The same nine endpoints are 281 lines of C# with plain Npgsql and 408 lines of Go, in addition to the same functions. All counts: Code you write.
All eight NpgsqlRest services
Pick a test to see the eight side by side.
↑ longer bar is betterHow many requests each service answered per second, with 100 users sending requests at the same time.
- 1NpgsqlRest 4.1 JIT · SQL files16,948req/sk6 limit
- 2NpgsqlRest 4.1 JIT · functions15,940req/sk6 limit
- 3NpgsqlRest 3.21 JIT · SQL files15,934req/s
- 4NpgsqlRest 4.1 AOT · SQL files15,861req/sk6 limit
- 5NpgsqlRest 4.1 AOT · functions15,681req/sk6 limit
- 6NpgsqlRest 3.21 JIT · functions15,478req/s
- 7NpgsqlRest 3.21 AOT · SQL files15,316req/s
- 8NpgsqlRest 3.21 AOT · functions15,171req/s
The code of this test:the script that sends the requests · the PostgreSQL function it calls · the same as a SQL file. Click a service's name (↗) to open its own code for this test.
| Service | Position on the leaderboard (of 31) | Speed score | Tests where it was the fastest (of 44) | Requests/s per CPU core | Peak memory MB |
|---|---|---|---|---|---|
| NpgsqlRest 4.1 JIT · SQL files | 4 | 82% | 6 | 4,131 | 224 |
| NpgsqlRest 4.1 JIT · functions | 6 | 79% | 1 | 4,079 | 204 |
| NpgsqlRest 3.21 JIT · SQL files | 7 | 77% | 3 | 2,829 | 542 |
| NpgsqlRest 4.1 AOT · SQL files | 9 | 77% | 0 | 3,557 | 190 |
| NpgsqlRest 4.1 AOT · functions | 14 | 75% | 0 | 3,539 | 133 |
| NpgsqlRest 3.21 JIT · functions | 15 | 75% | 0 | 2,779 | 422 |
| NpgsqlRest 3.21 AOT · SQL files | 17 | 73% | 0 | 2,842 | 436 |
| NpgsqlRest 3.21 AOT · functions | 19 | 71% | 0 | 2,828 | 365 |
Previous: ← .NET 10 and .NET 11 · Next: Compare Any Two →