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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE QuasiQuotes #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Postgres.MonadPostgres where

import Arg
import AtLeast (AtLeast)
import Control.Exception
  ( Exception (displayException),
    Handler (Handler),
    catches,
    try,
  )
import Control.Foldl qualified as Fold
import Control.Monad.Logger.CallStack (MonadLogger, logDebug, logWarn)
import Control.Monad.Reader (MonadReader (ask), ReaderT (..))
import Control.Monad.Trans.Resource
import Data.Aeson (FromJSON)
import Data.ByteString qualified as ByteString
import Data.Error.Tree
import Data.HashMap.Strict qualified as HashMap
import Data.Int (Int64)
import Data.List qualified as List
import Data.Pool (Pool)
import Data.Pool qualified as Pool
import Data.Text qualified as Text
import Data.Typeable (Typeable)
import Database.PostgreSQL.Simple (Connection, FormatError, FromRow, Query, QueryError, ResultError, SqlError, ToRow)
import Database.PostgreSQL.Simple qualified as PG
import Database.PostgreSQL.Simple qualified as Postgres
import Database.PostgreSQL.Simple.FromRow qualified as PG
import Database.PostgreSQL.Simple.ToField (ToField)
import Database.PostgreSQL.Simple.ToRow (ToRow (toRow))
import Database.PostgreSQL.Simple.Types (PGArray (PGArray), Query (..))
import GHC.IO.Handle (Handle)
import GHC.Records (getField)
import Label
import Language.Haskell.TH.Quote (QuasiQuoter)
import MyPrelude
import OpenTelemetry.Trace.Core (NewEvent (newEventName))
import OpenTelemetry.Trace.Core qualified as Otel hiding (inSpan, inSpan')
import OpenTelemetry.Trace.Monad qualified as Otel
import Postgres.Decoder
import Postgres.Decoder qualified as Dec
import Pretty (showPretty)
import PyF qualified
import Seconds
import System.Exit (ExitCode (..))
import Tool
import UnliftIO (MonadUnliftIO (withRunInIO), bracket, hClose, mask_)
import UnliftIO.Concurrent (forkIO)
import UnliftIO.Process (ProcessHandle)
import UnliftIO.Process qualified as Process
import UnliftIO.Resource qualified as Resource
import Prelude hiding (init, span)

-- | Postgres queries/commands that can be executed within a running transaction.
--
-- These are implemented with the @postgresql-simple@ primitives of the same name
-- and will behave the same unless othewise documented.
class (Monad m) => MonadPostgres (m :: Type -> Type) where
  -- | Execute an INSERT, UPDATE, or other SQL query that is not expected to return results.

  -- Returns the number of rows affected.
  execute ::
    (ToRow params, Typeable params) =>
    Query ->
    params ->
    Transaction m (Label "numberOfRowsAffected" Natural)

  -- | Execute a multi-row INSERT, UPDATE, or other SQL query that is not expected to return results.
  --
  -- Returns the number of rows affected. If the list of parameters is empty,
  -- this function will simply return 0 without issuing the query to the backend.
  -- If this is not desired, consider using the 'PG.Values' constructor instead.
  executeMany ::
    (ToRow params, Typeable params) =>
    Query ->
    NonEmpty params ->
    Transaction m (Label "numberOfRowsAffected" Natural)

  -- | Execute INSERT ... RETURNING, UPDATE ... RETURNING,
  -- or other SQL query that accepts multi-row input and is expected to return results.
  -- Note that it is possible to write query conn "INSERT ... RETURNING ..." ...
  -- in cases where you are only inserting a single row,
  -- and do not need functionality analogous to 'executeMany'.
  --
  -- If the list of parameters is empty, this function will simply return [] without issuing the query to the backend. If this is not desired, consider using the 'PG.Values' constructor instead.
  executeManyReturningWith :: (ToRow q) => Query -> NonEmpty q -> Decoder r -> Transaction m [r]

  -- | Run a query, passing parameters and result row parser.
  queryWith ::
    (PG.ToRow params, Typeable params, Typeable r) =>
    PG.Query ->
    params ->
    Decoder r ->
    Transaction m [r]

  -- | Run a query without any parameters and result row parser.
  queryWith_ ::
    (Typeable r) =>
    PG.Query ->
    Decoder r ->
    Transaction m [r]

  -- | Run a query, passing parameters, and fold over the resulting rows.
  --
  -- This doesn’t have to realize the full list of results in memory,
  -- rather results are streamed incrementally from the database.
  --
  -- When dealing with small results, it may be simpler (and perhaps faster) to use query instead.
  --
  -- This fold is _not_ strict. The stream consumer is responsible
  -- for forcing the evaluation of its result to avoid space leaks.
  --
  -- If you can, prefer aggregating in the database itself.
  foldRowsWithAcc ::
    (ToRow params, Typeable row, Typeable params) =>
    Query ->
    params ->
    Decoder row ->
    a ->
    (a -> row -> Transaction m a) ->
    Transaction m a

  -- | Run a given transaction in a transaction block, rolling back the transaction
  -- if any exception (postgres or Haskell Exception) is thrown during execution.
  --
  -- Re-throws the exception.
  --
  -- Don’t do any long-running things on the Haskell side during a transaction,
  -- because it will block a database connection and potentially also lock
  -- database tables from being written or read by other clients.
  --
  -- Nonetheless, try to push transactions as far out to the handlers as possible,
  -- don’t do something like @runTransaction $ query …@, because it will lead people
  -- to accidentally start nested transactions (the inner transaction is run on a new connections,
  -- thus can’t see any changes done by the outer transaction).
  -- Only handlers should run transactions.
  runTransaction :: Transaction m a -> m a

-- | Quasi-Quoter for multi-line SQL literals. Trims leading whitespace up to the least-indented line.
sql :: QuasiQuoter
sql = PyF.fmtTrim

-- | Run a query, passing parameters. Prefer 'queryWith' if possible.
query ::
  forall m params r.
  (PG.ToRow params, PG.FromRow r, Typeable params, Typeable r, MonadPostgres m) =>
  PG.Query ->
  params ->
  Transaction m [r]
query qry params = queryWith qry params (Decoder PG.fromRow)

-- | Run a query without any parameters. Prefer 'queryWith' if possible.
--
-- TODO: I think(?) this can always be replaced by passing @()@ to 'query', remove?
query_ ::
  forall m r.
  (Typeable r, PG.FromRow r, MonadPostgres m) =>
  PG.Query ->
  Transaction m [r]
query_ qry = queryWith_ qry (Decoder PG.fromRow)

-- TODO: implement via fold, so that the result doesn’t have to be realized in memory
querySingleRow ::
  ( MonadPostgres m,
    ToRow qParams,
    Typeable qParams,
    FromRow a,
    Typeable a,
    MonadThrow m
  ) =>
  Query ->
  qParams ->
  Transaction m a
querySingleRow qry params = do
  query qry params >>= ensureSingleRow

-- TODO: implement via fold, so that the result doesn’t have to be realized in memory
querySingleRowWith ::
  ( MonadPostgres m,
    ToRow qParams,
    Typeable qParams,
    Typeable a,
    MonadThrow m
  ) =>
  Query ->
  qParams ->
  Decoder a ->
  Transaction m a
querySingleRowWith qry params decoder = do
  queryWith qry params decoder >>= ensureSingleRow

-- | Return the first row, if any.
queryFirstRowWithMaybe ::
  ( MonadPostgres m,
    ToRow qParams,
    Typeable qParams,
    Typeable a
  ) =>
  Query ->
  qParams ->
  Decoder a ->
  Transaction m (Maybe a)
queryFirstRowWithMaybe qry params decoder = do
  queryWith qry params decoder >>= \case
    [] -> pure Nothing
    (one : _) -> pure $ Just one

-- TODO: implement via fold, so that the result doesn’t have to be realized in memory
querySingleRowMaybe ::
  ( MonadPostgres m,
    ToRow qParams,
    Typeable qParams,
    FromRow a,
    Typeable a,
    MonadThrow m
  ) =>
  Query ->
  qParams ->
  Transaction m (Maybe a)
querySingleRowMaybe qry params = do
  rows <- query qry params
  case rows of
    [] -> pure Nothing
    [one] -> pure (Just one)
    -- TODO: Should we MonadThrow this here? It’s really an implementation detail of MonadPostgres
    -- that a database function can error out, should probably handled by the instances.
    more -> throwM $ SingleRowError {numberOfRowsReturned = (List.length more)}

ensureSingleRow ::
  (MonadThrow m) =>
  [a] ->
  m a
ensureSingleRow = \case
  -- TODO: Should we MonadThrow this here? It’s really an implementation detail of MonadPostgres
  -- that a database function can error out, should probably handled by the instances.
  [] -> throwM (SingleRowError {numberOfRowsReturned = 0})
  [one] -> pure one
  more ->
    throwM $
      SingleRowError
        { numberOfRowsReturned =
            -- TODO: this is VERY bad, because it requires to parse the full database output, even if there’s 10000000000 elements
            List.length more
        }

ensureNoneOrSingleRow ::
  (MonadThrow m) =>
  [a] ->
  m (Maybe a)
ensureNoneOrSingleRow = \case
  -- TODO: Should we MonadThrow this here? It’s really an implementation detail of MonadPostgres
  -- that a database function can error out, should probably handled by the instances.
  [] -> pure Nothing
  [one] -> pure $ Just one
  more ->
    throwM $
      SingleRowError
        { numberOfRowsReturned =
            -- TODO: this is VERY bad, because it requires to parse the full database output, even if there’s 10000000000 elements
            List.length more
        }

-- | Run a query, passing parameters, and fold over the resulting rows.
--
-- This doesn’t have to realize the full list of results in memory,
-- rather results are streamed incrementally from the database.
--
-- When dealing with small results, it may be simpler (and perhaps faster) to use query instead.
--
-- The results are folded strictly into the Monoid returned by the decoder.
--
-- If you need more complex folding logic, use 'foldRowsWith' with a 'Fold'.
--
-- If you can, prefer aggregating in the database itself.
foldRowsWithMonoid ::
  forall row params m.
  ( MonadPostgres m,
    PG.ToRow params,
    Typeable row,
    Typeable params,
    Monoid row
  ) =>
  PG.Query ->
  params ->
  Decoder row ->
  Transaction m row
foldRowsWithMonoid qry params decoder = foldRowsWith qry params decoder Fold.mconcat

-- | Run a query, passing parameters, and fold over the resulting rows.
--
-- This doesn’t have to realize the full list of results in memory,
-- rather results are streamed incrementally from the database.
--
-- When dealing with small results, it may be simpler (and perhaps faster) to use query instead.
--
-- The results are folded strictly by the 'Fold.Fold' that is passed.
--
-- If you can, prefer aggregating in the database itself.
foldRowsWith ::
  forall row params m b.
  ( MonadPostgres m,
    PG.ToRow params,
    Typeable row,
    Typeable params
  ) =>
  PG.Query ->
  params ->
  Decoder row ->
  Fold.Fold row b ->
  Transaction m b
foldRowsWith qry params decoder = Fold.purely f
  where
    f :: forall x. (x -> row -> x) -> x -> (x -> b) -> Transaction m b
    f acc init extract = do
      x <- foldRowsWithAcc qry params decoder init (\a r -> pure $ acc a r)
      pure $ extract x

newtype Transaction m a = Transaction {unTransaction :: (ReaderT Connection m a)}
  deriving newtype
    ( Functor,
      Applicative,
      Monad,
      MonadThrow,
      MonadLogger,
      MonadIO,
      MonadUnliftIO,
      MonadTrans,
      Otel.MonadTracer
    )

-- | [Resource Pool](http://hackage.haskell.org/package/resource-pool-0.2.3.2/docs/Data-Pool.html) configuration.
data PoolingInfo = PoolingInfo
  { -- | Minimal amount of resources that are
    --   always available.
    numberOfStripes :: AtLeast 1 Int,
    -- | Time after which extra resources
    --   (above minimum) can stay in the pool
    --   without being used.
    unusedResourceOpenTime :: Seconds,
    -- | Max number of resources that can be
    --   in the Pool at any time
    maxOpenResourcesAcrossAllStripes :: AtLeast 1 Int
  }
  deriving stock (Generic, Eq, Show)
  deriving anyclass (FromJSON)

initMonadPostgres ::
  (Text -> IO ()) ->
  -- | Info describing the connection to the Postgres DB
  Postgres.ConnectInfo ->
  -- | Configuration info for pooling attributes
  PoolingInfo ->
  -- | Created Postgres connection pool
  ResourceT IO (Pool Postgres.Connection)
initMonadPostgres logInfoFn connectInfo poolingInfo = do
  (_releaseKey, connPool) <-
    Resource.allocate
      (logInfoFn "Creating Postgres Connection Pool" >> createPGConnPool)
      (\pool -> logInfoFn "Destroying Postgres Connection Pool" >> destroyPGConnPool pool)
  pure connPool
  where
    -- \| Create a Postgres connection pool
    createPGConnPool ::
      IO (Pool Postgres.Connection)
    createPGConnPool =
      Pool.newPool $
        Pool.defaultPoolConfig
          {- resource init action -} poolCreateResource
          {- resource destruction -} poolfreeResource
          ( poolingInfo.unusedResourceOpenTime.unSeconds
              & fromIntegral @Natural @Double
          )
          (poolingInfo.maxOpenResourcesAcrossAllStripes.unAtLeast)
      where
        poolCreateResource = Postgres.connect connectInfo
        poolfreeResource = Postgres.close

    -- \| Destroy a Postgres connection pool
    destroyPGConnPool ::
      -- \| Pool to be destroyed
      (Pool Postgres.Connection) ->
      IO ()
    destroyPGConnPool p = Pool.destroyAllResources p

-- | Improve a possible error message, by adding some context to it.
--
-- The given Exception type is caught, 'show'n and pretty-printed.
--
-- In case we get an `IOError`, we display it in a reasonable fashion.
addErrorInformation ::
  forall exc a.
  (Exception exc) =>
  Text.Text ->
  IO a ->
  IO a
addErrorInformation msg io =
  io
    & try @exc
    <&> first (showPretty >>> newError >>> errorContext msg)
    & try @IOError
    <&> first (showToError >>> errorContext "IOError" >>> errorContext msg)
    <&> join @(Either Error)
    >>= unwrapIOError

-- | Catch any Postgres exception that gets thrown,
-- print the query that was run and the query parameters,
-- then rethrow inside an 'Error'.
handlePGException ::
  forall a params m.
  ( ToRow params,
    MonadUnliftIO m,
    MonadLogger m
  ) =>
  PrettyPrintDatabaseQueries ->
  Text ->
  Query ->
  -- | Depending on whether we used `format` or `formatMany`.
  Either params (NonEmpty params) ->
  IO a ->
  Transaction m a
handlePGException prettyQuery queryType query' params io = do
  withRunInIO $ \unliftIO ->
    io
      `catches` [ Handler $ unliftIO . logQueryException @SqlError,
                  Handler $ unliftIO . logQueryException @QueryError,
                  Handler $ unliftIO . logQueryException @ResultError,
                  Handler $ unliftIO . logFormatException
                ]
  where
    -- TODO: use throwInternalError here (after pulling it into the MonadPostgres class)
    throwAsError = unwrapIOError . Left . newError
    throwErr err = liftIO $ throwAsError $ prettyErrorTree $ nestedMultiError "A Postgres query failed" err
    logQueryException :: (Exception e) => e -> Transaction m a
    logQueryException exc = do
      formattedQuery <-
        case params of
          Left one -> pgFormatQuery' prettyQuery query' one
          Right many -> pgFormatQueryMany' prettyQuery query' many
      throwErr
        ( singleError [fmt|Query Type: {queryType}|]
            :| [ nestedError "Exception" (exc & showPretty & newError & singleError),
                 nestedError "Query" (formattedQuery & bytesToTextUtf8Lenient & newError & singleError)
               ]
        )
    logFormatException :: FormatError -> Transaction m a
    logFormatException fe = throwErr (fe & showPretty & newError & singleError & singleton)

-- | Perform a Postgres action within a transaction
withPGTransaction ::
  -- | Postgres connection pool to be used for the action
  (Pool Postgres.Connection) ->
  -- | DB-action to be performed
  (Postgres.Connection -> IO a) ->
  -- | Result of the DB-action
  IO a
withPGTransaction connPool f =
  Pool.withResource
    connPool
    (\conn -> Postgres.withTransaction conn (f conn))

-- | `pg_formatter` is a perl script that does not support any kind of streaming.
-- Thus we initialize a pool with a bunch of these scripts running, waiting for input. This way we can have somewhat fast SQL formatting.
--
-- Call `initPgFormatPool` to initialize, then use `runPgFormat` to format some sql.
data PgFormatPool = PgFormatPool
  { pool :: Pool PgFormatProcess,
    pgFormat :: Tool
  }

data PgFormatProcess = PgFormatProcess
  { stdinHdl :: Handle,
    stdoutHdl :: Handle,
    stderrHdl :: Handle,
    procHdl :: ProcessHandle,
    startedAt :: Otel.Timestamp
  }

initPgFormatPool :: (HasField "pgFormat" tools Tool) => tools -> IO PgFormatPool
initPgFormatPool tools = do
  pool <-
    Pool.newPool
      ( Pool.defaultPoolConfig
          (pgFormatStartCommandWaitForInput tools)
          ( \pgFmt -> do
              Process.terminateProcess pgFmt.procHdl
              -- make sure we don’t leave any zombies
              _ <- forkIO $ do
                _ <- Process.waitForProcess pgFmt.procHdl
                pure ()
              pure ()
          )
          -- unused resource time
          100
          -- number of resources
          10
      )

  -- fill the pool with resources
  let go =
        Pool.tryWithResource pool (\_ -> go) >>= \case
          Nothing -> pure ()
          Just () -> pure ()
  _ <- go
  pure (PgFormatPool {pool, pgFormat = tools.pgFormat})

destroyPgFormatPool :: PgFormatPool -> IO ()
destroyPgFormatPool pool = Pool.destroyAllResources pool.pool

-- | Get the oldest resource from the pool, or stop if you find a resource that’s older than `cutoffPointMs`.
takeOldestResource :: PgFormatPool -> Arg "cutoffPointMs" Integer -> IO (PgFormatProcess, Pool.LocalPool PgFormatProcess)
takeOldestResource pool cutoffPointMs = do
  now <- Otel.getTimestamp
  mask_ $ do
    a <- Pool.takeResource pool.pool
    (putBack, res) <- go now [] a
    -- make sure we don’t leak any resources we didn’t use in the end
    for_ putBack $ \(x, xLocal) -> Pool.putResource xLocal x
    pure res
  where
    mkMs ts = (ts & Otel.timestampNanoseconds & toInteger) `div` 1000_000
    go now putBack a@(a', _) =
      if abs (mkMs now - mkMs a'.startedAt) > cutoffPointMs.unArg
        then pure (putBack, a)
        else
          Pool.tryTakeResource pool.pool >>= \case
            Nothing -> pure (putBack, a)
            Just b@(b', _) -> do
              if a'.startedAt < b'.startedAt
                then go now (b : putBack) a
                else go now (a : putBack) b

-- | Format the given SQL with pg_formatter. Will use the pool of already running formatters to speed up execution.
runPgFormat :: PgFormatPool -> ByteString -> IO (T3 "exitCode" ExitCode "formatted" ByteString "stderr" ByteString)
runPgFormat pool sqlStatement = do
  bracket
    (takeOldestResource pool 200)
    ( \(a, localPool) -> do
        -- we always destroy the resource, because the process exited
        Pool.destroyResource pool.pool localPool a
        -- create a new process to keep the pool “warm”
        new <- pgFormatStartCommandWaitForInput pool
        Pool.putResource localPool new
    )
    ( \(pgFmt, _localPool) -> do
        ByteString.hPut pgFmt.stdinHdl sqlStatement
        -- close stdin to make pg_formatter format (it exits …)
        -- issue: https://github.com/darold/pgFormatter/issues/333
        hClose pgFmt.stdinHdl
        formatted <- ByteString.hGetContents pgFmt.stdoutHdl
        errs <- ByteString.hGetContents pgFmt.stderrHdl
        exitCode <- Process.waitForProcess pgFmt.procHdl
        pure $
          T3
            (label @"exitCode" exitCode)
            (label @"formatted" formatted)
            (label @"stderr" errs)
    )

runPGTransactionImpl ::
  (MonadUnliftIO m) =>
  m (Pool Postgres.Connection) ->
  Transaction m a ->
  m a
{-# INLINE runPGTransactionImpl #-}
runPGTransactionImpl zoom (Transaction transaction) = do
  pool <- zoom
  withRunInIO $ \unliftIO ->
    withPGTransaction pool $ \conn -> do
      unliftIO $ runReaderT transaction conn

executeImpl ::
  (ToRow params, MonadUnliftIO m, MonadLogger m, Otel.MonadTracer m) =>
  m (DebugLogDatabaseQueries, PrettyPrintDatabaseQueries) ->
  Query ->
  params ->
  Transaction m (Label "numberOfRowsAffected" Natural)
{-# INLINE executeImpl #-}
executeImpl zoomDbOptions qry params =
  Otel.inSpan' "Postgres Query (execute)" Otel.defaultSpanArguments $ \span -> do
    (logDatabaseQueries, prettyQuery) <- lift @Transaction zoomDbOptions
    traceQueryIfEnabled span logDatabaseQueries prettyQuery qry (HasSingleParam params)
    conn <- Transaction ask
    PG.execute conn qry params
      & handlePGException prettyQuery "execute" qry (Left params)
      >>= toNumberOfRowsAffected "executeImpl"

executeImpl_ ::
  ( MonadUnliftIO m,
    MonadLogger m,
    Otel.MonadTracer m
  ) =>
  m (DebugLogDatabaseQueries, PrettyPrintDatabaseQueries) ->
  Query ->
  Transaction m (Label "numberOfRowsAffected" Natural)
{-# INLINE executeImpl_ #-}
executeImpl_ zoomDbOptions qry =
  Otel.inSpan' "Postgres Query (execute)" Otel.defaultSpanArguments $ \span -> do
    (logDatabaseQueries, prettyQuery) <- lift @Transaction zoomDbOptions
    traceQueryIfEnabled @() span logDatabaseQueries prettyQuery qry HasNoParams
    conn <- Transaction ask
    PG.execute_ conn qry
      & handlePGException prettyQuery "execute_" qry (Left ())
      >>= toNumberOfRowsAffected "executeImpl_"

executeManyImpl ::
  (ToRow params, MonadUnliftIO m, MonadLogger m, Otel.MonadTracer m) =>
  m (DebugLogDatabaseQueries, PrettyPrintDatabaseQueries) ->
  Query ->
  NonEmpty params ->
  Transaction m (Label "numberOfRowsAffected" Natural)
executeManyImpl zoomDbOptions qry params =
  Otel.inSpan' "Postgres Query (executeMany)" Otel.defaultSpanArguments $ \span -> do
    (logDatabaseQueries, prettyQuery) <- lift @Transaction zoomDbOptions
    traceQueryIfEnabled span logDatabaseQueries prettyQuery qry (HasMultiParams params)
    conn <- Transaction ask
    PG.executeMany conn qry (params & toList)
      & handlePGException prettyQuery "executeMany" qry (Right params)
      >>= toNumberOfRowsAffected "executeManyImpl"

toNumberOfRowsAffected :: (MonadIO m) => Text -> Int64 -> m (Label "numberOfRowsAffected" Natural)
toNumberOfRowsAffected functionName i64 =
  i64
    & intToNatural
    & annotate [fmt|{functionName}: postgres returned a negative number of rows affected: {i64}|]
    -- we throw this directly in IO here, because we don’t want to e.g. have to propagate MonadThrow through user code (it’s an assertion)
    & unwrapIOError
    & liftIO
    <&> label @"numberOfRowsAffected"

executeManyReturningWithImpl ::
  ( ToRow params,
    MonadUnliftIO m,
    MonadLogger m,
    Otel.MonadTracer m
  ) =>
  m (DebugLogDatabaseQueries, PrettyPrintDatabaseQueries) ->
  Query ->
  NonEmpty params ->
  Decoder r ->
  Transaction m [r]
{-# INLINE executeManyReturningWithImpl #-}
executeManyReturningWithImpl zoomDbOptions qry params (Decoder fromRow) = do
  Otel.inSpan' "Postgres Query (executeManyReturning)" Otel.defaultSpanArguments $ \span -> do
    (logDatabaseQueries, prettyQuery) <- lift @Transaction zoomDbOptions
    traceQueryIfEnabled span logDatabaseQueries prettyQuery qry (HasMultiParams params)
    conn <- Transaction ask
    PG.returningWith fromRow conn qry (params & toList)
      & handlePGException prettyQuery "executeManyReturning" qry (Right params)

foldRowsWithAccImpl ::
  ( ToRow params,
    MonadUnliftIO m,
    MonadLogger m,
    Otel.MonadTracer m
  ) =>
  m (DebugLogDatabaseQueries, PrettyPrintDatabaseQueries) ->
  Query ->
  params ->
  Decoder row ->
  a ->
  (a -> row -> Transaction m a) ->
  Transaction m a
{-# INLINE foldRowsWithAccImpl #-}
foldRowsWithAccImpl zoomDbOptions qry params (Decoder rowParser) accumulator f = do
  Otel.inSpan' "Postgres Query (foldRowsWithAcc)" Otel.defaultSpanArguments $ \span -> do
    (logDatabaseQueries, prettyQuery) <- lift @Transaction zoomDbOptions
    traceQueryIfEnabled span logDatabaseQueries prettyQuery qry (HasSingleParam params)
    conn <- Transaction ask
    withRunInIO
      ( \runInIO ->
          do
            PG.foldWithOptionsAndParser
              PG.defaultFoldOptions
              rowParser
              conn
              qry
              params
              accumulator
              (\acc row -> runInIO $ f acc row)
              & handlePGException prettyQuery "fold" qry (Left params)
              & runInIO
      )

pgFormatQueryNoParams' ::
  (MonadIO m, MonadLogger m) =>
  PrettyPrintDatabaseQueries ->
  Query ->
  Transaction m ByteString
pgFormatQueryNoParams' prettyQuery q = case prettyQuery of
  DontPrettyPrintDatabaseQueries -> pure q.fromQuery
  PrettyPrintDatabaseQueries pool -> lift $ pgFormatQueryByteString pool q.fromQuery

pgFormatQuery ::
  (ToRow params, MonadIO m) =>
  Query ->
  params ->
  Transaction m ByteString
pgFormatQuery qry params = Transaction $ do
  conn <- ask
  liftIO $ PG.formatQuery conn qry params

pgFormatQueryMany ::
  (MonadIO m, ToRow params) =>
  Query ->
  NonEmpty params ->
  Transaction m ByteString
pgFormatQueryMany qry params = Transaction $ do
  conn <- ask
  liftIO $
    PG.formatMany
      conn
      qry
      ( params
          -- upstream is partial on empty list, see https://github.com/haskellari/postgresql-simple/issues/129
          & toList
      )

queryWithImpl ::
  ( ToRow params,
    MonadUnliftIO m,
    MonadLogger m,
    Otel.MonadTracer m
  ) =>
  m (DebugLogDatabaseQueries, PrettyPrintDatabaseQueries) ->
  Query ->
  params ->
  Decoder r ->
  Transaction m [r]
{-# INLINE queryWithImpl #-}
queryWithImpl zoomDbOptions qry params (Decoder fromRow) = do
  Otel.inSpan' "Postgres Query (queryWith)" Otel.defaultSpanArguments $ \span -> do
    (logDatabaseQueries, prettyQuery) <- lift @Transaction zoomDbOptions
    traceQueryIfEnabled span logDatabaseQueries prettyQuery qry (HasSingleParam params)
    conn <- Transaction ask
    PG.queryWith fromRow conn qry params
      & handlePGException prettyQuery "query" qry (Left params)

queryWithImpl_ ::
  ( MonadUnliftIO m,
    MonadLogger m
  ) =>
  m PrettyPrintDatabaseQueries ->
  Query ->
  Decoder r ->
  Transaction m [r]
{-# INLINE queryWithImpl_ #-}
queryWithImpl_ zoomDbOptions qry (Decoder fromRow) = do
  prettyQuery <- lift @Transaction zoomDbOptions
  conn <- Transaction ask
  liftIO (PG.queryWith_ fromRow conn qry)
    & handlePGException prettyQuery "query" qry (Left ())

data SingleRowError = SingleRowError
  { -- | How many columns were actually returned by the query
    numberOfRowsReturned :: Int
  }
  deriving stock (Show)

instance Exception SingleRowError where
  displayException (SingleRowError {..}) = [fmt|Single row expected from SQL query result, {numberOfRowsReturned} rows were returned instead."|]

pgFormatQuery' ::
  ( MonadIO m,
    ToRow params,
    MonadLogger m
  ) =>
  PrettyPrintDatabaseQueries ->
  Query ->
  params ->
  Transaction m ByteString
pgFormatQuery' prettyQuery q p = case prettyQuery of
  DontPrettyPrintDatabaseQueries -> pgFormatQuery q p
  PrettyPrintDatabaseQueries pool ->
    pgFormatQuery q p
      >>= lift . pgFormatQueryByteString pool

pgFormatQueryMany' ::
  ( MonadIO m,
    ToRow params,
    MonadLogger m
  ) =>
  PrettyPrintDatabaseQueries ->
  Query ->
  NonEmpty params ->
  Transaction m ByteString
pgFormatQueryMany' prettyQuery q p = case prettyQuery of
  DontPrettyPrintDatabaseQueries -> pgFormatQueryMany q p
  PrettyPrintDatabaseQueries pool ->
    pgFormatQueryMany q p
      >>= lift . pgFormatQueryByteString pool

-- | Read the executable name "pg_format"
postgresToolsParser :: ToolParserT IO (Label "pgFormat" Tool)
postgresToolsParser = label @"pgFormat" <$> readTool "pg_format"

pgFormatQueryByteString ::
  ( MonadIO m,
    MonadLogger m
  ) =>
  PgFormatPool ->
  ByteString ->
  m ByteString
pgFormatQueryByteString pool queryBytes = do
  res <-
    liftIO $
      runPgFormat
        pool
        (queryBytes)
  case res.exitCode of
    ExitSuccess -> pure (res.formatted)
    ExitFailure status -> do
      logWarn [fmt|pg_format failed with status {status} while formatting the query, using original query string. Is there a syntax error?|]
      logDebug
        ( prettyErrorTree
            ( nestedMultiError
                "pg_format output"
                ( nestedError "stdout" (singleError (res.formatted & bytesToTextUtf8Lenient & newError))
                    :| [(nestedError "stderr" (singleError (res.stderr & bytesToTextUtf8Lenient & newError)))]
                )
            )
        )
      logDebug [fmt|pg_format stdout: stderr|]
      pure (queryBytes)

pgFormatStartCommandWaitForInput ::
  ( MonadIO m,
    HasField "pgFormat" tools Tool,
    MonadFail m
  ) =>
  tools ->
  m PgFormatProcess
pgFormatStartCommandWaitForInput tools = do
  do
    startedAt <- Otel.getTimestamp
    (Just stdinHdl, Just stdoutHdl, Just stderrHdl, procHdl) <-
      Process.createProcess
        ( ( Process.proc
              tools.pgFormat.toolPath
              [ "--no-rcfile",
                "-"
              ]
          )
            { Process.std_in = Process.CreatePipe,
              Process.std_out = Process.CreatePipe,
              Process.std_err = Process.CreatePipe
            }
        )

    pure PgFormatProcess {..}

data DebugLogDatabaseQueries
  = -- | Do not log the database queries
    DontLogDatabaseQueries
  | -- | Log the database queries as debug output;
    LogDatabaseQueries
  | -- | Log the database queries as debug output and additionally the EXPLAIN output (from the query analyzer, not the actual values after execution cause that’s a bit harder to do)
    LogDatabaseQueriesAndExplain
  deriving stock (Show, Enum, Bounded)

-- | Whether to pipe database queries thru `pg_format` before logging them. This takes a long (long! 200ms+) time per query, so should only be used in debugging environments where speed is not an issue.
data PrettyPrintDatabaseQueries
  = -- | Do not pretty-print database querios
    DontPrettyPrintDatabaseQueries
  | -- | Pretty-print database queries, slow
    PrettyPrintDatabaseQueries PgFormatPool

instance Show PrettyPrintDatabaseQueries where
  show DontPrettyPrintDatabaseQueries = "DontPrettyPrintDatabaseQueries"
  show (PrettyPrintDatabaseQueries _) = "PrettyPrintDatabaseQueries"

data HasQueryParams param
  = HasNoParams
  | HasSingleParam param
  | HasMultiParams (NonEmpty param)

-- | Log the postgres query depending on the given setting
traceQueryIfEnabled ::
  ( ToRow params,
    MonadUnliftIO m,
    MonadLogger m,
    Otel.MonadTracer m
  ) =>
  Otel.Span ->
  DebugLogDatabaseQueries ->
  PrettyPrintDatabaseQueries ->
  Query ->
  HasQueryParams params ->
  Transaction m ()
traceQueryIfEnabled span logDatabaseQueries prettyQuery qry params = do
  -- In case we have query logging enabled, we want to do that
  let formattedQuery =
        withEvent
          span
          "Query Format start"
          "Query Format end"
          $ case params of
            HasNoParams -> pgFormatQueryNoParams' prettyQuery qry
            HasSingleParam p -> pgFormatQuery' prettyQuery qry p
            HasMultiParams ps -> pgFormatQueryMany' prettyQuery qry ps

  let doLog errs = do
        Otel.addAttribute span "_.postgres.query" (errs.query & bytesToTextUtf8Lenient & Otel.toAttribute)
        for_ errs.explain $ \ex ->
          Otel.addAttribute span "_.postgres.explain" (Otel.toAttribute @Text ex)
  let doExplain = do
        q <- formattedQuery
        Otel.inSpan "Postgres EXPLAIN Query" Otel.defaultSpanArguments $ do
          queryWithImpl_
            (pure prettyQuery)
            ( "EXPLAIN "
                <> (
                     -- TODO: this is not nice, but the only way to get the `executeMany` form to work with this
                     -- because we need the query with all elements already interpolated.
                     Query q
                   )
            )
            (Dec.fromField @Text)
            <&> Text.intercalate "\n"
  case logDatabaseQueries of
    DontLogDatabaseQueries -> pure ()
    LogDatabaseQueries -> do
      q <- formattedQuery
      doLog (T2 (label @"query" q) (label @"explain" Nothing))
    LogDatabaseQueriesAndExplain -> do
      q <- formattedQuery
      -- XXX: stuff like `CREATE SCHEMA` cannot be EXPLAINed, so we should catch exceptions here
      -- and just ignore anything that errors (if it errors because of a problem with the query, it would have been caught by the query itself.
      ex <- doExplain
      doLog (T2 (label @"query" q) (label @"explain" (Just ex)))

-- | Add a start and end event to the span, and figure out how long the difference was.
--
-- This is more lightweight than starting an extra span for timing things.
withEvent :: (MonadIO f) => Otel.Span -> Text -> Text -> f b -> f b
withEvent span start end act = do
  let mkMs ts = (ts & Otel.timestampNanoseconds & toInteger) `div` 1000_000
  s <- Otel.getTimestamp
  Otel.addEvent
    span
    ( Otel.NewEvent
        { newEventName = start,
          newEventAttributes = mempty,
          newEventTimestamp = Just s
        }
    )
  res <- act
  e <- Otel.getTimestamp
  let tookMs =
        (mkMs e - mkMs s)
          -- should be small enough
          & fromInteger @Int
  Otel.addEvent
    span
    ( Otel.NewEvent
        { newEventName = end,
          newEventAttributes = HashMap.fromList [("took ms", Otel.toAttribute tookMs)],
          newEventTimestamp = Just e
        }
    )
  pure res

unzipPGArray ::
  forall l1 t1 l2 t2 r.
  ( HasField l1 r t1,
    HasField l2 r t2
  ) =>
  [r] ->
  (PGArray t1, PGArray t2)
{-# INLINEABLE unzipPGArray #-}
unzipPGArray xs =
  ( PGArray $ getField @l1 <$> xs,
    PGArray $ getField @l2 <$> xs
  )

unzip3PGArray ::
  forall l1 t1 l2 t2 l3 t3 r.
  ( HasField l1 r t1,
    HasField l2 r t2,
    HasField l3 r t3
  ) =>
  [r] ->
  (PGArray t1, PGArray t2, PGArray t3)
{-# INLINEABLE unzip3PGArray #-}
unzip3PGArray xs =
  ( PGArray $ getField @l1 <$> xs,
    PGArray $ getField @l2 <$> xs,
    PGArray $ getField @l3 <$> xs
  )

unzip4PGArray ::
  forall l1 t1 l2 t2 l3 t3 l4 t4 r.
  ( HasField l1 r t1,
    HasField l2 r t2,
    HasField l3 r t3,
    HasField l4 r t4
  ) =>
  [r] ->
  (PGArray t1, PGArray t2, PGArray t3, PGArray t4)
{-# INLINEABLE unzip4PGArray #-}
unzip4PGArray xs =
  ( PGArray $ getField @l1 <$> xs,
    PGArray $ getField @l2 <$> xs,
    PGArray $ getField @l3 <$> xs,
    PGArray $ getField @l4 <$> xs
  )

unzip5PGArray ::
  forall l1 t1 l2 t2 l3 t3 l4 t4 l5 t5 r.
  ( HasField l1 r t1,
    HasField l2 r t2,
    HasField l3 r t3,
    HasField l4 r t4,
    HasField l5 r t5
  ) =>
  [r] ->
  (PGArray t1, PGArray t2, PGArray t3, PGArray t4, PGArray t5)
{-# INLINEABLE unzip5PGArray #-}
unzip5PGArray xs =
  ( PGArray $ getField @l1 <$> xs,
    PGArray $ getField @l2 <$> xs,
    PGArray $ getField @l3 <$> xs,
    PGArray $ getField @l4 <$> xs,
    PGArray $ getField @l5 <$> xs
  )

instance (ToField t1) => ToRow (Label l1 t1) where
  toRow t2 = toRow $ PG.Only $ getField @l1 t2

instance (ToField t1, ToField t2) => ToRow (T2 l1 t1 l2 t2) where
  toRow t2 = toRow (getField @l1 t2, getField @l2 t2)

instance (ToField t1, ToField t2, ToField t3) => ToRow (T3 l1 t1 l2 t2 l3 t3) where
  toRow t3 = toRow (getField @l1 t3, getField @l2 t3, getField @l3 t3)