Skip to content

Commit 08d56ec

Browse files
committed
fix database debug mode and docs
1 parent 92ff532 commit 08d56ec

File tree

62 files changed

+432
-652
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+432
-652
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
# Revision history for cardano-db-sync
22

3+
## 13.7.0.0
4+
5+
### Summary
6+
- Complete migration from Persistent ORM to Hasql for direct PostgreSQL access.
7+
8+
### Performance Improvements
9+
- 3-4x faster epoch processing: ~30min → ~8min per epoch
10+
- Improved cache efficiency: Stake address hit rates increased from 57-79% to 89-99%
11+
- Reduced memory overhead: Eliminated ORM abstraction layer
12+
13+
### Compatibility
14+
- PostgreSQL schema remains unchanged
15+
- Existing database instances compatible without migration
16+
317
## 13.6.0.5
418
- Fix offchain data so it supports files up to 3MB [#1928]
519
- Upgrade to PostgreSQL 17

cabal.project

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ repository cardano-haskell-packages
1010
d4a35cd3121aa00d18544bb0ac01c3e1691d618f462c46129271bccf39f7e8ee
1111

1212
index-state:
13-
, hackage.haskell.org 2025-05-23T06:30:40Z
14-
, cardano-haskell-packages 2025-05-16T20:03:45Z
13+
, hackage.haskell.org 2025-06-26T20:35:31Z
14+
, cardano-haskell-packages 2025-06-25T13:51:34Z
1515

1616
packages:
1717
cardano-db
@@ -85,16 +85,9 @@ if impl (ghc >= 9.12)
8585
-- https://github.com/kapralVV/Unique/issues/11
8686
, Unique:hashable
8787

88-
-- https://github.com/Gabriella439/Haskell-Pipes-Safe-Library/pull/70
89-
, pipes-safe:base
90-
9188
-- https://github.com/haskellari/postgresql-simple/issues/152
9289
, postgresql-simple:base
9390
, postgresql-simple:template-haskell
94-
95-
-- https://github.com/haskell-hvr/int-cast/issues/10
96-
, int-cast:base
97-
9891
-- The two following one-liners will cut off / restore the remainder of this file (for nix-shell users):
9992
-- when using the "cabal" wrapper script provided by nix-shell.
10093
-- --------------------------- 8< --------------------------

cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Conway/Scenarios.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
#if __GLASGOW_HASKELL__ >= 908
77
{-# OPTIONS_GHC -Wno-x-partial #-}
8+
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
9+
{-# HLINT ignore "Use zipWith" #-}
810
#endif
911

1012
module Cardano.Mock.Forging.Tx.Conway.Scenarios (

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Alonzo/Config.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ insertConfig = do
3434
, sioPoolStats = PoolStatsConfig False
3535
, sioJsonType = JsonTypeDisable
3636
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
37-
, sioDbDebug = False
3837
}
3938

4039
dncInsertOptions cfg @?= expected

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Config/JsonbInSchema.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
{-# LANGUAGE NumericUnderscores #-}
21
{-# LANGUAGE OverloadedStrings #-}
32

43
module Test.Cardano.Db.Mock.Unit.Conway.Config.JsonbInSchema (

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Config/Parse.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ insertConfig = do
104104
, sioPoolStats = PoolStatsConfig False
105105
, sioJsonType = JsonTypeDisable
106106
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
107-
, sioDbDebug = False
108107
}
109108

110109
dncInsertOptions cfg @?= expected

cardano-db-sync/src/Cardano/DbSync.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ runSyncNode metricsSetters trce iomgr dbConnSetting runDelayedMigrationFnc syncN
241241
threadChannels <- liftIO newThreadChannels
242242
liftIO $
243243
race_
244-
(runDbThread syncEnv metricsSetters threadChannels) -- Main App thread
244+
-- We split the main thread into two parts to allow for graceful shutdown of the main App db thread.
245+
(runDbThread syncEnv metricsSetters threadChannels)
245246
( mapConcurrently_
246247
id
247-
[ -- Non-critical threads
248-
runSyncNodeClient metricsSetters syncEnv iomgr trce threadChannels (enpSocketPath syncNodeParams)
248+
[ runSyncNodeClient metricsSetters syncEnv iomgr trce threadChannels (enpSocketPath syncNodeParams)
249249
, runFetchOffChainPoolThread syncEnv syncNodeConfigFromFile
250250
, runFetchOffChainVoteThread syncEnv syncNodeConfigFromFile
251251
, runLedgerStateWriteThread (getTrace syncEnv) (envLedgerEnv syncEnv)

cardano-db-sync/src/Cardano/DbSync/Api.hs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ module Cardano.DbSync.Api (
5050
where
5151

5252
import Control.Concurrent.Class.MonadSTM.Strict (
53+
StrictTVar,
5354
newTBQueueIO,
5455
newTVarIO,
5556
readTVar,
5657
readTVarIO,
57-
writeTVar, StrictTVar,
58+
writeTVar,
5859
)
5960
import Control.Monad.Trans.Maybe (MaybeT (..))
6061
import qualified Data.Strict.Maybe as Strict
@@ -79,7 +80,7 @@ import qualified Ouroboros.Network.Point as Point
7980

8081
import qualified Cardano.Db as DB
8182
import Cardano.DbSync.Api.Types
82-
import Cardano.DbSync.Cache.Types (CacheCapacity (..), newEmptyCache, useNoCache, initCacheStatistics)
83+
import Cardano.DbSync.Cache.Types (CacheCapacity (..), initCacheStatistics, newEmptyCache, useNoCache)
8384
import Cardano.DbSync.Config.Cardano
8485
import Cardano.DbSync.Config.Shelley
8586
import Cardano.DbSync.Config.Types
@@ -445,21 +446,24 @@ mkSyncEnv trce dbEnv syncOptions protoInfo nw nwMagic systemStart syncNodeConfig
445446
initEpochStatistics :: MonadIO m => m (StrictTVar IO EpochStatistics)
446447
initEpochStatistics = do
447448
curTime <- liftIO getCurrentTime
448-
liftIO $ newTVarIO $ EpochStatistics
449-
{ elsStartTime = curTime
450-
, elsCaches = initCacheStatistics
451-
, elsUnicodeNull = Map.empty
452-
}
449+
liftIO $
450+
newTVarIO $
451+
EpochStatistics
452+
{ elsStartTime = curTime
453+
, elsCaches = initCacheStatistics
454+
, elsUnicodeNull = Map.empty
455+
}
453456

454457
resetEpochStatistics :: MonadIO m => SyncEnv -> m ()
455458
resetEpochStatistics syncEnv = liftIO $ do
456-
curTime <- getCurrentTime
457-
let newEpochStatsValue = EpochStatistics
458-
{ elsStartTime = curTime
459-
, elsCaches = initCacheStatistics
460-
, elsUnicodeNull = Map.empty
461-
}
462-
atomically $ writeTVar (envEpochStatistics syncEnv) newEpochStatsValue
459+
curTime <- getCurrentTime
460+
let newEpochStatsValue =
461+
EpochStatistics
462+
{ elsStartTime = curTime
463+
, elsCaches = initCacheStatistics
464+
, elsUnicodeNull = Map.empty
465+
}
466+
atomically $ writeTVar (envEpochStatistics syncEnv) newEpochStatsValue
463467

464468
-- | 'True' is for in memory points and 'False' for on disk
465469
getLatestPoints :: SyncEnv -> IO [(CardanoPoint, Bool)]

cardano-db-sync/src/Cardano/DbSync/Api/Types.hs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-# LANGUAGE GADTs #-}
2+
{-# LANGUAGE OverloadedStrings #-}
23
{-# LANGUAGE ScopedTypeVariables #-}
34
{-# LANGUAGE NoImplicitPrelude #-}
4-
{-# LANGUAGE OverloadedStrings #-}
55

66
module Cardano.DbSync.Api.Types (
77
SyncEnv (..),
@@ -11,24 +11,24 @@ module Cardano.DbSync.Api.Types (
1111
RunMigration,
1212
ConsistentLevel (..),
1313
CurrentEpochNo (..),
14-
UnicodeNullSource(..),
14+
UnicodeNullSource (..),
1515
EpochStatistics (..),
1616
formatUnicodeNullSource,
1717
) where
1818

1919
import Control.Concurrent.Class.MonadSTM.Strict (StrictTVar)
2020
import Control.Concurrent.Class.MonadSTM.Strict.TBQueue (StrictTBQueue)
21+
import qualified Data.Map.Strict as Map
2122
import qualified Data.Strict.Maybe as Strict
2223
import Data.Time.Clock (UTCTime)
2324
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (SystemStart (..))
2425
import Ouroboros.Network.Magic (NetworkMagic (..))
25-
import qualified Data.Map.Strict as Map
2626

27-
import Cardano.Prelude (Bool, Eq, IO, Show, Word64, Ord, Text)
27+
import Cardano.Prelude (Bool, Eq, IO, Ord, Show, Text, Word64)
2828
import Cardano.Slotting.Slot (EpochNo (..))
2929

3030
import qualified Cardano.Db as DB
31-
import Cardano.DbSync.Cache.Types (CacheStatus, CacheStatistics)
31+
import Cardano.DbSync.Cache.Types (CacheStatistics, CacheStatus)
3232
import Cardano.DbSync.Config.Types (SyncNodeConfig)
3333
import Cardano.DbSync.Ledger.Types (HasLedgerEnv)
3434
import Cardano.DbSync.LocalStateQuery (NoLedgerEnv)
@@ -112,7 +112,6 @@ data UnicodeNullSource
112112
| PrepareTxMetadata
113113
deriving (Eq, Ord, Show)
114114

115-
116115
formatUnicodeNullSource :: UnicodeNullSource -> Text
117116
formatUnicodeNullSource source = case source of
118117
InsertDatum -> "insertDatum: Column 'value' in table 'datum'"

cardano-db-sync/src/Cardano/DbSync/Cache.hs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ import Cardano.Prelude
4444

4545
import qualified Cardano.Db as DB
4646
import qualified Cardano.Db.Schema.Variants.TxOutAddress as VA
47+
import Cardano.DbSync.Api (getTrace)
48+
import Cardano.DbSync.Api.Types (EpochStatistics (..), SyncEnv (..))
4749
import Cardano.DbSync.Cache.Epoch (rollbackMapEpochInCache)
4850
import qualified Cardano.DbSync.Cache.FIFO as FIFO
4951
import qualified Cardano.DbSync.Cache.LRU as LRU
5052
import Cardano.DbSync.Cache.Types (CacheAction (..), CacheInternal (..), CacheStatistics (..), CacheStatus (..), StakeCache (..), shouldCache)
5153
import qualified Cardano.DbSync.Era.Shelley.Generic.Util as Generic
5254
import Cardano.DbSync.Era.Shelley.Query
5355
import Cardano.DbSync.Types
54-
import Cardano.DbSync.Api.Types (SyncEnv (..), EpochStatistics (..))
55-
import Cardano.DbSync.Api (getTrace)
5656

5757
-- Rollbacks make everything harder and the same applies to caching.
5858
-- After a rollback db entries are deleted, so we need to clean the same
@@ -552,76 +552,76 @@ withCacheOptimisationCheck ci ifOptimised ifNotOptimised = do
552552
-- Creds
553553
hitCreds :: SyncEnv -> IO ()
554554
hitCreds syncEnv =
555-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
556-
epochStats { elsCaches = (elsCaches epochStats) { credsHits = 1 + credsHits (elsCaches epochStats), credsQueries = 1 + credsQueries (elsCaches epochStats) } }
555+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
556+
epochStats {elsCaches = (elsCaches epochStats) {credsHits = 1 + credsHits (elsCaches epochStats), credsQueries = 1 + credsQueries (elsCaches epochStats)}}
557557

558558
missCreds :: SyncEnv -> IO ()
559559
missCreds syncEnv =
560-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
561-
epochStats { elsCaches = (elsCaches epochStats) { credsQueries = 1 + credsQueries (elsCaches epochStats) } }
560+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
561+
epochStats {elsCaches = (elsCaches epochStats) {credsQueries = 1 + credsQueries (elsCaches epochStats)}}
562562

563563
-- Pools
564564
hitPools :: SyncEnv -> IO ()
565565
hitPools syncEnv =
566-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
567-
epochStats { elsCaches = (elsCaches epochStats) { poolsHits = 1 + poolsHits (elsCaches epochStats), poolsQueries = 1 + poolsQueries (elsCaches epochStats) } }
566+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
567+
epochStats {elsCaches = (elsCaches epochStats) {poolsHits = 1 + poolsHits (elsCaches epochStats), poolsQueries = 1 + poolsQueries (elsCaches epochStats)}}
568568

569569
missPools :: SyncEnv -> IO ()
570570
missPools syncEnv =
571-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
572-
epochStats { elsCaches = (elsCaches epochStats) { poolsQueries = 1 + poolsQueries (elsCaches epochStats) } }
571+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
572+
epochStats {elsCaches = (elsCaches epochStats) {poolsQueries = 1 + poolsQueries (elsCaches epochStats)}}
573573

574574
-- Datum
575575
hitDatum :: SyncEnv -> IO ()
576576
hitDatum syncEnv =
577-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
578-
epochStats { elsCaches = (elsCaches epochStats) { datumHits = 1 + datumHits (elsCaches epochStats), datumQueries = 1 + datumQueries (elsCaches epochStats) } }
577+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
578+
epochStats {elsCaches = (elsCaches epochStats) {datumHits = 1 + datumHits (elsCaches epochStats), datumQueries = 1 + datumQueries (elsCaches epochStats)}}
579579

580580
missDatum :: SyncEnv -> IO ()
581581
missDatum syncEnv =
582-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
583-
epochStats { elsCaches = (elsCaches epochStats) { datumQueries = 1 + datumQueries (elsCaches epochStats) } }
582+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
583+
epochStats {elsCaches = (elsCaches epochStats) {datumQueries = 1 + datumQueries (elsCaches epochStats)}}
584584

585585
-- Assets
586586
hitMAssets :: SyncEnv -> IO ()
587587
hitMAssets syncEnv =
588-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
589-
epochStats { elsCaches = (elsCaches epochStats) { multiAssetsHits = 1 + multiAssetsHits (elsCaches epochStats), multiAssetsQueries = 1 + multiAssetsQueries (elsCaches epochStats) } }
588+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
589+
epochStats {elsCaches = (elsCaches epochStats) {multiAssetsHits = 1 + multiAssetsHits (elsCaches epochStats), multiAssetsQueries = 1 + multiAssetsQueries (elsCaches epochStats)}}
590590

591591
missMAssets :: SyncEnv -> IO ()
592592
missMAssets syncEnv =
593-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
594-
epochStats { elsCaches = (elsCaches epochStats) { multiAssetsQueries = 1 + multiAssetsQueries (elsCaches epochStats) } }
593+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
594+
epochStats {elsCaches = (elsCaches epochStats) {multiAssetsQueries = 1 + multiAssetsQueries (elsCaches epochStats)}}
595595

596596
-- Address
597597
hitAddress :: SyncEnv -> IO ()
598598
hitAddress syncEnv =
599-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
600-
epochStats { elsCaches = (elsCaches epochStats) { addressHits = 1 + addressHits (elsCaches epochStats), addressQueries = 1 + addressQueries (elsCaches epochStats) } }
599+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
600+
epochStats {elsCaches = (elsCaches epochStats) {addressHits = 1 + addressHits (elsCaches epochStats), addressQueries = 1 + addressQueries (elsCaches epochStats)}}
601601

602602
missAddress :: SyncEnv -> IO ()
603603
missAddress syncEnv =
604-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
605-
epochStats { elsCaches = (elsCaches epochStats) { addressQueries = 1 + addressQueries (elsCaches epochStats) } }
604+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
605+
epochStats {elsCaches = (elsCaches epochStats) {addressQueries = 1 + addressQueries (elsCaches epochStats)}}
606606

607607
-- Blocks
608608
hitPBlock :: SyncEnv -> IO ()
609609
hitPBlock syncEnv =
610-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
611-
epochStats { elsCaches = (elsCaches epochStats) { prevBlockHits = 1 + prevBlockHits (elsCaches epochStats), prevBlockQueries = 1 + prevBlockQueries (elsCaches epochStats) } }
610+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
611+
epochStats {elsCaches = (elsCaches epochStats) {prevBlockHits = 1 + prevBlockHits (elsCaches epochStats), prevBlockQueries = 1 + prevBlockQueries (elsCaches epochStats)}}
612612

613613
missPrevBlock :: SyncEnv -> IO ()
614614
missPrevBlock syncEnv =
615-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
616-
epochStats { elsCaches = (elsCaches epochStats) { prevBlockQueries = 1 + prevBlockQueries (elsCaches epochStats) } }
615+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
616+
epochStats {elsCaches = (elsCaches epochStats) {prevBlockQueries = 1 + prevBlockQueries (elsCaches epochStats)}}
617617

618618
-- TxIds
619619
hitTxIds :: SyncEnv -> IO ()
620620
hitTxIds syncEnv =
621-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
622-
epochStats { elsCaches = (elsCaches epochStats) { txIdsHits = 1 + txIdsHits (elsCaches epochStats), txIdsQueries = 1 + txIdsQueries (elsCaches epochStats) } }
621+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
622+
epochStats {elsCaches = (elsCaches epochStats) {txIdsHits = 1 + txIdsHits (elsCaches epochStats), txIdsQueries = 1 + txIdsQueries (elsCaches epochStats)}}
623623

624624
missTxIds :: SyncEnv -> IO ()
625625
missTxIds syncEnv =
626-
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
627-
epochStats { elsCaches = (elsCaches epochStats) { txIdsQueries = 1 + txIdsQueries (elsCaches epochStats) } }
626+
atomically $ modifyTVar (envEpochStatistics syncEnv) $ \epochStats ->
627+
epochStats {elsCaches = (elsCaches epochStats) {txIdsQueries = 1 + txIdsQueries (elsCaches epochStats)}}

0 commit comments

Comments
 (0)