Skip to content

Commit

Permalink
On error, only pop spans from the current ongoing level
Browse files Browse the repository at this point in the history
Query may still proceed despite the thrown error so we need to keep
spans from previous nested levels.
  • Loading branch information
bonnefoa committed Sep 10, 2024
1 parent cd2e01a commit 14b134c
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ endif
ifeq ($(shell test $(PG_VERSION) -lt 17; echo $$?),0)
REGRESSCHECKS += planstate_projectset
else
REGRESSCHECKS += planstate_projectset_17
REGRESSCHECKS += planstate_projectset_17 nested_17
endif

# PG 18 contains additional psql metacommand to test extended protocol
Expand Down
1 change: 1 addition & 0 deletions expected/nested.out
Original file line number Diff line number Diff line change
Expand Up @@ -295,5 +295,6 @@ select span_operation, lvl FROM peek_ordered_spans where trace_id='0000000000000
IndexOnlyScan using pg_class_oid_index on pg_class | 7
(12 rows)

CALL clean_spans();
-- Cleanup
CALL clean_spans();
27 changes: 27 additions & 0 deletions expected/nested_17.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- Test error thrown within a nested function
/*traceparent='00-00000000000000000000000000000001-0000000000000001-01'*/ SELECT
eval_expr(format('date %L + interval %L', '-infinity', 'infinity')),
eval_expr(format('date %L - interval %L', '-infinity', 'infinity'));
eval_expr | eval_expr
------------------------+-----------
timestamp out of range | -infinity
(1 row)

select span_operation, lvl FROM peek_ordered_spans where trace_id='00000000000000000000000000000001';
span_operation | lvl
----------------------------------------------------------------------+-----
SELECT eval_expr(format($1, $2, $3)), eval_expr(format($4, $5, $6)); | 1
Planner | 2
ExecutorRun | 2
Result | 3
$2||expr | 4
Planner | 5
Planner | 5
select date $1 - interval $2 | 4
Planner | 5
ExecutorRun | 5
Result | 6
(11 rows)

-- Cleanup
CALL clean_spans();
13 changes: 13 additions & 0 deletions expected/utility.out
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,19 @@ CREATE OR REPLACE FUNCTION function_with_error(IN anyarray, OUT x anyelement, OU
RETURNS SETOF RECORD
LANGUAGE sql STRICT IMMUTABLE PARALLEL SAFE
AS 'select s from pg_catalog.generate_series(1, 1, 1) as g(s)';
CREATE FUNCTION eval_expr(expr text)
RETURNS text AS
$$
DECLARE
result text;
BEGIN
EXECUTE 'select '||expr INTO result;
RETURN result;
EXCEPTION WHEN OTHERS THEN
RETURN SQLERRM;
END
$$
LANGUAGE plpgsql;
-- Check that tracing a function call with the wrong number of arguments is managed correctly
/*dddbs='postgres.db',traceparent='00-00000000000000000000000000000004-0000000000000004-01'*/ select function_with_error('{1,2,3}'::int[]);
ERROR: return type mismatch in function declared to return record
Expand Down
1 change: 1 addition & 0 deletions sql/nested.sql
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ SELECT count(distinct query_id)=2 from pg_tracing_consume_spans where trace_id='
-- Trace a statement with multiple nested levels
/*dddbs='postgres.db',traceparent='00-00000000000000000000000000000059-0000000000000059-01'*/ select * FROM test_2_nested_levels(1);
select span_operation, lvl FROM peek_ordered_spans where trace_id='00000000000000000000000000000059';
CALL clean_spans();

-- Cleanup
CALL clean_spans();
8 changes: 8 additions & 0 deletions sql/nested_17.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Test error thrown within a nested function
/*traceparent='00-00000000000000000000000000000001-0000000000000001-01'*/ SELECT
eval_expr(format('date %L + interval %L', '-infinity', 'infinity')),
eval_expr(format('date %L - interval %L', '-infinity', 'infinity'));
select span_operation, lvl FROM peek_ordered_spans where trace_id='00000000000000000000000000000001';

-- Cleanup
CALL clean_spans();
14 changes: 14 additions & 0 deletions sql/utility.sql
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,20 @@ CREATE OR REPLACE FUNCTION function_with_error(IN anyarray, OUT x anyelement, OU
LANGUAGE sql STRICT IMMUTABLE PARALLEL SAFE
AS 'select s from pg_catalog.generate_series(1, 1, 1) as g(s)';

CREATE FUNCTION eval_expr(expr text)
RETURNS text AS
$$
DECLARE
result text;
BEGIN
EXECUTE 'select '||expr INTO result;
RETURN result;
EXCEPTION WHEN OTHERS THEN
RETURN SQLERRM;
END
$$
LANGUAGE plpgsql;

-- Check that tracing a function call with the wrong number of arguments is managed correctly
/*dddbs='postgres.db',traceparent='00-00000000000000000000000000000004-0000000000000004-01'*/ select function_with_error('{1,2,3}'::int[]);

Expand Down
2 changes: 1 addition & 1 deletion src/pg_tracing.c
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ handle_pg_error(const Traceparent * traceparent,
process_query_desc(traceparent, queryDesc, sql_error_code, false, span_end_time);
}
span = peek_active_span();
while (span != NULL)
while (span != NULL && span->nested_level == nested_level)
{
/* Assign the error code to the latest top span */
span->sql_error_code = sql_error_code;
Expand Down

0 comments on commit 14b134c

Please sign in to comment.