Sequences

In the object hierarchy under any table, you will find Sequences. Sequences can be added by selecting CREATE SEQUENCE from the Sequences context menu. Once a sequence has been created, it can also be renamed, commented on, altered, and dropped. This section describes how to create, manage, and drop a sequence with RHDB Administrator.

PostgreSQL allows you to create sequences on a database. A sequence is a number generator that is restricted by various values such as start value, minimum value, maximum value, and increment. Each time the sequence is called upon, it returns the next acceptable number, given the restrictions in the definition of the sequence.

In this example, you will create a sequence named invoice_sequence.

  1. A sequence is an object under a database, so to create a sequence, first expand the database you wish to create the sequence under (click the + beside the database name).

  2. Right-click on Sequences and select CREATE SEQUENCE.

  3. In the Name For The New Sequence field, type: invoice_sequence

  4. Select the direction in which the sequence progresses. For this example, use Ascending as the direction.

  5. Select the Increment. Each time the sequence is accessed, it generates a new number that differs from the previous number by this amount. For this example, set the Increment to 5.

  6. Set the MinValue, the minimum value that the sequence can generate. For this example, set the MinValue to 0.

  7. Set the MaxValue, the maximum value that the sequence can generate. For this example, set the MaxValue to 100000.

  8. Set the Start value, the number generated by the sequence when it is invoked for the first time. If the start value is not specified, the MinValue is used as the starting value for ascending sequences and the MaxValue is used as the starting value for descending sequences. For this example, set the Start value to 20.

  9. Set the Cache, the number of values to be computed and stored in memory to prevent any slowdowns due to realtime computations. For this example, the Cache will be set to 3.

    Note

    Cache should be used very carefully; its use may generate unexpected results in certain cases. For example, when two backends are using the same sequence object, they will each cache the values and change the sequence object. As a result, the sequence may not produce the expected results. Refer to the Red Hat Database SQL Guide and Reference for further details.

  10. Set whether the sequence should Cycle. If this box is checked, the sequence values will restart once the MaxValue (for Ascending Sequences) or the MinValue (for Descending Sequences) has been reached. For this example, check the Cycle box.

  11. Click OK to create the sequence.

Figure 4-42. The Sequences Tree View and Detailed View

Note that the sequence invoice_sequence has been added under the Sequences node in the Tree View.

RHDB Administrator also enables you to perform the following operations on an existing sequence:

Dropping

To drop a sequence, right-click on it and select DROP SEQUENCE. If View -> Ask For Confirmations is enabled, you are asked to confirm that you want the sequence dropped; if it is disabled, the sequence is dropped immediately. This action cannot be undone.

Renaming

A sequence can be renamed once it has been created. To rename a sequence, right-click on it and select RENAME SEQUENCE. Type the new name in the dialog that appears and click OK.

Altering Owner

By default, the user creating the sequence is the owner. However, the owner can later be modified by a superuser. To modify the owner of a sequence, right-click on the sequence and select ALTER SEQUENCE OWNER. A dialog pops up asking for the new ownername. Because the owner name can be only one of the users in the cluster, the dialog has a drop down box from which the user can be selected. Once the username is selected, click OK to change the sequence owner.

Commenting

PostgreSQL allows commenting on sequences, which makes them easier to identify. To comment on a sequence, right click on it, and select COMMENT. A dialog pops up asking for the comment. If there is already a comment on the sequence, this comment is displayed. Type the new comment in the edit box and click OK (or press [Enter]) to save the new comment.