askvity

How Do You Modify a Sequence?

Published in Database Management 3 mins read

To modify a sequence, you primarily use the ALTER SEQUENCE command.

Modifying a sequence generally involves changing its properties, such as its increment, minimum value, maximum value, cycle behavior, and cache size. You cannot directly modify the current value of a sequence using ALTER SEQUENCE. To change the current value, you would typically use setval(). You can also change the sequence's schema.

Here's a breakdown of how to modify different aspects of a sequence:

1. Changing the Increment:

The increment determines how much the sequence value increases (or decreases) with each call.

ALTER SEQUENCE my_sequence INCREMENT BY 5;

This command modifies my_sequence to increment by 5 instead of its original increment. A negative increment will decrement the sequence.

2. Setting Minimum and Maximum Values:

You can define the minimum and maximum values that a sequence can reach.

ALTER SEQUENCE my_sequence MINVALUE 1 MAXVALUE 1000;

This sets the minimum value of my_sequence to 1 and the maximum value to 1000. If you don't want a minimum or maximum value, you can use NO MINVALUE or NO MAXVALUE.

3. Modifying Cycle Behavior:

The CYCLE option determines what happens when a sequence reaches its maximum (or minimum) value. If CYCLE is enabled, the sequence will wrap around to the minimum (or maximum) value. NO CYCLE prevents this.

ALTER SEQUENCE my_sequence CYCLE; -- Enable cycle
ALTER SEQUENCE my_sequence NO CYCLE; -- Disable cycle

4. Altering the Cache Size:

The cache size specifies how many sequence numbers are preallocated and stored in memory for faster access.

ALTER SEQUENCE my_sequence CACHE 20;

This sets the cache size of my_sequence to 20. A larger cache size can improve performance, but it also means that sequence numbers might be skipped if the server crashes. You can use NO CACHE to disable caching.

5. Changing the Sequence's Schema:

You can move a sequence to a different schema using:

ALTER SEQUENCE my_sequence SET SCHEMA new_schema;

This moves the sequence my_sequence to the schema named new_schema.

Important Considerations:

  • Permissions: You must have the necessary privileges to alter a sequence. Typically, the owner of the sequence has these privileges.
  • Concurrency: Modifying a sequence can affect concurrent transactions that are using the sequence. Consider the impact on other database operations.
  • START WITH Value: You cannot change the START WITH value after the sequence has been created. To effectively reset a sequence, you often need to use setval() function in conjunction with finding the correct new start value or drop and recreate the sequence.
  • setval() Function: Although not part of the ALTER SEQUENCE command, the setval() function is crucial for setting the current value of a sequence. For example: SELECT setval('my_sequence', 101); will set the next value returned by nextval('my_sequence') to 101.

In summary, modifying a sequence is accomplished through the ALTER SEQUENCE command, allowing you to adjust increment values, minimum and maximum values, cycle behavior, cache size, and schema. Use setval() to change the current value of the sequence.

Related Articles