gRPC implementation reference
This page describes how the gRPC wire is built: the tonic server, the two services in proto/synergydb.proto, authentication, every RPC, type and error mapping, transactions, limits and tests. For the port, the proof scores and how to connect, see the operator page gRPC. Authentication policy across all wires is in Security.
Code lives in src/protocol/grpc/ and proto/:
| File | Role |
|---|---|
proto/synergydb.proto |
Package synergydb.v1: services SynergyDb and Sql. |
build.rs |
Runs tonic-build with a vendored protoc (protoc-bin-vendored) and writes the descriptor set synergydb_descriptor.bin. |
mod.rs |
GrpcHandler, GrpcConfig, server start-up. |
interceptors.rs |
GrpcAuthGate (wired) and older interceptors (not wired). |
wire.rs |
GrpcWireService: the SynergyDb service. |
sql.rs |
SqlService: the Sql service. |
health.rs, reflection.rs, service.rs
|
Hand-written earlier versions; not wired. |
(source: Cargo.toml; build.rs; src/protocol/grpc/mod.rs)
Build feature
The grpc Cargo feature (tonic-health, tonic-reflection) is in the default feature set. Crates: tonic 0.12 with TLS, prost 0.13, tonic-health 0.12, tonic-reflection 0.12. (source: Cargo.toml, [features], [dependencies])
In a build without the feature, handle_grpc_connection reads the HTTP/2 preface, sends SETTINGS and then GOAWAY (INTERNAL_ERROR) with the text "SynergyDB gRPC is not enabled in this build; rebuild with –features grpc". (source: src/protocol/grpc/mod.rs, handle_grpc_connection under cfg(not(feature = "grpc")))
Listener and transport
- The default port is 9090 (
grpc_portinServerConfig::default). The wire starts only when the port is set; a config file that omits the key turns it off. (source:src/config.rs,ServerConfig::default;src/server/mod.rs) -
GrpcHandler::startbuilds a tonic server with HTTP/2 keepalive interval 60 s, keepalive timeout 20 s and at most 200 concurrent streams per connection (fromGrpcConfig::default). (source:src/protocol/grpc/mod.rs,GrpcHandler::start,GrpcConfig::default) - With a
[tls]section the server usestls::grpc_incoming: ALPNh2, TLS 1.3 and 1.2, and first-byte detection (accept_auto), so TLS and plaintext share the port. Plaintext is allowed only whenrequire_tlsis off or the peer is loopback. Without TLS it usesserve_with_shutdown. (source:src/protocol/tls.rs,grpc_incoming,TlsListener::accept_auto) Not verified: no test covers gRPC over TLS; the status entry lists TLS as untested.
Services served (source: src/protocol/grpc/mod.rs, GrpcHandler::start):
| Service | Auth gate |
|---|---|
synergydb.v1.SynergyDb (SynergyDbServer::with_interceptor(GrpcWireService, GrpcAuthGate)) |
yes |
synergydb.v1.Sql (SqlServer::with_interceptor(SqlService, GrpcAuthGate)) |
yes |
grpc.health.v1.Health (tonic-health; both services set to SERVING) |
no |
Server reflection v1 and v1alpha (tonic-reflection, both descriptor sets) |
no |
Not wired: GrpcConfig.max_message_size (64 MB), enable_compression, request_timeout and connection_timeout are never passed to the tonic server, so tonic's default 4 MiB decode limit applies to requests. (source: src/protocol/grpc/mod.rs, GrpcConfig, GrpcHandler::start)
Authentication
GrpcAuthGate::authorize is a synchronous tonic interceptor that runs on every RPC of both services. It checks the password with verify_credential through block_in_place and block_on, and stores GrpcUser(user) in the request extensions. (source: src/protocol/grpc/interceptors.rs, GrpcAuthGate::authorize)
authorization metadata |
Result |
|---|---|
Basic base64(user:pass) (also lowercase basic), right password |
accepted |
| Basic, wrong password | UNAUTHENTICATED "invalid username or password" |
| Basic, not decodable | UNAUTHENTICATED "malformed Basic credential" |
Bearer ... or ApiKey ...
|
UNAUTHENTICATED "token authentication is not configured; use Basic auth with a catalog credential" |
| any other scheme | UNAUTHENTICATED "unsupported authorization scheme" |
| missing | UNAUTHENTICATED "missing credentials: supply an 'authorization' header (Basic base64(user:pass))" |
(source: src/protocol/grpc/interceptors.rs, GrpcAuthGate::authorize)
- The check uses
verify_credential(catalog password plus the failed-login delay), notverify_wire_credential, so thesynergy_wire_grpcmembership check does not apply on this wire. (source:src/protocol/grpc/interceptors.rs,GrpcAuthGate::authorize;src/auth/wire_auth.rs,verify_credential,verify_wire_credential) -
SYNERGYDB_DEV_INSECURE_AUTHlets every request through with no user. (source:src/auth/wire_auth.rs,dev_insecure_auth_enabled) -
Not verified: no Rust test drives
GrpcAuthGate.
Service synergydb.v1.Sql
SqlService opens a new engine for each call unless the request names a transaction_id. The database metadata selects the database. The session is resolved for the authenticated user and audit records are tagged grpc. Statements run in spawn_blocking. (source: src/protocol/grpc/sql.rs, SqlService)
| RPC | Request | Behaviour |
|---|---|---|
Execute |
sql, params, transaction_id, fetch_size
|
Runs one statement; returns columns (name, type, type_oid), rows, rows_affected, command. |
ExecuteStream |
same | Server stream of ExecuteChunk. The whole result is computed first, then sent in chunks of fetch_size rows (default 1000). The first chunk carries the columns; the last has done = true, rows_affected and command. |
ExecuteBatch |
statements (sql, params), transaction_id, optional transactional
|
Without a transaction id the batch runs inside BEGIN/COMMIT by default. A failure rolls back, the message is prefixed "statement N of the batch:", and trailer x-statement-index holds the 0-based index. |
BeginTransaction |
isolation (ISOLATION_DEFAULT, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE), read_only
|
Runs BEGIN [ISOLATION LEVEL ...] [READ ONLY]; returns a transaction_id. |
Commit, Rollback
|
transaction_id |
Ends the transaction; returns command. |
(source: proto/synergydb.proto; src/protocol/grpc/sql.rs)
ExecuteStream is not an incremental read; memory use grows with the full result. (source: src/protocol/grpc/sql.rs, execute_stream) There are no client-streaming or bidirectional RPCs. (source: proto/synergydb.proto)
Transactions
-
BeginTransactionstores the engine under a random 16-byte hex id together with the opening user. A request that names another user's id gets NOT_FOUND with SQLSTATE 25P01 "transaction "id" does not exist". (source:src/protocol/grpc/sql.rs,SqlService::begin_transaction,SqlService::txn) - An idle reaper rolls back transactions unused for
SYNERGYDB_GRPC_TXN_IDLE_SECS(default 300), checked everymin(5 s, idle). (source:src/protocol/grpc/sql.rs,SqlService::new) -
refuse_txn_controlrefusesBEGIN,START,COMMIT,END,ABORTandROLLBACK(but notROLLBACK TO) sent as SQL, andSAVEPOINT/RELEASEoutside a transaction, with FAILED_PRECONDITION and SQLSTATE 25000. Use the RPCs instead. (source:src/protocol/grpc/sql.rs,refuse_txn_control) -
CommitandRollbackremove the transaction whatever the outcome. A failed COMMIT rolls back; when the engine reports a rollback the returnedcommandisROLLBACK. (source:src/protocol/grpc/sql.rs,SqlService::end)
Parameter and result types
Parameters use the Value oneof and are bound in text form with a type OID (source: src/protocol/grpc/sql.rs, bind_param):
Value field |
Text sent to the engine | OID |
|---|---|---|
null_value |
NULL | |
bool_value |
t / f
|
16 (bool) |
int_value (int64) |
decimal | 20 (int8) |
double_value |
decimal, NaN, Infinity, -Infinity
|
701 (float8) |
string_value |
as is | 0 (untyped) |
bytes_value |
\x hex |
17 (bytea) |
decimal_value (string) |
as is | 1700 (numeric) |
json_value (string) |
as is | 3802 (jsonb) |
Result cells are typed by column OID (source: src/protocol/grpc/sql.rs, typed_value):
| Column OID |
Value field |
|---|---|
| 16 |
bool_value (t/true/1, f/false/0) |
| 20, 21, 23, 26, 28 | int_value |
| 700, 701 |
double_value (NaN and infinities spelled out) |
| 1700 | decimal_value |
| 17 |
bytes_value (decodes \x hex) |
| 114, 3802 | json_value |
| anything else | string_value |
A value that does not parse as its column type falls back to string_value. Column.type is the PostgreSQL type name from type_oid_to_pg_typname. command is SELECT, or INSERT/UPDATE/DELETE/MERGE for statements with RETURNING, or a DDL tag such as CREATE TABLE from ddl_tag. (source: src/protocol/grpc/sql.rs, typed_value, ddl_tag) Unit test: tags_and_values.
Error mapping
The SQLSTATE comes from PostgresHandler::error_to_sqlstate and is returned in trailer x-sqlstate. code_for_sqlstate picks the gRPC status (source: src/protocol/grpc/sql.rs, code_for_sqlstate):
| SQLSTATE | gRPC status |
|---|---|
| 42501 | PERMISSION_DENIED |
| 42P01, 42703, 42704, 42883, 3D000, 3F000, 26000, 25P01 | NOT_FOUND |
| 42P07, 42710, 42P06, 42723, 23505 | ALREADY_EXISTS |
| 40001, 40P01, 55P03 | ABORTED |
| 57014 | DEADLINE_EXCEEDED |
| 0A000 | UNIMPLEMENTED |
| other 23xxx, 25xxx, 2Bxxx | FAILED_PRECONDITION |
| 22xxx, other 42xxx, 21xxx | INVALID_ARGUMENT |
| anything else | UNKNOWN |
Unit test: sqlstate_codes.
Service synergydb.v1.SynergyDb
GrpcWireService uses one server-wide QueryEngine behind a Tokio mutex, shared by all clients, so calls run one at a time. Each call sets the engine's session to the RPC user. The database metadata is ignored. (source: src/protocol/grpc/wire.rs, GrpcWireService, engine_as)
| RPC | Behaviour |
|---|---|
Query (query, language) |
Language empty or sql runs the SQL; any other language returns success = false with "language '<x>' not yet supported via gRPC; use 'sql'". Rows are repeated string; NULL is sent as an empty string. rows_affected counts only INSERT/UPDATE/DELETE. |
Cypher (query) |
Passes the text to engine.execute. |
Search (table, column, query, limit) |
Builds SELECT "col" FROM "table" WHERE match_text("col", 'q') [LIMIT n]. ids are 1-based result positions. |
VectorSearch (table, column, query_vector, metric, k) |
Metric empty or l2 uses <->, cosine uses <=>, inner or ip uses <#>; other metrics return an error message. k is clamped to 1..1000. |
Ping |
Returns "pong from SynergyDB" and the crate version. |
(source: src/protocol/grpc/wire.rs)
Search and VectorSearch wrap identifiers in double quotes without doubling embedded quotes. The status entry notes that Query returns untyped strings; use Sql.Execute for typed values. (source: src/protocol/grpc/wire.rs, search, vector_search; docs/status/2026-11-30-grpc-sql-service.md) Not verified: no Rust test calls any SynergyDb RPC.
Limits and configuration
| Setting | Default | Effect |
|---|---|---|
[server] grpc_port |
9090 | Listener port; omitted in a config file means off. |
| HTTP/2 keepalive | 60 s interval, 20 s timeout | From GrpcConfig::default. |
| Concurrent streams | 200 | From GrpcConfig::default. |
| Request size | 4 MiB | tonic default; max_message_size is not applied. |
SYNERGYDB_GRPC_TXN_IDLE_SECS |
300 | Idle transaction rollback. |
fetch_size |
1000 | Rows per ExecuteStream chunk. |
SYNERGYDB_DEV_INSECURE_AUTH |
unset | No authentication (test harnesses only). |
(source: src/config.rs; src/protocol/grpc/mod.rs; src/protocol/grpc/sql.rs)
Not wired and not implemented
-
Not wired:
health.rs,reflection.rsandservice.rs(hand-written versions replaced bytonic-health,tonic-reflectionand the generated services), and ininterceptors.rstheAuthInterceptor,MetricsInterceptor,RateLimiterandparse_grpc_timeout. (source:src/protocol/grpc/mod.rs,GrpcHandler::start) - The module doc comment in
mod.rslists RPCs such asStreamQuery,Savepoint,GraphTraverseandBackupthat are not in the proto. (source:src/protocol/grpc/mod.rs, module doc;proto/synergydb.proto) - Bearer tokens: the proto comment, the security guide and the status entry say
Bearerworks; the gate refuses it. (source:proto/synergydb.proto;src/protocol/grpc/interceptors.rs,GrpcAuthGate::authorize) - No client-streaming or bidirectional RPCs, no per-call deadline handling on the server, no compression setting. (source:
proto/synergydb.proto;src/protocol/grpc/mod.rs)
Tests and proof
| Where | What |
|---|---|
src/protocol/grpc/sql.rs |
sqlstate_codes, tags_and_values. |
src/protocol/grpc/mod.rs |
3 config tests. |
src/protocol/grpc/interceptors.rs |
Tests of the unwired AuthInterceptor, RateLimiter and metrics only. |
tests/protocol_grpc.rs |
GrpcConfig and handler construction only. |
docs/status/2026-11-30-grpc-sql-service.md |
95/95 in each of grpcio, grpc-go 1.68 and @grpc/grpc-js 1.12 against the same SQL on PostgreSQL 16, from test-scripts/grpc-apps/. Lists as untested: TLS, client streaming, bidirectional streaming. |
(source: the files named)
No Rust test drives the auth gate or any RPC over a socket. test-scripts/grpc-apps/ is not in the code snapshot used for this page, so the 95/95 result is taken from the status entry. There is no gRPC file in docs/proof/2026-11-30/proof-pack-a3232c9/.