plpgsql_check
extended check for plpgsql functions
Repository
okbob/plpgsql_check
https://github.com/okbob/plpgsql_check
Source
plpgsql_check-2.8.11.tar.gz
plpgsql_check-2.8.11.tar.gz
Overview
| Package | Version | Category | License | Language |
|---|---|---|---|---|
plpgsql_check | 2.8.11 | LANG | MIT | C |
| ID | Extension | Bin | Lib | Load | Create | Trust | Reloc | Schema |
|---|---|---|---|---|---|---|---|---|
| 3060 | plpgsql_check | No | Yes | Yes | Yes | No | No | - |
| Related | plpgsql pldbgapi plprofiler pg_hint_plan pgtap auto_explain plv8 plperl plpython3u |
|---|
Version
| Type | Repo | Version | PG Ver | Package | Deps |
|---|---|---|---|---|---|
| EXT | PGDG | 2.8.11 | 1817161514 | plpgsql_check | plpgsql |
| RPM | PGDG | 2.8.10 | 1817161514 | plpgsql_check_$v | - |
| DEB | PGDG | 2.8.11 | 1817161514 | postgresql-$v-plpgsql-check | - |
Install
You can install plpgsql_check directly. First, make sure the PGDG repository is added and enabled:
pig repo add pgdg -u # Add PGDG repo and update cache
Install the extension using pig or apt/yum/dnf:
pig install plpgsql_check; # Install for current active PG version
pig ext install -y plpgsql_check -v 18 # PG 18
pig ext install -y plpgsql_check -v 17 # PG 17
pig ext install -y plpgsql_check -v 16 # PG 16
pig ext install -y plpgsql_check -v 15 # PG 15
pig ext install -y plpgsql_check -v 14 # PG 14
dnf install -y plpgsql_check_18 # PG 18
dnf install -y plpgsql_check_17 # PG 17
dnf install -y plpgsql_check_16 # PG 16
dnf install -y plpgsql_check_15 # PG 15
dnf install -y plpgsql_check_14 # PG 14
apt install -y postgresql-18-plpgsql-check # PG 18
apt install -y postgresql-17-plpgsql-check # PG 17
apt install -y postgresql-16-plpgsql-check # PG 16
apt install -y postgresql-15-plpgsql-check # PG 15
apt install -y postgresql-14-plpgsql-check # PG 14
Preload:
shared_preload_libraries = 'plpgsql_check';
Create Extension:
CREATE EXTENSION plpgsql_check CASCADE; -- requires: plpgsql
Usage
plpgsql_check is a linter and checker for PL/pgSQL functions that detects errors at development time rather than runtime.
CREATE EXTENSION plpgsql_check;
Check a Function
SELECT * FROM plpgsql_check_function('my_function()');
SELECT * FROM plpgsql_check_function('my_function(int, text)');
SELECT * FROM plpgsql_check_function('my_function()', fatal_errors := false);
Output Formats
SELECT * FROM plpgsql_check_function('fx()', format := 'text');
SELECT * FROM plpgsql_check_function('fx()', format := 'json');
SELECT * FROM plpgsql_check_function('fx()', format := 'xml');
Check Trigger Functions
-- Trigger functions need the associated table
SELECT * FROM plpgsql_check_function('my_trigger_func()', 'my_table');
-- With transition tables
SELECT * FROM plpgsql_check_function(
'my_trigger_func()', 'my_table',
newtable := 'newtab', oldtable := 'oldtab'
);
Warning Categories
SELECT * FROM plpgsql_check_function('fx()',
extra_warnings := true, -- dead code, unused parameters
performance_warnings := true, -- index and casting issues
security_warnings := true, -- SQL injection checks
compatibility_warnings := true -- obsolete patterns
);
Batch Check All Functions
SELECT p.oid, p.proname, plpgsql_check_function(p.oid)
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc p ON pronamespace = n.oid
JOIN pg_catalog.pg_language l ON p.prolang = l.oid
WHERE l.lanname = 'plpgsql' AND p.prorettype <> 2279;
Passive Mode (Check on Execution)
LOAD 'plpgsql_check';
SET plpgsql_check.mode = 'every_start'; -- check before each execution
Or in postgresql.conf:
shared_preload_libraries = 'plpgsql,plpgsql_check'
plpgsql_check.mode = 'every_start'
Profiler
-- Enable profiling
SELECT plpgsql_check_profiler(true);
-- Execute functions to collect data
SELECT my_function();
-- View per-line execution times
SELECT lineno, avg_time, source
FROM plpgsql_profiler_function_tb('my_function()');
-- Per-statement profile
SELECT stmtid, parent_stmtid, lineno, exec_stmts, stmtname
FROM plpgsql_profiler_function_statements_tb('my_function()');
-- All function statistics
SELECT * FROM plpgsql_profiler_functions_all();
-- Reset profiling data
SELECT plpgsql_profiler_reset_all();
Dependency Tracking
SELECT * FROM plpgsql_show_dependency_tb('my_function(int)');
Coverage Metrics
SELECT * FROM plpgsql_coverage_statements('my_function()');
SELECT * FROM plpgsql_coverage_branches('my_function()');
Pragma Directives
Embed checking options in function comments:
CREATE OR REPLACE FUNCTION fx(anyelement) RETURNS text AS $$
BEGIN
/* @plpgsql_check_options: anyelementtype = text */
RETURN $1;
END;
$$ LANGUAGE plpgsql;
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.