11#! /usr/bin/env bash
22
3+ #
34# Copyright (c) 2018, Postgres Professional
4-
5+ #
6+ # supported levels:
7+ # * standard
8+ # * scan-build
9+ # * hardcore
10+ # * nightmare
11+ #
512
613set -ux
14+ status=0
715
816
9- status=0
17+ # rebuild PostgreSQL with cassert + valgrind support
18+ if [" $LEVEL " = " hardcore" ]|| \
19+ [" $LEVEL " = " nightmare" ]; then
20+
21+ set -e
22+
23+ CUSTOM_PG_BIN=$PWD /pg_bin
24+ CUSTOM_PG_SRC=$PWD /postgresql
25+
26+ # here PG_VERSION is provided by postgres:X-alpine docker image
27+ curl" https://ftp.postgresql.org/pub/source/v$PG_VERSION /postgresql-$PG_VERSION .tar.bz2" -o postgresql.tar.bz2
28+ echo " $PG_SHA256 *postgresql.tar.bz2" | sha256sum -c -
29+
30+ mkdir$CUSTOM_PG_SRC
31+
32+ tar \
33+ --extract \
34+ --file postgresql.tar.bz2 \
35+ --directory$CUSTOM_PG_SRC \
36+ --strip-components 1
37+
38+ cd $CUSTOM_PG_SRC
39+
40+ # enable Valgrind support
41+ sed -i.bak" s/\/* #define USE_VALGRIND *\//#define USE_VALGRIND/g" src/include/pg_config_manual.h
42+
43+ # enable additional options
44+ ./configure \
45+ CFLAGS=' -O0 -ggdb3 -fno-omit-frame-pointer' \
46+ --enable-cassert \
47+ --prefix=$CUSTOM_PG_BIN \
48+ --quiet
49+
50+ time make -s -j$( nproc) && make -s install
51+
52+ # override default PostgreSQL instance
53+ export PATH=$CUSTOM_PG_BIN /bin:$PATH
54+ export LD_LIBRARY_PATH=$CUSTOM_PG_BIN /lib
55+
56+ # show pg_config path (just in case)
57+ which pg_config
58+
59+ cd -
60+
61+ set +e
62+ fi
1063
1164# show pg_config just in case
1265pg_config
1366
14- # perform static analyzis
15- scan-build --status-bugs make USE_PGXS=1|| status=$?
67+ # perform code checks if asked to
68+ if [" $LEVEL " = " scan-build" ]|| \
69+ [" $LEVEL " = " hardcore" ]|| \
70+ [" $LEVEL " = " nightmare" ]; then
1671
17- # something's wrong, exit now!
18- if [ $ status-ne 0 ] ; then exit 1 ; fi
72+ # perform static analyzis
73+ scan-build -- status-bugs make USE_PGXS=1 || status= $?
1974
20- # don't forget to "make clean"
21- make USE_PGXS=1 clean
75+ # something's wrong, exit now!
76+ if [$status -ne 0 ]; then exit 1; fi
77+
78+ # don't forget to "make clean"
79+ make USE_PGXS=1 clean
80+ fi
81+
82+
83+ # build and install extension (using PG_CPPFLAGS and SHLIB_LINK for gcov)
84+ make USE_PGXS=1 PG_CPPFLAGS=" -coverage" SHLIB_LINK=" -coverage" install
2285
2386# initialize database
2487initdb -D$PGDATA
2588
26- # build and install extension (using PG_CPPFLAGS and SHLIB_LINK for gcov)
27- make USE_PGXS=1 PG_CPPFLAGS= " -coverage " SHLIB_LINK= " -coverage "
28- make USE_PGXS=1 install
89+ # set appropriate port
90+ export PGPORT=55435
91+ echo " port = $PGPORT " >> $PGDATA /postgresql.conf
2992
3093# restart cluster 'test'
31- echo " port = 55435" >> $PGDATA /postgresql.conf
32- pg_ctl start -l /tmp/postgres.log -w|| status=$?
94+ if [" $LEVEL " = " nightmare" ]; then
95+ ls$CUSTOM_PG_BIN /bin
96+
97+ valgrind \
98+ --tool=memcheck \
99+ --leak-check=no \
100+ --time-stamp=yes \
101+ --track-origins=yes \
102+ --trace-children=yes \
103+ --gen-suppressions=all \
104+ --suppressions=$CUSTOM_PG_SRC /src/tools/valgrind.supp \
105+ --suppressions=$PWD /valgrind.supp \
106+ --log-file=/tmp/valgrind-%p.log \
107+ pg_ctl start -l /tmp/postgres.log -w|| status=$?
108+ else
109+ pg_ctl start -l /tmp/postgres.log -w|| status=$?
110+ fi
33111
34112# something's wrong, exit now!
35113if [$status -ne 0 ]; then cat /tmp/postgres.log; exit 1; fi
36114
37115# run regression tests
38116export PG_REGRESS_DIFF_OPTS=" -w -U3" # for alpine's diff (BusyBox)
39- PGPORT=55435 make USE_PGXS=1 installcheck|| status=$?
117+ make USE_PGXS=1 installcheck|| status=$?
118+
119+ # show Valgrind logs if necessary
120+ if [" $LEVEL " = " nightmare" ]; then
121+ for f in $( find /tmp -name valgrind-* .log) ; do
122+ if grep -q' Command: [^ ]*/postgres' $f && grep -q' ERROR SUMMARY: [1-9]' $f ; then
123+ echo " ========= Contents of$f "
124+ cat$f
125+ status=1
126+ fi
127+ done
128+ fi
40129
41130# show diff if it exists
42131if test -f regression.diffs; then cat regression.diffs; fi
@@ -45,7 +134,6 @@ if test -f regression.diffs; then cat regression.diffs; fi
45134if [$status -ne 0 ]; then exit 1; fi
46135
47136# generate *.gcov files
48- rm -f* serialize.{gcda,gcno}
49137gcov* .c* .h
50138
51139