datumesh-store 0.4.3

Store for Datumesh on PostgreSQL or SQLite: descriptors, receipts, blobs, versions over transaction time, lineage, namespaces and names; a datumesh-core Resolver.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Every SQL statement the store runs, with its parameter types. `PgStore::check_statements`
//! prepares all of them against the live schema; the sync driver has no compile-time query
//! checking, so that test is what catches a statement and a schema that disagree.

use postgres::types::Type;

macro_rules! statements {
    ($($name:ident = $sql:expr => [$($ty:ident),*];)*) => {
        /// A statement.
        #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
        pub(crate) enum Q {
            $($name,)*
        }

        impl Q {
            pub(crate) const ALL: &'static [Q] = &[$(Q::$name,)*];

            pub(crate) fn sql(self) -> &'static str {
                match self {
                    $(Q::$name => $sql,)*
                }
            }

            pub(crate) fn types(self) -> &'static [Type] {
                match self {
                    $(Q::$name => &[$(Type::$ty),*],)*
                }
            }
        }
    };
}

/// The value-bound constraints ($6-$10: name, kind open / value / range, type, lo, hi; a
/// value's lexical form is its lo) of the cell `$cell` (a column reference): each one an
/// index-only probe of `cell_args_value` (fn_hash, name, type, value, cell_id) with the
/// function hash $1.
macro_rules! value_probes {
    ($cell:literal) => {
        concat!(
            "NOT EXISTS (
            SELECT 1 FROM unnest($6::text[], $7::text[], $8::text[], $9::text[], $10::text[]) AS k(name, kind, type, lo, hi)
             WHERE k.kind = 'value' AND NOT EXISTS (
                 SELECT 1 FROM cell_args e
                  WHERE e.fn_hash = $1 AND e.name = k.name AND e.type = k.type AND e.value = k.lo
                    AND e.cell_id = ",
            $cell,
            "))"
        )
    };
}

/// Whether cell `c` meets every constraint of a slice: the value-bound ones by `value_probes!`,
/// the open and range ones by a scan of the cell's arguments.
macro_rules! slice_match {
    () => {
        concat!(
            value_probes!("c.cell_id"),
            " AND NOT EXISTS (
            SELECT 1 FROM unnest($6::text[], $7::text[], $8::text[], $9::text[], $10::text[]) AS k(name, kind, type, lo, hi)
             WHERE k.kind <> 'value' AND NOT EXISTS (
                 SELECT 1 FROM cell_args a
                  WHERE a.cell_id = c.cell_id AND a.name = k.name
                    AND CASE WHEN k.kind = 'open' THEN true
                             WHEN k.type = 'number' THEN a.type = 'number' AND a.num BETWEEN k.lo::numeric AND k.hi::numeric
                             ELSE a.type = k.type AND a.value BETWEEN k.lo AND k.hi END))"
        )
    };
}

/// The statement of the members of a slice of cells: the columns `$outer` of the version
/// answering (`$inner` of the receipt), the cell id, and its key; the candidates from `$with`
/// (a CTE, or nothing) and `$from`.
macro_rules! slice_members {
    ($with:expr, $from:expr, $inner:literal, $outer:literal) => {
        concat!(
            $with,
            "SELECT m.tx, m.receipt_hash, m.asof, ",
            $outer,
            ", c.cell_id,
                    ARRAY(SELECT a.type || ':' || a.value FROM cell_args a
                           WHERE a.cell_id = c.cell_id AND a.name = ANY($11::text[]) ORDER BY a.name)
               ",
            $from,
            "
              CROSS JOIN LATERAL (SELECT v.tx, v.receipt_hash, r.asof, ",
            $inner,
            "
                                    FROM versions v JOIN receipts r ON r.hash = v.receipt_hash
                                   WHERE v.cell_id = c.cell_id AND v.tx <= $5 AND r.asof <= $5
                                   ORDER BY v.tx DESC LIMIT 1) m
              WHERE c.fn_hash = $1 AND c.ns = $2 AND c.fn = $3 AND c.nargs = $4 AND ",
            slice_match!()
        )
    };
}

/// Whether a candidate cell of a slice (from `$with` and `$from`) has a stored version whose
/// receipt's asof is after A.
macro_rules! changed_cand {
    ($with:expr, $from:expr) => {
        concat!(
            $with,
            "SELECT true ",
            $from,
            "
              WHERE c.fn_hash = $1 AND c.ns = $2 AND c.fn = $3 AND c.nargs = $4
                AND EXISTS (SELECT 1 FROM receipts r WHERE r.cell_id = c.cell_id AND r.asof > $5
                               AND EXISTS (SELECT 1 FROM versions v WHERE v.receipt_hash = r.hash))
                AND ",
            slice_match!(),
            " LIMIT 1"
        )
    };
}

/// The cells of a slice of cells that have a computed version answering as of A ($5): one
/// with tx <= A and its receipt's asof <= A (the computed versions' own asof). Ascending by
/// cell id, at most $16; the candidates from `$with` and `$from`.
macro_rules! computed_cells {
    ($with:expr, $from:expr) => {
        concat!(
            $with,
            "SELECT c.ns, c.fn, c.fn_hash,
                    ARRAY(SELECT a.name || ':' || a.type || ':' || a.value FROM cell_args a
                           WHERE a.cell_id = c.cell_id ORDER BY a.name)
               ",
            $from,
            "
              WHERE c.fn_hash = $1 AND c.ns = $2 AND c.fn = $3 AND c.nargs = $4
                AND EXISTS (SELECT 1 FROM computed_versions cv
                             WHERE cv.cell_id = c.cell_id AND cv.tx <= $5 AND cv.asof <= $5)
                AND ",
            slice_match!(),
            " ORDER BY c.cell_id LIMIT $16"
        )
    };
}

// The candidates of a slice: all the function's cells (`no_with!` and `from_all!`), or the
// cells of a driving argument's range ($12-$15: name, type, lo, hi) as the materialized CTE
// `cand`, which already applies the value-bound constraints by index probes, and then joined
// to `cells` by primary key (`from_drive!`). A text range is scanned in `cell_args_value`, a
// number's in `cell_args_num`.
macro_rules! no_with {
    () => {
        ""
    };
}
macro_rules! from_all {
    () => {
        "FROM cells c"
    };
}
macro_rules! from_drive {
    () => {
        "FROM cand k JOIN cells c ON c.cell_id = k.cell_id"
    };
}
macro_rules! drive_text {
    () => {
        concat!(
            "WITH cand AS MATERIALIZED (
            SELECT a.cell_id FROM cell_args a
             WHERE a.fn_hash = $1 AND a.name = $12 AND a.type = $13 AND a.value BETWEEN $14 AND $15
               AND ",
            value_probes!("a.cell_id"),
            ") "
        )
    };
}
macro_rules! drive_num {
    () => {
        concat!(
            "WITH cand AS MATERIALIZED (
            SELECT a.cell_id FROM cell_args a
             WHERE $13 = 'number' AND a.fn_hash = $1 AND a.name = $12 AND a.type = 'number'
               AND a.num BETWEEN $14::numeric AND $15::numeric
               AND ",
            value_probes!("a.cell_id"),
            ") "
        )
    };
}

statements! {
    Lock = "SELECT datumesh_lock($1, $2)" => [TEXT, TEXT];

    DescriptorGet = "SELECT bytes FROM descriptors WHERE fn_hash = $1" => [TEXT];
    DescriptorPut = "INSERT INTO descriptors (fn_hash, bytes) VALUES ($1, $2) ON CONFLICT (fn_hash) DO NOTHING"
        => [TEXT, BYTEA];
    DescriptorExists = "SELECT EXISTS (SELECT 1 FROM descriptors WHERE fn_hash = $1)" => [TEXT];

    ReceiptGet = "SELECT bytes FROM receipts WHERE hash = $1" => [TEXT];
    ReceiptMeta = "SELECT cell_id, tx, asof FROM receipts WHERE hash = $1" => [TEXT];
    ReceiptPut = "INSERT INTO receipts (hash, bytes, cell_id, tx, tx_at, asof, asof_at, fn_hash, publisher)
                  VALUES ($1, $2, $3, $4, datumesh_instant($4), $5, datumesh_instant($5), $6, $7)
                  ON CONFLICT (hash) DO NOTHING"
        => [TEXT, BYTEA, TEXT, TEXT, TEXT, TEXT, TEXT];
    SliceEdgesPut = "INSERT INTO slice_edges (receipt_hash, entry)
                     SELECT $1, e FROM unnest($2::text[]) AS e ON CONFLICT DO NOTHING"
        => [TEXT, TEXT_ARRAY];
    EdgesPut = "INSERT INTO edges (receipt_hash, input_hash, kind)
                SELECT $1, i, 'receipt' FROM unnest($2::text[]) AS i ON CONFLICT DO NOTHING"
        => [TEXT, TEXT_ARRAY];

    BlobGet = "SELECT bytes FROM blobs WHERE sha256 = $1" => [TEXT];
    BlobPut = "INSERT INTO blobs (sha256, bytes, length) VALUES ($1, $2, $3) ON CONFLICT (sha256) DO NOTHING"
        => [TEXT, BYTEA, INT8];

    // Versions come with their receipt's asof (Version::asof). A scalar subquery rather than
    // a join, so that the versions table is looked at first (a missing column of it is still
    // reported as such).
    VersionLatestTx = "SELECT tx FROM versions WHERE cell_id = $1 ORDER BY tx DESC LIMIT 1" => [TEXT];
    VersionLatest = "SELECT v.tx, v.receipt_hash, (SELECT r.asof FROM receipts r WHERE r.hash = v.receipt_hash)
                     FROM versions v WHERE v.cell_id = $1 ORDER BY v.tx DESC LIMIT 1"
        => [TEXT];
    VersionAsof = "SELECT v.tx, v.receipt_hash, (SELECT r.asof FROM receipts r WHERE r.hash = v.receipt_hash)
                   FROM versions v WHERE v.cell_id = $1 AND v.tx <= $2 ORDER BY v.tx DESC LIMIT 1"
        => [TEXT, TEXT];
    VersionRange = "SELECT v.tx, v.receipt_hash, (SELECT r.asof FROM receipts r WHERE r.hash = v.receipt_hash)
                    FROM versions v WHERE v.cell_id = $1 AND v.tx >= $2 AND v.tx <= $3 ORDER BY v.tx"
        => [TEXT, TEXT, TEXT];
    // A stored version answers as of A when its tx <= A and its receipt's asof <= A.
    StoredAsof = "SELECT v.tx, v.receipt_hash, r.asof FROM versions v JOIN receipts r ON r.hash = v.receipt_hash
                  WHERE v.cell_id = $1 AND v.tx <= $2 AND r.asof <= $2 ORDER BY v.tx DESC LIMIT 1"
        => [TEXT, TEXT];
    StoredAsofReceipt = "SELECT v.tx, v.receipt_hash, r.asof, r.bytes FROM versions v JOIN receipts r ON r.hash = v.receipt_hash
                         WHERE v.cell_id = $1 AND v.tx <= $2 AND r.asof <= $2 ORDER BY v.tx DESC LIMIT 1"
        => [TEXT, TEXT];
    VersionPut = "INSERT INTO versions (cell_id, tx, tx_at, receipt_hash) VALUES ($1, $2, datumesh_instant($2), $3)"
        => [TEXT, TEXT, TEXT];
    // The Resolver's evaluate of a stored function: the receipt of stored_asof's version.
    ValueAsof = "SELECT r.bytes FROM versions v JOIN receipts r ON r.hash = v.receipt_hash
                 WHERE v.cell_id = $1 AND v.tx <= $2 AND r.asof <= $2 ORDER BY v.tx DESC LIMIT 1"
        => [TEXT, TEXT];

    ComputedPut = "INSERT INTO computed_versions (cell_id, tx, tx_at, asof, asof_at, receipt_hash)
                   VALUES ($1, $2, datumesh_instant($2), $3, datumesh_instant($3), $4)
                   ON CONFLICT (cell_id, tx, asof) DO NOTHING"
        => [TEXT, TEXT, TEXT, TEXT];
    ComputedGet = "SELECT receipt_hash FROM computed_versions WHERE cell_id = $1 AND tx = $2 AND asof = $3"
        => [TEXT, TEXT, TEXT];
    // The greatest asof <= A, ties the greatest tx.
    ComputedAsof = "SELECT tx, receipt_hash, asof FROM computed_versions
                    WHERE cell_id = $1 AND asof <= $2 ORDER BY asof DESC, tx DESC LIMIT 1"
        => [TEXT, TEXT];
    ComputedAsofReceipt = "SELECT c.tx, c.receipt_hash, c.asof, r.bytes FROM
                               (SELECT tx, receipt_hash, asof FROM computed_versions
                                 WHERE cell_id = $1 AND asof <= $2 ORDER BY asof DESC, tx DESC LIMIT 1) c
                           JOIN receipts r ON r.hash = c.receipt_hash"
        => [TEXT, TEXT];

    // The cells of stored receipts that list any stored receipt of the cell among their inputs,
    // or a slice entry whose stored manifest lists one (0.3.1).
    CellDependents = "SELECT d.cell_id FROM receipts i
                      JOIN edges e ON e.input_hash = i.hash
                      JOIN receipts d ON d.hash = e.receipt_hash
                      WHERE i.cell_id = $1
                      UNION
                      SELECT d.cell_id FROM receipts i
                      JOIN manifest_members mm ON mm.member_hash = i.hash
                      JOIN slice_edges se ON se.entry = mm.entry
                      JOIN receipts d ON d.hash = se.receipt_hash
                      WHERE i.cell_id = $1
                      ORDER BY 1"
        => [TEXT];

    Dependents = "SELECT receipt_hash FROM edges WHERE input_hash = $1 ORDER BY receipt_hash" => [TEXT];
    Inputs = "SELECT input_hash FROM edges WHERE receipt_hash = $1 ORDER BY input_hash" => [TEXT];

    NamespaceGet = "SELECT keydoc, validated_at, suspended_at, consecutive_failures, last_checked_at
                    FROM namespaces WHERE host = $1" => [TEXT];
    NamespaceList = "SELECT host, keydoc, validated_at, suspended_at, consecutive_failures, last_checked_at
                     FROM namespaces ORDER BY host" => [];
    NamespaceCheck = "UPDATE namespaces SET consecutive_failures = $2, last_checked_at = $3,
                        last_checked_at_at = datumesh_instant($3), suspended_at = $4::text,
                        suspended_at_at = CASE WHEN $4::text IS NULL THEN NULL ELSE datumesh_instant($4::text) END
                      WHERE host = $1" => [TEXT, INT4, TEXT, TEXT];
    NamespacePut = "INSERT INTO namespaces (host, keydoc, validated_at, validated_at_at)
                    VALUES ($1, $2, $3, datumesh_instant($3))
                    ON CONFLICT (host) DO UPDATE SET keydoc = EXCLUDED.keydoc,
                        validated_at = EXCLUDED.validated_at, validated_at_at = EXCLUDED.validated_at_at"
        => [TEXT, BYTEA, TEXT];

    NameLatest = "SELECT tx FROM name_bindings WHERE fn_name = $1 ORDER BY tx DESC LIMIT 1" => [TEXT];
    NamePut = "INSERT INTO name_bindings (fn_name, tx, tx_at, fn_hash) VALUES ($1, $2, datumesh_instant($2), $3)"
        => [TEXT, TEXT, TEXT];
    NameAsof = "SELECT fn_hash FROM name_bindings WHERE fn_name = $1 AND tx <= $2 ORDER BY tx DESC LIMIT 1"
        => [TEXT, TEXT];
    BindingAsof = "SELECT fn_hash, tx FROM name_bindings WHERE fn_name = $1 AND tx <= $2 ORDER BY tx DESC LIMIT 1"
        => [TEXT, TEXT];
    // Whether a name is bound as a function (any binding), or as a class (any binding): the
    // two kinds of one namespace of names (0.4.1).
    NameBound = "SELECT EXISTS (SELECT 1 FROM name_bindings WHERE fn_name = $1)" => [TEXT];
    ClassNameBound = "SELECT EXISTS (SELECT 1 FROM class_bindings WHERE class_name = $1)" => [TEXT];

    // Classes (0.4.1): flattened class descriptors by class hash, and class-name bindings.
    ClassPut = "INSERT INTO classes (class_hash, bytes) VALUES ($1, $2) ON CONFLICT (class_hash) DO NOTHING"
        => [TEXT, BYTEA];
    ClassGet = "SELECT bytes FROM classes WHERE class_hash = $1" => [TEXT];
    ClassBindingLatest = "SELECT tx FROM class_bindings WHERE class_name = $1 ORDER BY tx DESC LIMIT 1" => [TEXT];
    ClassBindingPut = "INSERT INTO class_bindings (class_name, tx, tx_at, class_hash) VALUES ($1, $2, datumesh_instant($2), $3)"
        => [TEXT, TEXT, TEXT];
    ClassBindingAsof = "SELECT class_hash, tx FROM class_bindings WHERE class_name = $1 AND tx <= $2 ORDER BY tx DESC LIMIT 1"
        => [TEXT, TEXT];

    // Manifests (0.3.1).
    ManifestPut = "INSERT INTO manifests (entry, slice_id, members_hash, fn_hash, members, bytes)
                   VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (entry) DO NOTHING"
        => [TEXT, TEXT, TEXT, TEXT, INT4, BYTEA];
    ManifestMembersPut = "INSERT INTO manifest_members (entry, member_hash) SELECT $1, m FROM unnest($2::text[]) AS m"
        => [TEXT, TEXT_ARRAY];
    ManifestGet = "SELECT bytes FROM manifests WHERE entry = $1" => [TEXT];
    MemberManifests = "SELECT entry FROM manifest_members WHERE member_hash = $1 ORDER BY entry" => [TEXT];
    SliceEntries = "SELECT entry FROM slice_edges WHERE receipt_hash = $1 ORDER BY entry" => [TEXT];
    SliceDependents = "SELECT receipt_hash FROM slice_edges WHERE entry = $1 ORDER BY receipt_hash" => [TEXT];

    // A version slice: the stored versions with from <= tx <= to answering as of A.
    VersionsBetween = "SELECT v.tx, v.receipt_hash, r.asof, r.bytes FROM versions v JOIN receipts r ON r.hash = v.receipt_hash
                       WHERE v.cell_id = $1 AND v.tx >= $2 AND v.tx <= $3 AND v.tx <= $4 AND r.asof <= $4 ORDER BY v.tx"
        => [TEXT, TEXT, TEXT, TEXT];

    // The members of a slice of cells, as of A ($5): cells of the function ($1-$3) with its
    // number of arguments ($4) and every constraint ($6-$10: name, kind open / value / range,
    // type, lo, hi; a value's lexical form is its lo) met, each with the stored version
    // answering as of A and the "type:value" of its key arguments ($11, in name order).
    // Candidates come from all the function's cells, or from a range scan of one argument
    // ($12-$15: name, type, lo, hi) of the typed-argument index.
    SliceMembersAll = slice_members!(no_with!(), from_all!(), "r.bytes", "m.bytes") => [TEXT, TEXT, TEXT, INT4, TEXT, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT, TEXT, TEXT, TEXT];
    SliceMembersText = slice_members!(drive_text!(), from_drive!(), "r.bytes", "m.bytes") => [TEXT, TEXT, TEXT, INT4, TEXT, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT, TEXT, TEXT, TEXT];
    SliceMembersNum = slice_members!(drive_num!(), from_drive!(), "r.bytes", "m.bytes") => [TEXT, TEXT, TEXT, INT4, TEXT, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT, TEXT, TEXT, TEXT];
    // The same with the receipt's value and the member's cell from the index (0.3.2): (tx,
    // receipt_hash, asof, value_type, value, ns, fn, fn_hash, "name:type:value" of each
    // argument by name, cell_id, key).
    SliceValuesAll = slice_members!(no_with!(), from_all!(), "r.value_type, r.value", "m.value_type, m.value, c.ns, c.fn, c.fn_hash,
                    ARRAY(SELECT a.name || ':' || a.type || ':' || a.value FROM cell_args a WHERE a.cell_id = c.cell_id ORDER BY a.name)") => [TEXT, TEXT, TEXT, INT4, TEXT, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT, TEXT, TEXT, TEXT];
    SliceValuesText = slice_members!(drive_text!(), from_drive!(), "r.value_type, r.value", "m.value_type, m.value, c.ns, c.fn, c.fn_hash,
                    ARRAY(SELECT a.name || ':' || a.type || ':' || a.value FROM cell_args a WHERE a.cell_id = c.cell_id ORDER BY a.name)") => [TEXT, TEXT, TEXT, INT4, TEXT, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT, TEXT, TEXT, TEXT];
    SliceValuesNum = slice_members!(drive_num!(), from_drive!(), "r.value_type, r.value", "m.value_type, m.value, c.ns, c.fn, c.fn_hash,
                    ARRAY(SELECT a.name || ':' || a.type || ':' || a.value FROM cell_args a WHERE a.cell_id = c.cell_id ORDER BY a.name)") => [TEXT, TEXT, TEXT, INT4, TEXT, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT, TEXT, TEXT, TEXT];
    // How many cells (up to 10,001) an argument's range holds, to choose the range scanned.
    SliceProbeText = "SELECT count(*) FROM (SELECT 1 FROM cell_args a
                       WHERE a.fn_hash = $1 AND a.name = $2 AND a.type = $3 AND a.value BETWEEN $4 AND $5 LIMIT 10001) x"
        => [TEXT, TEXT, TEXT, TEXT, TEXT];
    SliceProbeNum = "SELECT count(*) FROM (SELECT 1 FROM cell_args a
                      WHERE a.fn_hash = $1 AND a.name = $2 AND a.type = 'number' AND $3 = 'number'
                        AND a.num BETWEEN $4::numeric AND $5::numeric LIMIT 10001) x"
        => [TEXT, TEXT, TEXT, TEXT, TEXT];

    // 0.3.2: a version slice's values.
    VersionsBetweenValues = "SELECT v.tx, v.receipt_hash, r.asof, r.value_type, r.value, c.ns, c.fn, c.fn_hash,
                                    ARRAY(SELECT a.name || ':' || a.type || ':' || a.value FROM cell_args a
                                           WHERE a.cell_id = c.cell_id ORDER BY a.name)
                               FROM versions v JOIN receipts r ON r.hash = v.receipt_hash JOIN cells c ON c.cell_id = v.cell_id
                              WHERE v.cell_id = $1 AND v.tx >= $2 AND v.tx <= $3 AND v.tx <= $4 AND r.asof <= $4 ORDER BY v.tx"
        => [TEXT, TEXT, TEXT, TEXT];
    // computed_cells (0.4.2): the cells of a slice with a computed version as of A, $16 at most;
    // the columns are the cell's namespace, function name, function hash and "name:type:value"
    // of each argument (the key's arguments are not needed here: $11 is unused).
    ComputedCellsAll = computed_cells!(no_with!(), from_all!()) => [TEXT, TEXT, TEXT, INT4, TEXT, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT, TEXT, TEXT, TEXT, INT8];
    ComputedCellsText = computed_cells!(drive_text!(), from_drive!()) => [TEXT, TEXT, TEXT, INT4, TEXT, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT, TEXT, TEXT, TEXT, INT8];
    ComputedCellsNum = computed_cells!(drive_num!(), from_drive!()) => [TEXT, TEXT, TEXT, INT4, TEXT, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT, TEXT, TEXT, TEXT, INT8];

    // has_bindings_under (0.4.2): whether a name binding or a class binding has the prefix $1
    // (a namespace and "/").
    HasBindingsUnder = "SELECT EXISTS (SELECT 1 FROM name_bindings WHERE starts_with(fn_name, $1))
                          OR EXISTS (SELECT 1 FROM class_bindings WHERE starts_with(class_name, $1))"
        => [TEXT];

    // Whether a matching cell has a stored version whose receipt's asof (never earlier than
    // its tx) is after A ($5), the slice's parameters as for its members ($11-$15 unused
    // here). From the function's receipts by asof, then each one's cell against the slice:
    ChangedRecent = concat!(
        "SELECT true FROM receipts r JOIN cells c ON c.cell_id = r.cell_id
          WHERE r.fn_hash = $1 AND r.asof > $5 AND c.ns = $2 AND c.fn = $3 AND c.nargs = $4
            AND EXISTS (SELECT 1 FROM versions v WHERE v.receipt_hash = r.hash) AND ",
        slice_match!(),
        " LIMIT 1"
    ) => [TEXT, TEXT, TEXT, INT4, TEXT, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT, TEXT, TEXT, TEXT];
    // or from the slice's candidate cells, then each one's receipts by cell and asof.
    ChangedCandAll = changed_cand!(no_with!(), from_all!()) => [TEXT, TEXT, TEXT, INT4, TEXT, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT, TEXT, TEXT, TEXT];
    ChangedCandText = changed_cand!(drive_text!(), from_drive!()) => [TEXT, TEXT, TEXT, INT4, TEXT, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT, TEXT, TEXT, TEXT];
    ChangedCandNum = changed_cand!(drive_num!(), from_drive!()) => [TEXT, TEXT, TEXT, INT4, TEXT, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT, TEXT, TEXT, TEXT];
    // How many receipts of the function have an asof after A, and how many cells it has (up
    // to 10,001), to choose between the two.
    // (Ordered as the indexes are, so that a probe is an index scan of at most 10,001 entries
    // even when every row matches.)
    ProbeRecent = "SELECT count(*) FROM (SELECT 1 FROM receipts WHERE fn_hash = $1 AND asof > $2
                                          ORDER BY fn_hash, asof LIMIT 10001) x"
        => [TEXT, TEXT];
    ProbeCells = "SELECT count(*) FROM (SELECT 1 FROM cells WHERE fn_hash = $1 ORDER BY fn_hash, cell_id LIMIT 10001) x"
        => [TEXT];
    // The same for a version slice: a version of the cell with tx in [from, to] ($2, $3).
    VersionsChangedAfter = "SELECT EXISTS (SELECT 1 FROM versions v JOIN receipts r ON r.hash = v.receipt_hash
                             WHERE v.cell_id = $1 AND v.tx >= $2 AND v.tx <= $3 AND r.asof > $4)"
        => [TEXT, TEXT, TEXT, TEXT];

    // Batched reads (0.4.3). One row per input, in input order (`ord`): latest_many's version
    // of each cell (its tx, receipt hash and the receipt's asof), receipts_many's bytes.
    VersionLatestMany = "SELECT l.tx, l.receipt_hash, (SELECT r.asof FROM receipts r WHERE r.hash = l.receipt_hash)
                         FROM unnest($1::text[]) WITH ORDINALITY AS u(id, ord)
                         LEFT JOIN LATERAL (SELECT v.tx, v.receipt_hash FROM versions v
                                             WHERE v.cell_id = u.id ORDER BY v.tx DESC LIMIT 1) l ON true
                         ORDER BY u.ord"
        => [TEXT_ARRAY];
    ReceiptsMany = "SELECT r.bytes FROM unnest($1::text[]) WITH ORDINALITY AS u(h, ord)
                    LEFT JOIN receipts r ON r.hash = u.h ORDER BY u.ord"
        => [TEXT_ARRAY];

    // put_cells (0.4.3), one transaction: the version locks of the call's cells in item order
    // (one statement); the stored receipts, namespaces and latest tx of the call's hashes,
    // signer hosts and cells; then the inserts (receipts, edges, slice edges, versions), each
    // one statement with arrays.
    VersionLocksMany = "SELECT datumesh_lock('versions', id) FROM unnest($1::text[]) WITH ORDINALITY AS u(id, ord)
                        ORDER BY ord"
        => [TEXT_ARRAY];
    ReceiptsGetMany = "SELECT hash, bytes FROM receipts WHERE hash = ANY($1::text[])" => [TEXT_ARRAY];
    NamespacesGetMany = "SELECT host, keydoc, suspended_at FROM namespaces WHERE host = ANY($1::text[])" => [TEXT_ARRAY];
    VersionsLatestTxMany = "SELECT cell_id, max(tx) FROM versions WHERE cell_id = ANY($1::text[]) GROUP BY cell_id"
        => [TEXT_ARRAY];
    ReceiptsPutMany = "INSERT INTO receipts (hash, bytes, cell_id, tx, tx_at, asof, asof_at, fn_hash, publisher)
                       SELECT h, b, c, t, datumesh_instant(t), s, datumesh_instant(s), f, p
                         FROM unnest($1::text[], $2::bytea[], $3::text[], $4::text[], $5::text[], $6::text[], $7::text[])
                              AS x(h, b, c, t, s, f, p)
                       ON CONFLICT (hash) DO NOTHING RETURNING hash"
        => [TEXT_ARRAY, BYTEA_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY];
    EdgesPutMany = "INSERT INTO edges (receipt_hash, input_hash, kind)
                    SELECT r, i, 'receipt' FROM unnest($1::text[], $2::text[]) AS x(r, i) ON CONFLICT DO NOTHING"
        => [TEXT_ARRAY, TEXT_ARRAY];
    SliceEdgesPutMany = "INSERT INTO slice_edges (receipt_hash, entry)
                         SELECT r, e FROM unnest($1::text[], $2::text[]) AS x(r, e) ON CONFLICT DO NOTHING"
        => [TEXT_ARRAY, TEXT_ARRAY];
    VersionsPutMany = "INSERT INTO versions (cell_id, tx, tx_at, receipt_hash)
                       SELECT c, t, datumesh_instant(t), r FROM unnest($1::text[], $2::text[], $3::text[]) AS x(c, t, r)"
        => [TEXT_ARRAY, TEXT_ARRAY, TEXT_ARRAY];
}