Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log errors in defaultConfig #288

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 58 additions & 45 deletions src/Database/Postgres/Temp/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Control.Exception
import Control.Monad (void, join)
import Control.Monad.Trans.Cont
import Data.ByteString (ByteString)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import qualified Database.PostgreSQL.Simple.Options as Client
import GHC.Generics
Expand Down Expand Up @@ -122,8 +123,8 @@ client_min_messages = ERROR

@since 1.21.0.0
-}
fastPostgresConfig :: [(String, String)]
fastPostgresConfig =
fastPostgresConfig :: Map String String
fastPostgresConfig = Map.fromList
[ ("shared_buffers", "12MB")
, ("fsync", "off")
, ("synchronous_commit", "off")
Expand All @@ -138,6 +139,18 @@ fastPostgresConfig =
, ("max_wal_senders", "0")
]

{-|
A config that might be slightly slower than 'fastPostgresConfig' because it
logs errors.
-}
loggingPostgresConfig :: Map String String
loggingPostgresConfig =
Map.fromList
[ ("log_min_messages", "WARNING")
, ("log_min_error_statement", "ERROR")
]
`Map.union` fastPostgresConfig

{-|
The default configuration. This will create a database called \"postgres\"
via @initdb@ (it's default behavior).
Expand All @@ -151,8 +164,8 @@ shared_buffers = 12MB
fsync = off
synchronous_commit = off
full_page_writes = off
log_min_messages = PANIC
log_min_error_statement = PANIC
log_min_messages = WARNING
log_min_error_statement = ERROR
log_statement = none
client_min_messages = ERROR
commit_delay = 100000
Expand Down Expand Up @@ -196,7 +209,7 @@ custom = defaultConfig <> mempty
-}
defaultConfig :: Config
defaultConfig = mempty
{ postgresConfigFile = fastPostgresConfig
{ postgresConfigFile = Map.toList loggingPostgresConfig
, initDbConfig = pure mempty
{ commandLine = mempty
{ keyBased = Map.singleton "--no-sync" Nothing
Expand All @@ -216,7 +229,7 @@ should use this config.
-}
defaultConfig_9_3_10 :: Config
defaultConfig_9_3_10 = mempty
{ postgresConfigFile = fastPostgresConfig
{ postgresConfigFile = Map.toList loggingPostgresConfig
, initDbConfig = pure mempty
{ commandLine = mempty
{ keyBased = Map.singleton "--nosync" Nothing
Expand All @@ -227,27 +240,25 @@ defaultConfig_9_3_10 = mempty
-- | Default postgres options
--
-- @since 1.21.0.0
verbosePostgresConfig :: [(String, String)]
verbosePostgresConfig :: Map String String
verbosePostgresConfig =
[ ("shared_buffers", "12MB")
, ("fsync", "off")
, ("synchronous_commit", "off")
, ("full_page_writes", "off")
, ("log_min_duration_statement", "0")
, ("client_min_messages", "WARNING")
, ("log_min_messages", "WARNING")
, ("log_min_error_statement", "WARNING")
, ("log_checkpoints", "on")
, ("log_connections", "on")
, ("log_disconnections", "on")
, ("log_lock_waits", "on")
, ("log_temp_files", "0")
, ("log_autovacuum_min_duration", "0")
, ("log_error_verbosity", "default")
, ("log_line_prefix", "'%t [%p]: '")
, ("lc_messages", "'C'")
, ("track_io_timing", "on")
]
Map.fromList
[ ("log_min_duration_statement", "0")
, ("client_min_messages", "WARNING")
, ("log_min_messages", "WARNING")
, ("log_min_error_statement", "WARNING")
, ("log_checkpoints", "on")
, ("log_connections", "on")
, ("log_disconnections", "on")
, ("log_lock_waits", "on")
, ("log_temp_files", "0")
, ("log_autovacuum_min_duration", "0")
, ("log_error_verbosity", "default")
, ("log_line_prefix", "'%t [%p]: '")
, ("lc_messages", "'C'")
, ("track_io_timing", "on")
]
`Map.union` loggingPostgresConfig

{-|
This is similar to 'defaultConfig' but it logs as much as possible..
Expand All @@ -257,7 +268,7 @@ This is similar to 'defaultConfig' but it logs as much as possible..
verboseConfig :: Config
verboseConfig = defaultConfig <> mempty
{ logger = pure print
, postgresConfigFile = verbosePostgresConfig
, postgresConfigFile = Map.toList verbosePostgresConfig
, initDbConfig = pure standardProcessConfig
, postgresConfig = standardProcessConfig
}
Expand All @@ -267,23 +278,25 @@ Useful options for configuring and loading @auto_explain@.

@since 1.34.1.0
-}
autoExplainPostgresConfig :: Int -> [(String, String)]
autoExplainPostgresConfig milliseconds = verbosePostgresConfig <>
[ ("log_min_duration_statement", show milliseconds <> "ms")
, ("shared_preload_libraries", "'auto_explain'")
, ("session_preload_libraries", "'auto_explain'")
, ("auto_explain.log_analyze", "1")
, ("auto_explain.log_buffers", "1")
, ("auto_explain.log_timing", "1")
, ("auto_explain.log_triggers", "1")
, ("auto_explain.log_verbose", "1")
, ("auto_explain.log_min_duration", show milliseconds <> "ms")
, ("auto_explain.log_nested_statements", "1")
, ("auto_explain.sample_rate", "1")
, ("auto_explain.log_verbose", "on")
, ("log_connections", "off")
, ("log_disconnections", "off")
]
autoExplainPostgresConfig :: Int -> Map String String
autoExplainPostgresConfig milliseconds =
Map.fromList
[ ("log_min_duration_statement", show milliseconds <> "ms")
, ("shared_preload_libraries", "'auto_explain'")
, ("session_preload_libraries", "'auto_explain'")
, ("auto_explain.log_analyze", "1")
, ("auto_explain.log_buffers", "1")
, ("auto_explain.log_timing", "1")
, ("auto_explain.log_triggers", "1")
, ("auto_explain.log_verbose", "1")
, ("auto_explain.log_min_duration", show milliseconds <> "ms")
, ("auto_explain.log_nested_statements", "1")
, ("auto_explain.sample_rate", "1")
, ("auto_explain.log_verbose", "on")
, ("log_connections", "off")
, ("log_disconnections", "off")
]
`Map.union` verbosePostgresConfig

{-|
A config which loads and configures @auto_explain@. Useful for
Expand All @@ -296,7 +309,7 @@ autoExplainConfig
-- ^ Minimum number of milliseconds to log. Use 0 to log all queries.
-> Config
autoExplainConfig milliseconds = defaultConfig <> mempty
{ postgresConfigFile = autoExplainPostgresConfig milliseconds
{ postgresConfigFile = Map.toList $ autoExplainPostgresConfig milliseconds
, postgresConfig = standardProcessConfig
}

Expand Down