github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/logictest/testdata/logic_test/create_index (about)

     1  # LogicTest: !3node-tenant
     2  statement ok
     3  CREATE TABLE t (
     4    a INT PRIMARY KEY,
     5    b INT,
     6    FAMILY (a),
     7    FAMILY (b)
     8  )
     9  
    10  statement ok
    11  INSERT INTO t VALUES (1,1)
    12  
    13  user root
    14  
    15  statement ok
    16  CREATE INDEX foo ON t (b)
    17  
    18  statement error relation \"foo\" already exists
    19  CREATE INDEX foo ON t (a)
    20  
    21  statement error column "c" does not exist
    22  CREATE INDEX bar ON t (c)
    23  
    24  statement error index \"bar\" contains duplicate column \"b\"
    25  CREATE INDEX bar ON t (b, b);
    26  
    27  query TTBITTBB colnames
    28  SHOW INDEXES FROM t
    29  ----
    30  table_name  index_name  non_unique  seq_in_index  column_name  direction  storing  implicit
    31  t           primary     false       1             a            ASC        false    false
    32  t           foo         true        1             b            ASC        false    false
    33  t           foo         true        2             a            ASC        false    true
    34  
    35  statement ok
    36  INSERT INTO t VALUES (2,1)
    37  
    38  statement error pgcode 23505 violates unique constraint "bar"
    39  CREATE UNIQUE INDEX bar ON t (b)
    40  
    41  query TTBITTBB colnames
    42  SHOW INDEXES FROM t
    43  ----
    44  table_name  index_name  non_unique  seq_in_index  column_name  direction  storing  implicit
    45  t           primary     false       1             a            ASC        false    false
    46  t           foo         true        1             b            ASC        false    false
    47  t           foo         true        2             a            ASC        false    true
    48  
    49  # test for DESC index
    50  
    51  statement ok
    52  DROP TABLE t
    53  
    54  statement ok
    55  CREATE TABLE t (
    56    a INT PRIMARY KEY,
    57    b INT,
    58    c INT
    59  )
    60  
    61  statement ok
    62  INSERT INTO t VALUES (1,1,1), (2,2,2)
    63  
    64  statement ok
    65  CREATE INDEX b_desc ON t (b DESC)
    66  
    67  statement ok
    68  CREATE INDEX b_asc ON t (b ASC, c DESC)
    69  
    70  query TTBITTBB colnames
    71  SHOW INDEXES FROM t
    72  ----
    73  table_name  index_name  non_unique  seq_in_index  column_name  direction  storing  implicit
    74  t           primary     false       1             a            ASC        false    false
    75  t           b_desc      true        1             b            DESC       false    false
    76  t           b_desc      true        2             a            ASC        false    true
    77  t           b_asc       true        1             b            ASC        false    false
    78  t           b_asc       true        2             c            DESC       false    false
    79  t           b_asc       true        3             a            ASC        false    true
    80  
    81  statement error pgcode 42P01 relation "foo" does not exist
    82  CREATE INDEX fail ON foo (b DESC)
    83  
    84  statement ok
    85  CREATE VIEW v AS SELECT a,b FROM t
    86  
    87  statement error pgcode 42809 "v" is not a table
    88  CREATE INDEX failview ON v (b DESC)
    89  
    90  statement ok
    91  CREATE TABLE privs (a INT PRIMARY KEY, b INT)
    92  
    93  user testuser
    94  
    95  statement error user testuser does not have CREATE privilege on relation privs
    96  CREATE INDEX foo ON privs (b)
    97  
    98  user root
    99  
   100  query TTBITTBB colnames
   101  SHOW INDEXES FROM privs
   102  ----
   103  table_name  index_name  non_unique  seq_in_index  column_name  direction  storing  implicit
   104  privs       primary     false       1             a            ASC        false    false
   105  
   106  statement ok
   107  GRANT CREATE ON privs TO testuser
   108  
   109  user testuser
   110  
   111  statement ok
   112  CREATE INDEX foo ON privs (b)
   113  
   114  query TTBITTBB colnames
   115  SHOW INDEXES FROM privs
   116  ----
   117  table_name  index_name  non_unique  seq_in_index  column_name  direction  storing  implicit
   118  privs       primary     false       1             a            ASC        false    false
   119  privs       foo         true        1             b            ASC        false    false
   120  privs       foo         true        2             a            ASC        false    true
   121  
   122  
   123  user root
   124  
   125  statement ok
   126  SET experimental_enable_hash_sharded_indexes = true;
   127  CREATE TABLE telemetry (
   128    x INT PRIMARY KEY,
   129    y INT,
   130    z JSONB
   131  )
   132  
   133  statement ok
   134  CREATE INVERTED INDEX ON telemetry (z);
   135  CREATE INDEX ON telemetry (y) USING HASH WITH BUCKET_COUNT = 4
   136  
   137  query T rowsort
   138  SELECT feature_name FROM crdb_internal.feature_usage
   139  WHERE feature_name IN (
   140    'sql.schema.inverted_index',
   141    'sql.schema.hash_sharded_index'
   142  )
   143  ----
   144  sql.schema.inverted_index
   145  sql.schema.hash_sharded_index
   146  
   147  subtest create_index_concurrently
   148  
   149  statement ok
   150  CREATE TABLE create_index_concurrently_tbl (a int)
   151  
   152  query T noticetrace
   153  CREATE INDEX CONCURRENTLY create_index_concurrently_idx ON create_index_concurrently_tbl(a)
   154  ----
   155  NOTICE: CONCURRENTLY is not required as all indexes are created concurrently
   156  
   157  query T noticetrace
   158  CREATE INDEX CONCURRENTLY IF NOT EXISTS create_index_concurrently_idx ON create_index_concurrently_tbl(a)
   159  ----
   160  
   161  query TT
   162  SHOW CREATE TABLE create_index_concurrently_tbl
   163  ----
   164  create_index_concurrently_tbl  CREATE TABLE create_index_concurrently_tbl (
   165                                 a INT8 NULL,
   166                                 INDEX create_index_concurrently_idx (a ASC),
   167                                 FAMILY "primary" (a, rowid)
   168  )
   169  
   170  query T noticetrace
   171  DROP INDEX CONCURRENTLY create_index_concurrently_idx
   172  ----
   173  NOTICE: CONCURRENTLY is not required as all indexes are dropped concurrently
   174  NOTICE: the data for dropped indexes is reclaimed asynchronously
   175  HINT: The reclamation delay can be customized in the zone configuration for the table.
   176  
   177  query T noticetrace
   178  DROP INDEX CONCURRENTLY IF EXISTS create_index_concurrently_idx
   179  ----
   180  NOTICE: CONCURRENTLY is not required as all indexes are dropped concurrently
   181  
   182  query TT
   183  SHOW CREATE TABLE create_index_concurrently_tbl
   184  ----
   185  create_index_concurrently_tbl  CREATE TABLE create_index_concurrently_tbl (
   186                                 a INT8 NULL,
   187                                 FAMILY "primary" (a, rowid)
   188  )
   189  
   190  statement ok
   191  DROP TABLE create_index_concurrently_tbl
   192  
   193  # Test that creating an index on a column which is currently being dropped
   194  # causes an error.
   195  subtest create_index_on_dropping_column
   196  
   197  statement ok
   198  CREATE TABLE create_idx_drop_column (c0 INT PRIMARY KEY, c1 INT);
   199  
   200  statement ok
   201  begin; ALTER TABLE create_idx_drop_column DROP COLUMN c1;
   202  
   203  statement error column "c1" does not exist
   204  CREATE INDEX idx_create_idx_drop_column ON create_idx_drop_column (c1);
   205  
   206  statement ok
   207  ROLLBACK;
   208  
   209  statement ok
   210  DROP TABLE create_idx_drop_column;