Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve extended protocol and tx block support #51

Merged
merged 8 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ endif

# PG 18 contains additional psql metacommand to test extended protocol
ifeq ($(shell test $(PG_VERSION) -ge 18; echo $$?),0)
REGRESSCHECKS += psql_extended
REGRESSCHECKS += psql_extended psql_extended_tx
endif

REGRESSCHECKS += sample planstate planstate_bitmap planstate_hash \
Expand Down
20 changes: 11 additions & 9 deletions expected/nested.out
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,17 @@ LANGUAGE sql IMMUTABLE;
(1 row)

select span_operation, lvl FROM peek_ordered_spans where trace_id='00000000000000000000000000000056';
span_operation | lvl
-----------------------------------------+-----
select test_immutable_function($1); | 1
Planner | 2
SELECT oid from pg_class where oid = a; | 3
Planner | 4
ExecutorRun | 2
Result | 3
(6 rows)
span_operation | lvl
----------------------------------------------------+-----
select test_immutable_function($1); | 1
Planner | 2
SELECT oid from pg_class where oid = a; | 3
Planner | 4
ExecutorRun | 4
IndexOnlyScan using pg_class_oid_index on pg_class | 5
ExecutorRun | 2
Result | 3
(8 rows)

-- Create function with generate series
CREATE OR REPLACE FUNCTION test_generate_series(IN anyarray, OUT x anyelement)
Expand Down
95 changes: 6 additions & 89 deletions expected/psql_extended.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Simple query with extended protocol
-- Select with extended protocol
/*dddbs='postgres.db',traceparent='00-00000000000000000000000000000001-0000000000000001-01'*/ SELECT $1, $2 \parse stmt1
\bind_named stmt1 1 2 \g
?column? | ?column?
Expand All @@ -16,97 +16,14 @@ SELECT trace_id, span_type, span_operation, parameters, lvl FROM peek_ordered_sp
00000000000000000000000000000001 | Result | Result | | 3
(5 rows)

BEGIN;
/*dddbs='postgres.db',traceparent='00-00000000000000000000000000000002-0000000000000001-01'*/ select $1 \parse ''
\bind_named '' 1 \g
WITH max_end AS (select max(span_end) from pg_tracing_peek_spans)
SELECT span_end = max_end.max from pg_tracing_peek_spans, max_end
where span_type='Select query' AND parameters IS NOT NULL;
?column?
----------
1
t
(1 row)

/*dddbs='postgres.db',traceparent='00-00000000000000000000000000000003-0000000000000001-01'*/ select $1, $2 \parse ''
\bind_named '' 1 2 \g
?column? | ?column?
----------+----------
1 | 2
(1 row)

/*dddbs='postgres.db',traceparent='00-00000000000000000000000000000004-0000000000000001-01'*/ select $1, $2, $3 \parse ''
\bind_named '' 1 2 3 \g
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

COMMIT;
SELECT trace_id, span_type, span_operation, parameters, lvl FROM peek_ordered_spans;
trace_id | span_type | span_operation | parameters | lvl
----------------------------------+------------------+-------------------+------------+-----
00000000000000000000000000000001 | Select query | SELECT $1, $2 | | 1
00000000000000000000000000000001 | Select query | SELECT $1, $2 | {1,2} | 1
00000000000000000000000000000001 | Planner | Planner | | 2
00000000000000000000000000000001 | ExecutorRun | ExecutorRun | | 2
00000000000000000000000000000001 | Result | Result | | 3
00000000000000000000000000000002 | TransactionBlock | TransactionBlock | | 1
00000000000000000000000000000002 | Select query | select $1 | {1} | 2
00000000000000000000000000000002 | Planner | Planner | | 3
00000000000000000000000000000002 | ExecutorRun | ExecutorRun | | 3
00000000000000000000000000000002 | Result | Result | | 4
00000000000000000000000000000003 | Select query | SELECT $1, $2 | {1,2} | 2
00000000000000000000000000000003 | Planner | Planner | | 3
00000000000000000000000000000003 | ExecutorRun | ExecutorRun | | 3
00000000000000000000000000000003 | Result | Result | | 4
00000000000000000000000000000004 | Select query | select $1, $2, $3 | {1,2,3} | 2
00000000000000000000000000000004 | Planner | Planner | | 3
00000000000000000000000000000004 | ExecutorRun | ExecutorRun | | 3
00000000000000000000000000000004 | Result | Result | | 4
(18 rows)

/*dddbs='postgres.db',traceparent='00-00000000000000000000000000000005-0000000000000001-01'*/ BEGIN;
select $1 \parse ''
\bind_named '' 1 \g
?column?
----------
1
(1 row)

select $1, $2 \parse ''
\bind_named '' 1 2 \g
?column? | ?column?
----------+----------
1 | 2
(1 row)

select $1, $2, $3 \parse ''
\bind_named '' 1 2 3 \g
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

COMMIT;
SELECT span_type, span_operation, parameters, lvl FROM peek_ordered_spans where trace_id='00000000000000000000000000000005';
span_type | span_operation | parameters | lvl
------------------+-------------------+------------+-----
TransactionBlock | TransactionBlock | | 1
Utility query | BEGIN; | | 2
ProcessUtility | ProcessUtility | | 3
Select query | select $1 | {1} | 2
Planner | Planner | | 3
ExecutorRun | ExecutorRun | | 3
Result | Result | | 4
Select query | SELECT $1, $2 | {1,2} | 2
Planner | Planner | | 3
ExecutorRun | ExecutorRun | | 3
Result | Result | | 4
Select query | select $1, $2, $3 | {1,2,3} | 2
Planner | Planner | | 3
ExecutorRun | ExecutorRun | | 3
Result | Result | | 4
Utility query | COMMIT; | | 2
ProcessUtility | ProcessUtility | | 3
(17 rows)

-- Cleanup
CALL clean_spans();
CALL reset_settings();
CALL clean_spans();
248 changes: 248 additions & 0 deletions expected/psql_extended_tx.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
-- Only trace individual statements using unnamed prepared statement
BEGIN;
/*traceparent='00-00000000000000000000000000000002-0000000000000001-01'*/ select $1 \parse ''
\bind_named '' 1 \g
?column?
----------
1
(1 row)

/*traceparent='00-00000000000000000000000000000003-0000000000000001-01'*/ select $1, $2 \parse ''
\bind_named '' 1 2 \g
?column? | ?column?
----------+----------
1 | 2
(1 row)

/*traceparent='00-00000000000000000000000000000004-0000000000000001-01'*/ select $1, $2, $3 \parse ''
\bind_named '' 1 2 3 \g
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

COMMIT;
SELECT trace_id, span_type, span_operation, parameters, lvl FROM peek_ordered_spans;
trace_id | span_type | span_operation | parameters | lvl
----------------------------------+------------------+-------------------+------------+-----
00000000000000000000000000000002 | TransactionBlock | TransactionBlock | | 1
00000000000000000000000000000002 | Select query | select $1 | {1} | 2
00000000000000000000000000000002 | Planner | Planner | | 3
00000000000000000000000000000002 | ExecutorRun | ExecutorRun | | 3
00000000000000000000000000000002 | Result | Result | | 4
00000000000000000000000000000003 | Select query | select $1, $2 | {1,2} | 2
00000000000000000000000000000003 | Planner | Planner | | 3
00000000000000000000000000000003 | ExecutorRun | ExecutorRun | | 3
00000000000000000000000000000003 | Result | Result | | 4
00000000000000000000000000000004 | Select query | select $1, $2, $3 | {1,2,3} | 2
00000000000000000000000000000004 | Planner | Planner | | 3
00000000000000000000000000000004 | ExecutorRun | ExecutorRun | | 3
00000000000000000000000000000004 | Result | Result | | 4
(13 rows)

CALL clean_spans();
-- Mix begin in simple protocol with extended protocol usage
/*traceparent='00-00000000000000000000000000000005-0000000000000001-01'*/ BEGIN;
select $1 \parse ''
\bind_named '' 1 \g
?column?
----------
1
(1 row)

select $1, $2 \parse ''
\bind_named '' 1 2 \g
?column? | ?column?
----------+----------
1 | 2
(1 row)

select $1, $2, $3 \parse ''
\bind_named '' 1 2 3 \g
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

COMMIT;
SELECT trace_id, span_type, span_operation, parameters, lvl FROM peek_ordered_spans;
trace_id | span_type | span_operation | parameters | lvl
----------------------------------+------------------+-------------------+------------+-----
00000000000000000000000000000005 | TransactionBlock | TransactionBlock | | 1
00000000000000000000000000000005 | Utility query | BEGIN; | | 2
00000000000000000000000000000005 | ProcessUtility | ProcessUtility | | 3
00000000000000000000000000000005 | Select query | select $1 | {1} | 2
00000000000000000000000000000005 | Planner | Planner | | 3
00000000000000000000000000000005 | ExecutorRun | ExecutorRun | | 3
00000000000000000000000000000005 | Result | Result | | 4
00000000000000000000000000000005 | Select query | select $1, $2 | {1,2} | 2
00000000000000000000000000000005 | Planner | Planner | | 3
00000000000000000000000000000005 | ExecutorRun | ExecutorRun | | 3
00000000000000000000000000000005 | Result | Result | | 4
00000000000000000000000000000005 | Select query | select $1, $2, $3 | {1,2,3} | 2
00000000000000000000000000000005 | Planner | Planner | | 3
00000000000000000000000000000005 | ExecutorRun | ExecutorRun | | 3
00000000000000000000000000000005 | Result | Result | | 4
00000000000000000000000000000005 | Utility query | COMMIT; | | 2
00000000000000000000000000000005 | ProcessUtility | ProcessUtility | | 3
(17 rows)

CALL clean_spans();
-- Test using extended protocol for utility statement
/*dddbs='postgres.db',traceparent='00-00000000000000000000000000000006-0000000000000001-01'*/ BEGIN \parse begin_stmt
\bind_named begin_stmt \g
SELECT 1 \parse stmt_mix_1
\bind_named stmt_mix_1 \g
?column?
----------
1
(1 row)

SELECT 1, 2 \parse stmt_mix_2
\bind_named stmt_mix_2 \g
?column? | ?column?
----------+----------
1 | 2
(1 row)

COMMIT \parse commit_stmt
\bind_named commit_stmt \g
SELECT span_type, span_operation, parameters, lvl FROM peek_ordered_spans
where trace_id='00000000000000000000000000000006' AND lvl <= 2;
span_type | span_operation | parameters | lvl
------------------+------------------+------------+-----
Utility query | BEGIN | | 1
TransactionBlock | TransactionBlock | | 1
Utility query | BEGIN | | 2
Select query | SELECT $1 | {1} | 2
Select query | SELECT $1, $2 | {1,2} | 2
Utility query | COMMIT | | 2
(6 rows)

SELECT get_epoch(span_end) as span_select_1_end
from pg_tracing_peek_spans where span_operation='SELECT $1' and trace_id='00000000000000000000000000000006' \gset
SELECT get_epoch(span_start) as span_select_2_start,
get_epoch(span_end) as span_select_2_end
from pg_tracing_peek_spans where span_operation='SELECT $1, $2' and trace_id='00000000000000000000000000000006' \gset
SELECT get_epoch(span_start) as span_commit_start
from pg_tracing_peek_spans where span_operation='COMMIT' and trace_id='00000000000000000000000000000006' \gset
SELECT :span_select_1_end <= :span_select_2_start,
:span_select_2_end <= :span_commit_start;
?column? | ?column?
----------+----------
t | t
(1 row)

CALL clean_spans();
-- Test with extended protocol while reusing prepared stmts
/*dddbs='postgres.db',traceparent='00-00000000000000000000000000000001-0000000000000001-01'*/ BEGIN;
\bind_named stmt_mix_1 \g
?column?
----------
1
(1 row)

\bind_named stmt_mix_2 \g
?column? | ?column?
----------+----------
1 | 2
(1 row)

SELECT 1, 2, 3 \parse stmt_mix_3
\bind_named stmt_mix_3 \g
?column? | ?column? | ?column?
----------+----------+----------
1 | 2 | 3
(1 row)

\bind_named commit_stmt \g
SELECT span_type, span_operation, parameters, lvl FROM peek_ordered_spans;
span_type | span_operation | parameters | lvl
------------------+-------------------+------------+-----
TransactionBlock | TransactionBlock | | 1
Utility query | BEGIN; | | 2
ProcessUtility | ProcessUtility | | 3
Select query | SELECT 1 | | 2
ExecutorRun | ExecutorRun | | 3
Result | Result | | 4
Select query | SELECT 1, 2 | | 2
ExecutorRun | ExecutorRun | | 3
Result | Result | | 4
Select query | SELECT $1, $2, $3 | {1,2,3} | 2
Planner | Planner | | 3
ExecutorRun | ExecutorRun | | 3
Result | Result | | 4
Utility query | COMMIT | | 2
ProcessUtility | ProcessUtility | | 3
(15 rows)

SELECT get_epoch(span_end) as span_select_1_end
from pg_tracing_peek_spans where span_operation='SELECT 1' and trace_id='00000000000000000000000000000001' \gset
SELECT get_epoch(span_start) as span_select_2_start,
get_epoch(span_end) as span_select_2_end
from pg_tracing_peek_spans where span_operation='SELECT 1, 2' and trace_id='00000000000000000000000000000001' \gset
SELECT get_epoch(span_start) as span_select_3_start,
get_epoch(span_end) as span_select_3_end
from pg_tracing_peek_spans where span_operation='SELECT $1, $2, $3' and trace_id='00000000000000000000000000000001' \gset
SELECT get_epoch(span_start) as span_commit_start
from pg_tracing_peek_spans where span_operation='COMMIT' and trace_id='00000000000000000000000000000001' \gset
SELECT :span_select_1_end <= :span_select_2_start,
:span_select_2_end <= :span_select_3_start,
:span_select_3_end <= :span_commit_start;
?column? | ?column? | ?column?
----------+----------+----------
t | t | t
(1 row)

DEALLOCATE ALL;
CALL clean_spans();
-- Test extended protocol while tracing everything
SET pg_tracing.sample_rate = 1.0;
BEGIN \parse begin_stmt
\bind_named begin_stmt \g
SELECT 1 \parse stmt_mix_1
\bind_named stmt_mix_1 \g
?column?
----------
1
(1 row)

SELECT 1, 2 \parse stmt_mix_2
\bind_named stmt_mix_2 \g
?column? | ?column?
----------+----------
1 | 2
(1 row)

COMMIT \parse commit_stmt
\bind_named commit_stmt \g
SELECT span_type, span_operation, parameters, lvl FROM peek_ordered_spans
WHERE lvl <= 2;
span_type | span_operation | parameters | lvl
------------------+------------------+------------+-----
Utility query | BEGIN | | 1
TransactionBlock | TransactionBlock | | 1
Utility query | BEGIN | | 2
Select query | SELECT $1 | {1} | 2
Select query | SELECT $1, $2 | {1,2} | 2
Utility query | COMMIT | | 2
(6 rows)

SELECT get_epoch(span_end) as span_select_1_end
from pg_tracing_peek_spans where span_operation='SELECT $1' \gset
SELECT get_epoch(span_start) as span_select_2_start,
get_epoch(span_end) as span_select_2_end
from pg_tracing_peek_spans where span_operation='SELECT $1, $2' \gset
SELECT get_epoch(span_start) as span_commit_start
from pg_tracing_peek_spans where span_operation='COMMIT' \gset
SELECT :span_select_1_end <= :span_select_2_start,
:span_select_2_end <= :span_commit_start;
?column? | ?column?
----------+----------
t | t
(1 row)

CALL clean_spans();
-- Cleanup
CALL reset_settings();
CALL clean_spans();
Loading