SAService Architect Open designer
SAService ArchitectDocumentation
Stream reference Request → Query → Result (0…N), Error

Query: PostgreSQL and S3

Query performs a storage operation for each incoming request. The endpoint selects the operation and the connector selects the native library.

When to use

Read or write PostgreSQL data or S3 objects as an explicit stage of a pipeline.

Behavior

  • Query is a separate stream, not a Sink subclass. Each request may produce zero, one or many typed results.
  • PostgreSQL endpoints choose Rows or Execute. Use Rows for SELECT and writes with RETURNING; Returning is not a separate operation. Execute reports the native affected-row result. S3 endpoints choose GetObject, HeadObject, ListObjects, PutObject or DeleteObject.
  • Shared PostgreSQL pools and S3 clients are created through service infrastructure makers during initialization, like gRPC clients. Endpoints reuse them. Customize the native library through the maker hook; do not create clients in main or for each request.
  • Build parameterized queries in the handler using the selected native library, or reference a user-owned SQL file. SQL values must remain separate from SQL text.
  • EndRequest receives the ordinary result collector. The application may send a completion value through it; Query has no automatic completion message or separate completion port.
  • An empty successful query emits no results. If a SubStream or HTTP caller needs a reply for empty/error outcomes, define that reply explicitly in the application; an Error branch alone does not return a result.
  • Query adds no second result queue. Synchronous downstream work delays mapping; queued links retain their configured buffering and overload policy.
  • Release the native cursor or response body before EndRequest. Finalization does not mean that asynchronously scheduled downstream business work has completed.

PostgreSQL: build a query in code

TypeScript / PostgreSQLKysely excerpt from the canonical storage-query example. The generated handler receives the maker-owned Kysely client. Database, HandlerState and StorageRequest are application types. This is the buildQuery method body, not a standalone program.

For queryOperation=Rows, map each row in handleRow through the supplied out collector. A write with RETURNING also uses Rows. For queryOperation=Execute, map the native execution result instead. Query does not choose the operation by inspecting SQL.

// Rows: parameterized SELECT
state.requestId = value.requestId;
return db.selectFrom("query_example_items")
  .select("value").where("key", "=", value.key).compile();

// Alternative Rows handler: parameterized INSERT ... RETURNING
return db.insertInto("query_example_items")
  .values({ key: value.key, value: value.value })
  .returning("value").compile();

User-owned SQL files

sqlFile identifies a user-owned SQL asset. It is not a migration and does not cause automatic execution at startup. Keep parameter binding in the endpoint handler using the selected library; do not interpolate request values into SQL. Placeholder syntax and loading helpers are library-specific.

The following PostgreSQL statement illustrates the asset contents. Supply the key separately through the native parameter API. Preserve both SQL and handler code when regenerating the project.

SELECT value FROM query_example_items WHERE key = $1;

S3: build a request and consume its result

TypeScript / S3AWS GetObject handler excerpts from the canonical example. buildRequest returns the native request; infrastructure owns and reuses the client. handleResult consumes the response body within the invocation. Import Readable from node:stream. The application chooses how chunks become typed results.

This Node body example does not apply to C++/userver or buffered C++/Coro responses. Use the native response representation of that connector and enforce application object-size limits. Do not retain a response body after handleResult returns.

// buildRequest(context, state, value)
state.requestId = value.requestId;
return { Bucket: "query-example", Key: value.key };

// async handleResult(context, state, result, out)
if (!(result.Body instanceof Readable)) throw new Error("Expected a Node S3 response body");
for await (const chunk of result.Body) {
  context.signal().throwIfAborted();
  await out.out(context, {
    requestId: state.requestId, kind: "object", value: Buffer.from(chunk),
  });
}

Application completion and errors

An application can send its own completed result from EndRequest (endRequest in TypeScript). This optional convention is shared with ordinary results, not a new port or an SDK-generated message. The example sends completion only on success; applications needing an error reply must define it explicitly.

Native resources are released before finalization. Already emitted results remain emitted if a later row, chunk or downstream call fails. EndRequest does not wait for queued downstream work to drain. Never replay partial results or writes automatically.

// async endRequest(context, stream, state, error, out, errors)
if (error !== undefined) {
  await errors.out(context, error);
  return;
}
if (state === undefined) throw new Error("Successful Query has no request state");
await out.out(context, {
  requestId: state.requestId, kind: "completed", value: new Uint8Array(),
});

Endpoint properties

PropertyTypeDescription
queryOperationenum

Operation supported by the selected connector.

sqlFilestring

Optional user-owned PostgreSQL SQL file; omit when building the query in code.

bucketstring

Default S3 bucket. C++/userver requires it in resolved runtime endpoint configuration (model or runtime override), because its native client is bucket-bound. AWS request builders may supply it dynamically.

fetchSizeinteger

Native PostgreSQL row batch size where supported; not a result limit or concurrency limit. PostgreSQLUserver accepts 1..4294967295 (uint32); this maximum is not a recommended batch size. Set S3 listing page size in the native request builder.

Pipeline

HTTP request → Validate request → Query (Rows) → Map result → HTTP response
HTTP request → Validate request → Query (PutObject) → Response