-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare.sh
executable file
·275 lines (246 loc) · 9.29 KB
/
prepare.sh
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
#!/usr/bin/env bash
show_help() {
cat <<EOF
Usage: bash ${0##*/} [-s scale] [-i pginstdir] [-d pgdatadir] [-t tpchtmp]
[-p pgport] [-n tpchdbname] [-g dbgenpath] [-e] [-x] [-h] [-a] [-q]
Prepare Postgres cluster for running TPC-H queries:
* Remove everything inside <pgdatadir>
* Create Postgres cluster at <pgdatadir> via initdb from <pginstdir>
* Add configuration from postgresql.conf to default configuration at
<pgdatadir>/postgresql.conf, if the former exists
* Run the cluster on port <pgport>
* Generate *.tbl files with TPC-H data, if needed
* Create database with TPC-H tables named <tpchdbname>
* Fill these tables with generated (or existing) data
* Remove generated data, if needed
* Create indexes, if needed
* Reset Postgres state (vacuum-analyze-checkpoint)
* Generate the TPC-H queries, if needed, and put them
to <pgdatadir>/queries
Options
The first seven options are read from $CONFIGFILE file, but you can override
them in command line args. See their meaning in that file. The rest are:
-e don't generate *.tbl files, use the existing ones
-r don't remove generated files after use, they are removed by default
-x don't create indexes, they are created by default
-a disable sanity checks: using Postgres built with assertions and
wal_level_minimal
-q don't generate queries, they are generated by default
-h display this help and exit
Example:
./prepare.sh -s 2 -d /mnt/ramdisk/mytpch-2
EOF
}
source common.sh
read_conf "$CONFIGFILE"
GENDATA=true
REMOVEGENDATA=true
CREATEINDEXES=true
SANITYCHECKS=true
GENQUERIES=true
OPTIND=1
while getopts "s:i:d:t:p:n:g:erxaqh" opt; do
case $opt in
h)
show_help
exit 0
;;
s)
SCALE="$OPTARG"
;;
i)
PGINSTDIR="$OPTARG"
;;
d)
PGDATADIR="$OPTARG"
;;
t)
TPCHTMP="$OPTARG"
;;
p)
PGPORT="$OPTARG"
;;
n)
TPCHDBNAME="$OPTARG"
;;
g)
DBGENPATH="$OPTARG"
;;
e)
GENDATA=false
;;
r)
REMOVEGENDATA=false
;;
x)
CREATEINDEXES=false
;;
a)
SANITYCHECKS=false
;;
q)
GENQUERIES=false
;;
\?)
show_help >&2
exit 1
;;
esac
done
if [ -z "$SCALE" ]; then die "scale is empty"; fi
if [ -z "$PGINSTDIR" ]; then die "pginstdir is empty"; fi
if [ -z "$PGDATADIR" ]; then die "pgdatadir is empty"; fi
if [ -z "$TPCHTMP" ]; then die "tpchtmp is empty"; fi
if [ -z "$PGPORT" ]; then die "pgport is empty"; fi
if [ -z "$TPCHDBNAME" ]; then die "tpchdbname is empty"; fi
# We need dbgenpath even if we don't generate *.tbl files because we always
# generate queries
if [ -z "$DBGENPATH" ]; then die "dbgenpath is empty"; fi
# directory with this script
BASEDIR=`dirname "$(readlink -f "$0")"`
PGBINDIR="${PGINSTDIR}/bin"
PGLIBDIR="${PGINSTDIR}/lib"
cd "$BASEDIR"
cd "$DBGENPATH" || die "dbgen directory not found"
DBGENABSPATH=`readlink -f "$(pwd)"`
echo "Using Postgres at $PGINSTDIR"
echo "Using datadir at $PGDATADIR"
echo "Scale is $SCALE"
echo "Using dbgen at $DBGENABSPATH"
# ========================== Preparing DB =========================
# Current time
CURRTIME=$(timer)
# create database cluster
rm -rf "$PGDATADIR"
mkdir -p "$PGDATADIR"
LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/initdb -D "$PGDATADIR" \
--encoding=UTF-8 --locale=C
# copy postgresql settings
if [ -f "$BASEDIR/postgresql.conf" ]; then
# Postgres uses the last read setting
cat "$BASEDIR/postgresql.conf" >> "$PGDATADIR/postgresql.conf"
echo "Postgres config applied"
else
echo "Config file postgresql.conf not found, using the default"
fi
# Start a new instance of Postgres
postgres_start 0
# create db with this user's name to give access
LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/createdb -h /tmp \
-p $PGPORT `whoami` --encoding=UTF-8 --locale=C;
echo "Current settings are"
LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/psql -h /tmp -p $PGPORT \
-c "select name, current_setting(name) from
pg_settings where name in('debug_assertions', 'wal_level',
'checkpoint_segments', 'shared_buffers', 'wal_buffers', 'fsync',
'maintenance_work_mem', 'checkpoint_completion_target', 'max_connections');"
if [ "$SANITYCHECKS" = true ]; then
WAL_LEVEL_MINIMAL=`LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/psql -h /tmp -p $PGPORT -c 'show wal_level' -t | grep minimal | wc -l`
if [ $WAL_LEVEL_MINIMAL != 1 ] ; then die "Postgres wal_level is not set to
minimal; 'Elide WAL traffic' optimization cannot be used"; fi
DEBUG_ASSERTIONS=`LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/psql -h /tmp -p $PGPORT -c 'show debug_assertions' -t | grep on | wc -l`
if [ $DEBUG_ASSERTIONS = 1 ] ; then die "Option debug_assertions is enabled"; fi
fi
# generate *.tbl files, if needed
if [ "$GENDATA" = true ]; then
make -j # build dbgen
if ! [ -x "$DBGENABSPATH/dbgen" ] || ! [ -x "$DBGENABSPATH/qgen" ]; then
die "Can't find dbgen or qgen.";
fi
mkdir -p "$TPCHTMP" || die "Failed to create temporary directory: '$TPCHTMP'"
cd "$TPCHTMP"
# needed by ./dbgen
cp "$DBGENABSPATH/dists.dss" . || die "dists.dss not found"
cp "$DBGENABSPATH/dss.ddl" . || die "dss.ddl not found" # table definitions
# foreign & primary keys
cp "$DBGENABSPATH/dss.ri" . || die "dss.ri not found"
# Create table files separately to have better IO throughput
# -v is verbose, -f for overwrtiting existing files, -T <letter> is
# "generate only table <letter>"
for TABLENAME in c s n r O L P s S; do
"$DBGENABSPATH/dbgen" -s $SCALE -f -T $TABLENAME &
done
wait_jobs
echo "TPC-H data *.tbl files generated at $TPCHTMP"
fi
LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/createdb -h /tmp \
-p $PGPORT $TPCHDBNAME --encoding=UTF-8 --locale=C
if [ $? != 0 ]; then die "Error: Can't proceed without database"; fi
TIME=`date`
LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/psql -h /tmp -p $PGPORT \
-d $TPCHDBNAME -c "comment on database
$TPCHDBNAME is 'TPC-H data, created at $TIME'"
echo "TPC-H database created"
LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/psql -h /tmp -p $PGPORT \
-d $TPCHDBNAME < "$TPCHTMP/dss.ddl"
echo "TPCH-H tables created"
cd "$TPCHTMP"
TBLFILESNUM=`find . -maxdepth 1 -type f -name '*.tbl' | wc -l`
if [ "$TBLFILESNUM" -eq "0" ]; then
die "No *.tbl files found"
fi
for f in *.tbl; do
# bf is f without .tbl extensions. Since unquoted names are case insensitive
# in Postgres, bf is basically a table name.
bf="$(basename $f .tbl)"
# We truncate the empty table in the sames transaction to enable Postgres to
# safely skip WAL-logging. See
# http://www.postgresql.org/docs/current/static/populate.html#POPULATE-PITR
echo "truncate $bf;
COPY $bf FROM '$(pwd)/$f' WITH DELIMITER AS '|'" |
LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/psql -h /tmp \
-p $PGPORT -d $TPCHDBNAME &
done
wait_jobs
echo "TPC-H tables are populated with data"
LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/psql -h /tmp -p $PGPORT \
-d $TPCHDBNAME < "dss.ri"
echo "primary and foreign keys added"
if [ "$REMOVEGENDATA" = true ]; then
cd && rm -rf "$TPCHTMP"
echo "tpch tmp directory removed"
fi
if [ "$CREATEINDEXES" = true ]; then
declare -a INDEXCMDS=(
# Pg does not create indexed on foreign keys, create them manually
"CREATE INDEX i_n_regionkey ON nation (n_regionkey);" #& #unused on 1GB
"CREATE INDEX i_s_nationkey ON supplier (s_nationkey);" #&
"CREATE INDEX i_c_nationkey ON customer (c_nationkey);" #&
"CREATE INDEX i_ps_suppkey ON partsupp (ps_suppkey);" #&
"CREATE INDEX i_ps_partkey ON partsupp (ps_partkey);" #&
"CREATE INDEX i_o_custkey ON orders (o_custkey);" #&
"CREATE INDEX i_l_orderkey ON lineitem (l_orderkey);" #&
"CREATE INDEX i_l_suppkey_partkey ON lineitem (l_partkey, l_suppkey);" #&
# other indexes
"CREATE INDEX i_l_shipdate ON lineitem (l_shipdate);" #&
"CREATE INDEX i_l_partkey ON lineitem (l_partkey);" #&
"CREATE INDEX i_l_suppkey ON lineitem (l_suppkey);" #&
"CREATE INDEX i_l_receiptdate ON lineitem (l_receiptdate);" #&
"CREATE INDEX i_l_orderkey_quantity ON lineitem (l_orderkey, l_quantity);" #&
"CREATE INDEX i_o_orderdate ON orders (o_orderdate);" #&
"CREATE INDEX i_l_commitdate ON lineitem (l_commitdate);" #& #unused on 1GB
)
for cmd in "${INDEXCMDS[@]}"; do
LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/psql -h /tmp \
-p $PGPORT -d $TPCHDBNAME -c "$cmd"
done
wait_jobs
echo "Indexes created"
else
echo "Indexes will not be created"
fi
# Always analyze after bulk-loading; when hacking Postgres, typically Postgres
# is run with autovacuum turned off.
echo "Running vacuum freeze analyze checkpoint..."
LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/psql -h /tmp -p $PGPORT \
-d $TPCHDBNAME -c "vacuum freeze"
LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/psql -h /tmp -p $PGPORT \
-d $TPCHDBNAME -c "analyze"
# Checkpoint, so we have a "clean slate". Just in-case.
LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$PGLIBDIR" $PGBINDIR/psql -h /tmp -p $PGPORT \
-d $TPCHDBNAME -c "checkpoint"
postgres_stop 0
if [ "$GENQUERIES" = true ]; then
gen_queries $PGDATADIR
fi
printf 'Preparing elapsed time: %s\n' $(timer $CURRTIME)