Repository navigation
Expand file tree
/
Copy patholap.sql.go
More file actions
4800 lines (4516 loc) · 158 KB
/
Copy patholap.sql.go
File metadata and controls
4800 lines (4516 loc) · 158 KB
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.29.0
// source: olap.sql
package sqlcv1
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
)
const acquireOrExtendOLAPCutoverJobLease = `-- name: AcquireOrExtendOLAPCutoverJobLease :one
WITH inputs AS (
SELECT
$2::DATE AS key,
$1::UUID AS lease_process_id,
$3::TIMESTAMPTZ AS lease_expires_at,
$4::UUID AS last_external_id
), any_lease_held_by_other_process AS (
-- need coalesce here in case there are no rows that don't belong to this process
SELECT COALESCE(BOOL_OR(lease_expires_at > NOW()), FALSE) AS lease_exists
FROM v1_payloads_olap_cutover_job_offset
WHERE lease_process_id != $1::UUID
), to_insert AS (
SELECT key, lease_process_id, lease_expires_at, last_external_id
FROM inputs
-- if a lease is held by another process, we shouldn't try to insert a new row regardless
-- of which key we're trying to acquire a lease on
WHERE NOT (SELECT lease_exists FROM any_lease_held_by_other_process)
)
INSERT INTO v1_payloads_olap_cutover_job_offset (key, lease_process_id, lease_expires_at, last_external_id)
SELECT ti.key, ti.lease_process_id, ti.lease_expires_at, ti.last_external_id
FROM to_insert ti
ON CONFLICT (key)
DO UPDATE SET
-- if the lease is held by this process, then we extend the offset to the new value
-- otherwise it's a new process acquiring the lease, so we should keep the offset where it was before
last_external_id = CASE
WHEN EXCLUDED.lease_process_id = v1_payloads_olap_cutover_job_offset.lease_process_id THEN EXCLUDED.last_external_id
ELSE v1_payloads_olap_cutover_job_offset.last_external_id
END,
lease_process_id = EXCLUDED.lease_process_id,
lease_expires_at = EXCLUDED.lease_expires_at
WHERE v1_payloads_olap_cutover_job_offset.lease_expires_at < NOW() OR v1_payloads_olap_cutover_job_offset.lease_process_id = $1::UUID
RETURNING key, is_completed, lease_process_id, lease_expires_at, last_external_id
`
type AcquireOrExtendOLAPCutoverJobLeaseParams struct {
Leaseprocessid uuid.UUID `json:"leaseprocessid"`
Key pgtype.Date `json:"key"`
Leaseexpiresat pgtype.Timestamptz `json:"leaseexpiresat"`
Lastexternalid uuid.UUID `json:"lastexternalid"`
}
func (q *Queries) AcquireOrExtendOLAPCutoverJobLease(ctx context.Context, db DBTX, arg AcquireOrExtendOLAPCutoverJobLeaseParams) (*V1PayloadsOlapCutoverJobOffset, error) {
row := db.QueryRow(ctx, acquireOrExtendOLAPCutoverJobLease,
arg.Leaseprocessid,
arg.Key,
arg.Leaseexpiresat,
arg.Lastexternalid,
)
var i V1PayloadsOlapCutoverJobOffset
err := row.Scan(
&i.Key,
&i.IsCompleted,
&i.LeaseProcessID,
&i.LeaseExpiresAt,
&i.LastExternalID,
)
return &i, err
}
const analyzeV1DAGToTaskOLAP = `-- name: AnalyzeV1DAGToTaskOLAP :exec
ANALYZE v1_dag_to_task_olap
`
func (q *Queries) AnalyzeV1DAGToTaskOLAP(ctx context.Context, db DBTX) error {
_, err := db.Exec(ctx, analyzeV1DAGToTaskOLAP)
return err
}
const analyzeV1DAGsOLAP = `-- name: AnalyzeV1DAGsOLAP :exec
ANALYZE v1_dags_olap
`
func (q *Queries) AnalyzeV1DAGsOLAP(ctx context.Context, db DBTX) error {
_, err := db.Exec(ctx, analyzeV1DAGsOLAP)
return err
}
const analyzeV1EventLookupTableOLAP = `-- name: AnalyzeV1EventLookupTableOLAP :exec
ANALYZE v1_event_lookup_table_olap
`
func (q *Queries) AnalyzeV1EventLookupTableOLAP(ctx context.Context, db DBTX) error {
_, err := db.Exec(ctx, analyzeV1EventLookupTableOLAP)
return err
}
const analyzeV1EventToRunOLAP = `-- name: AnalyzeV1EventToRunOLAP :exec
ANALYZE v1_event_to_run_olap
`
func (q *Queries) AnalyzeV1EventToRunOLAP(ctx context.Context, db DBTX) error {
_, err := db.Exec(ctx, analyzeV1EventToRunOLAP)
return err
}
const analyzeV1EventsOLAP = `-- name: AnalyzeV1EventsOLAP :exec
ANALYZE v1_events_olap
`
func (q *Queries) AnalyzeV1EventsOLAP(ctx context.Context, db DBTX) error {
_, err := db.Exec(ctx, analyzeV1EventsOLAP)
return err
}
const analyzeV1LookupTableOLAP = `-- name: AnalyzeV1LookupTableOLAP :exec
ANALYZE v1_lookup_table_olap
`
func (q *Queries) AnalyzeV1LookupTableOLAP(ctx context.Context, db DBTX) error {
_, err := db.Exec(ctx, analyzeV1LookupTableOLAP)
return err
}
const analyzeV1OtelTraceLookupOLAP = `-- name: AnalyzeV1OtelTraceLookupOLAP :exec
ANALYZE v1_otel_trace_lookup_olap
`
func (q *Queries) AnalyzeV1OtelTraceLookupOLAP(ctx context.Context, db DBTX) error {
_, err := db.Exec(ctx, analyzeV1OtelTraceLookupOLAP)
return err
}
const analyzeV1OtelTraceOLAP = `-- name: AnalyzeV1OtelTraceOLAP :exec
ANALYZE v1_otel_trace_olap
`
func (q *Queries) AnalyzeV1OtelTraceOLAP(ctx context.Context, db DBTX) error {
_, err := db.Exec(ctx, analyzeV1OtelTraceOLAP)
return err
}
const analyzeV1PayloadsOLAP = `-- name: AnalyzeV1PayloadsOLAP :exec
ANALYZE v1_payloads_olap
`
func (q *Queries) AnalyzeV1PayloadsOLAP(ctx context.Context, db DBTX) error {
_, err := db.Exec(ctx, analyzeV1PayloadsOLAP)
return err
}
const analyzeV1RunsOLAP = `-- name: AnalyzeV1RunsOLAP :exec
ANALYZE v1_runs_olap
`
func (q *Queries) AnalyzeV1RunsOLAP(ctx context.Context, db DBTX) error {
_, err := db.Exec(ctx, analyzeV1RunsOLAP)
return err
}
const analyzeV1StatusesOLAP = `-- name: AnalyzeV1StatusesOLAP :exec
ANALYZE v1_statuses_olap
`
func (q *Queries) AnalyzeV1StatusesOLAP(ctx context.Context, db DBTX) error {
_, err := db.Exec(ctx, analyzeV1StatusesOLAP)
return err
}
const analyzeV1TaskEventsOLAP = `-- name: AnalyzeV1TaskEventsOLAP :exec
ANALYZE v1_task_events_olap
`
func (q *Queries) AnalyzeV1TaskEventsOLAP(ctx context.Context, db DBTX) error {
_, err := db.Exec(ctx, analyzeV1TaskEventsOLAP)
return err
}
const analyzeV1TasksOLAP = `-- name: AnalyzeV1TasksOLAP :exec
ANALYZE v1_tasks_olap
`
func (q *Queries) AnalyzeV1TasksOLAP(ctx context.Context, db DBTX) error {
_, err := db.Exec(ctx, analyzeV1TasksOLAP)
return err
}
const bulkCreateEventTriggers = `-- name: BulkCreateEventTriggers :exec
WITH inputs AS (
SELECT
UNNEST($1::BIGINT[]) AS run_id,
UNNEST($2::TIMESTAMPTZ[]) AS run_inserted_at,
UNNEST($3::BIGINT[]) AS event_id,
UNNEST($4::TIMESTAMPTZ[]) AS event_seen_at,
UNNEST($5::UUID[]) AS filter_id
)
INSERT INTO v1_event_to_run_olap (
run_id,
run_inserted_at,
event_id,
event_seen_at,
filter_id
)
SELECT
run_id,
run_inserted_at,
event_id,
event_seen_at,
NULLIF(filter_id, '00000000-0000-0000-0000-000000000000'::UUID) AS filter_id
FROM inputs
ON CONFLICT DO NOTHING
`
type BulkCreateEventTriggersParams struct {
Runids []int64 `json:"runids"`
Runinsertedats []pgtype.Timestamptz `json:"runinsertedats"`
Eventids []int64 `json:"eventids"`
Eventseenats []pgtype.Timestamptz `json:"eventseenats"`
Filterids []uuid.UUID `json:"filterids"`
}
func (q *Queries) BulkCreateEventTriggers(ctx context.Context, db DBTX, arg BulkCreateEventTriggersParams) error {
_, err := db.Exec(ctx, bulkCreateEventTriggers,
arg.Runids,
arg.Runinsertedats,
arg.Eventids,
arg.Eventseenats,
arg.Filterids,
)
return err
}
const checkLastAutovacuumForPartitionedTables = `-- name: CheckLastAutovacuumForPartitionedTables :many
SELECT
s.schemaname,
s.relname AS tablename,
s.last_autovacuum,
EXTRACT(EPOCH FROM (NOW() - s.last_autovacuum)) AS seconds_since_last_autovacuum
FROM pg_stat_user_tables s
JOIN pg_catalog.pg_class c ON c.oid = (quote_ident(s.schemaname)||'.'||quote_ident(s.relname))::regclass
WHERE s.schemaname = 'public'
AND c.relispartition = true
AND c.relkind = 'r'
ORDER BY s.last_autovacuum ASC NULLS LAST
`
type CheckLastAutovacuumForPartitionedTablesRow struct {
Schemaname pgtype.Text `json:"schemaname"`
Tablename pgtype.Text `json:"tablename"`
LastAutovacuum pgtype.Timestamptz `json:"last_autovacuum"`
SecondsSinceLastAutovacuum pgtype.Numeric `json:"seconds_since_last_autovacuum"`
}
func (q *Queries) CheckLastAutovacuumForPartitionedTables(ctx context.Context, db DBTX) ([]*CheckLastAutovacuumForPartitionedTablesRow, error) {
rows, err := db.Query(ctx, checkLastAutovacuumForPartitionedTables)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*CheckLastAutovacuumForPartitionedTablesRow
for rows.Next() {
var i CheckLastAutovacuumForPartitionedTablesRow
if err := rows.Scan(
&i.Schemaname,
&i.Tablename,
&i.LastAutovacuum,
&i.SecondsSinceLastAutovacuum,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const cleanUpOLAPCutoverJobOffsets = `-- name: CleanUpOLAPCutoverJobOffsets :exec
DELETE FROM v1_payloads_olap_cutover_job_offset
WHERE NOT key = ANY($1::DATE[])
`
func (q *Queries) CleanUpOLAPCutoverJobOffsets(ctx context.Context, db DBTX, keystokeep []pgtype.Date) error {
_, err := db.Exec(ctx, cleanUpOLAPCutoverJobOffsets, keystokeep)
return err
}
const computeOLAPPayloadBatchSize = `-- name: ComputeOLAPPayloadBatchSize :one
SELECT compute_olap_payload_batch_size(
$1::DATE,
$2::UUID,
$3::INTEGER
) AS total_size_bytes
`
type ComputeOLAPPayloadBatchSizeParams struct {
Partitiondate pgtype.Date `json:"partitiondate"`
Lastexternalid uuid.UUID `json:"lastexternalid"`
Batchsize int32 `json:"batchsize"`
}
func (q *Queries) ComputeOLAPPayloadBatchSize(ctx context.Context, db DBTX, arg ComputeOLAPPayloadBatchSizeParams) (int64, error) {
row := db.QueryRow(ctx, computeOLAPPayloadBatchSize, arg.Partitiondate, arg.Lastexternalid, arg.Batchsize)
var total_size_bytes int64
err := row.Scan(&total_size_bytes)
return total_size_bytes, err
}
const countEvents = `-- name: CountEvents :one
WITH included_events AS (
SELECT e.tenant_id, e.id, e.external_id, e.seen_at, e.key, e.payload, e.additional_metadata, e.scope, e.triggering_webhook_name
FROM v1_events_olap e
WHERE
e.tenant_id = $1
AND (
$2::TEXT[] IS NULL OR
"key" = ANY($2::TEXT[])
)
AND e.seen_at >= $3::TIMESTAMPTZ
AND (
$4::TIMESTAMPTZ IS NULL OR
e.seen_at <= $4::TIMESTAMPTZ
)
AND (
$5::UUID[] IS NULL OR
EXISTS (
SELECT 1
FROM v1_event_to_run_olap etr
JOIN v1_runs_olap r ON (etr.run_id, etr.run_inserted_at) = (r.id, r.inserted_at)
WHERE
(etr.event_id, etr.event_seen_at) = (e.id, e.seen_at)
AND r.workflow_id = ANY($5::UUID[])
AND r.inserted_at >= $3::TIMESTAMPTZ
)
)
AND (
$6::UUID[] IS NULL OR
(e.id, e.seen_at) IN (
SELECT event_id, event_seen_at
FROM v1_event_lookup_table_olap elt
WHERE elt.tenant_id = $1::UUID AND elt.external_id = ANY($6::UUID[])
)
)
AND (
$7::JSONB IS NULL OR
e.additional_metadata @> $7::JSONB
)
AND (
CAST($8::TEXT[] AS v1_readable_status_olap[]) IS NULL OR
EXISTS (
SELECT 1
FROM v1_event_to_run_olap etr
JOIN v1_runs_olap r ON (etr.run_id, etr.run_inserted_at) = (r.id, r.inserted_at)
WHERE
(etr.event_id, etr.event_seen_at) = (e.id, e.seen_at)
AND r.readable_status = ANY(CAST($8::TEXT[] AS v1_readable_status_olap[]))
AND r.inserted_at >= $3::TIMESTAMPTZ
)
)
AND (
$9::TEXT[] IS NULL OR
e.scope = ANY($9::TEXT[])
)
ORDER BY e.seen_at DESC, e.id
LIMIT 20000
)
SELECT COUNT(*)
FROM included_events e
`
type CountEventsParams struct {
Tenantid uuid.UUID `json:"tenantid"`
Keys []string `json:"keys"`
Since pgtype.Timestamptz `json:"since"`
Until pgtype.Timestamptz `json:"until"`
WorkflowIds []uuid.UUID `json:"workflowIds"`
EventIds []uuid.UUID `json:"eventIds"`
AdditionalMetadata []byte `json:"additionalMetadata"`
Statuses []string `json:"statuses"`
Scopes []string `json:"scopes"`
}
func (q *Queries) CountEvents(ctx context.Context, db DBTX, arg CountEventsParams) (int64, error) {
row := db.QueryRow(ctx, countEvents,
arg.Tenantid,
arg.Keys,
arg.Since,
arg.Until,
arg.WorkflowIds,
arg.EventIds,
arg.AdditionalMetadata,
arg.Statuses,
arg.Scopes,
)
var count int64
err := row.Scan(&count)
return count, err
}
const countOLAPTempTableSizeForDAGStatusUpdates = `-- name: CountOLAPTempTableSizeForDAGStatusUpdates :one
SELECT COUNT(*) AS total
FROM v1_task_status_updates_tmp
`
func (q *Queries) CountOLAPTempTableSizeForDAGStatusUpdates(ctx context.Context, db DBTX) (int64, error) {
row := db.QueryRow(ctx, countOLAPTempTableSizeForDAGStatusUpdates)
var total int64
err := row.Scan(&total)
return total, err
}
const countOLAPTempTableSizeForTaskStatusUpdates = `-- name: CountOLAPTempTableSizeForTaskStatusUpdates :one
SELECT COUNT(*) AS total
FROM v1_task_events_olap_tmp
`
func (q *Queries) CountOLAPTempTableSizeForTaskStatusUpdates(ctx context.Context, db DBTX) (int64, error) {
row := db.QueryRow(ctx, countOLAPTempTableSizeForTaskStatusUpdates)
var total int64
err := row.Scan(&total)
return total, err
}
const createDagToTaskOLAPSelfMappings = `-- name: CreateDagToTaskOLAPSelfMappings :exec
INSERT INTO v1_dag_to_task_olap (dag_id, dag_inserted_at, task_id, task_inserted_at)
SELECT
UNNEST($1::bigint[]),
UNNEST($2::timestamptz[]),
UNNEST($1::bigint[]),
UNNEST($2::timestamptz[])
ON CONFLICT DO NOTHING
`
type CreateDagToTaskOLAPSelfMappingsParams struct {
Dagids []int64 `json:"dagids"`
Daginsertedats []pgtype.Timestamptz `json:"daginsertedats"`
}
// For operator-managed DAGs, maps the DAG to its orchestrator task's events (the orchestrator
// shares the DAG's id and inserted_at) so run metadata queries surface the orchestrator's error
// message and timings. Status derivation is unaffected: status queries join through
// v1_tasks_olap, which has no row for the orchestrator.
func (q *Queries) CreateDagToTaskOLAPSelfMappings(ctx context.Context, db DBTX, arg CreateDagToTaskOLAPSelfMappingsParams) error {
_, err := db.Exec(ctx, createDagToTaskOLAPSelfMappings, arg.Dagids, arg.Daginsertedats)
return err
}
const createIncomingWebhookValidationFailureLogs = `-- name: CreateIncomingWebhookValidationFailureLogs :exec
WITH inputs AS (
SELECT
UNNEST($2::TEXT[]) AS incoming_webhook_name,
UNNEST($3::TEXT[]) AS error
)
INSERT INTO v1_incoming_webhook_validation_failures_olap(
tenant_id,
incoming_webhook_name,
error
)
SELECT
$1::UUID,
i.incoming_webhook_name,
i.error
FROM inputs i
`
type CreateIncomingWebhookValidationFailureLogsParams struct {
Tenantid uuid.UUID `json:"tenantid"`
Incomingwebhooknames []string `json:"incomingwebhooknames"`
Errors []string `json:"errors"`
}
func (q *Queries) CreateIncomingWebhookValidationFailureLogs(ctx context.Context, db DBTX, arg CreateIncomingWebhookValidationFailureLogsParams) error {
_, err := db.Exec(ctx, createIncomingWebhookValidationFailureLogs, arg.Tenantid, arg.Incomingwebhooknames, arg.Errors)
return err
}
const createOLAPOffloadedPayloadIndexBlock = `-- name: CreateOLAPOffloadedPayloadIndexBlock :exec
INSERT INTO v1_payloads_olap_offloaded_block_index (
payload_inserted_at_date,
block_external_id_range,
index_file_key
)
VALUES (
$1::DATE,
uuidrange($2::UUID, $3::UUID, '(]'),
$4::TEXT
)
ON CONFLICT ON CONSTRAINT v1_payloads_olap_offloaded_block_index_date_range_excl DO NOTHING
`
type CreateOLAPOffloadedPayloadIndexBlockParams struct {
Payloadinsertedatdate pgtype.Date `json:"payloadinsertedatdate"`
Blocklowerexternalidbound uuid.UUID `json:"blocklowerexternalidbound"`
Blockupperexternalidbound uuid.UUID `json:"blockupperexternalidbound"`
Indexfilekey string `json:"indexfilekey"`
}
func (q *Queries) CreateOLAPOffloadedPayloadIndexBlock(ctx context.Context, db DBTX, arg CreateOLAPOffloadedPayloadIndexBlockParams) error {
_, err := db.Exec(ctx, createOLAPOffloadedPayloadIndexBlock,
arg.Payloadinsertedatdate,
arg.Blocklowerexternalidbound,
arg.Blockupperexternalidbound,
arg.Indexfilekey,
)
return err
}
const createOLAPOptionalTablePartitions = `-- name: CreateOLAPOptionalTablePartitions :one
SELECT
create_v1_range_partition('v1_events_olap'::text, $1::date) AS v1_events_olap,
create_v1_range_partition('v1_event_to_run_olap'::text, $1::date) AS v1_event_to_run_olap,
create_v1_weekly_range_partition('v1_event_lookup_table_olap'::text, $1::date) AS v1_event_lookup_table_olap,
create_v1_range_partition('v1_incoming_webhook_validation_failures_olap'::text, $1::date) AS v1_incoming_webhook_validation_failures_olap,
create_v1_range_partition('v1_cel_evaluation_failures_olap'::text, $1::date) AS v1_cel_evaluation_failures_olap,
create_v1_range_partition('v1_task_events_olap'::text, $1::date) AS v1_task_events_olap,
create_v1_range_partition('v1_dag_to_task_olap'::text, $1::date) AS v1_dag_to_task_olap,
create_v1_monthly_range_partition('v1_lookup_table_olap'::text, $1::date) AS v1_lookup_table_olap,
create_v1_monthly_range_partition('v1_statuses_olap'::text, $1::date) AS v1_statuses_olap
`
type CreateOLAPOptionalTablePartitionsRow struct {
V1EventsOlap int32 `json:"v1_events_olap"`
V1EventToRunOlap int32 `json:"v1_event_to_run_olap"`
V1EventLookupTableOlap int32 `json:"v1_event_lookup_table_olap"`
V1IncomingWebhookValidationFailuresOlap int32 `json:"v1_incoming_webhook_validation_failures_olap"`
V1CelEvaluationFailuresOlap int32 `json:"v1_cel_evaluation_failures_olap"`
V1TaskEventsOlap int32 `json:"v1_task_events_olap"`
V1DagToTaskOlap int32 `json:"v1_dag_to_task_olap"`
V1LookupTableOlap int32 `json:"v1_lookup_table_olap"`
V1StatusesOlap int32 `json:"v1_statuses_olap"`
}
func (q *Queries) CreateOLAPOptionalTablePartitions(ctx context.Context, db DBTX, date pgtype.Date) (*CreateOLAPOptionalTablePartitionsRow, error) {
row := db.QueryRow(ctx, createOLAPOptionalTablePartitions, date)
var i CreateOLAPOptionalTablePartitionsRow
err := row.Scan(
&i.V1EventsOlap,
&i.V1EventToRunOlap,
&i.V1EventLookupTableOlap,
&i.V1IncomingWebhookValidationFailuresOlap,
&i.V1CelEvaluationFailuresOlap,
&i.V1TaskEventsOlap,
&i.V1DagToTaskOlap,
&i.V1LookupTableOlap,
&i.V1StatusesOlap,
)
return &i, err
}
const createOLAPOtelPartitions = `-- name: CreateOLAPOtelPartitions :exec
SELECT
create_v1_range_partition('v1_otel_trace_olap'::text, $1::date),
create_v1_range_partition('v1_otel_trace_lookup_olap'::text, $1::date)
`
func (q *Queries) CreateOLAPOtelPartitions(ctx context.Context, db DBTX, date pgtype.Date) error {
_, err := db.Exec(ctx, createOLAPOtelPartitions, date)
return err
}
const createOLAPPartitions = `-- name: CreateOLAPPartitions :one
SELECT
create_v1_hash_partitions('v1_task_events_olap_tmp'::text, $1::int) AS v1_task_events_olap_tmp,
create_v1_hash_partitions('v1_task_status_updates_tmp'::text, $1::int) AS v1_task_status_updates_tmp,
create_v1_range_partition('v1_tasks_olap'::text, $2::date) AS v1_tasks_olap,
create_v1_range_partition('v1_runs_olap'::text, $2::date) AS v1_runs_olap,
create_v1_range_partition('v1_dags_olap'::text, $2::date) AS v1_dags_olap,
create_v1_range_partition('v1_payloads_olap'::text, $2::date) AS v1_payloads_olap
`
type CreateOLAPPartitionsParams struct {
Partitions int32 `json:"partitions"`
Date pgtype.Date `json:"date"`
}
type CreateOLAPPartitionsRow struct {
V1TaskEventsOlapTmp int32 `json:"v1_task_events_olap_tmp"`
V1TaskStatusUpdatesTmp int32 `json:"v1_task_status_updates_tmp"`
V1TasksOlap int32 `json:"v1_tasks_olap"`
V1RunsOlap int32 `json:"v1_runs_olap"`
V1DagsOlap int32 `json:"v1_dags_olap"`
V1PayloadsOlap int32 `json:"v1_payloads_olap"`
}
func (q *Queries) CreateOLAPPartitions(ctx context.Context, db DBTX, arg CreateOLAPPartitionsParams) (*CreateOLAPPartitionsRow, error) {
row := db.QueryRow(ctx, createOLAPPartitions, arg.Partitions, arg.Date)
var i CreateOLAPPartitionsRow
err := row.Scan(
&i.V1TaskEventsOlapTmp,
&i.V1TaskStatusUpdatesTmp,
&i.V1TasksOlap,
&i.V1RunsOlap,
&i.V1DagsOlap,
&i.V1PayloadsOlap,
)
return &i, err
}
const createOLAPPayloadRangeChunks = `-- name: CreateOLAPPayloadRangeChunks :many
WITH chunks AS (
SELECT
(p).*
FROM create_olap_payload_offload_range_chunks(
$1::DATE,
$2::INTEGER,
$3::INTEGER,
$4::UUID
) p
)
SELECT
lower_external_id::UUID,
upper_external_id::UUID
FROM chunks
`
type CreateOLAPPayloadRangeChunksParams struct {
Partitiondate pgtype.Date `json:"partitiondate"`
Windowsize int32 `json:"windowsize"`
Chunksize int32 `json:"chunksize"`
Lastexternalid uuid.UUID `json:"lastexternalid"`
}
type CreateOLAPPayloadRangeChunksRow struct {
LowerExternalID uuid.UUID `json:"lower_external_id"`
UpperExternalID uuid.UUID `json:"upper_external_id"`
}
func (q *Queries) CreateOLAPPayloadRangeChunks(ctx context.Context, db DBTX, arg CreateOLAPPayloadRangeChunksParams) ([]*CreateOLAPPayloadRangeChunksRow, error) {
rows, err := db.Query(ctx, createOLAPPayloadRangeChunks,
arg.Partitiondate,
arg.Windowsize,
arg.Chunksize,
arg.Lastexternalid,
)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*CreateOLAPPayloadRangeChunksRow
for rows.Next() {
var i CreateOLAPPayloadRangeChunksRow
if err := rows.Scan(&i.LowerExternalID, &i.UpperExternalID); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
type CreateTaskEventsOLAPParams struct {
TenantID uuid.UUID `json:"tenant_id"`
TaskID int64 `json:"task_id"`
TaskInsertedAt pgtype.Timestamptz `json:"task_inserted_at"`
EventType V1EventTypeOlap `json:"event_type"`
WorkflowID uuid.UUID `json:"workflow_id"`
EventTimestamp pgtype.Timestamptz `json:"event_timestamp"`
ReadableStatus V1ReadableStatusOlap `json:"readable_status"`
RetryCount int32 `json:"retry_count"`
ErrorMessage pgtype.Text `json:"error_message"`
Output []byte `json:"output"`
WorkerID *uuid.UUID `json:"worker_id"`
AdditionalEventData pgtype.Text `json:"additional__event_data"`
AdditionalEventMessage pgtype.Text `json:"additional__event_message"`
ExternalID uuid.UUID `json:"external_id"`
DurableInvocationCount int32 `json:"durable_invocation_count"`
IsDagOrchestrator bool `json:"is_dag_orchestrator"`
}
const createV1PayloadOLAPCutoverTemporaryTable = `-- name: CreateV1PayloadOLAPCutoverTemporaryTable :exec
SELECT copy_v1_payloads_olap_partition_structure($1::DATE)
`
func (q *Queries) CreateV1PayloadOLAPCutoverTemporaryTable(ctx context.Context, db DBTX, date pgtype.Date) error {
_, err := db.Exec(ctx, createV1PayloadOLAPCutoverTemporaryTable, date)
return err
}
const deleteOldOLAPPayloadOffloadedBlockIndexRows = `-- name: DeleteOldOLAPPayloadOffloadedBlockIndexRows :exec
DELETE FROM v1_payloads_olap_offloaded_block_index
WHERE payload_inserted_at_date < $1::DATE
`
func (q *Queries) DeleteOldOLAPPayloadOffloadedBlockIndexRows(ctx context.Context, db DBTX, before pgtype.Date) error {
_, err := db.Exec(ctx, deleteOldOLAPPayloadOffloadedBlockIndexRows, before)
return err
}
const findMinInsertedAtForDAGStatusUpdates = `-- name: FindMinInsertedAtForDAGStatusUpdates :one
WITH tenants AS (
SELECT UNNEST(
find_matching_tenants_in_task_status_updates_tmp_partition(
$1::int,
$3::UUID[]
)
) AS tenant_id
)
SELECT
MIN(u.dag_inserted_at)::TIMESTAMPTZ AS min_inserted_at
FROM tenants t,
LATERAL list_task_status_updates_tmp(
$1::int,
t.tenant_id,
$2::int
) u
`
type FindMinInsertedAtForDAGStatusUpdatesParams struct {
Partitionnumber int32 `json:"partitionnumber"`
Eventlimit int32 `json:"eventlimit"`
Tenantids []uuid.UUID `json:"tenantids"`
}
func (q *Queries) FindMinInsertedAtForDAGStatusUpdates(ctx context.Context, db DBTX, arg FindMinInsertedAtForDAGStatusUpdatesParams) (pgtype.Timestamptz, error) {
row := db.QueryRow(ctx, findMinInsertedAtForDAGStatusUpdates, arg.Partitionnumber, arg.Eventlimit, arg.Tenantids)
var min_inserted_at pgtype.Timestamptz
err := row.Scan(&min_inserted_at)
return min_inserted_at, err
}
const findMinInsertedAtForTaskStatusUpdates = `-- name: FindMinInsertedAtForTaskStatusUpdates :one
WITH tenants AS (
SELECT UNNEST(
find_matching_tenants_in_task_events_tmp_partition(
$1::int,
$3::UUID[]
)
) AS tenant_id
)
SELECT
MIN(e.task_inserted_at)::TIMESTAMPTZ AS min_inserted_at
FROM tenants t,
LATERAL list_task_events_tmp(
$1::int,
t.tenant_id,
$2::int
) e
`
type FindMinInsertedAtForTaskStatusUpdatesParams struct {
Partitionnumber int32 `json:"partitionnumber"`
Eventlimit int32 `json:"eventlimit"`
Tenantids []uuid.UUID `json:"tenantids"`
}
func (q *Queries) FindMinInsertedAtForTaskStatusUpdates(ctx context.Context, db DBTX, arg FindMinInsertedAtForTaskStatusUpdatesParams) (pgtype.Timestamptz, error) {
row := db.QueryRow(ctx, findMinInsertedAtForTaskStatusUpdates, arg.Partitionnumber, arg.Eventlimit, arg.Tenantids)
var min_inserted_at pgtype.Timestamptz
err := row.Scan(&min_inserted_at)
return min_inserted_at, err
}
const flattenTasksByExternalIds = `-- name: FlattenTasksByExternalIds :many
WITH lookups AS MATERIALIZED (
SELECT
tenant_id, external_id, task_id, dag_id, inserted_at
FROM
v1_lookup_table_olap
WHERE
external_id = ANY($1::uuid[])
AND tenant_id = $2::uuid
), tasks_from_dags AS (
SELECT
l.tenant_id,
dt.task_id,
dt.task_inserted_at
FROM
lookups l
JOIN
v1_dag_to_task_olap dt ON l.dag_id = dt.dag_id AND l.inserted_at = dt.dag_inserted_at
WHERE
l.dag_id IS NOT NULL
AND dt.dag_inserted_at >= (SELECT MIN(inserted_at) FROM lookups WHERE dag_id IS NOT NULL)
), unioned_tasks AS (
SELECT
l.tenant_id AS tenant_id,
l.task_id AS task_id,
l.inserted_at AS task_inserted_at
FROM
lookups l
UNION ALL
SELECT
t.tenant_id AS tenant_id,
t.task_id AS task_id,
t.task_inserted_at AS task_inserted_at
FROM
tasks_from_dags t
)
SELECT
t.tenant_id,
t.id,
t.inserted_at,
t.external_id,
t.latest_retry_count AS retry_count
FROM
v1_tasks_olap t
JOIN
unioned_tasks ut ON (t.inserted_at, t.id) = (ut.task_inserted_at, ut.task_id)
`
type FlattenTasksByExternalIdsParams struct {
Externalids []uuid.UUID `json:"externalids"`
Tenantid uuid.UUID `json:"tenantid"`
}
type FlattenTasksByExternalIdsRow struct {
TenantID uuid.UUID `json:"tenant_id"`
ID int64 `json:"id"`
InsertedAt pgtype.Timestamptz `json:"inserted_at"`
ExternalID uuid.UUID `json:"external_id"`
RetryCount int32 `json:"retry_count"`
}
// Get retry counts for each task
func (q *Queries) FlattenTasksByExternalIds(ctx context.Context, db DBTX, arg FlattenTasksByExternalIdsParams) ([]*FlattenTasksByExternalIdsRow, error) {
rows, err := db.Query(ctx, flattenTasksByExternalIds, arg.Externalids, arg.Tenantid)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*FlattenTasksByExternalIdsRow
for rows.Next() {
var i FlattenTasksByExternalIdsRow
if err := rows.Scan(
&i.TenantID,
&i.ID,
&i.InsertedAt,
&i.ExternalID,
&i.RetryCount,
); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getDagDurations = `-- name: GetDagDurations :many
SELECT
lt.external_id,
MIN(e.event_timestamp) FILTER (WHERE e.readable_status = 'RUNNING')::TIMESTAMPTZ AS started_at,
MAX(e.event_timestamp) FILTER (WHERE e.readable_status IN ('COMPLETED', 'FAILED', 'CANCELLED'))::TIMESTAMPTZ AS finished_at
FROM
v1_lookup_table_olap lt
JOIN
v1_dags_olap d ON (lt.dag_id, lt.inserted_at) = (d.id, d.inserted_at)
JOIN
v1_dag_to_task_olap dt ON (d.id, d.inserted_at) = (dt.dag_id, dt.dag_inserted_at)
JOIN
v1_task_events_olap e ON (dt.task_id, dt.task_inserted_at) = (e.task_id, e.task_inserted_at)
WHERE lt.external_id = ANY($1::UUID[])
AND lt.tenant_id = $2::UUID
AND d.inserted_at >= $3::TIMESTAMPTZ
AND dt.dag_inserted_at >= $3::TIMESTAMPTZ
AND e.task_inserted_at >= $3::TIMESTAMPTZ
GROUP BY lt.external_id
`
type GetDagDurationsParams struct {
Externalids []uuid.UUID `json:"externalids"`
Tenantid uuid.UUID `json:"tenantid"`
Mininsertedat pgtype.Timestamptz `json:"mininsertedat"`
}
type GetDagDurationsRow struct {
ExternalID uuid.UUID `json:"external_id"`
StartedAt pgtype.Timestamptz `json:"started_at"`
FinishedAt pgtype.Timestamptz `json:"finished_at"`
}
func (q *Queries) GetDagDurations(ctx context.Context, db DBTX, arg GetDagDurationsParams) ([]*GetDagDurationsRow, error) {
rows, err := db.Query(ctx, getDagDurations, arg.Externalids, arg.Tenantid, arg.Mininsertedat)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*GetDagDurationsRow
for rows.Next() {
var i GetDagDurationsRow
if err := rows.Scan(&i.ExternalID, &i.StartedAt, &i.FinishedAt); err != nil {
return nil, err
}
items = append(items, &i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getEventByExternalId = `-- name: GetEventByExternalId :one
SELECT e.tenant_id, e.id, e.external_id, e.seen_at, e.key, e.payload, e.additional_metadata, e.scope, e.triggering_webhook_name
FROM v1_event_lookup_table_olap elt
JOIN v1_events_olap e ON (elt.event_id, elt.event_seen_at) = (e.id, e.seen_at)
WHERE elt.external_id = $1::uuid
`
func (q *Queries) GetEventByExternalId(ctx context.Context, db DBTX, eventexternalid uuid.UUID) (*V1EventsOlap, error) {
row := db.QueryRow(ctx, getEventByExternalId, eventexternalid)
var i V1EventsOlap
err := row.Scan(
&i.TenantID,
&i.ID,
&i.ExternalID,
&i.SeenAt,
&i.Key,
&i.Payload,
&i.AdditionalMetadata,
&i.Scope,
&i.TriggeringWebhookName,
)
return &i, err
}
const getEventByExternalIdUsingTenantId = `-- name: GetEventByExternalIdUsingTenantId :one
SELECT e.tenant_id, e.id, e.external_id, e.seen_at, e.key, e.payload, e.additional_metadata, e.scope, e.triggering_webhook_name
FROM v1_event_lookup_table_olap elt
JOIN v1_events_olap e ON (elt.event_id, elt.event_seen_at) = (e.id, e.seen_at)
WHERE
elt.external_id = $1::uuid
AND elt.tenant_id = $2::uuid
`
type GetEventByExternalIdUsingTenantIdParams struct {
Eventexternalid uuid.UUID `json:"eventexternalid"`
Tenantid uuid.UUID `json:"tenantid"`
}
func (q *Queries) GetEventByExternalIdUsingTenantId(ctx context.Context, db DBTX, arg GetEventByExternalIdUsingTenantIdParams) (*V1EventsOlap, error) {
row := db.QueryRow(ctx, getEventByExternalIdUsingTenantId, arg.Eventexternalid, arg.Tenantid)
var i V1EventsOlap
err := row.Scan(
&i.TenantID,
&i.ID,
&i.ExternalID,
&i.SeenAt,
&i.Key,
&i.Payload,
&i.AdditionalMetadata,
&i.Scope,
&i.TriggeringWebhookName,
)
return &i, err
}
const getOLAPOffloadedPayloadIndexBlocks = `-- name: GetOLAPOffloadedPayloadIndexBlocks :many
WITH inputs AS (
SELECT
UNNEST($1::DATE[]) AS inserted_at_date,
UNNEST($2::UUID[]) AS external_id
)
SELECT i.external_id::UUID AS external_id, p.index_file_key
FROM v1_payloads_olap_offloaded_block_index p
JOIN inputs i ON
p.payload_inserted_at_date = i.inserted_at_date
AND p.block_external_id_range @> i.external_id
`
type GetOLAPOffloadedPayloadIndexBlocksParams struct {
Insertedats []pgtype.Date `json:"insertedats"`
Externalids []uuid.UUID `json:"externalids"`
}
type GetOLAPOffloadedPayloadIndexBlocksRow struct {
ExternalID uuid.UUID `json:"external_id"`
IndexFileKey string `json:"index_file_key"`
}
// todo: make sure this join uses the index correctly
func (q *Queries) GetOLAPOffloadedPayloadIndexBlocks(ctx context.Context, db DBTX, arg GetOLAPOffloadedPayloadIndexBlocksParams) ([]*GetOLAPOffloadedPayloadIndexBlocksRow, error) {
rows, err := db.Query(ctx, getOLAPOffloadedPayloadIndexBlocks, arg.Insertedats, arg.Externalids)
if err != nil {
return nil, err
}
defer rows.Close()
var items []*GetOLAPOffloadedPayloadIndexBlocksRow
for rows.Next() {
var i GetOLAPOffloadedPayloadIndexBlocksRow
if err := rows.Scan(&i.ExternalID, &i.IndexFileKey); err != nil {
return nil, err
}