13

As you know, we can view the details of a sequence by using '\d' command. I would like to know if there is a way to get these information with a SQL query so that I can view the details for all sequences in PostgreSQL database.

Eventually, the left 'Column' will be displayed horizontally like we see normally when we use a sql statement.

postgres=# \d next_empno
         Sequence "public.next_empno"
    Column     |  Type   |        Value        
---------------+---------+---------------------
 sequence_name | name    | next_empno
 last_value    | bigint  | 8000
 start_value   | bigint  | 8000
 increment_by  | bigint  | 1
 max_value     | bigint  | 9223372036854775807
 min_value     | bigint  | 1
 cache_value   | bigint  | 1
 log_cnt       | bigint  | 0
 is_cycled     | boolean | f
 is_called     | boolean | f
6
  • 4
    You can always \ds *.*, but otherwise, use the information_schema Commented Jul 31, 2016 at 2:41
  • I didn't know about information_schema. It has everything I need to check. Thank you. Commented Jul 31, 2016 at 4:05
  • 2
    A good way to find those queries is to start psql with the -E (echo hidden) option. It will show you exactly what queries psql uses internally Commented Jul 31, 2016 at 7:08
  • Oh. Really wonderful!. Awesome! With that option, we can find the internal queries very easily. Cool. Commented Jul 31, 2016 at 7:23
  • 1
    Though note that psql uses the PostgreSQL catalogs directly. Unlike information_schema these are subject to change from version to version and are not standard across database engines. Where possible, you should prefer the information_schema. Commented Aug 1, 2016 at 0:14

3 Answers 3

14

To view the DDL of the sequence, use this

select * from information_schema.sequences where sequence_name='<your_sequence_name_in_lower_case>'
Sign up to request clarification or add additional context in comments.

Comments

8

If I understand correctly, you can use `INFORMATION_SCHEMA.sequences.

There documentation is here.

2 Comments

It would have been nice if you actually put an example query. I don't find this any more useful than the first comment on the OPs question.
@PanamaJack . . . This answer was 33 minutes before the first comment.
1

SQL for view 'sequences':

SELECT * FROM information_schema.sequences

1 Comment

the better question is how to list them with their currval()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.