Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-17030

Reset Sequence doesn't reset the value the second time.

Details

    • Bug
    • Status: Closed (View Workflow)
    • Critical
    • Resolution: Won't Fix
    • 10.2.14
    • N/A
    • Sequences
    • None

    Description

      Reset Sequence doesn't reset the value the second time.

       
      Test case:-
       
      MariaDB [test1]> set sql_mode=ORACLE;
      Query OK, 0 rows affected (0.001 sec)
       
      MariaDB [test1]> CREATE or replace SEQUENCE "entkaisgc_sg" start with 1 minvalue 1 maxvalue 9999999999999999 increment by 1 cache 20 cycle ;
      Query OK, 0 rows affected (0.022 sec)
       
      MariaDB [test1]> select entkaisgc_sg.nextval;
      +----------------------+
      | entkaisgc_sg.nextval |
      +----------------------+
      | 1 |
      +----------------------+
      1 row in set (0.012 sec)
       
      MariaDB [test1]> select entkaisgc_sg.nextval;
      +----------------------+
      | entkaisgc_sg.nextval |
      +----------------------+
      | 2 |
      +----------------------+
      1 row in set (0.001 sec)
       
      MariaDB [test1]> SELECT setval(entkaisgc_sg,9999999999999999);
      +---------------------------------------+
      | setval(entkaisgc_sg,9999999999999999) |
      +---------------------------------------+
      | 9999999999999999 |
      +---------------------------------------+
      1 row in set (0.001 sec)
       
      MariaDB [test1]> select entkaisgc_sg.nextval;
      +----------------------+
      | entkaisgc_sg.nextval |
      +----------------------+
      | 1 |
      +----------------------+
      1 row in set (0.001 sec)
       
      MariaDB [test1]> select entkaisgc_sg.nextval;
      +----------------------+
      | entkaisgc_sg.nextval |
      +----------------------+
      | 2 |
      +----------------------+
      1 row in set (0.000 sec)
       
      MariaDB [test1]> SELECT setval(entkaisgc_sg,9999999999999999);
      +---------------------------------------+
      | setval(entkaisgc_sg,9999999999999999) |
      +---------------------------------------+
      | NULL |
      +---------------------------------------+
      1 row in set (0.000 sec)
       
      MariaDB [test1]> select entkaisgc_sg.nextval;
      +----------------------+
      | entkaisgc_sg.nextval |
      +----------------------+
      | 3 |
      +----------------------+
      1 row in set (0.001 sec)
       
      
      

      Attachments

        Activity

          alice Alice Sherepa added a comment -

          from documentation https://mariadb.com/kb/en/library/setval/
          "For SEQUENCE tables defined with CYCLE one should use both next_value and round to define the next value. In this case the current sequence value is defined to be round, next_value.

          The result returned by SETVAL() is next_value or NULL if the given next_value and round is smaller than the current value.

          SETVAL() will not set the SEQUENCE value to a something that is less than its current value. This is needed to ensure that SETVAL() is replication safe. If you want to set the SEQUENCE to a smaller number use ALTER SEQUENCE."

          I guess it is not very clear documented about round option and no examples there, but to return the correct results, round option should be used:

          MariaDB [test]> CREATE or replace SEQUENCE "s" start with 1 minvalue 1 maxvalue 99  increment by 1 cache 20 cycle;
          Query OK, 0 rows affected (0.030 sec)
           
          MariaDB [test]> select setval(s,99,true); 
          +-------------------+
          | setval(s,99,true) |
          +-------------------+
          |                99 |
          +-------------------+
          1 row in set (0.001 sec)
           
          MariaDB [test]> select * from s; 
          +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+
          | next_not_cached_value | minimum_value | maximum_value | start_value | increment | cache_size | cycle_option | cycle_count |
          +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+
          |                   100 |             1 |            99 |           1 |         1 |         20 |            1 |           0 |
          +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+
          1 row in set (0.000 sec)
           
          MariaDB [test]> select s.nextval;
          +-----------+
          | s.nextval |
          +-----------+
          |         1 |
          +-----------+
          1 row in set (0.000 sec)
           
          MariaDB [test]> select * from s; ##cycle_count=1
          +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+
          | next_not_cached_value | minimum_value | maximum_value | start_value | increment | cache_size | cycle_option | cycle_count |
          +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+
          |                    21 |             1 |            99 |           1 |         1 |         20 |            1 |           1 |
          +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+
          1 row in set (0.000 sec)
           
          MariaDB [test]> select setval(s,99,true,1); 
          +---------------------+
          | setval(s,99,true,1) |
          +---------------------+
          |                  99 |
          +---------------------+
          1 row in set (0.000 sec)
           
          MariaDB [test]> select * from s;
          +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+
          | next_not_cached_value | minimum_value | maximum_value | start_value | increment | cache_size | cycle_option | cycle_count |
          +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+
          |                   100 |             1 |            99 |           1 |         1 |         20 |            1 |           1 |
          +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+
          1 row in set (0.000 sec)
           
          MariaDB [test]> select s.nextval;
          +-----------+
          | s.nextval |
          +-----------+
          |         1 |
          +-----------+
          1 row in set (0.000 sec)
           
          MariaDB [test]> select * from s;
          +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+
          | next_not_cached_value | minimum_value | maximum_value | start_value | increment | cache_size | cycle_option | cycle_count |
          +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+
          |                    21 |             1 |            99 |           1 |         1 |         20 |            1 |           2 |
          +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+
          1 row in set (0.000 sec)
           
          MariaDB [test]> select setval(s,99,true,1); 
          +---------------------+
          | setval(s,99,true,1) |
          +---------------------+
          |                NULL |
          +---------------------+
          1 row in set (0.000 sec)
           
          MariaDB [test]> select setval(s,99,true,2); 
          +---------------------+
          | setval(s,99,true,2) |
          +---------------------+
          |                  99 |
          +---------------------+
          1 row in set (0.000 sec)
          
          

          alice Alice Sherepa added a comment - from documentation https://mariadb.com/kb/en/library/setval/ "For SEQUENCE tables defined with CYCLE one should use both next_value and round to define the next value. In this case the current sequence value is defined to be round, next_value. The result returned by SETVAL() is next_value or NULL if the given next_value and round is smaller than the current value. SETVAL() will not set the SEQUENCE value to a something that is less than its current value. This is needed to ensure that SETVAL() is replication safe. If you want to set the SEQUENCE to a smaller number use ALTER SEQUENCE." I guess it is not very clear documented about round option and no examples there, but to return the correct results, round option should be used: MariaDB [test]> CREATE or replace SEQUENCE "s" start with 1 minvalue 1 maxvalue 99 increment by 1 cache 20 cycle; Query OK, 0 rows affected (0.030 sec)   MariaDB [test]> select setval(s,99,true); +-------------------+ | setval(s,99,true) | +-------------------+ | 99 | +-------------------+ 1 row in set (0.001 sec)   MariaDB [test]> select * from s; +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+ | next_not_cached_value | minimum_value | maximum_value | start_value | increment | cache_size | cycle_option | cycle_count | +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+ | 100 | 1 | 99 | 1 | 1 | 20 | 1 | 0 | +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+ 1 row in set (0.000 sec)   MariaDB [test]> select s.nextval; +-----------+ | s.nextval | +-----------+ | 1 | +-----------+ 1 row in set (0.000 sec)   MariaDB [test]> select * from s; ##cycle_count=1 +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+ | next_not_cached_value | minimum_value | maximum_value | start_value | increment | cache_size | cycle_option | cycle_count | +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+ | 21 | 1 | 99 | 1 | 1 | 20 | 1 | 1 | +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+ 1 row in set (0.000 sec)   MariaDB [test]> select setval(s,99,true,1); +---------------------+ | setval(s,99,true,1) | +---------------------+ | 99 | +---------------------+ 1 row in set (0.000 sec)   MariaDB [test]> select * from s; +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+ | next_not_cached_value | minimum_value | maximum_value | start_value | increment | cache_size | cycle_option | cycle_count | +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+ | 100 | 1 | 99 | 1 | 1 | 20 | 1 | 1 | +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+ 1 row in set (0.000 sec)   MariaDB [test]> select s.nextval; +-----------+ | s.nextval | +-----------+ | 1 | +-----------+ 1 row in set (0.000 sec)   MariaDB [test]> select * from s; +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+ | next_not_cached_value | minimum_value | maximum_value | start_value | increment | cache_size | cycle_option | cycle_count | +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+ | 21 | 1 | 99 | 1 | 1 | 20 | 1 | 2 | +-----------------------+---------------+---------------+-------------+-----------+------------+--------------+-------------+ 1 row in set (0.000 sec)   MariaDB [test]> select setval(s,99,true,1); +---------------------+ | setval(s,99,true,1) | +---------------------+ | NULL | +---------------------+ 1 row in set (0.000 sec)   MariaDB [test]> select setval(s,99,true,2); +---------------------+ | setval(s,99,true,2) | +---------------------+ | 99 | +---------------------+ 1 row in set (0.000 sec)

          This is as expected and documented:

          "The result returned by SETVAL() is next_value or NULL if the given next_value and round is smaller than the current value."

          The problem here is that the second setval() command in the example is same as:
          SELECT setval(entkaisgc_sg,9999999999999999,1,0);
          In other words, it will only update nextvalue if round is 0 (or lower)

          As the sequence has already done one cycle, it's internal cycle_count (ie round) is 1 and this is what setval() fails.

          Another option to change the sequence that 'always' work independent of the current sequence value is to use ALTER SEQUENCE.

          monty Michael Widenius added a comment - This is as expected and documented: "The result returned by SETVAL() is next_value or NULL if the given next_value and round is smaller than the current value." The problem here is that the second setval() command in the example is same as: SELECT setval(entkaisgc_sg,9999999999999999,1,0); In other words, it will only update nextvalue if round is 0 (or lower) As the sequence has already done one cycle, it's internal cycle_count (ie round) is 1 and this is what setval() fails. Another option to change the sequence that 'always' work independent of the current sequence value is to use ALTER SEQUENCE.
          greenman Ian Gilfillan added a comment -

          The documentation needed expanding here, as there were no examples with round. I've added an example and clarification to make this clearer.

          greenman Ian Gilfillan added a comment - The documentation needed expanding here, as there were no examples with round. I've added an example and clarification to make this clearer.

          People

            greenman Ian Gilfillan
            pramod.mahto@mariadb.com Pramod Mahto
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.