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 9ffce21
Show file tree
Hide file tree
Showing 16 changed files with 396 additions and 95 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ 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
REGRESSCHECKS += extended trigger_16
REGRESSCHECKS += extended trigger_16 parameters_16
endif
REGRESSCHECKS += sample planstate planstate_bitmap planstate_hash \
planstate_projectset planstate_subplans planstate_union \
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
167 changes: 167 additions & 0 deletions expected/parameters.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
-- 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)

-- Cleanup
CALL clean_spans();
CALL reset_settings();
52 changes: 52 additions & 0 deletions expected/parameters_16.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-- 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
47 changes: 47 additions & 0 deletions sql/parameters.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- 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';

-- Cleanup
CALL clean_spans();
CALL reset_settings();
16 changes: 16 additions & 0 deletions sql/parameters_16.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- 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 9ffce21

Please sign in to comment.