Skip to content

Commit be7e77e

Browse files
committed
fix broken assert in profiler code #177
1 parent 716bb05 commit be7e77e

File tree

5 files changed

+309
-1
lines changed

5 files changed

+309
-1
lines changed

expected/plpgsql_check_active.out

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,89 @@ select f1();
265265
(1 row)
266266

267267
drop function f1();
268+
create or replace function covtest(int)
269+
returns int as $$
270+
declare a int = $1;
271+
begin
272+
a := a + 1;
273+
if a < 10 then
274+
a := a + 1;
275+
drop function test_function();
276+
drop function test_function();
277+
drop function test_function();
278+
drop function test_function();
279+
drop function test_function();
280+
drop function test_function();
281+
drop function test_function();
282+
drop function test_function();
283+
drop function test_function();
284+
drop function test_function();
285+
drop function test_function();
286+
drop function test_function;
287+
drop function test_function();
288+
drop function test_function();
289+
drop function test_function();
290+
drop function test_function();
291+
drop function test_function();
292+
drop function test_function();
293+
drop function test_function();
294+
drop function test_function();
295+
drop function test_function();
296+
drop function test_function();
297+
drop function test_function();
298+
drop function test_function();
299+
drop function test_function;
300+
drop function test_function();
301+
end if;
302+
a := a + 1;
303+
return a;
304+
end;
305+
$$ language plpgsql;
306+
select covtest(10);
307+
covtest
308+
---------
309+
12
310+
(1 row)
311+
312+
-- should not crash
313+
select stmtid, exec_stmts, stmtname from plpgsql_profiler_function_statements_tb('covtest');
314+
stmtid | exec_stmts | stmtname
315+
--------+------------+-----------------
316+
1 | 1 | statement block
317+
2 | 1 | assignment
318+
3 | 1 | IF
319+
4 | 0 | assignment
320+
5 | 0 | SQL statement
321+
6 | 0 | SQL statement
322+
7 | 0 | SQL statement
323+
8 | 0 | SQL statement
324+
9 | 0 | SQL statement
325+
10 | 0 | SQL statement
326+
11 | 0 | SQL statement
327+
12 | 0 | SQL statement
328+
13 | 0 | SQL statement
329+
14 | 0 | SQL statement
330+
15 | 0 | SQL statement
331+
16 | 0 | SQL statement
332+
17 | 0 | SQL statement
333+
18 | 0 | SQL statement
334+
19 | 0 | SQL statement
335+
20 | 0 | SQL statement
336+
21 | 0 | SQL statement
337+
22 | 0 | SQL statement
338+
23 | 0 | SQL statement
339+
24 | 0 | SQL statement
340+
25 | 0 | SQL statement
341+
26 | 0 | SQL statement
342+
27 | 0 | SQL statement
343+
28 | 0 | SQL statement
344+
29 | 0 | SQL statement
345+
30 | 0 | SQL statement
346+
31 | 1 | assignment
347+
32 | 1 | RETURN
348+
(32 rows)
349+
350+
drop function covtest;
268351
set plpgsql_check.profiler to off;
269352
create function f1()
270353
returns void as $$

expected/plpgsql_check_active_1.out

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,89 @@ select f1();
265265
(1 row)
266266

267267
drop function f1();
268+
create or replace function covtest(int)
269+
returns int as $$
270+
declare a int = $1;
271+
begin
272+
a := a + 1;
273+
if a < 10 then
274+
a := a + 1;
275+
drop function test_function();
276+
drop function test_function();
277+
drop function test_function();
278+
drop function test_function();
279+
drop function test_function();
280+
drop function test_function();
281+
drop function test_function();
282+
drop function test_function();
283+
drop function test_function();
284+
drop function test_function();
285+
drop function test_function();
286+
drop function test_function;
287+
drop function test_function();
288+
drop function test_function();
289+
drop function test_function();
290+
drop function test_function();
291+
drop function test_function();
292+
drop function test_function();
293+
drop function test_function();
294+
drop function test_function();
295+
drop function test_function();
296+
drop function test_function();
297+
drop function test_function();
298+
drop function test_function();
299+
drop function test_function;
300+
drop function test_function();
301+
end if;
302+
a := a + 1;
303+
return a;
304+
end;
305+
$$ language plpgsql;
306+
select covtest(10);
307+
covtest
308+
---------
309+
12
310+
(1 row)
311+
312+
-- should not crash
313+
select stmtid, exec_stmts, stmtname from plpgsql_profiler_function_statements_tb('covtest');
314+
stmtid | exec_stmts | stmtname
315+
--------+------------+-----------------
316+
1 | 1 | statement block
317+
2 | 1 | assignment
318+
3 | 1 | IF
319+
4 | 0 | assignment
320+
5 | 0 | SQL statement
321+
6 | 0 | SQL statement
322+
7 | 0 | SQL statement
323+
8 | 0 | SQL statement
324+
9 | 0 | SQL statement
325+
10 | 0 | SQL statement
326+
11 | 0 | SQL statement
327+
12 | 0 | SQL statement
328+
13 | 0 | SQL statement
329+
14 | 0 | SQL statement
330+
15 | 0 | SQL statement
331+
16 | 0 | SQL statement
332+
17 | 0 | SQL statement
333+
18 | 0 | SQL statement
334+
19 | 0 | SQL statement
335+
20 | 0 | SQL statement
336+
21 | 0 | SQL statement
337+
22 | 0 | SQL statement
338+
23 | 0 | SQL statement
339+
24 | 0 | SQL statement
340+
25 | 0 | SQL statement
341+
26 | 0 | SQL statement
342+
27 | 0 | SQL statement
343+
28 | 0 | SQL statement
344+
29 | 0 | SQL statement
345+
30 | 0 | SQL statement
346+
31 | 1 | assignment
347+
32 | 1 | RETURN
348+
(32 rows)
349+
350+
drop function covtest;
268351
set plpgsql_check.profiler to off;
269352
create function f1()
270353
returns void as $$

expected/plpgsql_check_active_2.out

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,89 @@ select f1();
265265
(1 row)
266266

267267
drop function f1();
268+
create or replace function covtest(int)
269+
returns int as $$
270+
declare a int = $1;
271+
begin
272+
a := a + 1;
273+
if a < 10 then
274+
a := a + 1;
275+
drop function test_function();
276+
drop function test_function();
277+
drop function test_function();
278+
drop function test_function();
279+
drop function test_function();
280+
drop function test_function();
281+
drop function test_function();
282+
drop function test_function();
283+
drop function test_function();
284+
drop function test_function();
285+
drop function test_function();
286+
drop function test_function;
287+
drop function test_function();
288+
drop function test_function();
289+
drop function test_function();
290+
drop function test_function();
291+
drop function test_function();
292+
drop function test_function();
293+
drop function test_function();
294+
drop function test_function();
295+
drop function test_function();
296+
drop function test_function();
297+
drop function test_function();
298+
drop function test_function();
299+
drop function test_function;
300+
drop function test_function();
301+
end if;
302+
a := a + 1;
303+
return a;
304+
end;
305+
$$ language plpgsql;
306+
select covtest(10);
307+
covtest
308+
---------
309+
12
310+
(1 row)
311+
312+
-- should not crash
313+
select stmtid, exec_stmts, stmtname from plpgsql_profiler_function_statements_tb('covtest');
314+
stmtid | exec_stmts | stmtname
315+
--------+------------+-----------------
316+
1 | 1 | statement block
317+
2 | 1 | assignment
318+
3 | 1 | IF
319+
4 | 0 | assignment
320+
5 | 0 | SQL statement
321+
6 | 0 | SQL statement
322+
7 | 0 | SQL statement
323+
8 | 0 | SQL statement
324+
9 | 0 | SQL statement
325+
10 | 0 | SQL statement
326+
11 | 0 | SQL statement
327+
12 | 0 | SQL statement
328+
13 | 0 | SQL statement
329+
14 | 0 | SQL statement
330+
15 | 0 | SQL statement
331+
16 | 0 | SQL statement
332+
17 | 0 | SQL statement
333+
18 | 0 | SQL statement
334+
19 | 0 | SQL statement
335+
20 | 0 | SQL statement
336+
21 | 0 | SQL statement
337+
22 | 0 | SQL statement
338+
23 | 0 | SQL statement
339+
24 | 0 | SQL statement
340+
25 | 0 | SQL statement
341+
26 | 0 | SQL statement
342+
27 | 0 | SQL statement
343+
28 | 0 | SQL statement
344+
29 | 0 | SQL statement
345+
30 | 0 | SQL statement
346+
31 | 1 | assignment
347+
32 | 1 | RETURN
348+
(32 rows)
349+
350+
drop function covtest;
268351
set plpgsql_check.profiler to off;
269352
create function f1()
270353
returns void as $$

sql/plpgsql_check_active.sql

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,52 @@ select f1();
137137

138138
drop function f1();
139139

140+
create or replace function covtest(int)
141+
returns int as $$
142+
declare a int = $1;
143+
begin
144+
a := a + 1;
145+
if a < 10 then
146+
a := a + 1;
147+
drop function test_function();
148+
drop function test_function();
149+
drop function test_function();
150+
drop function test_function();
151+
drop function test_function();
152+
drop function test_function();
153+
drop function test_function();
154+
drop function test_function();
155+
drop function test_function();
156+
drop function test_function();
157+
drop function test_function();
158+
drop function test_function;
159+
drop function test_function();
160+
drop function test_function();
161+
drop function test_function();
162+
drop function test_function();
163+
drop function test_function();
164+
drop function test_function();
165+
drop function test_function();
166+
drop function test_function();
167+
drop function test_function();
168+
drop function test_function();
169+
drop function test_function();
170+
drop function test_function();
171+
drop function test_function;
172+
drop function test_function();
173+
end if;
174+
a := a + 1;
175+
return a;
176+
end;
177+
$$ language plpgsql;
178+
179+
select covtest(10);
180+
181+
-- should not crash
182+
select stmtid, exec_stmts, stmtname from plpgsql_profiler_function_statements_tb('covtest');
183+
184+
drop function covtest;
185+
140186
set plpgsql_check.profiler to off;
141187

142188
create function f1()

src/profiler.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ typedef struct profiler_iterator
148148
HTAB *chunks;
149149
profiler_stmt_chunk *current_chunk;
150150
int current_statement;
151+
152+
#ifdef USE_ASSERT_CHECKING
153+
154+
int current_statement_no;
155+
156+
#endif
157+
151158
} profiler_iterator;
152159

153160
typedef struct
@@ -287,6 +294,12 @@ get_stmt_profile_next(profiler_iterator *pi)
287294
pi->current_statement = 0;
288295
}
289296

297+
#ifdef USE_ASSERT_CHECKING
298+
299+
pi->current_statement_no += 1;
300+
301+
#endif
302+
290303
return &pi->current_chunk->stmts[pi->current_statement++];
291304
}
292305

@@ -606,7 +619,7 @@ profiler_stmt_walker(profiler_info *pinfo,
606619
* iterator returns 0 stmtid.
607620
*/
608621
Assert(!opts->pi->current_chunk ||
609-
(opts->stmtid_map[opts->pi->current_statement] - 1) == stmtid);
622+
(opts->stmtid_map[opts->pi->current_statement_no] - 1) == stmtid);
610623

611624
/*
612625
* Get persistent statement info stored in shared memory

0 commit comments

Comments
 (0)