这就是delete.sh的样子:
#!/bin/bash
#db call to get database name
PSQL="psql -q"
pushd `dirname $0`
for dbname in `$PSQL -t -f "../sql/db_name.sql"`
do
echo "Results for database ${dbname}"
$PSQL -d "${dbname}" -c "select pgstatspack_delete_snap ();"
echo ""
echo ""
done
popd
我在哪里指定应删除快照的旧天数?
答案 0 :(得分:0)
http://pgfoundry.org/frs/?group_id=1000375
pgstatspack_version_2.3.1.tar.gz有pgstatspack\sql\pgstatspack_delete_old_stats.sql
个:
CREATE OR REPLACE FUNCTION pgstatspack_delete_snap () returns varchar(512) AS $$
DECLARE
old_snap_time TIMESTAMP;
old_snap_id BIGINT;
message VARCHAR(512);
BEGIN
SELECT current_timestamp - interval '30 days' INTO old_snap_time;
SELECT max(snapid) INTO old_snap_id FROM pgstatspack_snap WHERE ts < old_snap_time;
SELECT 'Deleted '||count(snapid)||' snapshots older than '||old_snap_time
INTO message
FROM pgstatspack_snap
WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_snap WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_database WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_tables WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_indexes WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_sequences WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_settings WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_statements WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_bgwriter WHERE snapid <= old_snap_id;
RETURN message;
END;
$$ LANGUAGE plpgsql;
所以只需创建你的功能:
CREATE OR REPLACE FUNCTION pgstatspack_delete_snap (_d interval default '30 days') returns varchar(512) AS $$
DECLARE
old_snap_time TIMESTAMP;
old_snap_id BIGINT;
message VARCHAR(512);
BEGIN
SELECT current_timestamp - _d INTO old_snap_time;
SELECT max(snapid) INTO old_snap_id FROM pgstatspack_snap WHERE ts < old_snap_time;
SELECT 'Deleted '||count(snapid)||' snapshots older than '||old_snap_time
INTO message
FROM pgstatspack_snap
WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_snap WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_database WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_tables WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_indexes WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_sequences WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_settings WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_statements WHERE snapid <= old_snap_id;
DELETE FROM pgstatspack_bgwriter WHERE snapid <= old_snap_id;
RETURN message;
END;
$$ LANGUAGE plpgsql;
并将其命名为:
$PSQL -d "${dbname}" -c "select pgstatspack_delete_snap ('15 days');"