procedure-rebuild-pg-get-functiondef
Procedure Rebuild via pg_get_functiondef
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 18
- Forks
- 6
01 前言 Front Matter
- name
- procedure-rebuild-pg-get-functiondef
- metadata
- { "original-index": 6, "short-description": "Procedure Rebuild via pg_get_functiondef" }
- description
- Procedure Rebuild via pg_get_functiondef
02 资源文件 1
- 参考文档skill.json175 B
03 SKILL.md 正文
Procedure Rebuild via pg_get_functiondef
What
A technique for modifying stored procedures by extracting their current definition from the catalog, performing text replacements on the SQL source, and re-executing the modified definition -- all within a single DO block.
Why
Manually rewriting a stored procedure is error-prone and doesn't scale. By
using pg_get_functiondef, you get the exact current definition from
PostgreSQL's catalog, modify only what you need, and re-create it with
CREATE OR REPLACE. This guarantees you don't accidentally change anything
else in the procedure.
How
DO $$
DECLARE
v_def text;
BEGIN
-- Step 1: Extract current definition
SELECT pg_get_functiondef(p.oid) INTO v_def
FROM pg_proc p
WHERE p.proname = 'MyProcedure'
AND p.pronamespace = 'public'::regnamespace;
-- Step 2: Check if replacement is needed
IF v_def IS NOT NULL
AND v_def LIKE '%"OldColumnName"%'
THEN
-- Step 3: Text replacement (surgical)
v_def := replace(v_def, '"OldColumnName"', '"NewColumnName"');
-- Step 4: Re-create the procedure
EXECUTE v_def;
RAISE NOTICE 'Updated: MyProcedure';
ELSE
RAISE NOTICE 'Skipped: MyProcedure (already correct or not found)';
END IF;
END $$;
What pg_get_functiondef Returns
It returns a complete CREATE OR REPLACE FUNCTION/PROCEDURE statement including:
- Full signature with parameter names and types
- Function body
- Language declaration
- Security attributes
- All options (VOLATILE, COST, ROWS, etc.)
This means EXECUTE v_def is equivalent to running the original DDL -- it
replaces the procedure in-place without dropping it.
When to Use
- Renaming column references inside procedures
- Any bulk search-and-replace across procedure bodies
- When you need to modify procedures atomically alongside other DDL changes
Where We Used It
- ****: Updated 4 procedures to replace misspelled quoted column names with corrected spellings. Each procedure had its definition extracted, 4 replacements applied, and re-created via EXECUTE.
Gotchas
pg_get_functiondefreturns the definition as PostgreSQL sees it, which may differ from the original DDL (formatting, quoting, etc.)- The
replace()function is case-sensitive -- match the exact casing - Parameter names appear in the function signature AND may appear in the body; use quoted column names in your LIKE/replace to avoid accidentally modifying parameter references
- If two procedures have the same name but different signatures (overloads), you need to disambiguate by OID or argument types
*Category: DDL | 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.