-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdb.go
More file actions
982 lines (892 loc) · 25.5 KB
/
db.go
File metadata and controls
982 lines (892 loc) · 25.5 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
package server
import (
"context"
"errors"
"fmt"
"regexp"
"runtime"
"runtime/debug"
// "strconv"
mathrand "math/rand"
"strings"
"sync"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
// "github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/urnetwork/glog"
)
/*
uses the shared transaction pool across all services:
- `Db` runs in raw connection mode (no transaction)
- `Tx` runs in read-write mode by default, which can be changed with `pgx.TxOptions`
uses a private connection pool local to the current service:
- `MaintenanceDb`
- `MaintenanceTx`
*/
// note all times in the db should be `timestamp` UTC. Do not use `timestamp with time zone`. See `NowUtc`
var DbContextDoneError = errors.New("Done")
const PgConnectTimeout = 30 * time.Second
// type aliases to simplify user code
type PgConn = *pgxpool.Conn
type PgTx = pgx.Tx
type PgResult = pgx.Rows
type PgTag = pgconn.CommandTag
type PgNamedArgs = pgx.NamedArgs
type PgBatch = *pgx.Batch
type PgBatchResults = pgx.BatchResults
const TxSerializable = pgx.Serializable
const TxRepeatableRead = pgx.RepeatableRead
const TxReadCommitted = pgx.ReadCommitted
var safePool = &safePgPool{
ctx: context.Background(),
vaultResourceName: DefaultPgVaultResourceName,
configResourceName: DefaultPgConfigResourceName,
}
// func pool() *pgxpool.Pool {
// return safePool.open()
// }
var safeMaintenancePool = &safePgPool{
ctx: context.Background(),
vaultResourceName: MaintenancePgVaultResourceName,
configResourceName: MaintenancePgConfigResourceName,
}
// func maintenancePool() *pgxpool.Pool {
// return safeMaintenancePool.open()
// }
// resets the connection pool
// call this after changes to the env
func PgReset() {
safePool.reset()
safeMaintenancePool.reset()
}
const DefaultPgVaultResourceName = "pg.yml"
const DefaultPgConfigResourceName = "db.yml"
const MaintenancePgVaultResourceName = "pg_maintenance.yml"
const MaintenancePgConfigResourceName = "db_maintenance.yml"
type safePgPool struct {
vaultResourceName string
configResourceName string
ctx context.Context
mutex sync.Mutex
pool *pgxpool.Pool
}
func (self *safePgPool) open() *pgxpool.Pool {
self.mutex.Lock()
defer self.mutex.Unlock()
if self.pool == nil {
// Logger().Printf("Db init\n")
dbKeys := Vault.RequireSimpleResource("pg.yml")
dbConfigKeys := Config.RequireSimpleResource("db.yml")
minConnections := dbConfigKeys.RequireInt("min_connections")
maxConnections := dbConfigKeys.RequireInt("max_connections")
healthCheckPeriod := "1s"
connectionMaxLifetime := "1h"
connectionMaxLifetimeJitter := "15m"
connectionMaxIdleTime := "5m"
if healthCheckPeriods := dbConfigKeys.String("health_check_period"); 0 < len(healthCheckPeriods) {
healthCheckPeriod = healthCheckPeriods[0]
}
if connectionMaxLifetimes := dbConfigKeys.String("conn_max_lifetime"); 0 < len(connectionMaxLifetimes) {
connectionMaxLifetime = connectionMaxLifetimes[0]
}
if connectionMaxLifetimeJitters := dbConfigKeys.String("conn_max_lifetime_jitter"); 0 < len(connectionMaxLifetimeJitters) {
connectionMaxLifetimeJitter = connectionMaxLifetimeJitters[0]
}
if connectionMaxIdleTimes := dbConfigKeys.String("conn_max_idle_time"); 0 < len(connectionMaxIdleTimes) {
connectionMaxIdleTime = connectionMaxIdleTimes[0]
}
if service, err := Service(); err == nil && service != "" {
if serviceMinConnections := dbConfigKeys.Int(service, "min_connections"); 0 < len(serviceMinConnections) {
minConnections = serviceMinConnections[0]
}
if serviceMaxConnections := dbConfigKeys.Int(service, "max_connections"); 0 < len(serviceMaxConnections) {
maxConnections = serviceMaxConnections[0]
}
if healthCheckPeriods := dbConfigKeys.String(service, "health_check_period"); 0 < len(healthCheckPeriods) {
healthCheckPeriod = healthCheckPeriods[0]
}
if connectionMaxLifetimes := dbConfigKeys.String(service, "conn_max_lifetime"); 0 < len(connectionMaxLifetimes) {
connectionMaxLifetime = connectionMaxLifetimes[0]
}
if connectionMaxLifetimeJitters := dbConfigKeys.String(service, "conn_max_lifetime_jitter"); 0 < len(connectionMaxLifetimeJitters) {
connectionMaxLifetimeJitter = connectionMaxLifetimeJitters[0]
}
if connectionMaxIdleTimes := dbConfigKeys.String(service, "conn_max_idle_time"); 0 < len(connectionMaxIdleTimes) {
connectionMaxIdleTime = connectionMaxIdleTimes[0]
}
}
// see the Config struct for human understandable docs
// https://github.com/jackc/pgx/blob/master/pgxpool/pool.go#L103
// https://github.com/jackc/pgx/blob/master/pgconn/config.go#L445
options := map[string]string{
"sslmode": "disable",
"connect_timeout": fmt.Sprintf("%d", PgConnectTimeout/time.Second),
"pool_max_conns": fmt.Sprintf("%d", maxConnections),
"pool_min_conns": fmt.Sprintf("%d", minConnections),
"pool_max_conn_lifetime": connectionMaxLifetime,
"pool_max_conn_lifetime_jitter": connectionMaxLifetimeJitter,
"pool_max_conn_idle_time": connectionMaxIdleTime,
"pool_health_check_period": healthCheckPeriod,
// must use `Tx` to write, which sets `AccessMode: pgx.ReadWrite`
// "default_transaction_read_only": "on",
// "default_transaction_isolation": "read committed",
}
glog.Infof("[db]options = %s\n", options)
optionsPairs := []string{}
for key, value := range options {
optionsPairs = append(optionsPairs, fmt.Sprintf("%s=%s", key, value))
}
optionsString := strings.Join(optionsPairs, "&")
postgresUrl := fmt.Sprintf(
"postgres://%s:%s@%s/%s?%s",
dbKeys.RequireString("user"),
dbKeys.RequireString("password"),
dbKeys.RequireString("authority"),
dbKeys.RequireString("db"),
optionsString,
)
// Logger().Printf("Db url %s\n", postgresUrl)
config, err := pgxpool.ParseConfig(postgresUrl)
if err != nil {
panic(fmt.Sprintf("Unable to parse url: %s", err))
}
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
// use `Id` instead of the default UUID type
pgxRegisterIdType(conn.TypeMap())
return nil
}
self.pool, err = pgxpool.NewWithConfig(self.ctx, config)
if err != nil {
panic(fmt.Sprintf("Unable to connect to database: %s", err))
}
}
return self.pool
}
func (self *safePgPool) close() {
self.reset()
}
func (self *safePgPool) reset() {
self.mutex.Lock()
defer self.mutex.Unlock()
if self.pool != nil {
self.pool.Close()
self.pool = nil
}
}
type DbRetryOptions struct {
// rerun the entire callback on commit error
rerunOnCommitError bool
rerunOnConnectionError bool
// this only works if the conflict, e.g. an ID, is changed on each run
// the BY coding style will generate the id in the callback, so this is generally considered safe
rerunOnTransientError bool
retryMinTimeout time.Duration
retryMaxTimeout time.Duration
endRetryTimeout time.Duration
// debugRetryTimeout time.Duration
}
func (self *DbRetryOptions) retryTimeout() time.Duration {
return self.retryMinTimeout + time.Duration(
mathrand.Int63n(int64(self.retryMaxTimeout-self.retryMinTimeout)),
)
}
// this is the default for `Db` and `Tx`
func OptRetryDefault() DbRetryOptions {
return DbRetryOptions{
rerunOnCommitError: true,
rerunOnConnectionError: true,
rerunOnTransientError: true,
retryMinTimeout: 100 * time.Millisecond,
retryMaxTimeout: 5 * time.Second,
endRetryTimeout: 60 * time.Second,
// debugRetryTimeout: 90 * time.Second,
}
}
func OptNoRetry() DbRetryOptions {
return DbRetryOptions{
rerunOnCommitError: false,
rerunOnConnectionError: false,
rerunOnTransientError: false,
}
}
type DbReadWriteOptions struct {
readOnly bool
}
func OptReadOnly() DbReadWriteOptions {
return DbReadWriteOptions{
readOnly: true,
}
}
func OptReadWrite() DbReadWriteOptions {
return DbReadWriteOptions{
readOnly: false,
}
}
/*
type DbDebugOptions struct {
txCommitSeparately bool
}
func OptNoDebug() DbDebugOptions {
return DbDebugOptions{
txCommitSeparately: false,
}
}
// it can be hard to know which `Exec` has issues in a large transaction
// use this to separate the `Exec`
func OptDebugTx() DbDebugOptions {
return DbDebugOptions{
txCommitSeparately: true,
}
}
*/
type PgRetry struct {
}
func (self *PgRetry) Error() string {
return "retry"
}
// transient errors can be resolved by either
// - changing the parameters of the query to avoid constraint conflicts
// - chaning the timing of the query to avoid rollbacks
// https://www.postgresql.org/docs/current/mvcc-serialization-failure-handling.html
// https://www.postgresql.org/docs/current/errcodes-appendix.html
func isTransientError(err error) bool {
switch v := err.(type) {
case *pgconn.PgError:
if pgerrcode.IsIntegrityConstraintViolation(v.Code) {
return true
}
if pgerrcode.IsTransactionRollback(v.Code) {
return true
}
// fmt.Printf("[db]intransient error = %d\n", v.Code)
return false
case *PgRetry:
return true
default:
return false
}
}
func isConnectionError(err error) bool {
switch v := err.(type) {
case *pgconn.PgError:
if pgerrcode.IsConnectionException(v.Code) {
// try a new connection
return true
}
return false
default:
switch err.Error() {
// pgconn.connLockError
// https://github.com/jackc/pgconn/blob/master/errors.go
case "conn closed":
// try a new connection
return true
default:
return false
}
}
}
// maintenance connection
func MaintenanceDb(ctx context.Context, callback func(PgConn), options ...any) {
c := func() {
dbWithPool(ctx, safeMaintenancePool, callback, options...)
}
if glog.V(2) {
pc, filename, line, _ := runtime.Caller(1)
pcName := runtime.FuncForPC(pc).Name()
parts := strings.Split(filename, "/")
Trace(
fmt.Sprintf("[db] %s %s:%d\n", pcName, parts[len(parts)-1], line),
c,
)
} else {
c()
}
}
func Db(ctx context.Context, callback func(PgConn), options ...any) {
c := func() {
dbWithPool(ctx, safePool, callback, options...)
}
if glog.V(2) {
pc, filename, line, _ := runtime.Caller(1)
pcName := runtime.FuncForPC(pc).Name()
parts := strings.Split(filename, "/")
Trace(
fmt.Sprintf("[db] %s %s:%d\n", pcName, parts[len(parts)-1], line),
c,
)
} else {
c()
}
}
func dbWithPool(ctx context.Context, pool *safePgPool, callback func(PgConn), options ...any) {
retryOptions := OptRetryDefault()
rwOptions := OptReadOnly()
// debugOptions := OptNoDebug()
for _, option := range options {
switch v := option.(type) {
case DbRetryOptions:
retryOptions = v
case DbReadWriteOptions:
rwOptions = v
// case DbDebugOptions:
// debugOptions = v
}
}
retryEndTime := NowUtc().Add(retryOptions.endRetryTimeout)
// retryDebugTime := NowUtc().Add(retryOptions.debugRetryTimeout)
for {
var pgErr error
conn, connErr := pool.open().Acquire(ctx)
if connErr != nil {
if retryOptions.rerunOnConnectionError {
select {
case <-ctx.Done():
panic(DbContextDoneError)
case <-time.After(retryOptions.retryTimeout()):
}
}
panic(connErr)
}
connErr = conn.Ping(ctx)
if connErr != nil {
// take the bad connection out of the pool
pgxConn := conn.Hijack()
pgxConn.Close(ctx)
conn = nil
if retryOptions.rerunOnConnectionError {
select {
case <-ctx.Done():
panic(DbContextDoneError)
case <-time.After(retryOptions.retryTimeout()):
}
}
panic(connErr)
}
func() {
defer func() {
if err := recover(); err != nil {
switch v := err.(type) {
case error:
if isTransientError(v) && retryOptions.rerunOnTransientError {
pgErr = v
} else if isConnectionError(v) && retryOptions.rerunOnConnectionError {
connErr = v
} else {
panic(v)
}
default:
panic(v)
}
}
}()
defer func() {
if connErr != nil {
// take the bad connection out of the pool
pgxConn := conn.Hijack()
pgxConn.Close(ctx)
conn = nil
} else {
conn.Release()
}
}()
// defer Logger().Printf("DB CLOSE\n")
if !rwOptions.readOnly {
// the default is read only, escalate to rw
RaisePgResult(conn.Exec(ctx, "SET default_transaction_read_only=off"))
}
callback(conn)
}()
if pgErr != nil {
if isTransientError(pgErr) && retryOptions.rerunOnTransientError {
select {
case <-ctx.Done():
panic(DbContextDoneError)
case <-time.After(retryOptions.retryTimeout()):
}
if retryEndTime.Before(NowUtc()) {
panic(pgErr)
}
if glog.V(2) {
glog.Infof("[db]transient error, retry: %s\n", ErrorJson(pgErr, debug.Stack()))
} else {
glog.Infof("[db]transient error, retry = %v\n", pgErr)
}
continue
}
panic(pgErr)
}
if connErr != nil {
if retryOptions.rerunOnConnectionError {
select {
case <-ctx.Done():
panic(DbContextDoneError)
case <-time.After(retryOptions.retryTimeout()):
}
continue
}
panic(connErr)
}
return
}
}
func MaintenanceTx(ctx context.Context, callback func(PgTx), options ...any) {
c := func() {
txWithPool(ctx, safeMaintenancePool, callback, options...)
}
if glog.V(2) {
pc, filename, line, _ := runtime.Caller(1)
pcName := runtime.FuncForPC(pc).Name()
parts := strings.Split(filename, "/")
Trace(
fmt.Sprintf("[tx] %s %s:%d\n", pcName, parts[len(parts)-1], line),
c,
)
} else {
c()
}
}
func Tx(ctx context.Context, callback func(PgTx), options ...any) {
c := func() {
txWithPool(ctx, safePool, callback, options...)
}
if glog.V(2) {
pc, filename, line, _ := runtime.Caller(1)
pcName := runtime.FuncForPC(pc).Name()
parts := strings.Split(filename, "/")
Trace(
fmt.Sprintf("[tx] %s %s:%d\n", pcName, parts[len(parts)-1], line),
c,
)
} else {
c()
}
}
func txWithPool(ctx context.Context, pool *safePgPool, callback func(PgTx), options ...any) {
retryOptions := OptRetryDefault()
// by default use RepeatableRead isolation
// https://www.postgresql.org/docs/current/transaction-iso.html
txOptions := pgx.TxOptions{
IsoLevel: pgx.RepeatableRead,
AccessMode: pgx.ReadWrite,
DeferrableMode: pgx.NotDeferrable,
}
// debugOptions := OptNoDebug()
for _, option := range options {
switch v := option.(type) {
case DbRetryOptions:
retryOptions = v
case pgx.TxOptions:
txOptions = v
case pgx.TxIsoLevel:
txOptions.IsoLevel = v
case pgx.TxAccessMode:
txOptions.AccessMode = v
case pgx.TxDeferrableMode:
txOptions.DeferrableMode = v
// case DbDebugOptions:
// debugOptions = v
}
}
retryEndTime := NowUtc().Add(retryOptions.endRetryTimeout)
// retryDebugTime := NowUtc().Add(retryOptions.debugRetryTimeout)
for {
var pgErr error
var commitErr error
dbWithPool(ctx, pool, func(conn PgConn) {
tx, err := conn.BeginTx(ctx, txOptions)
if err != nil {
panic(err)
}
// if debugOptions.txCommitSeparately {
// tx = newDebugTx(tx, conn, txOptions)
// }
defer func() {
if err := recover(); err != nil {
if rollbackErr := tx.Rollback(ctx); rollbackErr != nil {
panic(rollbackErr)
}
panic(err)
}
}()
func() {
defer func() {
if err := recover(); err != nil {
switch v := err.(type) {
case error:
if isTransientError(v) && retryOptions.rerunOnTransientError {
pgErr = v
} else {
panic(v)
}
default:
panic(v)
}
}
}()
callback(tx)
}()
if pgErr == nil {
// Logger().Printf("Db commit\n")
commitErr = tx.Commit(ctx)
} else {
if rollbackErr := tx.Rollback(ctx); rollbackErr != nil {
panic(rollbackErr)
}
}
}, options...)
if pgErr != nil {
if isTransientError(pgErr) && retryOptions.rerunOnTransientError {
select {
case <-ctx.Done():
panic(DbContextDoneError)
case <-time.After(retryOptions.retryTimeout()):
}
if retryEndTime.Before(NowUtc()) {
panic(pgErr)
}
if glog.V(2) {
glog.Infof("[db]transient error, retry: %s\n", ErrorJson(pgErr, debug.Stack()))
} else {
glog.Infof("[db]transient error, retry = %v\n", pgErr)
}
continue
}
panic(pgErr)
}
if commitErr != nil {
if retryOptions.rerunOnCommitError {
select {
case <-ctx.Done():
panic(DbContextDoneError)
case <-time.After(retryOptions.retryTimeout()):
}
if retryEndTime.Before(NowUtc()) {
panic(commitErr)
}
if glog.V(2) {
glog.Infof("[db]commit error, retry: %s\n", ErrorJson(commitErr, debug.Stack()))
} else {
glog.Infof("[db]commit error, retry = %v\n", commitErr)
}
continue
}
panic(commitErr)
}
return
}
}
/*
type debugTx struct {
conn PgConn
txOptions pgx.TxOptions
PgTx
}
func newDebugTx(tx pgx.Tx, conn PgConn, txOptions pgx.TxOptions) pgx.Tx {
return &debugTx{
conn: conn,
txOptions: txOptions,
PgTx: tx,
}
}
func (self *debugTx) commit(ctx context.Context) {
commitErr := self.Commit(ctx)
if commitErr != nil {
panic(fmt.Errorf("[Tx debug] Commit error. (%w)", commitErr))
}
tx, txErr := self.conn.BeginTx(ctx, self.txOptions)
if txErr != nil {
panic(fmt.Errorf("[Tx debug] Create new transaction error. (%w)", txErr))
}
self.PgTx = tx
}
func (self *debugTx) Exec(ctx context.Context, sql string, arguments ...any) (commandTag pgconn.CommandTag, err error) {
tempTableDropRe := regexp.MustCompile("(?si)^\\s*(CREATE TEMPORARY TABLE\\s*(\\S+).*)\\s+ON COMMIT DROP\\s*$")
groups := tempTableDropRe.FindStringSubmatch(sql)
if groups != nil {
// remove `ON COMMIT DROP`
sql = groups[1]
Logger().Printf("[Tx debug] Removed `ON COMMIT DROP` from temp table %s\n", groups[2])
}
commandTag, err = self.PgTx.Exec(ctx, sql, arguments...)
if err != nil {
return
}
self.commit(ctx)
return
}
// note the batch results need to be closed before commit
// func (self *debugTx) SendBatch(ctx context.Context, b *pgx.Batch) pgx.BatchResults {
// results := self.PgTx.SendBatch(ctx, b)
// self.commit(ctx)
// return results
// }
*/
func WithPgResult(r PgResult, err error, callback any) {
Raise(err)
defer r.Close()
switch v := callback.(type) {
case func():
v()
case func(PgResult):
v(r)
default:
panic(errors.New(fmt.Sprintf("Unknown callback: %s", callback)))
}
Raise(r.Err())
}
func RaisePgResult[T any](result T, err error) T {
Raise(err)
return result
}
func BatchInTx(ctx context.Context, tx PgTx, callback func(PgBatch), resultsCallbacks ...func(PgBatchResults)) {
batch := &pgx.Batch{}
callback(batch)
results := tx.SendBatch(ctx, batch)
for _, resultsCallback := range resultsCallbacks {
resultsCallback(results)
}
err := results.Close()
if err != nil {
panic(err)
}
}
type ComplexValue interface {
// unpack a complex value into individual values
Values() []any
}
// CreateTempTableInTxAllowDuplicates
// spec is `table_name(value_column_name type)`
func CreateTempTableInTx[T any](ctx context.Context, tx PgTx, spec string, values ...T) {
tableSpec := parseTempTableSpec(spec)
pgParts := []string{}
for i, valueColumnName := range tableSpec.valueColumnNames {
valuePgType := tableSpec.valuePgTypes[i]
valuePart := fmt.Sprintf("%s %s NOT NULL", valueColumnName, valuePgType)
pgParts = append(pgParts, valuePart)
}
pgPlaceholders := []string{}
i := 1
for range tableSpec.valueColumnNames {
pgPlaceholders = append(pgPlaceholders, fmt.Sprintf("$%d", i))
i += 1
}
RaisePgResult(tx.Exec(ctx, fmt.Sprintf(
`
CREATE TEMPORARY TABLE %s (
%s,
PRIMARY KEY (%s)
)
ON COMMIT DROP
`,
tableSpec.tableName,
strings.Join(pgParts, ", "),
strings.Join(tableSpec.valueColumnNames, ", "),
)))
BatchInTx(ctx, tx, func(batch PgBatch) {
for _, value := range values {
var pgValues = []any{}
pgValues = expandValue(value, pgValues)
if len(pgValues) != len(pgPlaceholders) {
panic(fmt.Errorf("Expected %d values but found %d.", len(pgPlaceholders), len(pgValues)))
}
batch.Queue(
fmt.Sprintf(
`
INSERT INTO %s (%s) VALUES (%s)
ON CONFLICT DO NOTHING
`,
tableSpec.tableName,
strings.Join(tableSpec.valueColumnNames, ", "),
strings.Join(pgPlaceholders, ", "),
),
pgValues...,
)
}
})
}
func CreateTempTableInTxAllowDuplicates[T any](ctx context.Context, tx PgTx, spec string, values ...T) {
tableSpec := parseTempTableSpec(spec)
pgParts := []string{}
for i, valueColumnName := range tableSpec.valueColumnNames {
valuePgType := tableSpec.valuePgTypes[i]
valuePart := fmt.Sprintf("%s %s NOT NULL", valueColumnName, valuePgType)
pgParts = append(pgParts, valuePart)
}
pgPlaceholders := []string{}
i := 1
for range tableSpec.valueColumnNames {
pgPlaceholders = append(pgPlaceholders, fmt.Sprintf("$%d", i))
i += 1
}
RaisePgResult(tx.Exec(ctx, fmt.Sprintf(
`
CREATE TEMPORARY TABLE %s (
%s
)
ON COMMIT DROP
`,
tableSpec.tableName,
strings.Join(pgParts, ", "),
)))
BatchInTx(ctx, tx, func(batch PgBatch) {
for _, value := range values {
pgValues := []any{}
pgValues = expandValue(value, pgValues)
if len(pgValues) != len(pgPlaceholders) {
panic(fmt.Errorf("Expected %d values but found %d.", len(pgPlaceholders), len(pgValues)))
}
batch.Queue(
fmt.Sprintf(
`
INSERT INTO %s (%s) VALUES (%s)
`,
tableSpec.tableName,
strings.Join(tableSpec.valueColumnNames, ", "),
strings.Join(pgPlaceholders, ", "),
),
pgValues...,
)
}
})
}
// many to one join table
// spec is `table_name(key_column_name type[, ...] -> value_column_name type[, ...])`
func CreateTempJoinTableInTx[K comparable, V any](ctx context.Context, tx PgTx, spec string, values map[K]V) {
tableSpec := parseTempJoinTableSpec(spec)
pgParts := []string{}
for i, keyColumnName := range tableSpec.keyColumnNames {
keyPgType := tableSpec.keyPgTypes[i]
keyPart := fmt.Sprintf("%s %s NOT NULL", keyColumnName, keyPgType)
pgParts = append(pgParts, keyPart)
}
for i, valueColumnName := range tableSpec.valueColumnNames {
valuePgType := tableSpec.valuePgTypes[i]
nullable := "NOT NULL"
if tableSpec.valueNullables[i] {
nullable = "NULL"
}
valuePart := fmt.Sprintf("%s %s %s", valueColumnName, valuePgType, nullable)
pgParts = append(pgParts, valuePart)
}
columnNames := []string{}
columnNames = append(columnNames, tableSpec.keyColumnNames...)
columnNames = append(columnNames, tableSpec.valueColumnNames...)
pgPlaceholders := []string{}
i := 1
for range tableSpec.keyColumnNames {
pgPlaceholders = append(pgPlaceholders, fmt.Sprintf("$%d", i))
i += 1
}
for range tableSpec.valueColumnNames {
pgPlaceholders = append(pgPlaceholders, fmt.Sprintf("$%d", i))
i += 1
}
RaisePgResult(tx.Exec(ctx, fmt.Sprintf(
`
CREATE TEMPORARY TABLE %s (
%s,
PRIMARY KEY (%s)
)
ON COMMIT DROP
`,
tableSpec.tableName,
strings.Join(pgParts, ", "),
strings.Join(tableSpec.keyColumnNames, ", "),
)))
BatchInTx(ctx, tx, func(batch PgBatch) {
for key, value := range values {
pgValues := []any{}
pgValues = expandValue(key, pgValues)
pgValues = expandValue(value, pgValues)
if len(pgValues) != len(pgPlaceholders) {
panic(fmt.Errorf("Expected %d values but found %d.", len(pgPlaceholders), len(pgValues)))
}
batch.Queue(
fmt.Sprintf(
`
INSERT INTO %s (%s) VALUES (%s)
ON CONFLICT DO NOTHING
`,
tableSpec.tableName,
strings.Join(columnNames, ", "),
strings.Join(pgPlaceholders, ", "),
),
pgValues...,
)
}
})
}
func expandValue[T any](value T, out []any) []any {
if v, ok := any(value).(ComplexValue); ok {
out = append(out, v.Values()...)
// value may be a struct, `&value` will convert it to an interface type
} else if v, ok := any(&value).(ComplexValue); ok {
out = append(out, v.Values()...)
} else {
out = append(out, value)
}
return out
}
type TempTableSpec struct {
tableName string
valueColumnNames []string
valuePgTypes []string
}
// spec is `table_name(value_column_name type)`
func parseTempTableSpec(spec string) *TempTableSpec {
re := regexp.MustCompile("(?s)^\\s*(\\w+)\\s*\\((.*)\\)")
groups := re.FindStringSubmatch(spec)
if groups == nil {
panic(errors.New(fmt.Sprintf("Bad spec: %s", spec)))
}
valueColumnNames, valuePgTypes, _ := parseSpec(groups[2])
return &TempTableSpec{
tableName: groups[1],
valueColumnNames: valueColumnNames,
valuePgTypes: valuePgTypes,
}
}
type TempJoinTableSpec struct {
tableName string
keyColumnNames []string
keyPgTypes []string
valueColumnNames []string
valuePgTypes []string
valueNullables []bool
}
// spec is `table_name(key_column_name type[, ...] -> value_column_name type[, ...])`
func parseTempJoinTableSpec(spec string) *TempJoinTableSpec {
re := regexp.MustCompile("(?s)^\\s*(\\w+)\\s*\\((.*)\\s*->\\s*(.*)\\)")
groups := re.FindStringSubmatch(spec)
if groups == nil {
panic(errors.New(fmt.Sprintf("Bad spec: %s", spec)))
}
keyColumnNames, keyPgTypes, _ := parseSpec(groups[2])
valueColumnNames, valuePgTypes, valueNullables := parseSpec(groups[3])
return &TempJoinTableSpec{
tableName: groups[1],
keyColumnNames: keyColumnNames,
keyPgTypes: keyPgTypes,
valueColumnNames: valueColumnNames,
valuePgTypes: valuePgTypes,
valueNullables: valueNullables,
}
}
func parseSpec(spec string) (columnNames []string, pgTypes []string, nullables []bool) {
re := regexp.MustCompile("(?is)^\\s*(\\w+)\\s+([^,]+?)(\\s+NULL)?\\s*(?:,|$)")
for {
groups := re.FindStringSubmatch(spec)
if groups == nil {
break
}
columnNames = append(columnNames, strings.TrimSpace(groups[1]))
pgTypes = append(pgTypes, strings.TrimSpace(groups[2]))
nullables = append(nullables, strings.TrimSpace(groups[3]) != "")
spec = spec[len(groups[0]):]
}
return
}