Skip to content

Commit

Permalink
Handle max parameter size
Browse files Browse the repository at this point in the history
pg_tracing.max_parameter_size GUC parameter was present but never used.
Use it to apply possibly truncate string stored in the parameter buffer
shared across a traced transaction.
  • Loading branch information
bonnefoa committed Aug 22, 2024
1 parent 3b4153a commit cff17de
Show file tree
Hide file tree
Showing 14 changed files with 393 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ OBJS = \
src/pg_tracing_strinfo.o \
src/version_compat.o

REGRESSCHECKS = setup utility select insert trigger cursor json transaction
REGRESSCHECKS = setup utility select parameters insert trigger cursor json transaction
ifeq ($(PG_VERSION),15)
REGRESSCHECKS += trigger_15
else
Expand Down
4 changes: 0 additions & 4 deletions doc/pg_tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,6 @@ Controls the fraction of statements that generate spans. Statements with traceco

Controls the fraction of statements with SQLCommenter tracecontext and an enabled sampled flag that will generate spans. The default value is 1.

### pg_tracing.export_parameters (boolean)

Controls whether the query's parameters should be exported in spans metadata. The default value is `on`.

### pg_tracing.otel_endpoint (string)

URL of the otel collector to send spans to. Example: 'http://127.0.0.1:4318/v1/traces'. This parameter can only be set at server start. The default value is NULL.
Expand Down
217 changes: 217 additions & 0 deletions expected/parameters.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
-- Check that parameters are not exported when disabled
SET pg_tracing.max_parameter_size=0;
/*traceparent='00-00000000000000000000000000000001-0000000000000001-01'*/ select 1, 2, 3;
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000001';
span_operation | parameters | lvl
--------------------+------------+-----
select $1, $2, $3; | | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

-- Saturate the parameter buffer
SET pg_tracing.max_parameter_size=1;
/*traceparent='00-00000000000000000000000000000002-0000000000000002-01'*/ select 1, 2, 3;
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000002';
span_operation | parameters | lvl
--------------------+-------------+-----
select $1, $2, $3; | {1,...,...} | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

SET pg_tracing.max_parameter_size=2;
/*traceparent='00-00000000000000000000000000000003-0000000000000003-01'*/ select 1, 2, 3;
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000003';
span_operation | parameters | lvl
--------------------+-------------+-----
select $1, $2, $3; | {1,...,...} | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

SET pg_tracing.max_parameter_size=3;
/*traceparent='00-00000000000000000000000000000004-0000000000000004-01'*/ select 1, 2, 3;
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000004';
span_operation | parameters | lvl
--------------------+------------+-----
select $1, $2, $3; | {1,2,...} | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

SET pg_tracing.max_parameter_size=4;
/*traceparent='00-00000000000000000000000000000005-0000000000000005-01'*/ select 1, 2, 3;
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000005';
span_operation | parameters | lvl
--------------------+------------+-----
select $1, $2, $3; | {1,2,...} | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

SET pg_tracing.max_parameter_size=5;
/*traceparent='00-00000000000000000000000000000006-0000000000000006-01'*/ select 1, 2, 3;
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000006';
span_operation | parameters | lvl
--------------------+------------+-----
select $1, $2, $3; | {1,2,3} | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

CALL clean_spans();
-- Test truncated string
SET pg_tracing.max_parameter_size=2;
/*traceparent='00-00000000000000000000000000000001-0000000000000001-01'*/ select 'testtruncatedstring';
?column?
---------------------
testtruncatedstring
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000001';
span_operation | parameters | lvl
----------------+------------+-----
select $1; | {'t...} | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

SET pg_tracing.max_parameter_size=19;
/*traceparent='00-00000000000000000000000000000002-0000000000000002-01'*/ select 'testtruncatedstring';
?column?
---------------------
testtruncatedstring
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000002';
span_operation | parameters | lvl
----------------+--------------------------+-----
select $1; | {'testtruncatedstrin...} | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

SET pg_tracing.max_parameter_size=20;
/*traceparent='00-00000000000000000000000000000003-0000000000000003-01'*/ select 'testtruncatedstring';
?column?
---------------------
testtruncatedstring
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000003';
span_operation | parameters | lvl
----------------+---------------------------+-----
select $1; | {'testtruncatedstring...} | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

SET pg_tracing.max_parameter_size=21;
/*traceparent='00-00000000000000000000000000000004-0000000000000004-01'*/ select 'testtruncatedstring';
?column?
---------------------
testtruncatedstring
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000004';
span_operation | parameters | lvl
----------------+-------------------------+-----
select $1; | {'testtruncatedstring'} | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

CALL clean_spans();
-- Saturate the parameter buffer with extended protocol
SET pg_tracing.max_parameter_size=1;
/*traceparent='00-00000000000000000000000000000001-0000000000000001-01'*/ select $1, $2, $3 \bind 1 2 3 \g
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000001';
span_operation | parameters | lvl
-------------------+-------------+-----
select $1, $2, $3 | {1,...,...} | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

SET pg_tracing.max_parameter_size=2;
/*traceparent='00-00000000000000000000000000000002-0000000000000002-01'*/ select $1, $2, $3 \bind 1 2 3 \g
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000002';
span_operation | parameters | lvl
-------------------+-------------+-----
select $1, $2, $3 | {1,...,...} | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

SET pg_tracing.max_parameter_size=3;
/*traceparent='00-00000000000000000000000000000003-0000000000000003-01'*/ select $1, $2, $3 \bind 1 2 3 \g
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000003';
span_operation | parameters | lvl
-------------------+------------+-----
select $1, $2, $3 | {1,2,...} | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

-- Cleanup
CALL clean_spans();
CALL reset_settings();
18 changes: 0 additions & 18 deletions expected/select.out
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,6 @@ SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='0
Result | | 3
(4 rows)

-- Check that parameters are not exported when disabled
SET pg_tracing.export_parameters=false;
/*dddbs='postgres.db',traceparent='00-0000000000000000000000000000000d-000000000000000d-01'*/ select 1, 2, 3;
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='0000000000000000000000000000000d';
span_operation | parameters | lvl
--------------------+------------+-----
select $1, $2, $3; | | 1
Planner | | 2
ExecutorRun | | 2
Result | | 3
(4 rows)

SET pg_tracing.export_parameters=true;
-- Check multi statement query
CALL clean_spans();
SET pg_tracing.sample_rate = 1.0;
Expand Down
1 change: 1 addition & 0 deletions expected/setup.out
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ AS $$
SET pg_tracing.sample_rate TO DEFAULT;
SET pg_tracing.caller_sample_rate TO DEFAULT;
SET pg_tracing.track_utility TO DEFAULT;
SET pg_tracing.max_parameter_size TO DEFAULT;
$$;
CREATE OR REPLACE PROCEDURE reset_pg_tracing_test_table() AS $$
BEGIN
Expand Down
63 changes: 63 additions & 0 deletions sql/parameters.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
-- Check that parameters are not exported when disabled
SET pg_tracing.max_parameter_size=0;
/*traceparent='00-00000000000000000000000000000001-0000000000000001-01'*/ select 1, 2, 3;
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000001';

-- Saturate the parameter buffer
SET pg_tracing.max_parameter_size=1;
/*traceparent='00-00000000000000000000000000000002-0000000000000002-01'*/ select 1, 2, 3;
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000002';

SET pg_tracing.max_parameter_size=2;
/*traceparent='00-00000000000000000000000000000003-0000000000000003-01'*/ select 1, 2, 3;
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000003';

SET pg_tracing.max_parameter_size=3;
/*traceparent='00-00000000000000000000000000000004-0000000000000004-01'*/ select 1, 2, 3;
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000004';

SET pg_tracing.max_parameter_size=4;
/*traceparent='00-00000000000000000000000000000005-0000000000000005-01'*/ select 1, 2, 3;
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000005';

SET pg_tracing.max_parameter_size=5;
/*traceparent='00-00000000000000000000000000000006-0000000000000006-01'*/ select 1, 2, 3;
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000006';
CALL clean_spans();

-- Test truncated string
SET pg_tracing.max_parameter_size=2;
/*traceparent='00-00000000000000000000000000000001-0000000000000001-01'*/ select 'testtruncatedstring';
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000001';

SET pg_tracing.max_parameter_size=19;
/*traceparent='00-00000000000000000000000000000002-0000000000000002-01'*/ select 'testtruncatedstring';
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000002';

SET pg_tracing.max_parameter_size=20;
/*traceparent='00-00000000000000000000000000000003-0000000000000003-01'*/ select 'testtruncatedstring';
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000003';

SET pg_tracing.max_parameter_size=21;
/*traceparent='00-00000000000000000000000000000004-0000000000000004-01'*/ select 'testtruncatedstring';
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000004';

CALL clean_spans();


-- Saturate the parameter buffer with extended protocol
SET pg_tracing.max_parameter_size=1;
/*traceparent='00-00000000000000000000000000000001-0000000000000001-01'*/ select $1, $2, $3 \bind 1 2 3 \g
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000001';

SET pg_tracing.max_parameter_size=2;
/*traceparent='00-00000000000000000000000000000002-0000000000000002-01'*/ select $1, $2, $3 \bind 1 2 3 \g
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000002';

SET pg_tracing.max_parameter_size=3;
/*traceparent='00-00000000000000000000000000000003-0000000000000003-01'*/ select $1, $2, $3 \bind 1 2 3 \g
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='00000000000000000000000000000003';

-- Cleanup
CALL clean_spans();
CALL reset_settings();
6 changes: 0 additions & 6 deletions sql/select.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='0
/*dddbs='postgres.db',traceparent='00-0000000000000000000000000000000c-000000000000000c-01'*/ select 1; select 2;
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='0000000000000000000000000000000c';

-- Check that parameters are not exported when disabled
SET pg_tracing.export_parameters=false;
/*dddbs='postgres.db',traceparent='00-0000000000000000000000000000000d-000000000000000d-01'*/ select 1, 2, 3;
SELECT span_operation, parameters, lvl from peek_ordered_spans where trace_id='0000000000000000000000000000000d';
SET pg_tracing.export_parameters=true;

-- Check multi statement query
CALL clean_spans();
SET pg_tracing.sample_rate = 1.0;
Expand Down
1 change: 1 addition & 0 deletions sql/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ AS $$
SET pg_tracing.sample_rate TO DEFAULT;
SET pg_tracing.caller_sample_rate TO DEFAULT;
SET pg_tracing.track_utility TO DEFAULT;
SET pg_tracing.max_parameter_size TO DEFAULT;
$$;

CREATE OR REPLACE PROCEDURE reset_pg_tracing_test_table() AS $$
Expand Down
Loading

0 comments on commit cff17de

Please sign in to comment.