# Dead Tuple Auto-Vacuum Guide

## What was added

The weekly PostgreSQL monitoring workflow now supports **conditional `VACUUM ANALYZE`** based on dead tuple thresholds.

This was added to:

- `scripts\Weekly-Performance-Monitor.ps1`
- `scripts\Setup-Weekly-Monitor.ps1`

When enabled, the monitor checks `pg_stat_user_tables` and runs:

```sql
VACUUM (ANALYZE, VERBOSE) schema.table_name;
```

only for tables that exceed both configured thresholds.

## New monitor parameters

`scripts\Weekly-Performance-Monitor.ps1` now supports:

- `-AutoVacuumAnalyze`  
  Enables conditional vacuum/analyze.
- `-DeadTupleThreshold <int>`  
  Minimum dead tuples required (default: `10000`).
- `-DeadTuplePercentThreshold <double>`  
  Minimum dead tuple percentage required (default: `20.0`).

## How to run manually

```powershell
.\scripts\Weekly-Performance-Monitor.ps1 -AutoVacuumAnalyze
```

Example with custom thresholds:

```powershell
.\scripts\Weekly-Performance-Monitor.ps1 -AutoVacuumAnalyze -DeadTupleThreshold 15000 -DeadTuplePercentThreshold 25
```

## How to enable in the scheduled task

`scripts\Setup-Weekly-Monitor.ps1` now supports:

- `-EnableAutoVacuumAnalyze`
- `-DeadTupleThreshold <int>`
- `-DeadTuplePercentThreshold <double>`

Create or update the weekly task with auto-vacuum enabled:

```powershell
.\scripts\Setup-Weekly-Monitor.ps1 -EnableAutoVacuumAnalyze -DeadTupleThreshold 10000 -DeadTuplePercentThreshold 20
```

The setup script passes these options to the weekly monitor when the scheduled task runs.

## Notes

- Conditional vacuum is **opt-in** and disabled unless you pass the enable flag.
- Threshold checks are currently scoped to these schemas:
  - `library`
  - `users`
  - `authentication`
  - `displaypreferences`
  - `activitylog`
