diff --git a/README.md b/README.md index 043ce7a..fe80643 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/files/common.sh b/files/common.sh new file mode 100644 index 0000000..8af0b0f --- /dev/null +++ b/files/common.sh @@ -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 diff --git a/tasks/reset_pgrepack_schema.json b/tasks/reset_pgrepack_schema.json new file mode 100644 index 0000000..10622c9 --- /dev/null +++ b/tasks/reset_pgrepack_schema.json @@ -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"} + ] +} diff --git a/tasks/reset_pgrepack_schema.sh b/tasks/reset_pgrepack_schema.sh new file mode 100644 index 0000000..84eb0bf --- /dev/null +++ b/tasks/reset_pgrepack_schema.sh @@ -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" }'