# LogicTest: local
# knob-opt: sync-event-log

# Check SHOW KV TRACE FOR SESSION.

let $trace_query
SELECT $$
  SELECT operation,
         regexp_replace(
      regexp_replace(
        regexp_replace(
          regexp_replace(
            regexp_replace(
              regexp_replace(
                message,
                'create_as_of_time:<[^>]+>',
                'create_as_of_time:<...>'
              ),
              e'created_at_nanos:[1-9]\\d*',
              'created_at_nanos:...',
              'g'
            ),
            'mutationJobs:<[^>]*>',
            'mutationJobs:<...>'
          ),
          e'drop_time:[1-9]\\d*',
          'drop_time:...'
        ),
        'declarative_schema_changer_state:<[^>]*>',
        'declarative_schema_changer_state:<...>'
      ),
      'metadata:<[^>]*>',
      'metadata:<...>'
         ) AS message
    FROM [SHOW KV TRACE FOR SESSION]
   WHERE message NOT LIKE '%Z/%'
         AND operation NOT LIKE 'kv.DistSender: sending partial batch%'
         AND message NOT SIMILAR TO '%(PushTxn|ResolveIntent|SystemConfigSpan)%'
         AND tag NOT LIKE '%intExec=%'
         AND tag NOT LIKE '%scExec%'
         AND tag NOT LIKE '%IndexBackfiller%'
         AND operation != 'dist sender send';
$$

statement ok
SET tracing = on,kv,results; CREATE DATABASE t; SET tracing = off

# Check the KV trace; we need to remove the eventlog entry and
# internal queries since the timestamp is non-deterministic.
query TT
$trace_query
----
create database  CPut /Table/3/1/106/2/1 -> database:<name:"t" id:106 modification_time:<> version:1 privileges:<users:<user_proto:"admin" privileges:2 with_grant_option:2 > users:<user_proto:"public" privileges:2048 with_grant_option:0 > users:<user_proto:"root" privileges:2 with_grant_option:2 > owner_proto:"root" version:3 > schemas:<key:"public" value:<id:107 > > state:PUBLIC offline_reason:"" default_privileges:<type:DATABASE > replicated_pcr_version:0 >
create database  CPut /NamespaceTable/30/1/0/0/"t"/4/1 -> 106
create database  CPut /Table/3/1/107/2/1 -> schema:<name:"public" id:107 state:PUBLIC offline_reason:"" modification_time:<> version:1 parent_id:106 privileges:<users:<user_proto:"admin" privileges:2 with_grant_option:2 > users:<user_proto:"public" privileges:516 with_grant_option:0 > users:<user_proto:"root" privileges:2 with_grant_option:2 > owner_proto:"root" version:3 > replicated_pcr_version:0 >
create database  CPut /NamespaceTable/30/1/106/0/"public"/4/1 -> 107
sql query        rows affected: 0


# More KV operations.
statement ok
SET tracing = on,kv,results; CREATE TABLE t.kv(k INT PRIMARY KEY, v INT, FAMILY "primary" (k, v)); SET tracing = off

query TT
$trace_query
----
create table  CPut /Table/3/1/108/2/1 -> table:<name:"kv" id:108 version:1 modification_time:<> parent_id:106 unexposed_parent_schema_id:107 columns:<name:"k" id:1 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:false hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns:<name:"v" id:2 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:3 families:<name:"primary" id:0 column_names:"k" column_names:"v" column_ids:1 column_ids:2 default_column_id:2 > next_family_id:1 primary_index:<name:"kv_pkey" id:1 unique:true version:4 key_column_names:"k" key_column_directions:ASC store_column_names:"v" key_column_ids:1 store_column_ids:2 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 on_delete:NO_ACTION on_update:NO_ACTION match:SIMPLE > interleave:<> partitioning:<num_columns:0 num_implicit_columns:0 > type:FORWARD created_explicitly:false encoding_type:1 sharded:<is_sharded:false name:"" shard_buckets:0 > disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 not_visible:false invisibility:0 > next_index_id:2 privileges:<users:<user_proto:"admin" privileges:2 with_grant_option:2 > users:<user_proto:"root" privileges:2 with_grant_option:2 > owner_proto:"root" version:3 > next_mutation_id:1 format_version:3 state:PUBLIC offline_reason:"" view_query:"" is_materialized_view:false refresh_view_required:false drop_time:0 replacement_of:<id:0 time:<> > audit_mode:DISABLED drop_job_id:0 create_query:"" create_as_of_time:<> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:2 import_start_wall_time:0 schema_locked:false import_epoch:0 import_type:IMPORT_WITH_START_TIME_ONLY replicated_pcr_version:0 next_trigger_id:0 >
create table  CPut /NamespaceTable/30/1/106/107/"kv"/4/1 -> 108
sql query     rows affected: 0

# We avoid using the full trace output, because that would make the
# ensuing trace especially chatty, as it traces the index backfill at
# the end of the implicit transaction. A chatty trace could be OK in
# tests, however the backfill also incur job table traffic which has a
# timestamp index, and we can't use (non-deterministic) timestamp
# values in expected values.
statement ok
SET tracing = on,kv,results; CREATE UNIQUE INDEX woo ON t.kv(v); SET tracing = off

query TT
$trace_query
----
create index  CPut /Table/3/1/108/2/1 -> table:<name:"kv" id:108 version:2 modification_time:<> parent_id:106 unexposed_parent_schema_id:107 columns:<name:"k" id:1 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:false hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns:<name:"v" id:2 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:3 families:<name:"primary" id:0 column_names:"k" column_names:"v" column_ids:1 column_ids:2 default_column_id:2 > next_family_id:1 primary_index:<name:"kv_pkey" id:1 unique:true version:4 key_column_names:"k" key_column_directions:ASC store_column_names:"v" key_column_ids:1 store_column_ids:2 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 on_delete:NO_ACTION on_update:NO_ACTION match:SIMPLE > interleave:<> partitioning:<num_columns:0 num_implicit_columns:0 > type:FORWARD created_explicitly:false encoding_type:1 sharded:<is_sharded:false name:"" shard_buckets:0 > disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 not_visible:false invisibility:0 > next_index_id:4 privileges:<users:<user_proto:"admin" privileges:2 with_grant_option:2 > users:<user_proto:"root" privileges:2 with_grant_option:2 > owner_proto:"root" version:3 > mutations:<index:<name:"woo" id:2 unique:true version:3 key_column_names:"v" key_column_directions:ASC key_column_ids:2 key_suffix_column_ids:1 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 on_delete:NO_ACTION on_update:NO_ACTION match:SIMPLE > interleave:<> partitioning:<num_columns:0 num_implicit_columns:0 > type:FORWARD created_explicitly:true encoding_type:0 sharded:<is_sharded:false name:"" shard_buckets:0 > disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:2 not_visible:false invisibility:0 > state:BACKFILLING direction:ADD mutation_id:1 rollback:false > mutations:<index:<name:"kv_v_crdb_internal_dpe_key" id:3 unique:true version:3 key_column_names:"v" key_column_directions:ASC key_column_ids:2 key_suffix_column_ids:1 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 on_delete:NO_ACTION on_update:NO_ACTION match:SIMPLE > interleave:<> partitioning:<num_columns:0 num_implicit_columns:0 > type:FORWARD created_explicitly:true encoding_type:0 sharded:<is_sharded:false name:"" shard_buckets:0 > disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:true created_at_nanos:... constraint_id:3 not_visible:false invisibility:0 > state:DELETE_ONLY direction:ADD mutation_id:1 rollback:false > next_mutation_id:2 format_version:3 state:PUBLIC offline_reason:"" view_query:"" is_materialized_view:false refresh_view_required:false mutationJobs:<...> drop_time:0 replacement_of:<id:0 time:<> > audit_mode:DISABLED drop_job_id:0 create_query:"" create_as_of_time:<...> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:4 import_start_wall_time:0 schema_locked:false import_epoch:0 import_type:IMPORT_WITH_START_TIME_ONLY replicated_pcr_version:0 next_trigger_id:0 >
sql query     rows affected: 0

statement ok
SET tracing = on,kv,results; INSERT INTO t.kv(k, v) VALUES (1,2); SET tracing = off

query TT
$trace_query
----
count      CPut /Table/108/1/1/0 -> /TUPLE/2:2:Int/2
count      InitPut /Table/108/2/2/0 -> /BYTES/0x89
count      fast path completed
sql query  rows affected: 1


statement error duplicate key value
SET tracing = on,kv,results; INSERT INTO t.kv(k, v) VALUES (1,2); SET tracing = off

query TT
set tracing=off;
$trace_query
----
count      CPut /Table/108/1/1/0 -> /TUPLE/2:2:Int/2
count      InitPut /Table/108/2/2/0 -> /BYTES/0x89
sql query  execution failed after 0 rows: duplicate key value violates unique constraint "kv_pkey"

statement error duplicate key value
SET tracing = on,kv,results; INSERT INTO t.kv(k, v) VALUES (2,2); SET tracing = off

query TT
set tracing=off;
$trace_query
----
count      CPut /Table/108/1/2/0 -> /TUPLE/2:2:Int/2
count      InitPut /Table/108/2/2/0 -> /BYTES/0x8a
sql query  execution failed after 0 rows: duplicate key value violates unique constraint "woo"

statement ok
SET tracing = on,kv,results; CREATE TABLE t.kv2 AS TABLE t.kv;

statement ok
SET tracing = off

query TT
$trace_query
----
create table  CPut /Table/3/1/109/2/1 -> table:<name:"kv2" id:109 version:1 modification_time:<> parent_id:106 unexposed_parent_schema_id:107 columns:<name:"k" id:1 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns:<name:"v" id:2 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns:<name:"rowid" id:3 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:false default_expr:"unique_rowid()" hidden:true inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:4 families:<name:"primary" id:0 column_names:"k" column_names:"v" column_names:"rowid" column_ids:1 column_ids:2 column_ids:3 default_column_id:0 > next_family_id:1 primary_index:<name:"kv2_pkey" id:1 unique:true version:4 key_column_names:"rowid" key_column_directions:ASC store_column_names:"k" store_column_names:"v" key_column_ids:3 store_column_ids:1 store_column_ids:2 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 on_delete:NO_ACTION on_update:NO_ACTION match:SIMPLE > interleave:<> partitioning:<num_columns:0 num_implicit_columns:0 > type:FORWARD created_explicitly:false encoding_type:1 sharded:<is_sharded:false name:"" shard_buckets:0 > disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 not_visible:false invisibility:0 > next_index_id:2 privileges:<users:<user_proto:"admin" privileges:2 with_grant_option:2 > users:<user_proto:"root" privileges:2 with_grant_option:2 > owner_proto:"root" version:3 > next_mutation_id:1 format_version:3 state:ADD offline_reason:"" view_query:"" is_materialized_view:false refresh_view_required:false drop_time:0 replacement_of:<id:0 time:<> > audit_mode:DISABLED drop_job_id:0 create_query:"TABLE t.public.kv" create_as_of_time:<> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:2 import_start_wall_time:0 schema_locked:false import_epoch:0 import_type:IMPORT_WITH_START_TIME_ONLY replicated_pcr_version:0 next_trigger_id:0 >
create table  CPut /NamespaceTable/30/1/106/107/"kv2"/4/1 -> 109
sql query     rows affected: 0

statement ok
SET tracing = on,kv,results; UPDATE t.kv2 SET v = v + 2;

statement ok
SET tracing = off

query TT
$trace_query
----
colbatchscan  Scan /Table/109/{1-2} lock Exclusive (Block, Unreplicated)
colbatchscan  fetched: /kv2/kv2_pkey/-9222809086901354496/k/v -> /1/2
count         Put /Table/109/1/-9222809086901354496/0 -> /TUPLE/1:1:Int/1/1:2:Int/4
count         fast path completed
sql query     rows affected: 1

statement ok
SET tracing = on,kv,results; DELETE FROM t.kv2; SET tracing = off

query TT
$trace_query
----
delete range  DelRange /Table/109/1 - /Table/109/2
delete range  fast path completed
sql query     rows affected: 1

statement ok
SET tracing = on,kv,results; DROP TABLE t.kv2

statement ok
SET tracing = off

query TT
$trace_query
----
sql query       rows affected: 0
commit sql txn  CPut /Table/3/1/109/2/1 -> table:<name:"kv2" id:109 version:3 modification_time:<> parent_id:106 unexposed_parent_schema_id:107 columns:<name:"k" id:1 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns:<name:"v" id:2 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns:<name:"rowid" id:3 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:false default_expr:"unique_rowid()" hidden:true inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:4 families:<name:"primary" id:0 column_names:"k" column_names:"v" column_names:"rowid" column_ids:1 column_ids:2 column_ids:3 default_column_id:0 > next_family_id:1 primary_index:<name:"kv2_pkey" id:1 unique:true version:4 key_column_names:"rowid" key_column_directions:ASC store_column_names:"k" store_column_names:"v" key_column_ids:3 store_column_ids:1 store_column_ids:2 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 on_delete:NO_ACTION on_update:NO_ACTION match:SIMPLE > interleave:<> partitioning:<num_columns:0 num_implicit_columns:0 > type:FORWARD created_explicitly:false encoding_type:1 sharded:<is_sharded:false name:"" shard_buckets:0 > disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 not_visible:false invisibility:0 > next_index_id:2 privileges:<users:<user_proto:"admin" privileges:2 with_grant_option:2 > users:<user_proto:"root" privileges:2 with_grant_option:2 > owner_proto:"root" version:3 > next_mutation_id:1 format_version:3 state:DROP offline_reason:"" view_query:"" is_materialized_view:false refresh_view_required:false declarative_schema_changer_state:<...> > metadata:<...> target_status:ABSENT > targets:<element_proto:<owner:<descriptor_id:109 owner:"root" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<user_privileges:<descriptor_id:109 user_name:"admin" privileges:2 with_grant_option:2 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<user_privileges:<descriptor_id:109 user_name:"root" privileges:2 with_grant_option:2 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<table:<table_id:109 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<schema_child:<child_object_id:109 schema_id:107 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_family:<table_id:109 name:"primary" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column:<table_id:109 column_id:1 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_name:<table_id:109 column_id:1 name:"k" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_type:<table_id:109 column_id:1 embedded_type_t:<type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > type_name:"INT8" > is_nullable:true element_creation_metadata:<in_23_1_or_later:true in_24_3_or_later:true > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column:<table_id:109 column_id:2 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_name:<table_id:109 column_id:2 name:"v" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_type:<table_id:109 column_id:2 embedded_type_t:<type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > type_name:"INT8" > is_nullable:true element_creation_metadata:<in_23_1_or_later:true in_24_3_or_later:true > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column:<table_id:109 column_id:3 is_hidden:true > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_name:<table_id:109 column_id:3 name:"rowid" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_type:<table_id:109 column_id:3 embedded_type_t:<type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > type_name:"INT8" > element_creation_metadata:<in_23_1_or_later:true in_24_3_or_later:true > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_not_null:<table_id:109 column_id:3 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_default_expression:<table_id:109 column_id:3 embedded_expr:<expr:"unique_rowid()" > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column:<table_id:109 column_id:4294967295 is_hidden:true is_system_column:true > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_name:<table_id:109 column_id:4294967295 name:"crdb_internal_mvcc_timestamp" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_type:<table_id:109 column_id:4294967295 embedded_type_t:<type:<family: DecimalFamily width: 0 precision: 0 locale: "" visible_type: 0 oid: 1700 time_precision_is_set: false > type_name:"DECIMAL" > is_nullable:true element_creation_metadata:<in_23_1_or_later:true in_24_3_or_later:true > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column:<table_id:109 column_id:4294967294 is_hidden:true is_system_column:true > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_name:<table_id:109 column_id:4294967294 name:"tableoid" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_type:<table_id:109 column_id:4294967294 embedded_type_t:<type:<family: OidFamily width: 0 precision: 0 locale: "" visible_type: 0 oid: 26 time_precision_is_set: false > type_name:"OID" > is_nullable:true element_creation_metadata:<in_23_1_or_later:true in_24_3_or_later:true > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column:<table_id:109 column_id:4294967293 is_hidden:true is_system_column:true > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_name:<table_id:109 column_id:4294967293 name:"crdb_internal_origin_id" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_type:<table_id:109 column_id:4294967293 embedded_type_t:<type:<family: IntFamily width: 32 precision: 0 locale: "" visible_type: 0 oid: 23 time_precision_is_set: false > type_name:"INT4" > is_nullable:true element_creation_metadata:<in_23_1_or_later:true in_24_3_or_later:true > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column:<table_id:109 column_id:4294967292 is_hidden:true is_system_column:true > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_name:<table_id:109 column_id:4294967292 name:"crdb_internal_origin_timestamp" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_type:<table_id:109 column_id:4294967292 embedded_type_t:<type:<family: DecimalFamily width: 0 precision: 0 locale: "" visible_type: 0 oid: 1700 time_precision_is_set: false > type_name:"DECIMAL" > is_nullable:true element_creation_metadata:<in_23_1_or_later:true in_24_3_or_later:true > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<index_column:<table_id:109 index_id:1 column_id:3 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<index_column:<table_id:109 index_id:1 column_id:1 kind:STORED > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<index_column:<table_id:109 index_id:1 column_id:2 ordinal_in_kind:1 kind:STORED > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<primary_index:<embedded_index:<table_id:109 index_id:1 is_unique:true constraint_id:1 > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<index_name:<table_id:109 index_id:1 name:"kv2_pkey" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<index_data:<table_id:109 index_id:1 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<table_data:<table_id:109 database_id:106 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:DROPPED current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:PUBLIC current_statuses:PUBLIC target_ranks:0 target_ranks:1 target_ranks:2 target_ranks:3 target_ranks:4 target_ranks:5 target_ranks:6 target_ranks:7 target_ranks:8 target_ranks:9 target_ranks:10 target_ranks:11 target_ranks:12 target_ranks:13 target_ranks:14 target_ranks:15 target_ranks:16 target_ranks:17 target_ranks:18 target_ranks:19 target_ranks:20 target_ranks:21 target_ranks:22 target_ranks:23 target_ranks:24 target_ranks:25 target_ranks:26 target_ranks:27 target_ranks:28 target_ranks:29 target_ranks:30 target_ranks:31 target_ranks:32 target_ranks:33 target_ranks:34 target_ranks:35 target_ranks:36 relevant_statements:<statement:<statement:"DROP TABLE t.kv2" redacted_statement:"DROP TABLE \342\200\271t\342\200\272.public.\342\200\271kv2\342\200\272" statement_tag:"DROP TABLE" > > authorization:<user_name:"root" > name_mapping:<id:109 name:"kv2" > > drop_time:... replacement_of:<id:0 time:<> > audit_mode:DISABLED drop_job_id:0 create_query:"TABLE t.public.kv" create_as_of_time:<...> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:2 import_start_wall_time:0 schema_locked:false import_epoch:0 import_type:IMPORT_WITH_START_TIME_ONLY replicated_pcr_version:0 next_trigger_id:0 >
commit sql txn  Del /NamespaceTable/30/1/106/107/"kv2"/4/1

statement ok
SET tracing = on,kv,results; DELETE FROM t.kv

statement ok
SET tracing = off

query TT
$trace_query
----
colbatchscan  Scan /Table/108/{1-2} lock Exclusive (Block, Unreplicated)
colbatchscan  fetched: /kv/kv_pkey/1/v -> /2
count         Del /Table/108/2/2/0
count         Del /Table/108/1/1/0
count         fast path completed
sql query     rows affected: 1

statement ok
SET tracing = on,kv,results; DROP INDEX t.kv@woo CASCADE

statement ok
SET tracing = off

query TT
$trace_query
----
sql query       rows affected: 0
commit sql txn  CPut /Table/3/1/108/2/1 -> table:<name:"kv" id:108 version:8 modification_time:<> parent_id:106 unexposed_parent_schema_id:107 columns:<name:"k" id:1 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:false hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns:<name:"v" id:2 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:3 families:<name:"primary" id:0 column_names:"k" column_names:"v" column_ids:1 column_ids:2 default_column_id:2 > next_family_id:1 primary_index:<name:"kv_pkey" id:1 unique:true version:4 key_column_names:"k" key_column_directions:ASC store_column_names:"v" key_column_ids:1 store_column_ids:2 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 on_delete:NO_ACTION on_update:NO_ACTION match:SIMPLE > interleave:<> partitioning:<num_columns:0 num_implicit_columns:0 > type:FORWARD created_explicitly:false encoding_type:1 sharded:<is_sharded:false name:"" shard_buckets:0 > disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 not_visible:false invisibility:0 > next_index_id:4 privileges:<users:<user_proto:"admin" privileges:2 with_grant_option:2 > users:<user_proto:"root" privileges:2 with_grant_option:2 > owner_proto:"root" version:3 > mutations:<index:<name:"woo" id:2 unique:true version:3 key_column_names:"v" key_column_directions:ASC key_column_ids:2 key_suffix_column_ids:1 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 on_delete:NO_ACTION on_update:NO_ACTION match:SIMPLE > interleave:<> partitioning:<num_columns:0 num_implicit_columns:0 > type:FORWARD created_explicitly:true encoding_type:0 sharded:<is_sharded:false name:"" shard_buckets:0 > disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:2 not_visible:false invisibility:0 > state:WRITE_ONLY direction:DROP mutation_id:2 rollback:false > next_mutation_id:2 format_version:3 state:PUBLIC offline_reason:"" view_query:"" is_materialized_view:false refresh_view_required:false declarative_schema_changer_state:<...> > metadata:<...> target_status:PUBLIC > targets:<element_proto:<index_column:<table_id:108 index_id:2 column_id:2 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<index_column:<table_id:108 index_id:2 column_id:1 kind:KEY_SUFFIX > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<secondary_index:<embedded_index:<table_id:108 index_id:2 is_unique:true is_created_explicitly:true constraint_id:2 > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<index_name:<table_id:108 index_id:2 name:"woo" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<index_data:<table_id:108 index_id:2 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<table_data:<table_id:108 database_id:106 > > metadata:<> target_status:PUBLIC > current_statuses:PUBLIC current_statuses:PUBLIC current_statuses:PUBLIC current_statuses:VALIDATED current_statuses:PUBLIC current_statuses:PUBLIC current_statuses:PUBLIC target_ranks:0 target_ranks:1 target_ranks:2 target_ranks:3 target_ranks:4 target_ranks:5 target_ranks:6 relevant_statements:<statement:<statement:"DROP INDEX t.kv@woo CASCADE" redacted_statement:"DROP INDEX \342\200\271t\342\200\272.public.\342\200\271kv\342\200\272@\342\200\271woo\342\200\272 CASCADE" statement_tag:"DROP INDEX" > > authorization:<user_name:"root" > name_mapping:<id:108 name:"kv" columns:<key:1 value:"k" > columns:<key:2 value:"v" > columns:<key:4294967292 value:"crdb_internal_origin_timestamp" > columns:<key:4294967293 value:"crdb_internal_origin_id" > columns:<key:4294967294 value:"tableoid" > columns:<key:4294967295 value:"crdb_internal_mvcc_timestamp" > families:<key:0 value:"primary" > indexes:<key:1 value:"kv_pkey" > > > drop_time:0 replacement_of:<id:0 time:<> > audit_mode:DISABLED drop_job_id:0 create_query:"" create_as_of_time:<...> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:4 import_start_wall_time:0 schema_locked:false import_epoch:0 import_type:IMPORT_WITH_START_TIME_ONLY replicated_pcr_version:0 next_trigger_id:0 >

statement ok
SET tracing = on,kv,results; DROP TABLE t.kv

statement ok
SET tracing = off

query TT
$trace_query
----
sql query       rows affected: 0
commit sql txn  CPut /Table/3/1/108/2/1 -> table:<name:"kv" id:108 version:11 modification_time:<> parent_id:106 unexposed_parent_schema_id:107 columns:<name:"k" id:1 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:false hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > columns:<name:"v" id:2 type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > nullable:true hidden:false inaccessible:false generated_as_identity_type:NOT_IDENTITY_COLUMN virtual:false pg_attribute_num:0 alter_column_type_in_progress:false system_column_kind:NONE > next_column_id:3 families:<name:"primary" id:0 column_names:"k" column_names:"v" column_ids:1 column_ids:2 default_column_id:2 > next_family_id:1 primary_index:<name:"kv_pkey" id:1 unique:true version:4 key_column_names:"k" key_column_directions:ASC store_column_names:"v" key_column_ids:1 store_column_ids:2 foreign_key:<table:0 index:0 name:"" validity:Validated shared_prefix_len:0 on_delete:NO_ACTION on_update:NO_ACTION match:SIMPLE > interleave:<> partitioning:<num_columns:0 num_implicit_columns:0 > type:FORWARD created_explicitly:false encoding_type:1 sharded:<is_sharded:false name:"" shard_buckets:0 > disabled:false geo_config:<> predicate:"" use_delete_preserving_encoding:false created_at_nanos:... constraint_id:1 not_visible:false invisibility:0 > next_index_id:4 privileges:<users:<user_proto:"admin" privileges:2 with_grant_option:2 > users:<user_proto:"root" privileges:2 with_grant_option:2 > owner_proto:"root" version:3 > next_mutation_id:2 format_version:3 state:DROP offline_reason:"" view_query:"" is_materialized_view:false refresh_view_required:false declarative_schema_changer_state:<...> > metadata:<...> target_status:ABSENT > targets:<element_proto:<owner:<descriptor_id:108 owner:"root" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<user_privileges:<descriptor_id:108 user_name:"admin" privileges:2 with_grant_option:2 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<user_privileges:<descriptor_id:108 user_name:"root" privileges:2 with_grant_option:2 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<table:<table_id:108 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<schema_child:<child_object_id:108 schema_id:107 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_family:<table_id:108 name:"primary" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column:<table_id:108 column_id:1 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_name:<table_id:108 column_id:1 name:"k" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_type:<table_id:108 column_id:1 embedded_type_t:<type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > type_name:"INT8" > element_creation_metadata:<in_23_1_or_later:true in_24_3_or_later:true > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_not_null:<table_id:108 column_id:1 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column:<table_id:108 column_id:2 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_name:<table_id:108 column_id:2 name:"v" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_type:<table_id:108 column_id:2 embedded_type_t:<type:<family: IntFamily width: 64 precision: 0 locale: "" visible_type: 0 oid: 20 time_precision_is_set: false > type_name:"INT8" > is_nullable:true element_creation_metadata:<in_23_1_or_later:true in_24_3_or_later:true > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column:<table_id:108 column_id:4294967295 is_hidden:true is_system_column:true > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_name:<table_id:108 column_id:4294967295 name:"crdb_internal_mvcc_timestamp" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_type:<table_id:108 column_id:4294967295 embedded_type_t:<type:<family: DecimalFamily width: 0 precision: 0 locale: "" visible_type: 0 oid: 1700 time_precision_is_set: false > type_name:"DECIMAL" > is_nullable:true element_creation_metadata:<in_23_1_or_later:true in_24_3_or_later:true > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column:<table_id:108 column_id:4294967294 is_hidden:true is_system_column:true > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_name:<table_id:108 column_id:4294967294 name:"tableoid" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_type:<table_id:108 column_id:4294967294 embedded_type_t:<type:<family: OidFamily width: 0 precision: 0 locale: "" visible_type: 0 oid: 26 time_precision_is_set: false > type_name:"OID" > is_nullable:true element_creation_metadata:<in_23_1_or_later:true in_24_3_or_later:true > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column:<table_id:108 column_id:4294967293 is_hidden:true is_system_column:true > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_name:<table_id:108 column_id:4294967293 name:"crdb_internal_origin_id" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_type:<table_id:108 column_id:4294967293 embedded_type_t:<type:<family: IntFamily width: 32 precision: 0 locale: "" visible_type: 0 oid: 23 time_precision_is_set: false > type_name:"INT4" > is_nullable:true element_creation_metadata:<in_23_1_or_later:true in_24_3_or_later:true > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column:<table_id:108 column_id:4294967292 is_hidden:true is_system_column:true > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_name:<table_id:108 column_id:4294967292 name:"crdb_internal_origin_timestamp" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<column_type:<table_id:108 column_id:4294967292 embedded_type_t:<type:<family: DecimalFamily width: 0 precision: 0 locale: "" visible_type: 0 oid: 1700 time_precision_is_set: false > type_name:"DECIMAL" > is_nullable:true element_creation_metadata:<in_23_1_or_later:true in_24_3_or_later:true > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<index_column:<table_id:108 index_id:1 column_id:1 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<index_column:<table_id:108 index_id:1 column_id:2 kind:STORED > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<primary_index:<embedded_index:<table_id:108 index_id:1 is_unique:true constraint_id:1 > > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<index_name:<table_id:108 index_id:1 name:"kv_pkey" > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<index_data:<table_id:108 index_id:1 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > targets:<element_proto:<table_data:<table_id:108 database_id:106 > > metadata:<sub_work_id:1 source_element_id:1 > target_status:ABSENT > current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:DROPPED current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:ABSENT current_statuses:PUBLIC current_statuses:PUBLIC target_ranks:0 target_ranks:1 target_ranks:2 target_ranks:3 target_ranks:4 target_ranks:5 target_ranks:6 target_ranks:7 target_ranks:8 target_ranks:9 target_ranks:10 target_ranks:11 target_ranks:12 target_ranks:13 target_ranks:14 target_ranks:15 target_ranks:16 target_ranks:17 target_ranks:18 target_ranks:19 target_ranks:20 target_ranks:21 target_ranks:22 target_ranks:23 target_ranks:24 target_ranks:25 target_ranks:26 target_ranks:27 target_ranks:28 target_ranks:29 target_ranks:30 target_ranks:31 relevant_statements:<statement:<statement:"DROP TABLE t.kv" redacted_statement:"DROP TABLE \342\200\271t\342\200\272.public.\342\200\271kv\342\200\272" statement_tag:"DROP TABLE" > > authorization:<user_name:"root" > name_mapping:<id:108 name:"kv" > > drop_time:... replacement_of:<id:0 time:<> > audit_mode:DISABLED drop_job_id:0 create_query:"" create_as_of_time:<...> temporary:false partition_all_by:false exclude_data_from_backup:false next_constraint_id:4 import_start_wall_time:0 schema_locked:false import_epoch:0 import_type:IMPORT_WITH_START_TIME_ONLY replicated_pcr_version:0 next_trigger_id:0 >
commit sql txn  Del /NamespaceTable/30/1/106/107/"kv"/4/1

# Check that session tracing does not inhibit the fast path for inserts &
# friends (the path resulting in 1PC transactions).

subtest autocommit

statement ok
CREATE TABLE t.kv3(k INT PRIMARY KEY, v INT, FAMILY "primary" (k, v))

statement ok
SET tracing = on; INSERT INTO t.kv3 (k, v) VALUES (1,1)

statement ok
SET tracing = off

# We look for rows containing an EndTxn as proof that the
# insertNode is committing the txn.
query T
SELECT message FROM [SHOW TRACE FOR SESSION] WHERE message LIKE e'%1 CPut, 1 EndTxn%' AND message NOT LIKE e'%proposing command%'
----
r76: sending batch 1 CPut, 1 EndTxn to (n1,s1):1
node received request: 1 CPut, 1 EndTxn

# Check that we can run set tracing regardless of the current tracing state.
# This is convenient; sometimes it's unclear, for example, if you previously
# stopped tracing or not, so issuing a set tracing=off should just work.
subtest idempotent

statement ok
SET tracing = on; SET tracing = on;

statement ok
SET tracing = off; SET tracing = off;

# Check that we can run set tracing in the aborted state (this is implemented by
# making set tracing an ObserverStmt). This is very convenient for clients that
# start tracing, then might get an error, then want to stop tracing.
subtest aborted_txn

query error pq: foo
BEGIN; SELECT crdb_internal.force_error('', 'foo')

statement ok
SET tracing = off

statement ok
ROLLBACK

subtest system_table_lookup

# Read from the table once to remove any intents. This test can be flakey due
# to the test encountering an intent on system.public.eventlog from previous
# transactions.
statement ok
SELECT * FROM system.public.eventlog;

# We use AOST to bypass the table cache.
statement ok
SET tracing = on,kv; SELECT * FROM system.eventlog AS OF SYSTEM TIME '-1us'; SET tracing = off

query TT
SELECT operation, message
  FROM [SHOW KV TRACE FOR SESSION]
WHERE (message LIKE 'querying next range%' OR message LIKE '%batch%')
  AND message NOT LIKE '%SystemConfigSpan%'
  AND message NOT LIKE '%PushTxn%'
----
dist sender send  querying next range at /Table/12/1
dist sender send  r15: sending batch 1 Scan to (n1,s1):1

# Regression tests for incorrect interaction between consecutive session traces
# (#59203, #60672).
statement ok
CREATE TABLE a (a INT PRIMARY KEY)

# Get the range id.
let $rangeid
SELECT range_id FROM [ SHOW RANGES FROM TABLE a ]

# Populate table descriptor cache.
query I
SELECT * FROM a
----

statement ok
BEGIN;
SET TRACING=ON;
  INSERT INTO a VALUES (1);
ROLLBACK

# The tracing is still enabled. Insert a couple of rows with auto-commit, and
# stop the tracing.
statement ok
INSERT INTO a VALUES (2), (3)

statement ok
SET TRACING=OFF

query TT
SELECT operation, message FROM [SHOW KV TRACE FOR SESSION]
WHERE message     LIKE '%r$rangeid: sending batch%'
  AND message NOT LIKE '%PushTxn%'
  AND message NOT LIKE '%QueryTxn%'
----
dist sender send  r76: sending batch 1 CPut to (n1,s1):1
dist sender send  r76: sending batch 1 EndTxn to (n1,s1):1
dist sender send  r76: sending batch 2 CPut, 1 EndTxn to (n1,s1):1

# Make another session trace.
statement ok
BEGIN;
SET TRACING=ON;
  INSERT INTO a VALUES (4), (5), (6);
SET TRACING=OFF;
COMMIT

# Start the tracing again and insert a few rows with auto-commit.
statement ok
SET TRACING=ON;
  INSERT INTO a VALUES (7), (8), (9), (10);

# The tracing is still enabled. Insert a few rows, rollback the txn, and stop
# the tracing.
statement ok
BEGIN;
  INSERT INTO a VALUES (11), (12), (13), (14), (15);
ROLLBACK;
SET TRACING=OFF;

query TT
SELECT operation, message FROM [SHOW KV TRACE FOR SESSION]
WHERE message     LIKE '%r$rangeid: sending batch%'
  AND message NOT LIKE '%PushTxn%'
  AND message NOT LIKE '%QueryTxn%'
----
dist sender send  r76: sending batch 4 CPut, 1 EndTxn to (n1,s1):1
dist sender send  r76: sending batch 5 CPut to (n1,s1):1
dist sender send  r76: sending batch 1 EndTxn to (n1,s1):1

# make a table with some big strings in it.
statement ok
CREATE TABLE blobs (i INT PRIMARY KEY, j STRING, FAMILY (i, j))

# make a table with some big (1mb) strings in it.
statement ok
SET TRACING=ON;
  INSERT INTO blobs SELECT generate_series(1, 24), repeat('0123456789ab', 65536);

statement ok
SET TRACING=OFF;

# verify insert of 24 rows paginated into 4 batches since they are .75mb each.
query TT
SELECT operation, message FROM [SHOW KV TRACE FOR SESSION]
WHERE message     LIKE '%r$rangeid: sending batch%'
  AND message NOT LIKE '%PushTxn%'
  AND message NOT LIKE '%QueryTxn%'
----
dist sender send  r76: sending batch 6 CPut to (n1,s1):1
dist sender send  r76: sending batch 6 CPut to (n1,s1):1
dist sender send  r76: sending batch 6 CPut to (n1,s1):1
dist sender send  r76: sending batch 6 CPut to (n1,s1):1
dist sender send  r76: sending batch 1 EndTxn to (n1,s1):1

statement ok
CREATE TABLE streamer (pk INT PRIMARY KEY, attribute INT, blob TEXT, INDEX(attribute), FAMILY (pk, attribute, blob));
INSERT INTO streamer SELECT i, 1, repeat('a', 10) FROM generate_series(1, 42) AS g(i);

# Get the range id.
let $rangeid
SELECT range_id FROM [ SHOW RANGES FROM TABLE streamer ]

# Populate table descriptor cache.
statement ok
SELECT * FROM streamer

# Trace the statement that performs a point read followed by an index join that
# is implemented via the Streamer API.
statement ok
SET tracing = on;
SELECT * FROM streamer@streamer_attribute_idx WHERE attribute=1;
SET tracing = off;

# The index join will issue a batch with 42 Get requests, so we want to verify
# that the corresponding log is included into the trace by the DistSender.
query TT
SELECT operation, message FROM [SHOW KV TRACE FOR SESSION]
WHERE message     LIKE '%r$rangeid: sending batch 42 Get%'
  AND message NOT LIKE '%PushTxn%'
  AND message NOT LIKE '%QueryTxn%'
----
dist sender send  r76: sending batch 42 Get to (n1,s1):1
