nodedb 0.3.0-beta.1

Local-first, real-time, edge-to-cloud hybrid database for multi-modal workloads
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// SPDX-License-Identifier: BUSL-1.1

use pgwire::api::results::Response;
use pgwire::error::PgWireResult;

use crate::control::security::identity::AuthenticatedIdentity;
use crate::control::state::SharedState;

pub(super) async fn dispatch(
    state: &SharedState,
    identity: &AuthenticatedIdentity,
    sql: &str,
    upper: &str,
    parts: &[&str],
) -> Option<PgWireResult<Vec<Response>>> {
    // BACKUP/RESTORE TENANT are fully dispatched via typed AST (ast.rs).

    // Schedules: CREATE/DROP/ALTER/SHOW SCHEDULE
    // CREATE SCHEDULE is fully dispatched via typed AST (ast.rs).
    if upper.starts_with("DROP SCHEDULE ") {
        return Some(super::super::schedule::drop_schedule(
            state, identity, parts,
        ));
    }
    // ALTER SCHEDULE is fully dispatched via typed AST (ast.rs).
    if upper.starts_with("SHOW SCHEDULE HISTORY ") {
        let name = parts.get(3).unwrap_or(&"");
        return Some(super::super::schedule::show_schedule_history(
            state, identity, name,
        ));
    }
    if upper.starts_with("SHOW SCHEDULE") {
        return Some(super::super::schedule::show_schedules(state, identity));
    }

    // Sequences: CREATE/DROP/ALTER/SHOW SEQUENCE
    // CREATE SEQUENCE is fully dispatched via typed AST (ast.rs).
    if upper.starts_with("DROP SEQUENCE ") {
        return Some(super::super::sequence::drop_sequence(
            state, identity, parts,
        ));
    }
    // ALTER SEQUENCE is fully dispatched via typed AST (ast.rs).
    if upper == "SHOW SEQUENCES" || upper.starts_with("SHOW SEQUENCES ") {
        return Some(super::super::sequence::show_sequences(state, identity));
    }

    // Maintenance: ANALYZE, COMPACT, REINDEX, SHOW STORAGE, SHOW COMPACTION
    if upper.starts_with("ANALYZE ") {
        return Some(super::super::maintenance::handle_analyze(state, identity, sql).await);
    }
    if upper.starts_with("COMPACT ") {
        return Some(super::super::maintenance::handle_compact(
            state, identity, parts,
        ));
    }
    // REINDEX is fully dispatched via typed AST (ast.rs).
    if upper.starts_with("SHOW STORAGE ") {
        return Some(super::super::maintenance::handle_show_storage(
            state, identity, parts,
        ));
    }
    if upper == "SHOW COMPACTION STATUS" || upper.starts_with("SHOW COMPACTION STATUS ") {
        return Some(super::super::maintenance::handle_show_compaction_status(
            state, identity,
        ));
    }

    // Alert rules.
    // CREATE ALERT is fully dispatched via typed AST (ast.rs).
    if upper.starts_with("DROP ALERT ") {
        return Some(super::super::alert::drop_alert(state, identity, parts));
    }
    // ALTER ALERT is fully dispatched via typed AST (ast.rs).
    if upper.starts_with("SHOW ALERT STATUS ") {
        let name = parts.get(4).unwrap_or(&"");
        return Some(super::super::alert::show_alert_status(
            state, identity, name,
        ));
    }
    if upper.starts_with("SHOW ALERT") {
        return Some(super::super::alert::show_alerts(state, identity));
    }

    // Retention policies.
    // CREATE RETENTION POLICY is fully dispatched via typed AST (ast.rs).
    if upper.starts_with("DROP RETENTION POLICY ") {
        return Some(
            super::super::retention_policy::drop_retention_policy(state, identity, parts).await,
        );
    }
    // ALTER RETENTION POLICY is fully dispatched via typed AST (ast.rs).
    if upper.starts_with("SHOW RETENTION POLIC") {
        return Some(super::super::retention_policy::show_retention_policy(
            state, identity, parts,
        ));
    }

    // Continuous aggregates.
    // CREATE CONTINUOUS AGGREGATE is fully dispatched via typed AST (ast.rs).
    if upper.starts_with("DROP CONTINUOUS AGGREGATE ") {
        return Some(
            super::super::continuous_agg::drop_continuous_aggregate(state, identity, parts).await,
        );
    }
    if upper.starts_with("SHOW CONTINUOUS AGGREGATE") {
        return Some(
            super::super::continuous_agg::show_continuous_aggregates(state, identity, parts).await,
        );
    }

    // CONVERT COLLECTION.
    if upper.starts_with("CONVERT COLLECTION ")
        || upper.starts_with("CONVERT ") && upper.contains(" TO ")
    {
        return Some(super::super::convert::convert_collection(state, identity, sql).await);
    }

    // Materialized views (HTAP).
    // CREATE MATERIALIZED VIEW is fully dispatched via typed AST (ast.rs).
    if upper.starts_with("DROP MATERIALIZED VIEW ") {
        return Some(super::super::materialized_view::drop_materialized_view(
            state, identity, parts,
        ));
    }
    if upper.starts_with("REFRESH MATERIALIZED VIEW ") {
        return Some(
            super::super::materialized_view::refresh_materialized_view(state, identity, parts)
                .await,
        );
    }
    if upper.starts_with("SHOW MATERIALIZED VIEW") {
        return Some(super::super::materialized_view::show_materialized_views(
            state, identity, parts,
        ));
    }

    // Blacklist management.
    if upper.starts_with("BLACKLIST ") {
        return Some(super::super::blacklist_ddl::handle_blacklist(
            state, identity, parts,
        ));
    }
    if upper.starts_with("SHOW BLACKLIST") {
        return Some(super::super::blacklist_ddl::show_blacklist(
            state, identity, parts,
        ));
    }

    // Auth user management (JIT-provisioned users).
    if upper.starts_with("DEACTIVATE AUTH USER ") || upper.starts_with("ALTER AUTH USER ") {
        return Some(super::super::auth_user_ddl::handle_auth_user(
            state, identity, parts,
        ));
    }
    if upper.starts_with("PURGE AUTH USERS ") {
        return Some(super::super::auth_user_ddl::purge_auth_users(
            state, identity, parts,
        ));
    }
    if upper.starts_with("SHOW AUTH USERS") {
        return Some(super::super::auth_user_ddl::show_auth_users(
            state, identity, parts,
        ));
    }

    // Organization management.
    if upper.starts_with("CREATE ORG ")
        || upper.starts_with("ALTER ORG ")
        || upper.starts_with("DROP ORG ")
    {
        return Some(super::super::org_ddl::handle_org(state, identity, parts));
    }
    if upper.starts_with("SHOW ORGS") {
        return Some(super::super::org_ddl::show_orgs(state, identity, parts));
    }
    if upper.starts_with("SHOW MEMBERS OF ORG") {
        return Some(super::super::org_ddl::show_members(state, identity, parts));
    }

    // Scope management.
    if upper.starts_with("DEFINE SCOPE ") {
        return Some(super::super::scope_ddl::define_scope(
            state, identity, parts,
        ));
    }
    if upper.starts_with("DROP SCOPE ") {
        return Some(super::super::scope_ddl::drop_scope(state, identity, parts));
    }
    if upper.starts_with("GRANT SCOPE ") {
        return Some(super::super::scope_ddl::grant_scope(state, identity, parts));
    }
    if upper.starts_with("REVOKE SCOPE ") {
        return Some(super::super::scope_ddl::revoke_scope(
            state, identity, parts,
        ));
    }
    if upper.starts_with("ALTER SCOPE ") {
        return Some(super::super::scope_query_ddl::alter_scope(
            state, identity, parts,
        ));
    }
    if upper.starts_with("SHOW MY SCOPES") {
        return Some(super::super::scope_query_ddl::show_my_scopes(
            state, identity, parts,
        ));
    }
    if upper.starts_with("SHOW SCOPES FOR ") {
        return Some(super::super::scope_query_ddl::show_scopes_for(
            state, identity, parts,
        ));
    }
    if upper.starts_with("RENEW SCOPE ") {
        return Some(super::super::scope_ddl::renew_scope(state, identity, parts));
    }

    // EXPLAIN TIERS ON <collection> [RANGE <start> <end>]
    if upper.starts_with("EXPLAIN TIERS ") {
        return Some(super::helpers::explain_tiers(state, identity, parts));
    }

    // EXPLAIN PERMISSION / EXPLAIN SCOPE.
    if upper.starts_with("EXPLAIN PERMISSION ") {
        return Some(super::super::explain_ddl::explain_permission(
            state, identity, parts,
        ));
    }
    if upper.starts_with("EXPLAIN SCOPE ") {
        return Some(super::super::explain_ddl::explain_scope(
            state, identity, parts,
        ));
    }

    // Emergency response.
    if upper.starts_with("EMERGENCY LOCKDOWN") {
        return Some(super::super::emergency_ddl::emergency_lockdown(
            state, identity, parts,
        ));
    }
    if upper.starts_with("EMERGENCY UNLOCK") {
        return Some(super::super::emergency_ddl::emergency_unlock(
            state, identity, parts,
        ));
    }
    if upper.starts_with("BLACKLIST AUTH USERS WHERE") {
        return Some(super::super::emergency_ddl::bulk_blacklist(
            state, identity, parts,
        ));
    }

    // Auth-scoped API keys.
    if upper.starts_with("CREATE AUTH KEY ") {
        return Some(super::super::auth_key_ddl::create_auth_key(
            state, identity, parts,
        ));
    }
    if upper.starts_with("ROTATE AUTH KEY ") {
        return Some(super::super::auth_key_ddl::rotate_auth_key(
            state, identity, parts,
        ));
    }
    if upper.starts_with("LIST AUTH KEYS") {
        return Some(super::super::auth_key_ddl::list_auth_keys(
            state, identity, parts,
        ));
    }

    // Impersonation & delegation.
    if upper.starts_with("IMPERSONATE AUTH USER ") {
        return Some(super::super::impersonation_ddl::impersonate(
            state, identity, parts,
        ));
    }
    if upper.starts_with("STOP IMPERSONATION") {
        return Some(super::super::impersonation_ddl::stop_impersonation(
            state, identity, parts,
        ));
    }
    if upper.starts_with("DELEGATE AUTH USER ") {
        return Some(super::super::impersonation_ddl::delegate(
            state, identity, parts,
        ));
    }
    if upper.starts_with("REVOKE DELEGATION ") {
        return Some(super::super::impersonation_ddl::revoke_delegation(
            state, identity, parts,
        ));
    }
    if upper.starts_with("SHOW DELEGATIONS") {
        return Some(super::super::impersonation_ddl::show_delegations(
            state, identity, parts,
        ));
    }

    // Session management.
    if upper.starts_with("SHOW SESSIONS") {
        return Some(super::super::session_ddl::show_sessions(
            state, identity, parts,
        ));
    }
    if upper.starts_with("KILL SESSION ") {
        return Some(super::super::session_ddl::kill_session(
            state, identity, parts,
        ));
    }
    if upper.starts_with("KILL USER SESSIONS ") {
        return Some(super::super::session_ddl::kill_user_sessions(
            state, identity, parts,
        ));
    }
    if upper.starts_with("VERIFY AUDIT CHAIN") {
        return Some(super::super::session_ddl::verify_audit_chain(
            state, identity, parts,
        ));
    }

    // Usage metering.
    if upper.starts_with("DEFINE METERING DIMENSION ") {
        return Some(super::super::metering_ddl::define_dimension(
            state, identity, parts,
        ));
    }
    if upper.starts_with("SHOW USAGE FOR TENANT ") {
        return Some(super::super::metering_ddl::show_usage_for_tenant(
            state, identity, parts,
        ));
    }
    if upper.starts_with("EXPORT USAGE ") {
        return Some(super::super::metering_ddl::export_usage(
            state, identity, parts,
        ));
    }
    if upper.starts_with("SHOW USAGE ") {
        return Some(super::super::metering_ddl::show_usage(
            state, identity, parts,
        ));
    }
    if upper.starts_with("SHOW QUOTA ") {
        return Some(super::super::metering_ddl::show_quota(
            state, identity, parts,
        ));
    }

    if upper.starts_with("SHOW SCOPE GRANTS") {
        return Some(super::super::scope_ddl::show_scope_grants(
            state, identity, parts,
        ));
    }
    if upper.starts_with("SHOW SCOPE") {
        return Some(super::super::scope_ddl::show_scopes(state, identity, parts));
    }

    // API keys.
    if upper.starts_with("CREATE API KEY ") {
        return Some(super::super::apikey::create_api_key(state, identity, parts));
    }
    if upper.starts_with("REVOKE API KEY ") {
        return Some(super::super::apikey::revoke_api_key(state, identity, parts));
    }
    if upper.starts_with("LIST API KEYS") {
        return Some(super::super::apikey::list_api_keys(state, identity, parts));
    }
    if upper.starts_with("SHOW API KEYS") {
        return Some(super::super::apikey::list_api_keys(state, identity, parts));
    }

    // Cluster management & observability.
    if upper.starts_with("SHOW CLUSTER") {
        return Some(super::super::cluster::show_cluster(state, identity));
    }
    if upper.starts_with("SHOW RAFT GROUPS") {
        return Some(super::super::cluster::show_raft_groups(state, identity));
    }
    if upper.starts_with("SHOW RAFT GROUP ") {
        return Some(super::super::cluster::show_raft_group(
            state, identity, parts,
        ));
    }
    // ALTER RAFT GROUP is fully dispatched via typed AST (ast.rs).
    if upper.starts_with("SHOW MIGRATIONS") {
        return Some(super::super::cluster::show_migrations(state, identity));
    }
    if upper.starts_with("REBALANCE") {
        return Some(super::super::cluster::rebalance(state, identity));
    }
    if upper.starts_with("SHOW PEER HEALTH") {
        return Some(super::super::cluster::show_peer_health(state, identity));
    }
    if upper.starts_with("SHOW NODES") {
        return Some(super::super::cluster::show_nodes(state, identity));
    }
    if upper.starts_with("SHOW NODE ") {
        return Some(super::super::cluster::show_node(state, identity, parts));
    }
    if upper.starts_with("REMOVE NODE ") {
        return Some(super::super::cluster::remove_node(state, identity, parts));
    }
    if upper.starts_with("SHOW RANGES") {
        return Some(super::super::cluster::show_ranges(state, identity));
    }
    if upper.starts_with("SHOW ROUTING") {
        return Some(super::super::cluster::show_routing(state, identity));
    }
    if upper.starts_with("SHOW SCHEMA VERSION") {
        return Some(super::super::cluster::show_schema_version(state, identity));
    }

    // Introspection.
    if upper.starts_with("SHOW USERS") {
        return Some(super::super::inspect::show_users(state, identity));
    }
    // Exact-match only. Filtered forms (`SHOW TENANTS WITH NAME <name>`,
    // `SHOW TENANT <ident>`) are parsed into typed variants and routed
    // through the AST dispatcher; a prefix match here would silently
    // drop the filter and list every tenant.
    if upper == "SHOW TENANTS" {
        return Some(super::super::inspect::show_tenants(state, identity));
    }
    if upper == "SHOW ROLES" || upper.starts_with("SHOW ROLES ") {
        return Some(super::super::inspect::show_roles(state, identity));
    }

    // Administrative observability — server-wide counters, per-engine
    // memory budgets. `SHOW STATS` and `SHOW SERVER STATS` share a
    // handler (UX synonyms over the same `SystemMetrics` source);
    // `SHOW METRICS` adds histogram percentiles; `SHOW MEMORY`
    // reports per-engine memory governor state.
    if upper == "SHOW SERVER STATS" || upper.starts_with("SHOW SERVER STATS ") {
        return Some(super::super::observability::show_server_stats(
            state, identity,
        ));
    }
    if upper == "SHOW STATS" || upper.starts_with("SHOW STATS ") {
        return Some(super::super::observability::show_server_stats(
            state, identity,
        ));
    }
    if upper == "SHOW METRICS" || upper.starts_with("SHOW METRICS ") {
        return Some(super::super::observability::show_metrics(state, identity));
    }
    if upper == "SHOW MEMORY" || upper.starts_with("SHOW MEMORY ") {
        return Some(super::super::observability::show_memory(state, identity));
    }
    if upper.starts_with("SHOW SESSION") {
        return Some(super::super::inspect::show_session(identity));
    }
    if upper.starts_with("TRUNCATE AUDIT")
        || upper.starts_with("DELETE AUDIT")
        || upper.starts_with("CLEAR AUDIT")
    {
        return Some(Err(super::super::super::types::sqlstate_error(
            "42501",
            "audit log cannot be manually truncated. Entries are pruned automatically by the retention policy (audit_retention_days in config).",
        )));
    }
    if upper.starts_with("EXPORT AUDIT") {
        return Some(super::super::inspect::export_audit_log(
            state, identity, parts,
        ));
    }
    if upper.starts_with("SHOW AUDIT IN DATABASE") {
        // SHOW AUDIT IN DATABASE <name> [LIMIT <n>]
        // parts: ["SHOW", "AUDIT", "IN", "DATABASE", "<name>", ...]
        let db_name = if parts.len() >= 5 {
            parts[4]
        } else {
            return Some(Err(super::super::super::types::sqlstate_error(
                "42601",
                "syntax: SHOW AUDIT IN DATABASE <name> [LIMIT <n>]",
            )));
        };
        let limit = if parts.len() >= 7 && parts[5].eq_ignore_ascii_case("LIMIT") {
            parts[6].parse::<usize>().unwrap_or(100)
        } else {
            100
        };
        return Some(super::super::inspect::show_audit_in_database(
            state, identity, db_name, limit,
        ));
    }
    if upper.starts_with("SHOW AUDIT WHERE") {
        return Some(super::super::inspect::show_audit_where(
            state, identity, parts,
        ));
    }
    if upper.starts_with("SHOW AUDIT LOG") || upper.starts_with("SHOW AUDIT_LOG") {
        return Some(super::super::inspect::show_audit_log(
            state, identity, parts,
        ));
    }
    if upper.starts_with("SHOW GRANTS") {
        return Some(super::super::inspect::show_grants(state, identity, parts));
    }

    None
}