Skip to content

Instantly share code, notes, and snippets.

@codygman
Last active November 8, 2021 22:06
Show Gist options
  • Save codygman/eaf3d06667a13ce06ce0eb14d021661a to your computer and use it in GitHub Desktop.
Save codygman/eaf3d06667a13ce06ce0eb14d021661a to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE NumericUnderscores #-}
module Main where
import Database.PostgreSQL.Simple
import Control.Monad
import qualified Data.Text as Text
import Data.Time
import qualified GHC.Clock as Clock
import qualified Text.Printf as Printf
main :: IO ()
main = do
conn <- connectPostgreSQL "port=5435 dbname=postgres"
execute_ conn "drop table if exists foo;"
putStrLn "creating table"
execute_ conn "create table foo (id serial4, name varchar);"
putStrLn ""
let numRows = 20_000_000
putStrLn $ "inserting " <> show numRows <> " rows"
putStrLn ""
execute_ conn
"insert into foo(name) select * from generate_series(1, 20000000)"
putStrLn "length of foo"
query_ @(Only Int) conn "select count(*) from foo;" >>= print
putStrLn ""
putStrLn "print top 5 rows"
query_ @(Int, Maybe Text.Text) conn "select * from foo limit 5"
>>= mapM_ print
putStrLn ""
putStrLn "start alter table"
timeBefore <- Clock.getMonotonicTime
void $ execute_ conn "ALTER TABLE foo ALTER COLUMN id SET DATA TYPE bigint"
timeAfter <- Clock.getMonotonicTime
putStrLn ""
let duration = timeAfter - timeBefore
putStrLn $ Printf.printf "duration %.3f" duration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment