PostgreSQL sequences are created to automatically insert or update values on function calls like Nextval, Setval, and currval functions. In this article, we will be discussing the Setval function present in the list of sequence functions. The Setval function is called in a sequence when we have to change the next value in the current sequence due to some conditions. The manual alteration in a sequence is done by the Setval function in PostgreSQL.

Sequence Functions in SQL

When we use a column in our table that has sequential values like a serial number or a roll number that increments with a pattern, we can create a sequence to encounter the problem. In this sequence, we will give several constraints according to our needs. The SQL syntax for creating a sequence is as follows:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-955.png" data-lazy- height="304" src="data:image/svg xml,” width=”641″>

The name of the series must be written against the ” CREATE SEQUENCE ” command in the above syntax. The starting value of the sequence will then be written to the ” START WITH ” command, followed by the incremental value to the ” INCREMENT BY ” command. The ” MINVALUE ” and “ MAXVALUE” commands will be used to establish the sequence’s minimum value or maximum value. We will explain whether the sequence should end when it reaches the maximum value or return to the original value in the last line of the sequence.

When we have to alter a sequence in between due to some constraints or errors in the program, we use several sequence functions in PostgreSQL. They are also referred to as the Sequence Manipulation Functions. The functions that are associated with the concept of sequence function are as follows:

  1. Nextval function.
  2. Currval function.
  3. Lastval function.
  4. Setval function.

In this guide, we will be talking over the Setval function in PostgreSQL.

Setval Sequence Manipulation Function

The Setval function resets the current sequence counter variable that keeps incrementing as per the constraints set by the user and gives it a check at how the sequence should proceed after this point. The Setval function is adjustable to our needs and can take different sets of parameters like the name of the sequence, the next value that the nextval function would return, and the Boolean algebra constraint of true and false.

The Setval function is typically used in association with the ” SELECT ” command, in which the user specifies the constraints and then specifies the order in which the change must be made inside the table.

The Setval function manipulates the next value of the nextval function as it takes the current nextval value as a parameter. If the third parameter that is the state of the value that can either be true or false is determined, then the next nextval value is altered according to the given state that is compiled at the runtime when the sequence is located. Let’s look at the syntax for executing this manipulation function in PostgreSQL:

>> SELECT SETVAL(name of the sequence, next value of the sequence, true or false);

The above syntax for executing this function in PostgreSQL can also be written as:

>> SELECT SETVAL(name of the sequence, next value of the sequence);

<img data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-958.png" height="145" src="data:image/svg xml,” width=”290″>

We can choose both these expressions to execute the Setval function in the PostgreSQL environment, but the second expression does not take in the Boolean check as the parameter and makes it a less refined state of the function.

Now that we know the syntax and the working of the Setval function, we will look into the core purpose this function serves in the PostgreSQL environment.

Manipulation of a Sequence by the Setval Function

As we discussed above the concept of sequences and how we create a sequence. In this case, we will create a sequence first to understand and manipulate that sequence by the Setval function. We will create a sequence for roll numbers of students in one class by writing this code as shown in the below snapshot:

<img alt="Text Description automatically generated" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/text-description-automatically-generated-43.png" height="132" src="data:image/svg xml,” width=”245″>

As you can see from the above syntax, we have constructed a sequence called ” sq 1 ” that begins with the number 001 and is increased by one at each iteration. The minimum value for this sequence is 0 while the maximum value is 100. This sequence goes in a cycle which means when the maximum value is achieved it will start again from 001 as it would suggest that the class is full, and the next student must be added to another class.

Now, let’s assume that a student drops from a class after its data values are inserted by the nextval function, we will have to alter the sequence to assign the roll numbers in a hierarchical order by the Setval function.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-960.png" data-lazy- height="34" src="data:image/svg xml,” width=”323″>

In this piece of code, we have set the next value of the nextval function to “012” by stating it as false. So, the student’s data that we will enter after the “012” roll number student, who dropped out of class we automatically get the “012” roll number as you can see in the demonstration below:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-964.png" data-lazy- height="29" src="data:image/svg xml,” width=”437″>

You can verify the output from the appended image.

<img alt="A picture containing table Description automatically generated" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/a-picture-containing-table-description-automatica.png" height="260" src="data:image/svg xml,” width=”229″>

As we executed the insert query for entering a value in the table, the nextval function gave the value that was set by the Setval function in the sequence sq_1.

Different Ways to Use Setval Function

If we have to ensure the last value of the nextval function is true and we can add the other value as per the constraints defined in the sequence, we can write this query in two different ways in the PostgreSQL environment:

  1. Setval function without the Boolean state.
  2. Setval function with the Boolean state.

Setval Function Without the Boolean State

The syntax for executing the Setval function without the Boolean state is stated below:

>> SELECT setval(‘seq_1’ , 052);

<img data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-970.png" height="28" src="data:image/svg xml,” width=”269″>

The output can be verified from the appended screenshot.

<img alt="A screenshot of a computer screen Description automatically generated with medium confidence" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/a-screenshot-of-a-computer-screen-description-aut.png" height="158" src="data:image/svg xml,” width=”258″>

In the above syntax, the next value of the nextval function will be set to “053” which means the newest value of the function is utilized.

Setval Function with the Boolean State

The Setval function can also be executed with the Boolean function, the statement for that is:

>> SELECT setval(‘seq_1’ , 052, true);

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/word-image-974.png" data-lazy- height="31" src="data:image/svg xml,” width=”323″>

The output can be verified from the appended screenshot.

<img alt="A screenshot of a computer screen Description automatically generated with medium confidence" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/a-screenshot-of-a-computer-screen-description-aut-1.png" height="158" src="data:image/svg xml,” width=”258″>

In both the above examples the next value of the nextval function will be set to “053” as it satisfies the constraint that the latest value of the nextval function is utilized. In the output above, when we inserted the next value using an insert query with the nextval function in the sequence after 52 it gave 53 in the table as shown above.

Conclusion

In this article, we have learned about the concept of sequences. We discussed why we use these sequences in our tables to generate values in this article. The different sequence functions that are used to alter the sequences in the PostgreSQL were also discussed. One of them was the Setval function that we discussed in a great ordeal in this article. The different types of uses this function can be utilized were also discussed. In the end, we also looked at how this function works in the PostgreSQL environment with its different kinds of constraints given in the parameter.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/02/echo/Author-Image-150×150.jpg621700d874b41.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.