From 8ecd15af95d5cc8c7f4029378650cc3f52e1aed5 Mon Sep 17 00:00:00 2001 From: Anthonin Bonnefoy Date: Fri, 19 Apr 2024 09:27:25 +0200 Subject: [PATCH] Handle top spans with empty query string 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. --- expected/select.out | 26 +++++++++++++++++++++++++- sql/select.sql | 6 +++++- src/pg_tracing.c | 5 +++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/expected/select.out b/expected/select.out index d123060..a257780 100644 --- a/expected/select.out +++ b/expected/select.out @@ -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 ----------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------+----- @@ -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(); diff --git a/sql/select.sql b/sql/select.sql index 516614b..69273cd 100644 --- a/sql/select.sql +++ b/sql/select.sql @@ -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 diff --git a/src/pg_tracing.c b/src/pg_tracing.c index 90d63dc..c57344a 100644 --- a/src/pg_tracing.c +++ b/src/pg_tracing.c @@ -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); } /*