Serverless & Edge
Execution Modes
Choose the right execution mode for your serverless and edge workloads.
Choosing an Execution Mode
HTTP PostgreSQL Gateway supports four distinct execution modes. Selecting the right mode depends on statement duration, atomicity requirements, and your serverless platform's invocation constraints.
Comparison Matrix
| Mode | Endpoint | Transport | State Location | Max Duration | Best For |
|---|---|---|---|---|---|
| Immediate Query | POST /query | Synchronous | In-memory | 35 seconds | Fast reads and single-statement writes |
| Async Query | POST /query (type: "async") | Polling | PostgreSQL DB | 5 minutes | Heavy analytical queries and reports |
| Atomic Batch | POST /transaction/execute | Polling / Async | PostgreSQL DB | 60 seconds | Multi-statement transactional workflows |
| Queued Transaction | POST /transaction/create | Interactive | Process memory | 15 min TTL | Interactive multi-step workflows |
1. Immediate Queries (Synchronous)
Immediate queries are the standard execution mode for serverless functions.
- How it works: The client sends SQL and parameters in a single
POSTrequest. The gateway checks out a pooled connection, executes the statement, and returns the result in the HTTP response. - Deadlines: Connection checkout waits up to 10 seconds. PostgreSQL
statement_timeoutis set to 30 seconds. The gateway client response timeout is 35 seconds. - Best for: Webhook handlers, API endpoints, user lookups, CRUD operations.
curl -X POST http://localhost:8080/query \
-H 'x-api-key: <api-key>' \
-H 'x-database: sales' \
-H 'content-type: application/json' \
-d '{"query":"SELECT count(*) FROM orders WHERE status = $1","parameters":["paid"]}'2. Asynchronous Queries (Background Polling)
Serverless functions often have hard execution limits (e.g., Cloudflare Workers free tier is 50ms CPU time; Vercel hobby functions timeout after 10–60 seconds). If a query might exceed that limit, use asynchronous execution.
- How it works: Pass
"type": "async"in the request body. The gateway immediately returns202 Acceptedwith aqueryIdand aLocationheader (/query/<queryId>). - Persistence: The query runs in the background. The final result is saved in PostgreSQL (
http_postgres_gateway.async_queries) and survives gateway restarts. - Polling: The client polls
GET /query/:iduntilfinalized: true. - Limits: Allows up to 5 minutes of PostgreSQL execution time. Each gateway process handles up to 32 concurrent asynchronous queries.
# 1. Submit async query
curl -X POST http://localhost:8080/query \
-H 'x-api-key: <api-key>' \
-H 'x-database: analytics' \
-d '{"type":"async","query":"SELECT generate_series(1, 1000000)"}'
# Response:
# {"status":"running","queryId":"qry_01jk98..."}
# 2. Poll for results from another invocation
curl http://localhost:8080/query/qry_01jk98... \
-H 'x-api-key: <api-key>' \
-H 'x-database: analytics'3. Single-Request Atomic Batches
When multiple statements must succeed or fail together, use atomic batches.
- How it works: Send an array of 1 to 100 statements to
POST /transaction/execute. The gateway manages the transaction block (BEGIN, statements,COMMIT). - Idempotency: Pass an
Idempotency-Keyheader. If a network blip occurs, resubmitting the same batch with the same key returns the existing transaction status without re-executing SQL. - Durability: The batch record is staged in PostgreSQL (
http_postgres_gateway.transactions). - Options: Supports configuring
isolationLevel(read committed,repeatable read,serializable),readOnly, anddeferrable.
{
"queries": [
{"query": "UPDATE accounts SET balance = balance - 100 WHERE id = $1", "parameters": [1]},
{"query": "UPDATE accounts SET balance = balance + 100 WHERE id = $2", "parameters": [2]},
{"query": "INSERT INTO audit_log (action, amount) VALUES ($1, $2)", "parameters": ["transfer", 100]}
],
"options": {
"isolationLevel": "serializable"
}
}4. Interactive Queued Transactions
Queued transactions allow building a transaction incrementally across multiple HTTP requests.
- How it works:
- Call
POST /transaction/createto obtain atransactionId. - Send statements one-by-one to
POST /transaction/add. - Optionally remove statements using
POST /transaction/remove. - Submit
{"query":"COMMIT"}toPOST /transaction/addto run the entire queue atomically on one PostgreSQL connection.
- Call
- Limitations:
- Queues are kept in process memory. They do not survive gateway restarts.
- In a multi-replica deployment, requests for the same transaction must route to the same gateway instance via sticky sessions (session affinity).
For stateless serverless applications, Single-Request Atomic Batches (
POST /transaction/execute) are strongly recommended over queued transactions because they are durable, replica-independent, and support idempotency keys.