# LogicTest: local

query T colnames
SELECT * FROM [SHOW client_encoding]
----
client_encoding
UTF8

query T colnames
SELECT c.x FROM [SHOW client_encoding] AS c(x)
----
x
UTF8

query TI colnames
SELECT * FROM [SHOW client_encoding] WITH ORDINALITY
----
client_encoding     ordinality
UTF8                1

# We filter here because optimizer will be different depending on which
# configuration this logic test is running in, and session ID will vary.
query TT colnames
SELECT *
FROM [SHOW ALL]
WHERE variable NOT IN ('optimizer', 'crdb_version', 'session_id', 'distsql_workmem', 'copy_fast_path_enabled', 'direct_columnar_scans_enabled', 'multiple_active_portals_enabled')
ORDER BY variable
----
variable                                                   value
allow_ordinal_column_references                            off
allow_role_memberships_to_change_during_transaction        off
alter_primary_region_super_region_override                 off
always_distribute_full_scans                               off
application_name                                           ·
authentication_method                                      cert-password
autocommit_before_ddl                                      off
avoid_buffering                                            off
avoid_full_table_scans_in_mutations                        on
backslash_quote                                            safe_encoding
bypass_pcr_reader_catalog_aost                             off
bytea_output                                               hex
check_function_bodies                                      on
client_encoding                                            UTF8
client_min_messages                                        notice
close_cursors_at_commit                                    on
copy_from_atomic_enabled                                   on
copy_from_retries_enabled                                  on
copy_num_retries_per_batch                                 5
copy_transaction_quality_of_service                        background
copy_write_pipelining_enabled                              off
cost_scans_with_default_col_size                           off
database                                                   test
datestyle                                                  ISO, MDY
deadlock_timeout                                           0
declare_cursor_statement_timeout_enabled                   on
default_int_size                                           8
default_table_access_method                                heap
default_tablespace                                         ·
default_text_search_config                                 pg_catalog.english
default_transaction_isolation                              serializable
default_transaction_priority                               normal
default_transaction_quality_of_service                     regular
default_transaction_read_only                              off
default_transaction_use_follower_reads                     off
default_with_oids                                          off
descriptor_validation                                      on
disable_changefeed_replication                             off
disable_hoist_projection_in_join_limitation                off
disable_partially_distributed_plans                        off
disable_plan_gists                                         off
disable_vec_union_eager_cancellation                       off
disallow_full_table_scans                                  off
distribute_group_by_row_count_threshold                    1000
distribute_join_row_count_threshold                        1000
distribute_scan_row_count_threshold                        10000
distribute_sort_row_count_threshold                        1000
distsql                                                    off
distsql_plan_gateway_bias                                  2
enable_auto_rehoming                                       off
enable_create_stats_using_extremes                         on
enable_create_stats_using_extremes_bool_enum               off
enable_durable_locking_for_serializable                    off
enable_experimental_alter_column_type_general              off
enable_implicit_fk_locking_for_serializable                off
enable_implicit_select_for_update                          on
enable_implicit_transaction_for_batch_statements           on
enable_insert_fast_path                                    on
enable_multiple_modifications_of_table                     off
enable_multiregion_placement_policy                        off
enable_seqscan                                             on
enable_shared_locking_for_serializable                     off
enable_super_regions                                       off
enable_zigzag_join                                         off
enforce_home_region                                        off
enforce_home_region_follower_reads_enabled                 off
escape_string_warning                                      on
expect_and_ignore_not_visible_columns_in_copy              off
experimental_enable_implicit_column_partitioning           off
experimental_enable_temp_tables                            off
experimental_enable_unique_without_index_constraints       off
experimental_hash_group_join_enabled                       off
extra_float_digits                                         1
force_savepoint_restart                                    off
foreign_key_cascades_limit                                 10000
idle_in_transaction_session_timeout                        0
idle_session_timeout                                       0
index_join_streamer_batch_size                             8.0 MiB
index_recommendations_enabled                              off
inject_retry_errors_enabled                                off
inject_retry_errors_on_commit_enabled                      off
integer_datetimes                                          on
intervalstyle                                              postgres
is_superuser                                               on
join_reader_index_join_strategy_batch_size                 4.0 MiB
join_reader_no_ordering_strategy_batch_size                2.0 MiB
join_reader_ordering_strategy_batch_size                   100 KiB
large_full_scan_rows                                       0
lc_collate                                                 C.UTF-8
lc_ctype                                                   C.UTF-8
lc_messages                                                C.UTF-8
lc_monetary                                                C.UTF-8
lc_numeric                                                 C.UTF-8
lc_time                                                    C.UTF-8
legacy_varchar_typing                                      off
locality                                                   region=test,dc=dc1
locality_optimized_partitioned_index_scan                  on
lock_timeout                                               0
log_timezone                                               UTC
max_connections                                            -1
max_identifier_length                                      128
max_index_keys                                             32
max_prepared_transactions                                  2147483647
max_retries_for_read_committed                             10
node_id                                                    1
null_ordered_last                                          off
on_update_rehome_row_enabled                               on
opt_split_scan_limit                                       2048
optimizer_always_use_histograms                            on
optimizer_hoist_uncorrelated_equality_subqueries           on
optimizer_merge_joins_enabled                              on
optimizer_prove_implication_with_virtual_computed_columns  on
optimizer_push_limit_into_project_filtered_scan            on
optimizer_push_offset_into_index_join                      on
optimizer_use_conditional_hoist_fix                        on
optimizer_use_forecasts                                    on
optimizer_use_histograms                                   on
optimizer_use_improved_computed_column_filters_derivation  on
optimizer_use_improved_disjunction_stats                   on
optimizer_use_improved_distinct_on_limit_hint_costing      on
optimizer_use_improved_join_elimination                    on
optimizer_use_improved_multi_column_selectivity_estimate   on
optimizer_use_improved_split_disjunction_for_joins         on
optimizer_use_improved_trigram_similarity_selectivity      on
optimizer_use_improved_zigzag_join_costing                 on
optimizer_use_limit_ordering_for_streaming_group_by        on
optimizer_use_lock_op_for_serializable                     off
optimizer_use_merged_partial_statistics                    off
optimizer_use_multicol_stats                               on
optimizer_use_not_visible_indexes                          off
optimizer_use_polymorphic_parameter_fix                    on
optimizer_use_provided_ordering_fix                        on
optimizer_use_trigram_similarity_optimization              on
optimizer_use_virtual_computed_column_stats                on
override_multi_region_zone_config                          off
parallelize_multi_key_lookup_joins_enabled                 off
password_encryption                                        scram-sha-256
pg_trgm.similarity_threshold                               0.3
plan_cache_mode                                            auto
plpgsql_use_strict_into                                    off
prefer_lookup_joins_for_fks                                off
prepared_statements_cache_size                             0 B
propagate_input_ordering                                   off
recursion_depth_limit                                      1000
reorder_joins_limit                                        8
require_explicit_primary_keys                              off
results_buffer_size                                        524288
role                                                       none
row_security                                               off
search_path                                                "$user", public
serial_normalization                                       rowid
server_encoding                                            UTF8
server_version                                             13.0.0
server_version_num                                         130000
show_primary_key_constraint_on_not_visible_columns         on
sql_safe_updates                                           off
ssl                                                        on
standard_conforming_strings                                on
statement_timeout                                          0
streamer_always_maintain_ordering                          off
streamer_enabled                                           on
streamer_head_of_line_only_fraction                        0.8
streamer_in_order_eager_memory_usage_fraction              0.5
streamer_out_of_order_eager_memory_usage_fraction          0.8
strict_ddl_atomicity                                       off
stub_catalog_tables                                        on
synchronize_seqscans                                       on
synchronous_commit                                         on
system_identity                                            root
testing_optimizer_cost_perturbation                        0
testing_optimizer_disable_rule_probability                 0
testing_optimizer_inject_panics                            off
testing_optimizer_random_seed                              0
testing_vectorize_inject_panics                            off
timezone                                                   UTC
tracing                                                    off
transaction_isolation                                      serializable
transaction_priority                                       normal
transaction_read_only                                      off
transaction_rows_read_err                                  0
transaction_rows_read_log                                  0
transaction_rows_written_err                               0
transaction_rows_written_log                               0
transaction_status                                         NoTxn
transaction_timeout                                        0
troubleshooting_mode                                       off
unbounded_parallel_scans                                   off
unconstrained_non_covering_index_scan_enabled              off
unsafe_allow_triggers_modifying_cascades                   off
use_declarative_schema_changer                             on
variable_inequality_lookup_join_enabled                    on
vectorize                                                  on
xmloption                                                  content

query T colnames
SELECT * FROM [SHOW CLUSTER SETTING sql.defaults.distsql]
----
sql.defaults.distsql
off

query TTTBTTT colnames
SELECT * FROM [SHOW ALL CLUSTER SETTINGS] WHERE variable LIKE 'cluster%label'
----
variable       value  setting_type  public  description                       default_value  origin
cluster.label  ·      s             false   cluster specific free-form label  ·              default

query TTTTTT colnames
SELECT * FROM [SHOW CLUSTER SETTINGS] WHERE variable LIKE 'cluster%label'
----
variable  value  setting_type  description  default_value  origin

query TTTTTT colnames
SELECT * FROM [SHOW PUBLIC CLUSTER SETTINGS] WHERE variable LIKE 'cluster%label'
----
variable  value  setting_type  description  default_value  origin

query T colnames
SELECT * FROM [SHOW SESSION_USER]
----
session_user
root

query T colnames
SELECT * FROM [SHOW DATABASE]
----
database
test

query TT colnames
SELECT * FROM [SHOW ZONE CONFIGURATIONS] LIMIT 0
----
target  raw_config_sql

query TT colnames
SELECT * FROM [SHOW ZONE CONFIGURATION FOR TABLE system.users] LIMIT 0
----
target  raw_config_sql

query TTTTTT colnames,rowsort
SHOW DATABASES
----
database_name  owner  primary_region  secondary_region  regions  survival_goal
defaultdb      root   NULL            NULL              {}       NULL
postgres       root   NULL            NULL              {}       NULL
system         node   NULL            NULL              {}       NULL
test           root   NULL            NULL              {}       NULL

query TTTTTB colnames,rowsort
SELECT * FROM [SHOW GRANTS ON system.descriptor]
----
database_name  schema_name  table_name  grantee  privilege_type  is_grantable
system         public       descriptor  admin    SELECT          true
system         public       descriptor  root     SELECT          true

query TTBITTTBBBF colnames,rowsort
SELECT * FROM [SHOW INDEX FROM system.descriptor]
----
table_name  index_name  non_unique  seq_in_index  column_name  definition  direction  storing  implicit  visible  visibility
descriptor  primary     false       1             id           id          ASC        false    false     true     1
descriptor  primary     false       2             descriptor   descriptor  N/A        true     false     true     1

query TTTTB colnames
SELECT * FROM [SHOW CONSTRAINT FROM system.descriptor]
----
table_name  constraint_name  constraint_type  details               validated
descriptor  primary          PRIMARY KEY      PRIMARY KEY (id ASC)  true

query TTBITTTBBBF colnames,rowsort
SELECT * FROM [SHOW KEYS FROM system.descriptor]
----
table_name  index_name  non_unique  seq_in_index  column_name  definition  direction  storing  implicit  visible  visibility
descriptor  primary     false       1             id           id          ASC        false    false     true     1
descriptor  primary     false       2             descriptor   descriptor  N/A        true     false     true     1

query TT colnames,rowsort
SELECT * FROM [SHOW SCHEMAS FROM system]
----
schema_name         owner
public              admin
crdb_internal       node
pg_extension        node
pg_catalog          node
information_schema  node

query TT colnames,rowsort
SELECT * FROM [SHOW SEQUENCES FROM system]
----
sequence_schema  sequence_name
public           descriptor_id_seq
public           role_id_seq
public           tenant_id_seq

query TTTTT colnames
SELECT schema_name, table_name, type, owner, locality FROM [SHOW TABLES FROM system]
WHERE table_name IN ('descriptor_id_seq', 'comments', 'locations')
ORDER BY schema_name, table_name
----
schema_name  table_name         type      owner  locality
public       comments           table     node   NULL
public       descriptor_id_seq  sequence  node   NULL
public       locations          table     node   NULL

query TTTTTT colnames
SELECT schema_name, table_name, type, owner, locality, comment FROM [SHOW TABLES FROM system WITH COMMENT]
WHERE table_name IN ('comments', 'locations', 'descriptor_id_seq')
ORDER BY schema_name, table_name
----
schema_name  table_name         type      owner  locality  comment
public       comments           table     node   NULL      ·
public       descriptor_id_seq  sequence  node   NULL      ·
public       locations          table     node   NULL      ·

query ITTT colnames
SELECT node_id, user_name, application_name, active_queries
  FROM [SHOW SESSIONS]
 WHERE active_queries != ''
----
node_id  user_name  application_name  active_queries
1        root       ·                 SELECT node_id, user_name, application_name, active_queries FROM [SHOW CLUSTER SESSIONS] WHERE active_queries != ''

statement ok
SET tracing=off

query B colnames
SELECT trace_id > 0 AS has_trace_id FROM [SHOW SESSIONS] WHERE active_queries != ''
----
has_trace_id
false

statement ok
SET tracing=on

query B colnames
SELECT trace_id > 0 AS has_trace_id FROM [SHOW SESSIONS] WHERE active_queries != ''
----
has_trace_id
true

statement ok
SET tracing=off

query B colnames
SELECT goroutine_id > 0 AS has_goroutine_id FROM [SHOW SESSIONS] WHERE active_queries != ''
----
has_goroutine_id
true

query ITT colnames
SELECT node_id, user_name, query FROM [SHOW QUERIES]
----
node_id  user_name  query
1        root       SELECT node_id, user_name, query FROM [SHOW CLUSTER STATEMENTS]

query ITT colnames
SELECT node_id, user_name, query FROM [SHOW STATEMENTS]
----
node_id  user_name  query
1        root       SELECT node_id, user_name, query FROM [SHOW CLUSTER STATEMENTS]

query TT colnames,rowsort
SELECT * FROM [SHOW SCHEMAS]
----
schema_name         owner
public              root
crdb_internal       node
pg_extension        node
pg_catalog          node
information_schema  node

query TTTTIT colnames
CREATE TABLE foo(x INT); SELECT * FROM [SHOW TABLES]
----
schema_name  table_name  type   owner  estimated_row_count  locality
public       foo         table  root   0                    NULL


query T colnames
SELECT * FROM [SHOW TIMEZONE]
----
timezone
UTC


query T colnames
SELECT * FROM [SHOW TIME ZONE]
----
timezone
UTC


query T colnames
SELECT * FROM [SHOW TRANSACTION ISOLATION LEVEL]
----
transaction_isolation
serializable


query T colnames
SELECT * FROM [SHOW TRANSACTION PRIORITY]
----
transaction_priority
normal

query T colnames
SELECT * FROM [SHOW TRANSACTION STATUS]
----
transaction_status
NoTxn


query TT colnames
SELECT * FROM [SHOW CREATE TABLE system.descriptor]
----
table_name                create_statement
system.public.descriptor  CREATE TABLE public.descriptor (
                            id INT8 NOT NULL,
                            descriptor BYTES NULL,
                            CONSTRAINT "primary" PRIMARY KEY (id ASC),
                            FAMILY "primary" (id),
                            FAMILY fam_2_descriptor (descriptor)
                          )

query TT colnames
CREATE VIEW v AS SELECT id FROM system.descriptor; SELECT * FROM [SHOW CREATE VIEW v]
----
table_name  create_statement
v           CREATE VIEW public.v (
              id
            ) AS SELECT id FROM system.public.descriptor


query TTT colnames
SELECT * FROM [SHOW USERS] ORDER BY 1
----
username  options  member_of
admin     ·        {}
root      ·        {admin}
testuser  ·        {}


query TTTI colnames
SELECT start_key, end_key, replicas, lease_holder FROM [SHOW RANGES FROM CURRENT_CATALOG WITH DETAILS]
----
start_key  end_key  replicas  lease_holder
/Table/72  /Max     {1}       1

query TTTI colnames
SELECT start_key, end_key, replicas, lease_holder FROM [SHOW RANGES FROM TABLE system.descriptor WITH DETAILS]
----
start_key     end_key       replicas  lease_holder
…/<TableMin>  …/<TableMax>  {1}       1

statement ok
CREATE INDEX ix ON foo(x)

query TTTI colnames
SELECT start_key, end_key, replicas, lease_holder FROM [SHOW RANGES FROM INDEX foo@ix WITH DETAILS]
----
start_key     end_key       replicas  lease_holder
…/<IndexMin>  …/<IndexMax>  {1}       1

query TTTTTTT colnames
SELECT * FROM [SHOW TRACE FOR SESSION] LIMIT 0
----
timestamp  age  message  tag  location  operation  span

query TTTT colnames
SELECT * FROM [SHOW COMPACT TRACE FOR SESSION] LIMIT 0
----
age  message  tag  operation

query ITTTTTTTTTRTI colnames
SELECT * FROM [SHOW JOBS] LIMIT 0
----
job_id  job_type  description  user_name  status  running_status  created  started  finished  modified  fraction_completed  error  coordinator_id

query TT colnames
SELECT * FROM [SHOW SYNTAX 'select 1; select 2']
ORDER BY message
----
field  message
sql    SELECT 1
sql    SELECT 2

# Test the SHOW SYNTAX statement. We avoid printing out the line number
# here because that is highly likely to change as code is edited.
query TT colnames
SELECT field, replace(message, e'\n', ' ') AS message FROM [SHOW SYNTAX 'foo']
WHERE field != 'line'
ORDER BY field
----
field     message
code      42601
detail    source SQL: foo ^
error     at or near "foo": syntax error
file      lexer.go
function  Error

# Test the SHOW TRANSFER STATE statement.
statement error pgcode 0A000 cannot use SHOW TRANSFER STATE as a statement source
SELECT * FROM [SHOW TRANSFER STATE]

statement error pgcode 0A000 cannot use SHOW TRANSFER STATE as a statement source
SELECT * FROM [SHOW TRANSFER STATE WITH 'foo-bar']

# Test the SHOW INDEXES FROM DATABASE COMMAND
statement ok
CREATE DATABASE showdbindexestest;

statement ok
CREATE TABLE showdbindexestest.table1 (key1 INT PRIMARY KEY);

statement ok
CREATE TABLE showdbindexestest.table2 (key2 INT PRIMARY KEY);

query TTTBITTTBBBF rowsort
SHOW INDEXES FROM DATABASE showdbindexestest;
----
table1  table1_pkey  public  false  1  key1  key1  ASC  false  false  true  1
table2  table2_pkey  public  false  1  key2  key2  ASC  false  false  true  1

statement ok
CREATE DATABASE "$peci@l";

statement ok
CREATE TABLE "$peci@l".table1 (key1 INT PRIMARY KEY);

statement ok
CREATE TABLE "$peci@l".table2 (key2 INT PRIMARY KEY);

query TTTBITTTBBBF rowsort
SHOW INDEXES FROM DATABASE "$peci@l";
----
table1  table1_pkey  public  false  1  key1  key1  ASC  false  false  true  1
table2  table2_pkey  public  false  1  key2  key2  ASC  false  false  true  1

# Test SHOW LOCALITY telemetry.
query T
SHOW LOCALITY
----
region=test,dc=dc1

query T
SELECT feature_name FROM crdb_internal.feature_usage WHERE feature_name='sql.show.locality' AND usage_count > 0
----
sql.show.locality

# Test SHOW INDEXES telemetry.
statement ok
CREATE TABLE show_test (x INT PRIMARY KEY);
SHOW INDEXES FROM show_test

query T
SELECT feature_name FROM crdb_internal.feature_usage WHERE feature_name='sql.show.indexes' AND usage_count > 0
----
sql.show.indexes

# Test SHOW CONSTRAINTS telemetry.
statement ok
SHOW CONSTRAINTS FROM show_test

query T
SELECT feature_name FROM crdb_internal.feature_usage WHERE feature_name='sql.show.constraints' AND usage_count > 0
----
sql.show.constraints

# Test SHOW QUERIES telemetry.
statement ok
SHOW QUERIES

query T
SELECT feature_name FROM crdb_internal.feature_usage WHERE feature_name='sql.show.queries' AND usage_count > 0
----
sql.show.queries

# Test SHOW JOBS telemetry.
statement ok
SHOW JOBS

query T
SELECT feature_name FROM crdb_internal.feature_usage WHERE feature_name='sql.show.jobs' AND usage_count > 0
----
sql.show.jobs

# Tests identically named indexes do not show up twice with SHOW INDEXES.
subtest regression_46333

statement ok
CREATE TABLE t (
  x INT,
  y INT,
  z INT,
  INDEX i1 (x),
  INDEX i2 (y),
  INDEX i3 (z)
); CREATE TABLE t2 (
  x INT,
  y INT,
  z INT,
  INDEX i1 (x),
  INDEX i2 (y),
  INDEX i3 (z)
); COMMENT ON COLUMN t.x IS 'comment1';
COMMENT ON COLUMN t.z IS 'comm"en"t2';
COMMENT ON INDEX t@i2 IS 'comm''ent3'

query TTBITTTBBBFT rowsort
SHOW INDEXES FROM t WITH COMMENT
----
t  i1      true   1  x      x      ASC  false  false  true  1  NULL
t  i1      true   2  rowid  rowid  ASC  false  true   true  1  NULL
t  i2      true   1  y      y      ASC  false  false  true  1  comm'ent3
t  i2      true   2  rowid  rowid  ASC  false  true   true  1  comm'ent3
t  i3      true   1  z      z      ASC  false  false  true  1  NULL
t  i3      true   2  rowid  rowid  ASC  false  true   true  1  NULL
t  t_pkey  false  1  rowid  rowid  ASC  false  false  true  1  NULL
t  t_pkey  false  2  x      x      N/A  true   false  true  1  NULL
t  t_pkey  false  3  y      y      N/A  true   false  true  1  NULL
t  t_pkey  false  4  z      z      N/A  true   false  true  1  NULL

query TTBITTTBBBFT rowsort
SHOW INDEXES FROM t2 WITH COMMENT
----
t2  i1       true   1  x      x      ASC  false  false  true  1  NULL
t2  i1       true   2  rowid  rowid  ASC  false  true   true  1  NULL
t2  i2       true   1  y      y      ASC  false  false  true  1  NULL
t2  i2       true   2  rowid  rowid  ASC  false  true   true  1  NULL
t2  i3       true   1  z      z      ASC  false  false  true  1  NULL
t2  i3       true   2  rowid  rowid  ASC  false  true   true  1  NULL
t2  t2_pkey  false  1  rowid  rowid  ASC  false  false  true  1  NULL
t2  t2_pkey  false  2  x      x      N/A  true   false  true  1  NULL
t2  t2_pkey  false  3  y      y      N/A  true   false  true  1  NULL
t2  t2_pkey  false  4  z      z      N/A  true   false  true  1  NULL

subtest show_timezones

statement ok
SET TIME ZONE 'EST'

query B
SELECT session_start::STRING LIKE '%-05' FROM [SHOW SESSIONS] WHERE status = 'ACTIVE'
----
true

query B
SELECT start::STRING LIKE '%-05' FROM [SHOW QUERIES] WHERE phase = 'executing'
----
true

statement ok
RESET TIME ZONE

subtest end
