Skip to content

(SUP-2372) add pg_repack schema reset task #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,16 @@ This module provides cron jobs to VACUUM FULL tables in the `pe-puppetdb` databa

With PE 2018.1.7 and 2019.0.2 and newer, this module uses `pg_repack` instead of `VACUUM FULL`.

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:

```
INFO: repacking table "public.fact_paths"
WARNING: the table "public.fact_paths" already has a trigger called "repack_trigger"
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.
```

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.

### Vacuuming

Generally speaking, PostgreSQL keeps itself in good shape with a process called [auto vacuuming](https://www.postgresql.org/docs/11/runtime-config-autovacuum.html).
Expand Down
37 changes: 37 additions & 0 deletions files/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

# TODO: helper task?

# Exit with an error message and error code, defaulting to 1
fail() {
# Print a stderr: entry if there were anything printed to stderr
if [[ -s $_tmp ]]; then
# Hack to try and output valid json by replacing newlines with spaces.
echo "{ \"status\": \"error\", \"message\": \"$1\", \"stderr\": \"$(tr '\n' ' ' <$_tmp)\" }"
else
echo "{ \"status\": \"error\", \"message\": \"$1\" }"
fi

exit ${2:-1}
}

success() {
echo "$1"
exit 0
}

# Test for colors. If unavailable, unset variables are ok
if tput colors &>/dev/null; then
green="$(tput setaf 2)"
red="$(tput setaf 1)"
reset="$(tput sgr0)"
fi

_tmp="$(mktemp)"
exec 2>>"$_tmp"

# Use indirection to munge PT_ environment variables
# e.g. "$PT_version" becomes "$version"
for v in ${!PT_*}; do
declare "${v#*PT_}"="${!v}"
done
8 changes: 8 additions & 0 deletions tasks/reset_pgrepack_schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"puppet_task_version": 1,
"supports_noop": false,
"description": "This task will remove and recreate the pg_repack extension",
"implementations": [
{"name": "reset_pgrepack_schema.sh", "requirements": ["shell"], "files": ["pe_databases/files/common.sh"], "input_method": "environment"}
]
}
20 changes: 20 additions & 0 deletions tasks/reset_pgrepack_schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

# Puppet Task Name: reset_pgrepack_schema
declare PT__installdir
# shellcheck disable=SC1090
source "$PT__installdir/pe_databases/files/common.sh"

#Determine if PE Postgres is available
if puppet resource service pe-postgresql | grep -q running; then
#Remove the pg_repack extension
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"

#Then, recreate the pg_repack extension
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"
else
fail "No running PE-PostgreSQL instance found, please run this task against your Primary or PE-PostgreSQL server"
fi


success '{ "status": "success" }'