pg-cron-scheduled-maintenance
pg_cron Scheduled Maintenance
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 18
- Forks
- 6
01 前言 Front Matter
- name
- pg-cron-scheduled-maintenance
- metadata
- { "original-index": 16, "short-description": "pg_cron Scheduled Maintenance" }
- description
- pg_cron Scheduled Maintenance
02 资源文件 1
- 参考文档skill.json157 B
03 SKILL.md 正文
pg_cron Scheduled Maintenance
What
Using PostgreSQL's pg_cron extension to schedule recurring database maintenance
jobs -- data purges, retention enforcement, statistics refresh -- directly inside
the database engine, without external schedulers.
Why
External schedulers (cron, Azure Automation, Task Scheduler) add moving parts:
credentials, network access, deployment pipelines. pg_cron runs inside PostgreSQL
itself -- no network hops, no external auth, no separate deployment. The job
definition lives alongside the data it maintains.
How
Step 1: Enable the extension
CREATE EXTENSION IF NOT EXISTS pg_cron;
Step 2: Create the maintenance procedure
CREATE OR REPLACE PROCEDURE maintenance.purge_expired_tokens()
LANGUAGE plpgsql AS $$
DECLARE
v_deleted bigint;
BEGIN
DELETE FROM public."RefreshTokens"
WHERE "ExpiresAt" < now() - interval '7 days';
GET DIAGNOSTICS v_deleted = ROW_COUNT;
RAISE NOTICE 'Purged % expired tokens', v_deleted;
END;
$$;
Step 3: Schedule the job
-- Run every hour at minute 0
SELECT cron.schedule(
'purge-expired-tokens', -- job name
'0 * * * *', -- cron expression
$$CALL maintenance.purge_expired_tokens()$$
);
Step 4: Validate
-- Check job is registered
SELECT jobid, jobname, schedule, command
FROM cron.job
WHERE jobname = 'purge-expired-tokens';
-- Check execution history
SELECT jobid, start_time, end_time, status, return_message
FROM cron.job_run_details
ORDER BY start_time DESC
LIMIT 10;
Step 5: Rollback plan
SELECT cron.unschedule('purge-expired-tokens');
DROP PROCEDURE IF EXISTS maintenance.purge_expired_tokens();
When to Use
- Expired data cleanup (tokens, sessions, logs)
- Retention policy enforcement (delete rows older than N days)
- Statistics refresh (
ANALYZEon hot tables) - Any recurring task that only touches data inside the database
Where We Used It
- ****: Hourly purge of expired refresh tokens
- ****: Retention policy for ShiftAuditLog via scheduled procedure
Related Skills
- Skill #21 (Data Retention Lifecycle) -- uses pg_cron for retention scheduling
- Skill #18 (Schema Separation) -- maintenance procedures in
maintenanceschema
References
- PostgreSQL Best Practices -- see Backlog #13 (Log rotation mechanism)
Gotchas
pg_cronruns jobs in thepostgresdatabase by default -- usecron.schedule_in_database()for other databases on Azure Flexible Server- Jobs run as the
pg_cronsuperuser -- ensure procedures have proper security context (SECURITY DEFINERif needed) - Azure Flexible Server requires
pg_croninshared_preload_libraries(set via Portal > Server parameters) - Always include a validation step: check
cron.job_run_detailsafter first run - Include a rollback script (
cron.unschedule) in the ticket
*Category: Tooling | Origin: , *
04 同仓库更多 Skills
yeet
Use only when the user explicitly asks to stage, commit, push, and open a GitHub pull request in one flow using the GitHub CLI (`gh`).
wrangler
Cloudflare Workers CLI for deploying, developing, and managing Workers, KV, R2, D1, Vectorize, Hyperdrive, Workers AI, Containers, Queues, Workflows, Pipelines, and Secrets Store. Load before running wrangler commands to ensure correct syntax and best practices. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.
workspace-skill-discovery
Discover and include ALL skills across the workspace — not just global ones. Use at the start of every session or when the user asks about available skills. Ensures project-level skills (under .claude/skills/ or skills/ in any repo) are never missed.
workers-best-practices
Reviews and authors Cloudflare Workers code against production best practices. Load when writing new Workers, reviewing Worker code, configuring wrangler.jsonc, or checking for common Workers anti-patterns (streaming, floating promises, global state, secrets, bindings, observability). Biases towards retrieval from Cloudflare docs over pre-trained knowledge.
winui-app
Create, build and design WinUI 3 desktop apps with C# and the Windows App SDK following Microsoft's official guidance and CommunityToolkit. Covers XAML, navigation, theming, accessibility and deployment.
windows-brain-script-portability
Scripts in ~/.claude/scripts/ that run on a Windows brain: use the python3.cmd shim and not bare python3, and reconfigure stdout to UTF-8 if you print symbols. Use it when writing a brain script or triaging a Windows-only crash.