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

     1  # LogicTest: !3node-tenant
     2  # see also files `drop_sequence`, `alter_sequence`, `rename_sequence`
     3  
     4  # USING THE `lastval` FUNCTION
     5  # (at the top because it requires a session in which `lastval` has never been called)
     6  
     7  statement ok
     8  SET DATABASE = test
     9  
    10  statement ok
    11  CREATE SEQUENCE lastval_test
    12  
    13  statement ok
    14  CREATE SEQUENCE lastval_test_2 START WITH 10
    15  
    16  statement error pgcode 55000 pq: lastval\(\): lastval is not yet defined in this session
    17  SELECT lastval()
    18  
    19  query I
    20  SELECT nextval('lastval_test')
    21  ----
    22  1
    23  
    24  query I
    25  SELECT lastval()
    26  ----
    27  1
    28  
    29  query I
    30  SELECT nextval('lastval_test_2')
    31  ----
    32  10
    33  
    34  query I
    35  SELECT lastval()
    36  ----
    37  10
    38  
    39  query I
    40  SELECT nextval('lastval_test')
    41  ----
    42  2
    43  
    44  query I
    45  SELECT lastval()
    46  ----
    47  2
    48  
    49  query I
    50  SELECT nextval('lastval_test_2')
    51  ----
    52  11
    53  
    54  query I
    55  SELECT lastval()
    56  ----
    57  11
    58  
    59  # SEQUENCE CREATION
    60  
    61  statement ok
    62  CREATE SEQUENCE foo
    63  
    64  # A sequence with the same name can't be created again.
    65  statement error pgcode 42P07 relation "foo" already exists
    66  CREATE SEQUENCE foo
    67  
    68  statement ok
    69  CREATE SEQUENCE IF NOT EXISTS foo
    70  
    71  statement error pgcode 42601 conflicting or redundant options
    72  CREATE SEQUENCE bar INCREMENT 5 MAXVALUE 1000 INCREMENT 2
    73  
    74  # Sequences are in the same namespace as tables.
    75  statement error pgcode 42P07 relation "foo" already exists
    76  CREATE TABLE foo (k BYTES PRIMARY KEY, v BYTES)
    77  
    78  # You can't create with 0 increment.
    79  statement error pgcode 22023 INCREMENT must not be zero
    80  CREATE SEQUENCE zero_test INCREMENT 0
    81  
    82  statement ok
    83  CREATE SEQUENCE high_minvalue_test MINVALUE 5
    84  
    85  # Test unimplemented syntax.
    86  statement error at or near "EOF": syntax error: unimplemented
    87  CREATE SEQUENCE err_test AS INT2
    88  
    89  # Verify validation of START vs MINVALUE/MAXVALUE.
    90  
    91  statement error pgcode 22023 START value \(11\) cannot be greater than MAXVALUE \(10\)
    92  CREATE SEQUENCE limit_test MAXVALUE 10 START WITH 11
    93  
    94  statement error pgcode 22023 START value \(5\) cannot be less than MINVALUE \(10\)
    95  CREATE SEQUENCE limit_test MINVALUE 10 START WITH 5
    96  
    97  statement error pgcode 22023 CACHE \(-1\) must be greater than zero
    98  CREATE SEQUENCE cache_test CACHE -1
    99  
   100  statement error pgcode 22023 CACHE \(0\) must be greater than zero
   101  CREATE SEQUENCE cache_test CACHE 0
   102  
   103  statement error pgcode 0A000 CACHE values larger than 1 are not supported, found 5
   104  CREATE SEQUENCE cache_test CACHE 5
   105  
   106  statement error pgcode 0A000 CYCLE option is not supported
   107  CREATE SEQUENCE cycle_test CYCLE
   108  
   109  statement ok
   110  CREATE SEQUENCE ignored_options_test CACHE 1 NO CYCLE
   111  
   112  # Verify presence in crdb_internal.create_statements.
   113  
   114  statement ok
   115  CREATE SEQUENCE show_create_test
   116  
   117  query ITTITTTTTTTB colnames
   118  SELECT * FROM crdb_internal.create_statements WHERE descriptor_name = 'show_create_test'
   119  ----
   120  database_id  database_name  schema_name  descriptor_id  descriptor_type  descriptor_name   create_statement                                                                              state   create_nofks                                                                                  alter_statements  validate_statements  has_partitions
   121  52           test           public       66             sequence         show_create_test  CREATE SEQUENCE show_create_test MINVALUE 1 MAXVALUE 9223372036854775807 INCREMENT 1 START 1  PUBLIC  CREATE SEQUENCE show_create_test MINVALUE 1 MAXVALUE 9223372036854775807 INCREMENT 1 START 1  {}                {}                  false
   122  
   123  query TT colnames
   124  SHOW CREATE SEQUENCE show_create_test
   125  ----
   126  table_name        create_statement
   127  show_create_test  CREATE SEQUENCE show_create_test MINVALUE 1 MAXVALUE 9223372036854775807 INCREMENT 1 START 1
   128  
   129  # DML ERRORS
   130  
   131  statement error pgcode 42809 "foo" is not a table
   132  INSERT INTO foo VALUES (1, 2, 3)
   133  
   134  statement error pgcode 42809 "foo" is not a table
   135  UPDATE foo SET value = 5
   136  
   137  statement error pgcode 42809 "foo" is not a table
   138  DELETE FROM foo
   139  
   140  statement error pgcode 42809 "foo" is not a table
   141  TRUNCATE foo
   142  
   143  # Drop table on sequences doesn't work; you have to use DROP SEQUENCE.
   144  statement error pgcode 42809 "foo" is not a table
   145  DROP TABLE foo
   146  
   147  # List sequences with SHOW
   148  
   149  query T
   150  SHOW SEQUENCES
   151  ----
   152  foo
   153  high_minvalue_test
   154  ignored_options_test
   155  lastval_test
   156  lastval_test_2
   157  show_create_test
   158  
   159  statement ok
   160  CREATE DATABASE seqdb; USE seqdb; CREATE SEQUENCE otherseq; USE test
   161  
   162  query T
   163  SHOW SEQUENCES FROM seqdb
   164  ----
   165  otherseq
   166  
   167  # You can select from a sequence to get its value.
   168  
   169  statement ok
   170  CREATE SEQUENCE select_test
   171  
   172  query IIB colnames
   173  SELECT * FROM select_test
   174  ----
   175  last_value log_cnt is_called
   176  0          0       true
   177  
   178  # Test selecting just last_value.
   179  query I
   180  SELECT last_value FROM select_test
   181  ----
   182  0
   183  
   184  statement ok
   185  SELECT nextval('select_test')
   186  
   187  query I
   188  SELECT last_value FROM select_test
   189  ----
   190  1
   191  
   192  # Since this is a custom plan node, verify that column validation works.
   193  statement error pq: column "foo" does not exist
   194  SELECT foo from select_test
   195  
   196  # USING THE `nextval` AND `currval` FUNCTIONS
   197  
   198  statement error pgcode 55000 pq: currval\(\): currval of sequence "foo" is not yet defined in this session
   199  SELECT currval('foo')
   200  
   201  query I
   202  SELECT nextval('foo')
   203  ----
   204  1
   205  
   206  query I
   207  SELECT nextval('foo')
   208  ----
   209  2
   210  
   211  query I
   212  SELECT currval('foo')
   213  ----
   214  2
   215  
   216  query T
   217  SELECT pg_sequence_parameters('foo'::regclass::oid)
   218  ----
   219  (1,1,9223372036854775807,1,f,1,20)
   220  
   221  # You can create a sequence with different increment.
   222  
   223  statement ok
   224  CREATE SEQUENCE bar INCREMENT 5
   225  
   226  query I
   227  SELECT nextval('bar')
   228  ----
   229  1
   230  
   231  query I
   232  SELECT nextval('bar')
   233  ----
   234  6
   235  
   236  query T
   237  SELECT pg_sequence_parameters('bar'::regclass::oid)
   238  ----
   239  (1,1,9223372036854775807,5,f,1,20)
   240  
   241  # You can create a sequence with different start and increment.
   242  
   243  statement ok
   244  CREATE SEQUENCE baz START 2 INCREMENT 5
   245  
   246  query I
   247  SELECT nextval('baz')
   248  ----
   249  2
   250  
   251  query I
   252  SELECT nextval('baz')
   253  ----
   254  7
   255  
   256  query T
   257  SELECT pg_sequence_parameters('baz'::regclass::oid)
   258  ----
   259  (2,1,9223372036854775807,5,f,1,20)
   260  
   261  # You can create a sequence that goes down.
   262  
   263  statement ok
   264  CREATE SEQUENCE down_test INCREMENT BY -1 START -5
   265  
   266  query I
   267  SELECT nextval('down_test')
   268  ----
   269  -5
   270  
   271  query I
   272  SELECT nextval('down_test')
   273  ----
   274  -6
   275  
   276  query T
   277  SELECT pg_sequence_parameters('down_test'::regclass::oid)
   278  ----
   279  (-5,-9223372036854775808,-1,-1,f,1,20)
   280  
   281  
   282  # You can create and use a sequence with special characters.
   283  
   284  statement ok
   285  CREATE SEQUENCE spécial
   286  
   287  query I
   288  SELECT nextval('spécial')
   289  ----
   290  1
   291  
   292  # You can't call nextval on a table.
   293  
   294  statement ok
   295  CREATE TABLE kv (k bytes primary key, v bytes)
   296  
   297  statement error pgcode 42809 "kv" is not a sequence
   298  SELECT nextval('kv')
   299  
   300  # Parse errors in the argument to nextval are handled.
   301  statement error pq: nextval\(\): at or near "@": syntax error
   302  SELECT nextval('@#%@!324234')
   303  
   304  # You can create and find sequences from other databases.
   305  
   306  statement ok
   307  CREATE DATABASE other_db
   308  
   309  statement ok
   310  SET DATABASE = other_db
   311  
   312  statement ok
   313  CREATE SEQUENCE other_db_test
   314  
   315  statement ok
   316  SET DATABASE = test
   317  
   318  # Sequence names are resolved based on the search path.
   319  
   320  statement ok
   321  CREATE DATABASE foo
   322  
   323  statement ok
   324  CREATE DATABASE bar
   325  
   326  statement ok
   327  CREATE SEQUENCE foo.x
   328  
   329  statement ok
   330  SET DATABASE = bar
   331  
   332  query I
   333  SELECT nextval('foo.x')
   334  ----
   335  1
   336  
   337  query I
   338  SELECT nextval('other_db.other_db_test')
   339  ----
   340  1
   341  
   342  # USING THE `setval` FUNCTION
   343  
   344  statement ok
   345  SET DATABASE = test
   346  
   347  statement ok
   348  CREATE SEQUENCE setval_test
   349  
   350  query I
   351  SELECT nextval('setval_test')
   352  ----
   353  1
   354  
   355  query I
   356  SELECT nextval('setval_test')
   357  ----
   358  2
   359  
   360  query I
   361  SELECT setval('setval_test', 10)
   362  ----
   363  10
   364  
   365  # Calling setval doesn't affect currval or lastval; they return the last value obtained with nextval.
   366  query I
   367  SELECT currval('setval_test')
   368  ----
   369  2
   370  
   371  query I
   372  SELECT lastval()
   373  ----
   374  2
   375  
   376  query I
   377  SELECT nextval('setval_test')
   378  ----
   379  11
   380  
   381  query I
   382  SELECT currval('setval_test')
   383  ----
   384  11
   385  
   386  query I
   387  SELECT lastval()
   388  ----
   389  11
   390  
   391  # setval doesn't let you set values outside the bounds.
   392  
   393  statement ok
   394  CREATE SEQUENCE setval_bounds_test MINVALUE 5 MAXVALUE 10
   395  
   396  query I
   397  SELECT nextval('setval_bounds_test')
   398  ----
   399  5
   400  
   401  statement error pgcode 22003 pq: setval\(\): value 11 is out of bounds for sequence "setval_bounds_test" \(5\.\.10\)
   402  SELECT setval('setval_bounds_test', 11)
   403  
   404  statement error pgcode 22003 pq: setval\(\): value 0 is out of bounds for sequence "setval_bounds_test" \(5\.\.10\)
   405  SELECT setval('setval_bounds_test', 0)
   406  
   407  # nextval fails with nonexistent sequences.
   408  
   409  statement error pgcode 42P01 relation "nonexistent_seq" does not exist
   410  SELECT nextval('nonexistent_seq')
   411  
   412  # The three-argument variant of setval lets you set the next value to be retrieved from nextval().
   413  
   414  statement ok
   415  CREATE SEQUENCE setval_is_called_test
   416  
   417  query I
   418  SELECT setval('setval_is_called_test', 10, false)
   419  ----
   420  10
   421  
   422  query I
   423  SELECT nextval('setval_is_called_test')
   424  ----
   425  10
   426  
   427  query I
   428  SELECT nextval('setval_is_called_test')
   429  ----
   430  11
   431  
   432  query I
   433  SELECT setval('setval_is_called_test', 20, true)
   434  ----
   435  20
   436  
   437  query I
   438  SELECT nextval('setval_is_called_test')
   439  ----
   440  21
   441  
   442  query I
   443  SELECT nextval('setval_is_called_test')
   444  ----
   445  22
   446  
   447  # You can use setval to reset to minvalue.
   448  
   449  statement ok
   450  CREATE SEQUENCE setval_minval_test MINVALUE 10
   451  
   452  query I
   453  SELECT nextval('setval_minval_test')
   454  ----
   455  10
   456  
   457  query I
   458  SELECT nextval('setval_minval_test')
   459  ----
   460  11
   461  
   462  query I
   463  SELECT setval('setval_minval_test', 10, false)
   464  ----
   465  10
   466  
   467  query I
   468  SELECT nextval('setval_minval_test')
   469  ----
   470  10
   471  
   472  query I
   473  SELECT setval('setval_minval_test', 10, true)
   474  ----
   475  10
   476  
   477  query I
   478  SELECT nextval('setval_minval_test')
   479  ----
   480  11
   481  
   482  # BEHAVIOR UPON HITTING LIMITS (minvalue, maxvalue)
   483  
   484  statement ok
   485  CREATE SEQUENCE limit_test MAXVALUE 10 START WITH 9
   486  
   487  query I
   488  SELECT nextval('limit_test')
   489  ----
   490  9
   491  
   492  query I
   493  SELECT nextval('limit_test')
   494  ----
   495  10
   496  
   497  statement error pgcode 2200H pq: nextval\(\): reached maximum value of sequence "limit_test" \(10\)
   498  SELECT nextval('limit_test')
   499  
   500  query I
   501  SELECT currval('limit_test')
   502  ----
   503  10
   504  
   505  statement ok
   506  CREATE SEQUENCE downward_limit_test INCREMENT BY -1 MINVALUE -10 START WITH -10
   507  
   508  query I
   509  SELECT nextval('downward_limit_test')
   510  ----
   511  -10
   512  
   513  statement error pgcode 2200H pq: nextval\(\): reached minimum value of sequence "downward_limit_test" \(-10\)
   514  SELECT nextval('downward_limit_test')
   515  
   516  # Verify that it still works with integer overflows and underflows.
   517  
   518  statement ok
   519  CREATE SEQUENCE overflow_test START WITH 9223372036854775807
   520  
   521  query I
   522  SELECT nextval('overflow_test')
   523  ----
   524  9223372036854775807
   525  
   526  statement error pgcode 2200H pq: nextval\(\): reached maximum value of sequence "overflow_test" \(9223372036854775807\)
   527  SELECT nextval('overflow_test')
   528  
   529  statement ok
   530  CREATE SEQUENCE underflow_test MINVALUE -9223372036854775808 START WITH -9223372036854775808 INCREMENT -1
   531  
   532  query I
   533  SELECT nextval('underflow_test')
   534  ----
   535  -9223372036854775808
   536  
   537  statement error pgcode 2200H pq: nextval\(\): reached minimum value of sequence "underflow_test" \(-9223372036854775808\)
   538  SELECT nextval('underflow_test')
   539  
   540  # USE WITH TABLES
   541  
   542  # You can use a sequence in a DEFAULT expression to create an auto-incrementing primary key.
   543  
   544  statement ok
   545  CREATE SEQUENCE blog_posts_id_seq
   546  
   547  statement ok
   548  CREATE TABLE blog_posts (id INT PRIMARY KEY DEFAULT nextval('blog_posts_id_seq'), title text)
   549  
   550  statement ok
   551  INSERT INTO blog_posts (title) values ('foo')
   552  
   553  statement ok
   554  INSERT INTO blog_posts (title) values ('bar')
   555  
   556  query I
   557  SELECT id FROM blog_posts ORDER BY id
   558  ----
   559  1
   560  2
   561  
   562  # USE WITH (DEPRECATED) PARALLEL STATEMENTS
   563  
   564  # Both accesses to the sequence value in the KV layer and the sequenceState struct in
   565  # the Session are serialized, so after the last parallel statement you'll get the last value.
   566  
   567  statement ok
   568  BEGIN
   569  
   570  statement ok
   571  INSERT INTO blog_posts (title) VALUES ('par_test_1') RETURNING NOTHING
   572  
   573  statement ok
   574  INSERT INTO blog_posts (title) VALUES ('par_test_2') RETURNING NOTHING
   575  
   576  statement ok
   577  INSERT INTO blog_posts (title) VALUES ('par_test_3') RETURNING NOTHING
   578  
   579  query I
   580  SELECT lastval()
   581  ----
   582  5
   583  
   584  statement ok
   585  COMMIT
   586  
   587  # BEHAVIOR WITH TRANSACTIONS
   588  
   589  # Verify that sequence updates are not rolled back with their corresponding transactions, leaving a gap.
   590  
   591  statement ok
   592  CREATE SEQUENCE txn_test_seq;
   593  
   594  statement ok
   595  CREATE TABLE txn_test (id INT PRIMARY KEY DEFAULT nextval('txn_test_seq'), something text)
   596  
   597  statement ok
   598  INSERT INTO txn_test (something) VALUES ('foo')
   599  
   600  statement ok
   601  BEGIN
   602  
   603  statement ok
   604  INSERT INTO txn_test (something) VALUES ('bar')
   605  
   606  statement ok
   607  ROLLBACK
   608  
   609  statement ok
   610  INSERT INTO txn_test (something) VALUES ('baz')
   611  
   612  query IT rowsort
   613  SELECT * FROM txn_test
   614  ----
   615  1 foo
   616  3 baz
   617  
   618  # PREVENTION OF DROPPING A SEQUENCE WHICH IS BEING USED
   619  
   620  statement ok
   621  CREATE SEQUENCE drop_prevention_test
   622  
   623  statement ok
   624  CREATE TABLE drop_prevention_test_tbl (id INT PRIMARY KEY DEFAULT nextval('drop_prevention_test'))
   625  
   626  statement error pq: cannot drop sequence drop_prevention_test because other objects depend on it
   627  DROP SEQUENCE drop_prevention_test
   628  
   629  # Giving a nonexistent function doesn't mess up the nextval-detection algorithm.
   630  
   631  statement error pq: unknown function: nxtvl()
   632  CREATE TABLE seq_using_table (id INT PRIMARY KEY DEFAULT nxtvl('foo'))
   633  
   634  # Sequence deletion is allowed once the sequence-using column is removed.
   635  
   636  statement ok
   637  CREATE SEQUENCE drop_col_test_seq
   638  
   639  statement ok
   640  CREATE TABLE drop_col_test_tbl (id INT PRIMARY KEY, foo INT DEFAULT nextval('drop_col_test_seq'))
   641  
   642  statement ok
   643  ALTER TABLE drop_col_test_tbl DROP COLUMN foo
   644  
   645  statement ok
   646  DROP SEQUENCE drop_col_test_seq
   647  
   648  # Sequence deletion is prevented when a sequence-using column is added to a table.
   649  
   650  statement ok
   651  CREATE TABLE add_col_test_tbl (id INT PRIMARY KEY)
   652  
   653  statement ok
   654  CREATE SEQUENCE add_col_test_seq
   655  
   656  statement ok
   657  ALTER TABLE add_col_test_tbl ADD COLUMN foo INT DEFAULT nextval('add_col_test_seq')
   658  
   659  statement error pq: cannot drop sequence add_col_test_seq because other objects depend on it
   660  DROP SEQUENCE add_col_test_seq
   661  
   662  # Sequence deletion is prevented when a column is altered to depend on the sequence.
   663  
   664  statement ok
   665  CREATE TABLE set_default_test_tbl (id INT PRIMARY KEY, foo INT)
   666  
   667  statement ok
   668  CREATE SEQUENCE set_default_test_seq
   669  
   670  statement ok
   671  ALTER TABLE set_default_test_tbl ALTER COLUMN foo SET DEFAULT nextval('set_default_test_seq')
   672  
   673  statement error pq: cannot drop sequence set_default_test_seq because other objects depend on it
   674  DROP SEQUENCE set_default_test_seq
   675  
   676  # When a column's DEFAULT is altered from using seq A to using seq B,
   677  # A can now be dropped, and B can't.
   678  
   679  statement ok
   680  CREATE SEQUENCE initial_seq
   681  
   682  statement ok
   683  CREATE SEQUENCE changed_to_seq
   684  
   685  statement ok
   686  CREATE TABLE set_default_test (id INT PRIMARY KEY DEFAULT nextval('initial_seq'))
   687  
   688  statement error pq: cannot drop sequence initial_seq because other objects depend on it
   689  DROP SEQUENCE initial_seq
   690  
   691  statement ok
   692  ALTER TABLE set_default_test ALTER COLUMN id SET DEFAULT nextval('changed_to_seq')
   693  
   694  statement ok
   695  DROP SEQUENCE initial_seq
   696  
   697  statement error pq: cannot drop sequence changed_to_seq because other objects depend on it
   698  DROP SEQUENCE changed_to_seq
   699  
   700  # Sequence deletion is allowed after a column's usage of a sequence is dropped with DROP DEFAULT.
   701  
   702  statement ok
   703  CREATE SEQUENCE drop_default_test_seq
   704  
   705  statement ok
   706  CREATE TABLE drop_default_test_tbl (id INT PRIMARY KEY DEFAULT nextval('drop_default_test_seq'))
   707  
   708  statement ok
   709  ALTER TABLE drop_default_test_tbl ALTER COLUMN id DROP DEFAULT
   710  
   711  statement ok
   712  DROP SEQUENCE drop_default_test_seq
   713  
   714  # Verify that a new default can be added.
   715  
   716  statement ok
   717  CREATE SEQUENCE drop_default_test_seq_2
   718  
   719  statement ok
   720  ALTER TABLE drop_default_test_tbl ALTER COLUMN id SET DEFAULT nextval('drop_default_test_seq_2')
   721  
   722  # Test that dependencies are recorded correctly when a column uses multiple sequences.
   723  
   724  statement ok
   725  CREATE SEQUENCE multiple_seq_test1
   726  
   727  statement ok
   728  CREATE SEQUENCE multiple_seq_test2
   729  
   730  statement ok
   731  CREATE TABLE multiple_seq_test_tbl (
   732    id INT PRIMARY KEY DEFAULT nextval('multiple_seq_test1') + nextval('multiple_seq_test2')
   733  )
   734  
   735  statement error pq: cannot drop sequence multiple_seq_test1 because other objects depend on it
   736  DROP SEQUENCE multiple_seq_test1
   737  
   738  statement error pq: cannot drop sequence multiple_seq_test2 because other objects depend on it
   739  DROP SEQUENCE multiple_seq_test2
   740  
   741  # This should remove both sequence dependencies.
   742  statement ok
   743  ALTER TABLE multiple_seq_test_tbl ALTER COLUMN id SET DEFAULT unique_rowid()
   744  
   745  statement ok
   746  DROP SEQUENCE multiple_seq_test1
   747  
   748  statement ok
   749  DROP SEQUENCE multiple_seq_test2
   750  
   751  # Test that dependencies are recorded when multiple columns in a table use sequences.
   752  
   753  statement ok
   754  CREATE SEQUENCE multiple_usage_test_1
   755  
   756  statement ok
   757  CREATE SEQUENCE multiple_usage_test_2
   758  
   759  statement ok
   760  CREATE TABLE multiple_usage_test_tbl (
   761    id INT PRIMARY KEY DEFAULT nextval('multiple_usage_test_1'),
   762    other_id INT DEFAULT nextval('multiple_usage_test_2')
   763  )
   764  
   765  # We're prevented from dropping the first sequence until the dep is removed.
   766  
   767  statement error pq: cannot drop sequence multiple_usage_test_1 because other objects depend on it
   768  DROP SEQUENCE multiple_usage_test_1
   769  
   770  statement ok
   771  ALTER TABLE multiple_usage_test_tbl ALTER COLUMN id DROP DEFAULT
   772  
   773  statement ok
   774  DROP SEQUENCE multiple_usage_test_1
   775  
   776  # We're prevented from dropping the second sequence until the dep is removed.
   777  
   778  statement error pq: cannot drop sequence multiple_usage_test_2 because other objects depend on it
   779  DROP SEQUENCE multiple_usage_test_2
   780  
   781  statement ok
   782  ALTER TABLE multiple_usage_test_tbl ALTER COLUMN other_id DROP DEFAULT
   783  
   784  statement ok
   785  DROP SEQUENCE multiple_usage_test_2
   786  
   787  # Verify that deps are removed when a sequence-using table is dropped.
   788  
   789  statement ok
   790  CREATE SEQUENCE drop_test
   791  
   792  statement ok
   793  CREATE TABLE drop_test_tbl (id INT PRIMARY KEY DEFAULT nextval('drop_test'))
   794  
   795  statement error pq: cannot drop sequence drop_test because other objects depend on it
   796  DROP SEQUENCE drop_test
   797  
   798  statement ok
   799  DROP TABLE drop_test_tbl
   800  
   801  statement ok
   802  DROP SEQUENCE drop_test
   803  
   804  # Test that sequences can only be modified with the UPDATE permission
   805  # and read with the SELECT permission.
   806  
   807  statement ok
   808  CREATE SEQUENCE priv_test
   809  
   810  user testuser
   811  
   812  statement error pq: user testuser does not have SELECT privilege on relation priv_test
   813  SELECT * FROM priv_test
   814  
   815  statement error pq: nextval\(\): user testuser does not have UPDATE privilege on relation priv_test
   816  SELECT nextval('priv_test')
   817  
   818  statement error pq: setval\(\): user testuser does not have UPDATE privilege on relation priv_test
   819  SELECT setval('priv_test', 5)
   820  
   821  user root
   822  
   823  # Verify that the value hasn't been changed.
   824  query I
   825  SELECT last_value FROM priv_test
   826  ----
   827  0
   828  
   829  statement ok
   830  GRANT UPDATE, SELECT ON priv_test TO testuser
   831  
   832  user testuser
   833  
   834  # After the grant, testuser can select, increment, and set.
   835  
   836  statement ok
   837  SELECT nextval('priv_test')
   838  
   839  statement ok
   840  SELECT setval('priv_test', 5)
   841  
   842  query I
   843  SELECT last_value FROM priv_test
   844  ----
   845  5
   846  
   847  user root
   848  
   849  subtest virtual_sequences
   850  
   851  statement ok
   852  CREATE SEQUENCE sv VIRTUAL
   853  
   854  query T
   855  SELECT create_statement FROM [SHOW CREATE SEQUENCE sv]
   856  ----
   857  CREATE SEQUENCE sv MINVALUE 1 MAXVALUE 9223372036854775807 INCREMENT 1 START 1 VIRTUAL
   858  
   859  statement ok
   860  CREATE TABLE svals(x INT)
   861  
   862  statement ok
   863  BEGIN;
   864    INSERT INTO svals VALUES(nextval('sv'));
   865    INSERT INTO svals VALUES(lastval());
   866    INSERT INTO svals VALUES(currval('sv'));
   867  END
   868  
   869  # Check that lastval returns the last auto-generated virtual value.
   870  query I
   871  SELECT count(DISTINCT x) FROM svals
   872  ----
   873  1
   874  
   875  # Check that the KV trace is empty.
   876  statement ok
   877  BEGIN;
   878    SELECT nextval('sv'); -- acquire the lease, so that doesn't go to the KV trace
   879    SET tracing = on; SELECT nextval('sv'); SET tracing = off;
   880    COMMIT
   881  
   882  query T
   883  SELECT message FROM [SHOW KV TRACE FOR SESSION]
   884  ----
   885  rows affected: 1
   886  
   887  statement ok
   888  DROP SEQUENCE sv
   889  
   890  # Check that generators can be interrupted by statement timeouts.
   891  subtest generator_timeout
   892  
   893  statement ok
   894  SET statement_timeout = 1
   895  
   896  statement error pq: query execution canceled due to statement timeout
   897  select * from generate_series(1,10000000) where generate_series = 0;
   898  
   899  # Clean up
   900  statement ok
   901  SET statement_timeout = 0
   902  
   903  # Test that multiple columns associated with the same sequence follow correct
   904  # dependency behavior. Regression test for #40852
   905  
   906  statement ok
   907  SET sql_safe_updates = false
   908  
   909  statement ok
   910  CREATE SEQUENCE seq;
   911  
   912  statement ok
   913  CREATE TABLE abc(a INT DEFAULT nextval('seq'), b INT default nextval('seq'), c int)
   914  
   915  statement error pq: cannot drop sequence seq because other objects depend on it
   916  DROP SEQUENCE seq;
   917  
   918  statement ok
   919  ALTER TABLE abc DROP COLUMN b;
   920  
   921  statement error pq: cannot drop sequence seq because other objects depend on it
   922  DROP SEQUENCE seq;
   923  
   924  statement ok
   925  ALTER TABLE abc DROP COLUMN a;
   926  
   927  statement ok
   928  DROP SEQUENCE seq;
   929  
   930  # Sequence Ownership tests
   931  
   932  # Sequence can be owned be owned by a table column
   933  
   934  statement ok
   935  CREATE TABLE owner(owner_col INT)
   936  
   937  statement ok
   938  CREATE SEQUENCE owned_seq OWNED BY owner.owner_col
   939  
   940  query TTT
   941  SELECT seqclass.relname AS sequence_name,
   942         depclass.relname AS table_name,
   943         attrib.attname   as column_name
   944  FROM   pg_class AS seqclass
   945         JOIN pg_depend AS dep
   946           ON seqclass.oid = dep.objid
   947         JOIN pg_class AS depclass
   948           ON dep.refobjid = depclass.oid
   949         JOIN pg_attribute AS attrib
   950           ON attrib.attnum = dep.refobjsubid
   951                AND attrib.attrelid = dep.refobjid
   952  WHERE seqclass.relkind = 'S';
   953  ----
   954  owned_seq owner owner_col
   955  
   956  # Sequence owner can be removed
   957  
   958  statement ok
   959  ALTER SEQUENCE owned_seq OWNED BY NONE
   960  
   961  statement count 0
   962  SELECT seqclass.relname AS sequence_name,
   963         depclass.relname AS table_name,
   964         attrib.attname   as column_name
   965  FROM   pg_class AS seqclass
   966         JOIN pg_depend AS dep
   967           ON seqclass.oid = dep.objid
   968         JOIN pg_class AS depclass
   969           ON dep.refobjid = depclass.oid
   970         JOIN pg_attribute AS attrib
   971           ON attrib.attnum = dep.refobjsubid
   972                AND attrib.attrelid = dep.refobjid
   973  WHERE seqclass.relkind = 'S';
   974  
   975  # cleanup
   976  statement ok
   977  DROP TABLE owner
   978  
   979  statement ok
   980  DROP SEQUENCE owned_seq
   981  
   982  
   983  # Sequence is dropped when its owner is dropped
   984  statement ok
   985  CREATE SEQUENCE owned_seq;
   986  
   987  statement ok
   988  CREATE TABLE a(a INT DEFAULT nextval('owned_seq'));
   989  
   990  statement ok
   991  ALTER SEQUENCE owned_seq OWNED BY a.a;
   992  
   993  statement ok
   994  DROP TABLE a;
   995  
   996  statement error relation "owned_seq" does not exist
   997  DROP SEQUENCE owned_seq;
   998  
   999  
  1000  # DROP TABLE and ALTER TABLE ... DROP COLUMN work correctly with multiple sequence dependencies
  1001  # and Ownership scenarios
  1002  
  1003  statement ok
  1004  CREATE SEQUENCE owned_seq;
  1005  
  1006  statement ok
  1007  CREATE TABLE ab(a INT DEFAULT nextval('owned_seq'), b INT DEFAULT nextval('owned_seq'));
  1008  
  1009  statement ok
  1010  ALTER SEQUENCE owned_seq OWNED BY ab.a;
  1011  
  1012  statement error cannot drop table ab because other objects depend on it
  1013  ALTER TABLE ab DROP COLUMN a;
  1014  
  1015  statement ok
  1016  DROP TABLE ab
  1017  
  1018  statement error relation "owned_seq" does not exist
  1019  DROP SEQUENCE owned_seq;
  1020  
  1021  
  1022  # Test changing owners works correctly by ensuring sequence is not dropped
  1023  # when previous owners are dropped.
  1024  statement ok
  1025  CREATE TABLE a(a INT);
  1026  
  1027  statement ok
  1028  CREATE TABLE b(b INT);
  1029  
  1030  statement ok
  1031  CREATE SEQUENCE seq OWNED BY a.a;
  1032  
  1033  statement ok
  1034  ALTER SEQUENCE seq OWNED BY a.a;
  1035  
  1036  statement ok
  1037  ALTER SEQUENCE seq OWNED BY b.b;
  1038  
  1039  statement ok
  1040  DROP TABLE a;
  1041  
  1042  statement ok
  1043  ALTER SEQUENCE seq OWNED BY NONE;
  1044  
  1045  statement ok
  1046  DROP TABLE b;
  1047  
  1048  statement ok
  1049  DROP SEQUENCE seq;
  1050  
  1051  
  1052  # Test a table/column can not be dropped if it owns a Sequence that is used
  1053  # by another table.
  1054  
  1055  statement ok
  1056  CREATE TABLE a(a INT);
  1057  
  1058  statement ok
  1059  CREATE SEQUENCE seq OWNED BY a.a;
  1060  
  1061  statement ok
  1062  CREATE TABLE b(b INT DEFAULT nextval('seq'));
  1063  
  1064  statement error cannot drop table a because other objects depend on it
  1065  DROP TABLE a
  1066  
  1067  statement error cannot drop table a because other objects depend on it
  1068  ALTER TABLE a DROP COLUMN a;
  1069  
  1070  statement ok
  1071  DROP TABLE b;
  1072  
  1073  statement ok
  1074  DROP TABLE a;