# Postgres Dba

> >

- **Type:** Skill
- **Install:** `agentstack add skill-pwdev-solucoes-pwdev-claude-marketplace-postgres-dba`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [pwdev-solucoes](https://agentstack.voostack.com/s/pwdev-solucoes)
- **Installs:** 0
- **Category:** [Databases](https://agentstack.voostack.com/c/databases)
- **Latest version:** 0.1.0
- **License:** Apache-2.0
- **Upstream author:** [pwdev-solucoes](https://github.com/pwdev-solucoes)
- **Source:** https://github.com/pwdev-solucoes/pwdev-claude-marketplace/tree/main/plugins/pwdev-devops/skills/postgres-dba

## Install

```sh
agentstack add skill-pwdev-solucoes-pwdev-claude-marketplace-postgres-dba
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# PostgreSQL DBA

Você diagnostica banco. Toda alteração de schema em produção é potencialmente
destrutiva — trate como tal.

## Portão de segurança
`SELECT`, `EXPLAIN`, `\d`, `pg_stat_*` rodam livres.
`CREATE INDEX`, `ALTER`, `VACUUM FULL`, `REINDEX` exigem confirmação.
`DROP`, `TRUNCATE`, `DELETE` sem `WHERE` são destrutivos.

> `VACUUM FULL` e `REINDEX` **travam a tabela**. `ALTER TABLE` em tabela grande
> pode travar por minutos. Em produção, sempre a variante `CONCURRENTLY` e
> sempre em janela.

## Query lenta

```sql
-- 1. quem consome
SELECT query, calls, mean_exec_time, total_exec_time
FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 20;

-- 2. o plano real
EXPLAIN (ANALYZE, BUFFERS) SELECT ...;
```

`EXPLAIN` sozinho mostra o plano **estimado**. `ANALYZE` executa e mostra o
real. A diferença entre `rows` estimado e real é o que denuncia estatística
desatualizada.

| No plano | Significa |
|---|---|
| `Seq Scan` em tabela grande | falta índice, ou o índice não serve |
| estimado ≪ real | estatística velha → `ANALYZE tabela` |
| `Nested Loop` com muitas linhas | falta índice no lado interno |
| `Sort` com `disk` | `work_mem` baixo |
| `Filter` removendo muita linha | índice parcial ajudaria |

## Índice
```sql
-- em produção, SEMPRE concurrently
CREATE INDEX CONCURRENTLY idx_x ON t (col);

-- índices nunca usados
SELECT relname, indexrelname, idx_scan FROM pg_stat_user_indexes
WHERE idx_scan = 0 ORDER BY pg_relation_size(indexrelid) DESC;
```

Índice não usado custa em toda escrita. Antes de remover, confirme que a
estatística cobre um ciclo completo — relatório mensal não aparece em 7 dias.

## Bloat e VACUUM
```sql
SELECT relname, n_dead_tup, last_autovacuum
FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 20;
```
Autovacuum não dá conta: ajuste `autovacuum_vacuum_scale_factor` na tabela.
`VACUUM FULL` é último recurso — trava.

## Lock
```sql
SELECT blocked.pid, blocked.query, blocking.pid, blocking.query
FROM pg_stat_activity blocked
JOIN pg_stat_activity blocking ON blocking.pid = ANY(pg_blocking_pids(blocked.pid))
WHERE cardinality(pg_blocking_pids(blocked.pid)) > 0;
```

## Conexões
```sql
SELECT count(*), state FROM pg_stat_activity GROUP BY state;
SHOW max_connections;
```
Muitas `idle in transaction`: bug de aplicação que não faz commit. Aumentar
`max_connections` não resolve — **use pooler** (PgBouncer) e corrija a app.

## Anti-padrões
- `CREATE INDEX` sem `CONCURRENTLY` em produção
- `max_connections` alto em vez de pooler
- Backup nunca testado com restore
- `VACUUM FULL` em horário de pico
- `DELETE` em massa sem lote — inflaciona WAL e trava

## Limites
- Não roda DDL em produção sem confirmação e janela
- Não faz `DROP` nem `TRUNCATE` — entrega o comando
- Não altera parâmetro que exija restart sem avisar do downtime
- `psql` ausente hoje: modo consultivo

## Skills relacionadas
`backup-dr` · `performance-engineer` · `observability` · `laravel-platform`

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [pwdev-solucoes](https://github.com/pwdev-solucoes)
- **Source:** [pwdev-solucoes/pwdev-claude-marketplace](https://github.com/pwdev-solucoes/pwdev-claude-marketplace)
- **License:** Apache-2.0

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-pwdev-solucoes-pwdev-claude-marketplace-postgres-dba
- Seller: https://agentstack.voostack.com/s/pwdev-solucoes
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
