-
Notifications
You must be signed in to change notification settings - Fork 1
/
p-pgcontrol.sh
executable file
·427 lines (374 loc) · 13.7 KB
/
p-pgcontrol.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#!/bin/bash
set -euo pipefail
SCRIPTNAME=${0##*/}
__DOC__="
================================================================================
$SCRIPTNAME MANUAL
================================================================================
This script controls PostgreSQL servers, clients, and build processes.
Some information have been taken from:
http://petereisentraut.blogspot.it/2010/03/running-sql-scripts-with-psql.html
Example setup for this script:
export PW_PGC_PORT=5112
export PW_PGC_LOG=/tmp/postgresql-temporal-serverlog
export PW_PGC_DATA=~/projects/WORK/IDSE/tpg/source/data
export PW_PGC_BUILD=~/projects/WORK/IDSE/tpg/source/server
"
# All output should be in English
export LC_ALL=C
function showConfig {
loadINI
echo " Port : $PORT"
echo " Log : $LOG"
echo " Data : $DATA"
echo " Build : $BUILD"
}
function showHelp {
echo "
USAGE:
$SCRIPTNAME [OPTIONS]
This is a script to control a PostgreSQL instance.
OPTIONS:
-h, --help Show this help message
--manual Show the manual of this command
-i, --info Show configuration for current directory
-s, --start Start the PostgreSQL Server
-S, --stop Stop the PostgreSQL Server
-r, --restart Restart the PostgreSQL Server
--status Show the status of the PostgreSQL Server
-c, --createdb DB Create a database with name DB
--dropdb DB Drop a database with name DB
-I, --initdb Create a new PostgreSQL database cluster in \$DATA
Change \$DATA in the ini-file
-t, --test DB FILE Test FILE with database DB (batch mode; single
transaction; stop on error)
-T, --testall DB FILE Test FILE with database DB (batch mode; multiple
transactions; do not stop on error)
--regressiontest DB FILE
Run FILE with database DB to create a regression test
file for PG test/regress/expected
-l, --load DB FILE Load FILE with SQL data into the database DB
-p, --psql DB Start psql for database DB with current configuration
--execute DB COMMAND
Execute COMMAND on database DB
--csvout DB FILE Same as 'load', but writes results as CSV to stdout
--csvload DB TABLE FILE
Load a csv-file and store contents in table TABLE
--comparetables DB QUERY1 QUERY2
Run both queries and compare results
--patchcreate PATCHFILE
Create a patch against PostgreSQL origin/master
without src/test
--patchcreatetestonly PATCHFILE
Create a patch against PostgreSQL origin/master,
but src/test only!
--patch PATCHFILE Apply a patch to a Postgres source code directory
See man patch for further details.
-m, --make Compiles the source of PostgreSQL, restarts the server,
and displays server's log file
-x, --restartclean Remove logfile, restart server and output log cont.
--configure Run configure with default parameters
--testinitdb Tests to initialize a temporary database
--archive Creates a tar.gz archive of the actual git HEAD in
'\$BUILD/postgresql-PGVERSION-temporal.tar.gz'
Note: Change environmental variables if you want to have a different
configuration. See \"$SCRIPTNAME --manual\" for details.
EXAMPLE SETUP FOR THIS SCRIPT:
export PW_PGC_PORT=5112
export PW_PGC_LOG=/tmp/postgresql-temporal-serverlog
export PW_PGC_DATA=~/projects/tpg/source/data
export PW_PGC_BUILD=~/projects/tpg/source/server
"
exit 0
}
function showError {
echo "$SCRIPTNAME: ERROR: $1" >&2
exit 1
}
# Fetch environment information about the PostgreSQL installation
function loadINI {
PORT=$(printf "%s\n" "${PW_PGC_PORT:?You must set PW_PGC_PORT}")
DATA=$(printf "%s\n" "${PW_PGC_DATA:?You must set PW_PGC_DATA}")
LOG=$(printf "%s\n" "${PW_PGC_LOG:?You must set PW_PGC_LOG}")
BUILD=$(printf "%s\n" "${PW_PGC_BUILD:?You must set PW_PGC_BUILD}")
return 0
}
function checkArguments {
if test $1 -lt $2; then
showError "$3"
fi
}
# callPgCtl
# Call the PostgreSQL control program pg_ctl, either with or without log file
# output.
#
# $1 - pg_ctl command (ex., status, start, stop)
# $2 - data dir of the PostgreSQL cluster
# $3 - server port
# $4 - log file
function callPgCtl {
loadINI
L=""
test -n $LOG && L="-l $LOG"
$BUILD/bin/pg_ctl $1 -D $DATA $L -o "-p $PORT"
return $?
}
# callPsql
# Call the PostgreSQL client program psql for the server on localhost with a
# given port, data cluster directory, and file that should be executed.
#
# $1 - server port
# $2 - data dir of the PostgreSQL cluster
# $3 - SQL file that must be executed
# $4 - Additional parameters
function callPsql {
loadINI
$BUILD/bin/psql -p $PORT -h localhost -d $DATA -f $PORT $LOG
return $?
}
################################################################################
## MAIN
################################################################################
# Handling of script arguments...
# Each short option character in shortopts may be followed by one colon to
# indicate it has a required argument, and by two colons to indicate it has
# an optional argument.
ARGS=$(
getopt -o "hisrt:T:SIc:l:p:mx" \
-l "help,info,start,stop,restart,status,initdb,createdb:,dropdb:,test:,
testall:,load:,psql:,csvout:,csvload:,comparetables:,patchcreate:,patch:,
make,restartclean,regressiontest:,configure,patchcreatetestonly:,execute:,
testinitdb,manual,archive" \
-n $SCRIPTNAME -- "$@"
)
#2>/tmp/pw_pgcontrol.sh_getopt$$
if [ $? != 0 ] ; then
showError "Wrong argument given: $@"
fi
eval set -- "$ARGS"
test "$1" == "-h" || test "$1" == "--help" && {
showHelp
exit 0
}
test "$1" == "--manual" && {
echo "$__DOC__"
echo
showHelp
exit 0
}
CMD=
while true; do
case "$1" in
-i | --info)
showConfig
exit 0
;;
-s | --start)
callPgCtl start
exit $?
;;
-r | --restart)
callPgCtl restart
exit $?
;;
--status)
callPgCtl status
exit $?
;;
-S | --stop)
callPgCtl stop
exit $?
;;
-c | --createdb)
loadINI
checkArguments $# 2 "$1: no database name specified!"
$BUILD/bin/createdb -p $PORT -h localhost $2
exit $?
;;
--dropdb)
loadINI
checkArguments $# 2 "$1: no database name specified!"
$BUILD/bin/dropdb -p $PORT -h localhost $2
exit $?
;;
-l | --load)
loadINI
checkArguments $# 3 "$1: no database name and/or data-file specified!"
$BUILD/bin/psql -p $PORT -h localhost -d $2 -f $4
exit $?
;;
-t | --test)
loadINI
checkArguments $# 4 "--test DB FILE: no database name or test-file specified!"
PGOPTIONS='--client-min-messages=warning' $BUILD/bin/psql -p $PORT \
-h localhost -X -a -q -1 -v ON_ERROR_STOP=1 --pset pager=off \
-d $2 -f $4
exit $?
;;
--regressiontest)
loadINI
# The -f parameter produces error messages with filename and line of
# code numbers, where the error occurred. Therefore, we use
# "< filename" instead of "-f filename"
checkArguments $# 4 "--regressiontest DB FILE: no database name or test-file specified!"
PGOPTIONS='--client-min-messages=warning' $BUILD/bin/psql -p $PORT \
-h localhost -X -a -q -v ON_ERROR_STOP=0 --pset pager=off \
-d $2 < $4 2>&1
exit $?
;;
-T | --testall)
loadINI
checkArguments $# 4 "--testall DB FILE: no database name or test-file specified!"
PGOPTIONS='--client-min-messages=warning' $BUILD/bin/psql -p $PORT \
-h localhost -X -a -q -v ON_ERROR_STOP=0 --pset pager=off \
-d $2 -f $4 2>&1
exit $?
;;
--debug)
loadINI
$BUILD/bin/psql -a -e -p $PORT -h localhost -d $2 -f $3
exit $?
;;
-p | --psql)
loadINI
# TODO Build a better checkArguments here... pass additional parameters if any.
checkArguments $# 2 "$1: database name missing or too many arguments given."
$BUILD/bin/psql -p $PORT -h localhost -d $2
exit $?
;;
--csvout)
loadINI
query=$(sed 's/;//' $3 | grep -v ^SET )
$BUILD/bin/psql -p $PORT -h localhost -d $2 -c "COPY ( $query ) TO STDOUT WITH CSV HEADER DELIMITER ';'"
exit $?
;;
--csvout2)
loadINI
query=$(sed 's/;//' $3 | grep -v ^SET )
$BUILD/bin/psql -p $PORT -h localhost -d $2 -c "COPY ( $query ) TO STDOUT WITH CSV DELIMITER ','"
exit $?
;;
--csvload)
loadINI
# To fetch the absolute path with filename
file=$(readlink -m $4)
$BUILD/bin/psql -p $PORT -h localhost -d $2 -c "COPY $3 FROM '$file' DELIMITER ';' CSV HEADER"
exit $?
;;
--comparetables)
loadINI
$BUILD/bin/psql -p $PORT -h localhost -d $2 -c "WITH test AS ($4), test2 AS ($5) SELECT * FROM ((TABLE test EXCEPT ALL TABLE test2) UNION (TABLE test2 EXCEPT ALL TABLE test)) d;"
exit $?
;;
--execute)
loadINI
$BUILD/bin/psql -p $PORT -h localhost -d $2 -c "$4"
exit $?
;;
-I | --initdb)
loadINI
$BUILD/bin/initdb -D $DATA
exit $?
;;
--testinitdb)
loadINI
TMPDIR="/tmp/$SCRIPTNAME-initdb-test.$$"
$BUILD/bin/initdb -D $TMPDIR
RES=$?
rm -rf $TMPDIR
exit $RES
;;
--patchcreate)
checkArguments $# 2 "$1: No <patch-file> given."
# Git supports exclusion patterns now, therefore this code is deprecated
# We leave it here for reference, or if on a system an old git is installed:
#
# git diff --no-prefix origin/master -- src/ \
# | filterdiff -p 0 -x "src/test/*" \
# | sed '/diff --git src\/test/,/^index/{d}' - > $2
# It is best to generate PG patches without any parameters!
#
# git diff --no-prefix origin/master -- src/ ':!src/test/*' > $2
# Generate the patch against the same branch!
git diff master -- src/ > $2
exit $?
;;
--patchcreatetestonly)
checkArguments $# 2 "$1: No <patch-file> given."
git diff --no-prefix master -- src/test/ > $2
exit $?
;;
--patch)
checkArguments $# 2 "$1: Provide a patch-file to apply a patch to a postgres directory."
patch -p1 < $2
exit $?
;;
-x | --restartclean | -m | --make )
loadINI
# Remove logfile, restart server and show log constantly...
test -z $LOG && {
showError "LOG not set in INI file $INI."
}
rm -f $LOG || {
showError "Can not remove LOG file $LOG."
}
test "$1" = "-m" || test "$1" == "--make" && {
make && make install || {
showError "make or make install failed with error-code $?"
}
}
# Server restart
callPgCtl restart $DATA $PORT $LOG || {
exit $?
}
# Wait until log file exists, i.e. server has been started...
while [ ! -f $LOG ]
do
echo -n -e "\rWaiting for PostgreSQL server to start up..."
done
clear
tail -f $LOG
exit 0
;;
--configure )
loadINI
# Disable all compile optimization techniques. Step-by-step
# debugging works better without.
export CFLAGS="-O0"
./configure \
--prefix="$(readlink -f $BUILD)" \
--enable-debug \
--enable-depend \
--enable-cassert
exit $?
;;
--archive )
loadINI
(
cd $BUILD/..
PGVERSION=$(grep -E "^VERSION = " src/Makefile.global | awk '{print $3}')
TARFILE=$(readlink -m $BUILD/../postgresql-$PGVERSION-temporal.tar)
git archive --format=tar --prefix=source/ HEAD -o $TARFILE
# Uncomment this lines, if you want to put the PDF manual too
#cd ../manual/
#cp document.pdf temporal_postgresql_manual.pdf
#tar -rvf $TARFILE temporal_postgresql_manual.pdf
cd ../docs/
tar -rvf $TARFILE \
install.txt \c
examples.sql \
tpg_data.sql \
tpg_quickbuild.sh
gzip -f $TARFILE
echo "DONE! Archive can be found at '$TARFILE'..."
)
exit $?
;;
-- )
shift
break
;;
esac
done
# We should not reach this line!
showError "No parameter specified"