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

ModeEndpointTransportState LocationMax DurationBest For
Immediate QueryPOST /querySynchronousIn-memory35 secondsFast reads and single-statement writes
Async QueryPOST /query (type: "async")PollingPostgreSQL DB5 minutesHeavy analytical queries and reports
Atomic BatchPOST /transaction/executePolling / AsyncPostgreSQL DB60 secondsMulti-statement transactional workflows
Queued TransactionPOST /transaction/createInteractiveProcess memory15 min TTLInteractive 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 POST request. 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_timeout is 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 returns 202 Accepted with a queryId and a Location header (/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/:id until finalized: 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-Key header. 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, and deferrable.
{ "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:
    1. Call POST /transaction/create to obtain a transactionId.
    2. Send statements one-by-one to POST /transaction/add.
    3. Optionally remove statements using POST /transaction/remove.
    4. Submit {"query":"COMMIT"} to POST /transaction/add to run the entire queue atomically on one PostgreSQL connection.
  • 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.
Copyright © 2026