Skip to content

Commit a898ec6

Browse files
doc/md: updated ydb docs
1 parent 90296b6 commit a898ec6

File tree

1 file changed

+48
-49
lines changed

1 file changed

+48
-49
lines changed

doc/md/ydb.md

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,48 @@ func main() {
4545
}
4646
```
4747

48+
## Interactive & Non-Interactive Transactions
49+
50+
It is important to say that every request in YDB is executed in a transaction.
51+
YDB supports two modes of working with transactions, and ent uses both depending on the API you choose.
52+
53+
### Non-interactive transactions
54+
55+
When you use the standard CRUD builders (`.Create()`, `.Query()`, `.Update()`, `.Delete()`), ent executes them through ydb-go-sdk's retry helpers:
56+
57+
- **Write operations** (Create, Update, Delete) go through
58+
[`retry.DoTx`](https://pkg.go.dev/github.com/ydb-platform/ydb-go-sdk/v3/retry#DoTx) — the SDK
59+
begins a transaction, executes the operation as a **callback**, commits, and on a transient error
60+
rolls back and re-executes the callback from scratch.
61+
- **Read operations** (Query) go through
62+
[`retry.Do`](https://pkg.go.dev/github.com/ydb-platform/ydb-go-sdk/v3/retry#Do) — the SDK
63+
obtains a connection, executes the read callback, and retries on transient errors. No explicit
64+
transaction is created; the read runs with an implicit snapshot.
65+
66+
This is the recommended way to work with YDB through ent. Automatic retries and session management
67+
are handled transparently.
68+
69+
### Interactive transactions
70+
71+
When you call `Client.BeginTx()`, ent opens a transaction via the standard `database/sql` API and **returns a `Tx` object** to the caller. You then perform operations on it and manually call
72+
`Commit()` or `Rollback()`. In this model:
73+
74+
- There is **no callback** for the SDK to re-execute, so automatic retries are not possible.
75+
- Session and transaction lifetime are managed by your code.
76+
77+
Use interactive transactions only when you need explicit control over commit/rollback boundaries
78+
that can't be expressed through the standard builders.
79+
4880
## Automatic Retry Mechanism
4981

50-
YDB requires special handling for transient errors (network issues, temporary unavailability, etc.).
82+
Since YDB is a distributed database, it requires special handling for transient errors (network issues, temporary unavailability, etc.).
5183
The ent YDB driver integrates with [ydb-go-sdk's retry package](https://pkg.go.dev/github.com/ydb-platform/ydb-go-sdk/v3/retry)
5284
to automatically handle these scenarios.
5385

86+
:::note
87+
However, ent does not use automatic retries when you create an interactive transaction using `Client.BeginTx()`. [Read more](ydb.md#no-automatic-retries-when-using-clientbegintx).
88+
:::
89+
5490
### Using WithRetryOptions
5591

5692
All CRUD operations support the `WithRetryOptions()` method to configure retry behavior:
@@ -95,61 +131,29 @@ Common retry options from `ydb-go-sdk`:
95131
| `retry.WithLabel(string)` | Add a label for debugging/tracing |
96132
| `retry.WithTrace(trace.Retry)` | Enable retry tracing |
97133

98-
## YDB-Specific SQL Features
99-
100-
The ent SQL builder supports several YDB-specific SQL constructs that are used internally
101-
or can be leveraged for advanced use cases.
102-
103-
### UPSERT and REPLACE
104-
105-
YDB doesn't support the standard `ON CONFLICT` clause. Instead, it uses `UPSERT` and `REPLACE` statements:
106-
107-
- **UPSERT**: Inserts a new row or updates an existing row if a conflict occurs
108-
- **REPLACE**: Replaces the entire row if a conflict occurs
109-
110-
Ent automatically uses `UPSERT` when you configure `OnConflict` options in your create operations.
111-
112-
### BATCH Operations
113-
114-
For large tables, YDB provides `BATCH UPDATE` and `BATCH DELETE` statements that process data
115-
in batches, minimizing lock invalidation risk. These are used internally by ent when appropriate.
116-
117-
### Special JOIN Types
118-
119-
YDB supports additional JOIN types beyond the standard SQL joins:
120-
121-
| Join Type | Description |
122-
|-----------|-------------|
123-
| `LEFT SEMI JOIN` | Returns rows from left table that have matches in right table |
124-
| `RIGHT SEMI JOIN` | Returns rows from right table that have matches in left table |
125-
| `LEFT ONLY JOIN` | Returns rows from left table that have no matches in right table |
126-
| `RIGHT ONLY JOIN` | Returns rows from right table that have no matches in left table |
127-
| `EXCLUSION JOIN` | Returns rows that don't have matches in either table |
134+
## Known Limitations
128135

129-
### VIEW Clause for Secondary Indexes
136+
When using ent with YDB, be aware of the following limitations:
130137

131-
YDB allows explicit use of secondary indexes via the `VIEW` clause. This is an optimization
132-
hint that tells YDB to use a specific index for the query.
138+
### No automatic retries when using Client.BeginTx
133139

134-
### Named Parameters
140+
`Client.BeginTx()` returns a transaction object to the caller instead of accepting a callback,
141+
so the retry mechanism from ydb-go-sdk cannot be applied. See
142+
[Interactive transactions](ydb.md#interactive-transactions) for a detailed explanation.
135143

136-
YDB uses named parameters with the `$paramName` syntax (e.g., `$p1`, `$p2`) instead of
137-
positional placeholders. Ent handles this automatically.
144+
If you still need interactive transactions, you may write a retry wrapper manually, as done in
145+
[ydb-go-sdk's examples](https://github.com/ydb-platform/ydb-go-sdk/blob/master/examples/basic/database/sql/series.go).
138146

139-
## Known Limitations
147+
### No Nested Transactions
140148

141-
When using ent with YDB, be aware of the following limitations:
149+
YDB uses flat transactions and doesn't support nested transactions. The ent YDB driver
150+
handles this by returning a no-op transaction when nested transactions are requested.
142151

143152
### No Correlated Subqueries
144153

145154
YDB doesn't support correlated subqueries with `EXISTS` or `NOT EXISTS`. Ent automatically
146155
rewrites such queries to use `IN` with subqueries instead.
147156

148-
### No Nested Transactions
149-
150-
YDB uses flat transactions and doesn't support nested transactions. The ent YDB driver
151-
handles this by returning a no-op transaction when nested transactions are requested.
152-
153157
### Float/Double Index Restriction
154158

155159
YDB doesn't allow `Float` or `Double` types as index keys. If you define an index on a
@@ -160,11 +164,6 @@ float field, it will be skipped during migration.
160164
YDB doesn't support enum types in DDL statements. Ent maps enum fields to `Utf8` (string)
161165
type, with validation handled at the application level.
162166

163-
### RowsAffected Behavior
164-
165-
YDB's `RowsAffected()` doesn't return accurate counts. The ent driver uses the `RETURNING`
166-
clause internally to count affected rows when needed.
167-
168167
### Primary Key Requirements
169168

170169
YDB requires explicit primary keys for all tables. Make sure your ent schemas define

0 commit comments

Comments
 (0)