Skip to content

Commit 406df3c

Browse files
(SUP-2372) add pg_repack schema reset task (#53)
* (SUP-2372) reset pgrepack task added Co-authored-by: martyewings <martyewings@gmail.com>
1 parent 3c9ef36 commit 406df3c

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ This module provides cron jobs to VACUUM FULL tables in the `pe-puppetdb` databa
111111
112112
With PE 2018.1.7 and 2019.0.2 and newer, this module uses `pg_repack` instead of `VACUUM FULL`.
113113

114+
Please note that when using `pg_repack` as part of the pe_databases module, unclean exits can leave behind the schema when otherwise it should have been cleaned up. This can result in the messages similar to the following:
115+
116+
```
117+
INFO: repacking table "public.fact_paths"
118+
WARNING: the table "public.fact_paths" already has a trigger called "repack_trigger"
119+
DETAIL: The trigger was probably installed during a previous attempt to run pg_repack on the table which was interrupted and for some reason failed to clean up the temporary objects. Please drop the trigger or drop and recreate the pg_repack extension altogether to remove all the temporary objects left over.
120+
```
121+
122+
The module now contains a task `reset_pgrepack_schema` to mitigate this issue. This needs to be run against your Primary or Postgrsql server to resolve this and it will drop and recreate the extension, removing the temporary objects.
123+
114124
### Vacuuming
115125

116126
Generally speaking, PostgreSQL keeps itself in good shape with a process called [auto vacuuming](https://www.postgresql.org/docs/11/runtime-config-autovacuum.html).

files/common.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
# TODO: helper task?
4+
5+
# Exit with an error message and error code, defaulting to 1
6+
fail() {
7+
# Print a stderr: entry if there were anything printed to stderr
8+
if [[ -s $_tmp ]]; then
9+
# Hack to try and output valid json by replacing newlines with spaces.
10+
echo "{ \"status\": \"error\", \"message\": \"$1\", \"stderr\": \"$(tr '\n' ' ' <$_tmp)\" }"
11+
else
12+
echo "{ \"status\": \"error\", \"message\": \"$1\" }"
13+
fi
14+
15+
exit ${2:-1}
16+
}
17+
18+
success() {
19+
echo "$1"
20+
exit 0
21+
}
22+
23+
# Test for colors. If unavailable, unset variables are ok
24+
if tput colors &>/dev/null; then
25+
green="$(tput setaf 2)"
26+
red="$(tput setaf 1)"
27+
reset="$(tput sgr0)"
28+
fi
29+
30+
_tmp="$(mktemp)"
31+
exec 2>>"$_tmp"
32+
33+
# Use indirection to munge PT_ environment variables
34+
# e.g. "$PT_version" becomes "$version"
35+
for v in ${!PT_*}; do
36+
declare "${v#*PT_}"="${!v}"
37+
done

tasks/reset_pgrepack_schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"puppet_task_version": 1,
3+
"supports_noop": false,
4+
"description": "This task will remove and recreate the pg_repack extension",
5+
"implementations": [
6+
{"name": "reset_pgrepack_schema.sh", "requirements": ["shell"], "files": ["pe_databases/files/common.sh"], "input_method": "environment"}
7+
]
8+
}

tasks/reset_pgrepack_schema.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/sh
2+
3+
# Puppet Task Name: reset_pgrepack_schema
4+
declare PT__installdir
5+
# shellcheck disable=SC1090
6+
source "$PT__installdir/pe_databases/files/common.sh"
7+
8+
#Determine if PE Postgres is available
9+
if puppet resource service pe-postgresql | grep -q running; then
10+
#Remove the pg_repack extension
11+
su - pe-postgres -s '/bin/bash' -c '/opt/puppetlabs/server/bin/psql -d pe-puppetdb -c "DROP EXTENSION pg_repack CASCADE"' || fail "unable to drop pg_repack extension"
12+
13+
#Then, recreate the pg_repack extension
14+
su - pe-postgres -s '/bin/bash' -c '/opt/puppetlabs/server/bin/psql -d pe-puppetdb -c "CREATE EXTENSION pg_repack"' || fail "unable to create pg_repack estenstion"
15+
else
16+
fail "No running PE-PostgreSQL instance found, please run this task against your Primary or PE-PostgreSQL server"
17+
fi
18+
19+
20+
success '{ "status": "success" }'

0 commit comments

Comments
 (0)