Skip to content

Commit

Permalink
Handle top spans with empty query string
Browse files Browse the repository at this point in the history
Some functions will only store the query tree and no source in pg_proc.
When those are called, the query text will be empty. We don't have
information available from the hook so just skip setting an operation
name.
  • Loading branch information
bonnefoa committed Apr 19, 2024
1 parent db3b8f7 commit 8ecd15a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
26 changes: 25 additions & 1 deletion expected/select.out
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ SELECT lazy_function('{1,2,3,4}'::int[]) FROM (VALUES (1,2)) as t;
4
(4 rows)

-- Check lazy function spans
SELECT span_type, span_operation, parameters, lvl from peek_ordered_spans;
span_type | span_operation | parameters | lvl
----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------+-----
Expand All @@ -278,6 +277,31 @@ SELECT span_type, span_operation, parameters, lvl from peek_ordered_spans;
Planner | Planner | | 5
(9 rows)

CALL clean_spans();
-- Check function calls that don't have sources in pg_proc
SELECT information_schema._pg_truetypid(a, t) FROM pg_attribute a, pg_type t limit 1;
_pg_truetypid
---------------
26
(1 row)

SELECT span_type, span_operation, parameters, lvl from peek_ordered_spans;
span_type | span_operation | parameters | lvl
--------------+----------------------------------------------------------------------------------------+------------+-----
Select query | SELECT information_schema._pg_truetypid(a, t) FROM pg_attribute a, pg_type t limit $1; | $1 = 1 | 1
Planner | Planner | | 2
Executor | ExecutorRun | | 2
Limit | Limit | | 3
NestedLoop | NestedLoop | | 4
SeqScan | SeqScan on pg_attribute a | | 5
Materialize | Materialize | | 5
SeqScan | SeqScan on pg_type t | | 6
Select query | Top | | 3
Planner | Planner | | 4
Executor | ExecutorRun | | 4
Result | Result | | 5
(12 rows)

-- Cleanup
CALL clean_spans();
CALL reset_settings();
6 changes: 5 additions & 1 deletion sql/select.sql
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ CREATE OR REPLACE FUNCTION lazy_function(IN anyarray, OUT x anyelement)
LANGUAGE sql
AS 'select * from pg_catalog.generate_series(array_lower($1, 1), array_upper($1, 1), 1)';
SELECT lazy_function('{1,2,3,4}'::int[]) FROM (VALUES (1,2)) as t;
-- Check lazy function spans
SELECT span_type, span_operation, parameters, lvl from peek_ordered_spans;
CALL clean_spans();

-- Check function calls that don't have sources in pg_proc
SELECT information_schema._pg_truetypid(a, t) FROM pg_attribute a, pg_type t limit 1;
SELECT span_type, span_operation, parameters, lvl from peek_ordered_spans;

-- Cleanup
Expand Down
5 changes: 3 additions & 2 deletions src/pg_tracing.c
Original file line number Diff line number Diff line change
Expand Up @@ -1395,8 +1395,9 @@ begin_top_span(pgTracingTraceContext * trace_context, Span * top_span,
}
normalised_query = normalise_query(query_text, stmt_location, &query_len);
}
top_span->operation_name_offset = add_str_to_trace_buffer(normalised_query,
query_len);
if (query_len > 0)
top_span->operation_name_offset = add_str_to_trace_buffer(normalised_query,
query_len);
}

/*
Expand Down

0 comments on commit 8ecd15a

Please sign in to comment.