HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Moduletmp-postgres-1.35.0.0Haskell2010

Database.Postgres.Temp.Internal

This module provides the high level functions that are re-exported by Database.Postgres.Temp. Additionally it includes some identifiers that are used for testing but are not exported.

  • 4 types
  • 40 values
datadata DB
#

Handle for holding temporary resources, the postgres process handle and postgres connection information. The DB also includes the final plan used to start initdb, createdb and postgres.

Constructors

Instances1Pretty
  • Pretty DBDefined in tmp-postgres-1.35.0.0 · Database.Postgres.Temp.Internal

The fastest config we can make.

shared_buffers = 12MB
fsync = off
synchronous_commit = off
full_page_writes = off
log_min_messages = PANIC
log_min_error_statement = PANIC
log_statement = none
client_min_messages = ERROR
valuedefaultConfig :: Config
#

The default configuration. This will create a database called "postgres" via initdb (it's default behavior). It will create a temporary directory for the data and a temporary directory for a unix socket and listen on 127.0.0.1 and ::1 on a random port. Additionally it will use the following "postgresql.conf" which is optimized for performance.

shared_buffers = 12MB
fsync = off
synchronous_commit = off
full_page_writes = off
log_min_messages = PANIC
log_min_error_statement = PANIC
log_statement = none
client_min_messages = ERROR
commit_delay = 100000
wal_level = minimal
archive_mode = off
max_wal_senders = 0

defaultConfig also passes the --no-sync flag to initdb.

If you would like to customize this behavior you can start with the defaultConfig and overwrite fields or combine a defaultConfig with another Config using <> (mappend).

Alternatively you can eschew defaultConfig altogether, however your postgres might start and run faster if you use defaultConfig.

The defaultConfig redirects all output to /dev/null. See verboseConfig for a version that logs more output.

To append additional lines to "postgresql.conf" file create a custom Config like the following.

custom = defaultConfig <> mempty
  { postgresConfigFile =
      [ ("wal_level", "replica")
      , ("archive_mode", "on")
      , ("max_wal_senders", "2")
      , ("fsync", "on")
      , ("synchronous_commit", "on")
      ]
  }

As an alternative to using defaultConfig one could create a config from connections parameters using optionsToDefaultConfig.

Default configuration for PostgreSQL versions 9.3 and greater but less than 10.

If you get an error that "--no-sync" is an invalid parameter then you should use this config.

valueautoExplainConfig
  1. :: Int

    Minimum number of milliseconds to log. Use 0 to log all queries.

  2. -> Config
#

A config which loads and configures auto_explain. Useful for understanding slow queries plans.

valuestartConfig
  1. :: Config

    extra configuration that is mappended last to the generated Config. generated <> extra.

  2. -> IO (Either StartError DB)
#

Create zero or more temporary resources and use them to make a Config.

The passed in config is inspected and a generated config is created. The final config is built by

generated <> extra

Based on the value of socketDirectory a "postgresql.conf" is created with:

listen_addresses = '127.0.0.1, ::1'
unix_socket_directories = 'SOCKET_DIRECTORY'

Additionally the generated Config also:

All of these values can be overrided by the extra config.

The returned DB requires cleanup. startConfig should be used with a bracket and stop, e.g.

withConfig :: Config -> (DB -> IO a) -> IO (Either StartError a)
withConfig plan f = bracket (startConfig plan) (either mempty stop) $
  either (pure . Left) (fmap Right . f)

or just use withConfig. If you are calling startConfig you probably want withConfig anyway.

valuestop :: DB -> IO ()
#

Stop the postgres process and cleanup any temporary resources that might have been created.

Only stop the postgres process but leave any temporary resources. In contrast to stopPostgres this function makes sure postgres has time to properly write files to the data directory.

Attempt to create a Config from a Options. Useful if you want to create a database

  • owned by a specific user you will also login with

  • with a specific name (i.e. not the default name, "postgres")

among other use cases. Changing the connectionOptions field of Config does not achieve these results and you are likely to see unexpected behaviour if you try to.

datadata CacheConfig
#

Configuration for the initdb data directory cache.

Constructors

datadata Cache
#

A handle to cache temporary resources and configuration.

Instances3Generic, NFData, Rep
valuecowCheck :: Bool
#

A bool that is True if the cp on the path supports "copy on write" flags.

newtypenewtype Snapshot
#

A type to track a possibly temporary snapshot directory

Instances3Generic, NFData, Rep
valuewithSnapshot :: DB -> (Snapshot -> IO a) -> IO (Either StartError a)
#

Exception safe method for taking a file system level copy of the database cluster.

Snapshots are useful if you would like to start every test from a migrated database and the migration process is more time consuming then copying the additional data.

Here is an example with caching and snapshots:

withDbCache $ \cache -> withConfig (cacheConfig cache) $ \db ->
  migrate db
  withSnapshot Temporary db $ \snapshot -> do
    withConfig (snapshotConfig db) $ \migratedDb -> ...
    withConfig (snapshotConfig db) $ \migratedDb -> ...
    withConfig (snapshotConfig db) $ \migratedDb -> ...

The Snapshots are ephemeral. If you would like the Snapshots to persistent consider using cacheAction instead.

valuecacheAction
  1. :: FilePath

    Location of the data directory cache.

  2. -> (DB -> IO ())

    action to cache.

  3. -> Config

    initial Config.

  4. -> IO (Either StartError Config)
#

Check to see if a cached data directory exists.

If the file path does not exist the initial config is used to start a postgres instance. After which the action is applied, the data directory is cached and postgres is shutdown.

cacheAction mappends a config to copy the cached data directory on startup onto the initial config and returns it. In other words:

initialConfig <> configFromCachePath

cacheAction can be used to create a snapshot of migrated database and not remigrate as long as the migration does not change. See withSnapshot for a ephemeral version of taking snapshots.

You can nest calls to cacheAction and safe to call it from several threads. However cacheAction uses locks internal to prevent multiple threads from stomping on each other.

If one makes a nested call and accidently uses the same cache directory in both calls the calls will deadlock. If this occurs on the same thread RTS will throw an exception. However do not rely on this and just be careful to not reuse the same cache path when nesting calls.

There is no good reuse the cache path when nesting so one is unlikely to run into this.