Skip to content

Commit 5356ab1

Browse files
authored
Merge pull request #1 from okbob/master
Update from master
2 parents c49dd72 + 6508d8d commit 5356ab1

43 files changed

Lines changed: 12453 additions & 1325 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

META.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
"name": "plpgsql_check",
33
"abstract": "Additional tools for plpgsql functions validation",
44
"description": "The plpgsql_check is PostgreSQL extension with functionality for direct or indirect extra validation of functions in plpgsql language. It verifies a validity of SQL identifiers used in plpgsql code. It try to identify a performance issues.",
5-
"version": "1.0.5",
5+
"version": "1.2.3",
66
"maintainer": "Pavel STEHULE <pavel.stehule@gmail.com>",
77
"license": "bsd",
88
"provides": {
99
"plpgsql_check": {
1010
"abstract": "Additional tools for plpgsql functions validation",
1111
"file": "sql/plpgsql_check_active.sql",
1212
"docfile": "README.md",
13-
"version": "1.0.5"
13+
"version": "1.2.3"
1414
}
1515
},
1616
"prereqs": {
1717
"runtime": {
1818
"requires": {
19-
"PostgreSQL": "9.2.14",
19+
"PostgreSQL": "9.4.17",
2020
"plpgsql": 0
2121
}
2222
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
MODULE_big = plpgsql_check
44
OBJS = plpgsql_check.o
5-
DATA = plpgsql_check--1.0.sql plpgsql_check--1.1.sql plpgsql_check--1.0--1.1.sql plpgsql_check--1.2.sql plpgsql_check--1.1--1.2.sql
5+
DATA = plpgsql_check--1.0--1.1.sql plpgsql_check--1.3.sql plpgsql_check--1.1--1.2.sql plpgsql_check--1.2--1.3.sql
66
EXTENSION = plpgsql_check
77

88
ifndef MAJORVERSION

README.md

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@ If you like it and if you would to join to development of this extension, regist
1212
yourself to [postgresql extension hacking](https://groups.google.com/forum/#!forum/postgresql-extensions-hacking)
1313
google group.
1414

15+
# Features
16+
17+
* check fields of referenced database objects and types inside embedded SQL
18+
* using correct types of function parameters
19+
* unused variables and function argumens, unmodified OUT argumens
20+
* partially detection of dead code (due RETURN command)
21+
* detection of missing RETURN command in function
22+
* try to identify unwanted hidden casts, that can be performance issue like unused indexes
23+
* possibility to collect relations and functions used by function
24+
1525
I invite any ideas, patches, bugreports
1626

1727
plpgsql_check is next generation of plpgsql_lint. It allows to check source code by explicit call
1828
<i>plpgsql_check_function</i>.
1929

20-
PostgreSQL 9.2 is required, PostgreSQL 9.3, 9.4, 9.5 and 9.6 are supported
30+
PostgreSQL PostgreSQL 9.4, 9.5, 9.6, 10, 11 are supported (Develop 12 is supported too).
31+
32+
PostgreSQL 9.3 is compileable, but regress tests are not maintained.
2133

2234
The SQL statements inside PL/pgSQL functions are checked by validator for semantic errors. These errors
2335
can be found by plpgsql_check_function:
@@ -111,6 +123,23 @@ Function plpgsql_check_function() has two possible formats: text or xml
111123
</Function>
112124
(1 row)
113125

126+
## Options
127+
128+
You can set level of warnings via function's parameters:
129+
130+
* `fatal_errors boolean DEFAULT true` - stop on first error
131+
132+
* `other_warnings boolean DEFAULT true` - show warnings like different attributes number
133+
in assignmenet on left and right side, variable overlaps function's parameter, unused
134+
variables, unwanted casting, ..
135+
136+
* `extra_warnings boolean DEFAULT true` - show warnings like missing `RETURN`,
137+
shadowed variables, dead code, never read (unused) function's parameter,
138+
unmodified variables, ..
139+
140+
* `performance_warnings boolean DEFAULT false` - performance related warnings like
141+
declared type with type modificator, casting, implicit casts in where clause (can be
142+
reason why index is not used), ..
114143

115144
## Triggers
116145

@@ -156,6 +185,16 @@ triggers. Please, test following queries:
156185
JOIN pg_catalog.pg_language l ON p.prolang = l.oid
157186
WHERE l.lanname = 'plpgsql' AND p.prorettype <> 2279;
158187

188+
or
189+
190+
SELECT p.proname, tgrelid::regclass, cf.*
191+
FROM pg_proc p
192+
JOIN pg_trigger t ON t.tgfoid = p.oid
193+
JOIN pg_language l ON p.prolang = l.oid
194+
JOIN pg_namespace n ON p.pronamespace = n.oid,
195+
LATERAL plpgsql_check_function(p.oid, t.tgrelid) cf
196+
WHERE n.nspname = 'public' and l.lanname = 'plpgsql'
197+
159198
or
160199

161200
-- check all plpgsql functions (functions or trigger functions with defined triggers)
@@ -297,6 +336,22 @@ tables. So you can do (with following trick safetly):
297336
This trick emulates GLOBAL TEMP tables partially and it allows a statical validation.
298337
Other possibility is using a [template foreign data wrapper] (https://github.com/okbob/template_fdw)
299338

339+
# Dependency list
340+
341+
A function <i>plpgsql_show_dependency_tb</i> can show all functions and relations used
342+
inside processed function:
343+
344+
postgres=# select * from plpgsql_show_dependency_tb('testfunc(int,float)');
345+
┌──────────┬───────┬────────┬─────────┬────────────────────────────┐
346+
│ type │ oid │ schema │ name │ params │
347+
╞══════════╪═══════╪════════╪═════════╪════════════════════════════╡
348+
│ FUNCTION │ 36008 │ public │ myfunc1 │ (integer,double precision) │
349+
│ FUNCTION │ 35999 │ public │ myfunc2 │ (integer,double precision) │
350+
│ RELATION │ 36005 │ public │ myview │ │
351+
│ RELATION │ 36002 │ public │ mytable │ │
352+
└──────────┴───────┴────────┴─────────┴────────────────────────────┘
353+
(4 rows)
354+
300355
# Compilation
301356

302357
You need a development environment for PostgreSQL extensions:
@@ -387,12 +442,13 @@ or compile by self:
387442
1. copy `plpgsql_check.dll` to `PostgreSQL\9.3\lib`
388443
2. copy `plpgsql_check.control` and `plpgsql_check--0.8.sql` to `PostgreSQL\9.3\share\extension`
389444

390-
391445
## Checked on
392446

393447
* gcc on Linux (against all supported PostgreSQL)
394448
* clang 3.4 on Linux (against PostgreSQL 9.5)
395-
* for success regress tests the PostgreSQL 9.2.14, 9.3.10, 9.4.5, 9.5 or 9.6 is required.
449+
* for success regress tests the PostgreSQL 9.3.10, 9.4.5, 9.5 or higher is required
450+
451+
Compilation against PostgreSQL 10 requires libICU!
396452

397453
# Licence
398454

TODO.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* performance warning - correct flag for function VOLATILE, STABLE, IMMUTABLE
2+
* coverage calculation
3+
* simple PLpgSQL profiler

_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
theme: jekyll-theme-cayman

expected/README

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
plpgsql_check_active_2.out PostgreSQL 9.4, 9.5, 9.6
2+
plpgsql_check_active.out PostgreSQL 11
3+
plpgsql_check_active_1.out PostgreSQL 10
4+

expected/plpgsql_check_active-10.out

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,10 @@ begin
188188
end;
189189
$$ language plpgsql;
190190
select * from plpgsql_check_function('fx2()', performance_warnings := true);
191-
plpgsql_check_function
192-
------------------------
193-
(0 rows)
191+
plpgsql_check_function
192+
---------------------------------------------------------
193+
warning extra:00000:2:DECLARE:never read variable "_pa"
194+
(1 row)
194195

195196
drop function fx2();
196197
create or replace function fx2()
@@ -202,9 +203,10 @@ begin
202203
end;
203204
$$ language plpgsql;
204205
select * from plpgsql_check_function('fx2()', performance_warnings := true);
205-
plpgsql_check_function
206-
------------------------
207-
(0 rows)
206+
plpgsql_check_function
207+
---------------------------------------------------------
208+
warning extra:00000:2:DECLARE:never read variable "_pa"
209+
(1 row)
208210

209211
drop function fx2();
210212
create type _exception_type as (
@@ -257,9 +259,10 @@ select f1();
257259
(1 row)
258260

259261
select * from plpgsql_check_function_tb('f1()');
260-
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
261-
------------+--------+-----------+----------+---------+--------+------+-------+----------+-------+---------
262-
(0 rows)
262+
functionid | lineno | statement | sqlstate | message | detail | hint | level | position | query | context
263+
------------+--------+-----------+----------+----------------------------------+--------+------+---------------+----------+-------+---------
264+
f1 | 3 | DECLARE | 00000 | never read variable "_exception" | | | warning extra | | |
265+
(1 row)
263266

264267
drop function f1();
265268
drop type _exception_type;

0 commit comments

Comments
 (0)