Thread: Supplements and suggestions and about postgresql sequence setval function in the documents



in the following example;
select setval('seq_test',111);
---this setval('seq_test',111) :  update   the current schema's seq_test   last_value =111

select setval('schemalei.seq_test',222);
---this setval('seq_test',111) :  update   the  schemalei schema's seq_test  last_value =222

but in the setval document(https://www.postgresql.org/docs/current/functions-sequence.html), there is no description about it.

[pg164@localhost bin]$ ./psql -d postgres
psql (16.4)
Type "help" for help.

postgres=# \d
Did not find any relations.
postgres=# 
postgres=# 
postgres=# create schema schemalei;
CREATE SCHEMA
postgres=# create sequence public.seq_test ;
CREATE SEQUENCE
postgres=# create sequence schemalei.seq_test ;
CREATE SEQUENCE
postgres=# show search_path;
   search_path   
-----------------
 "$user", public
(1 row)

postgres=# \dus
                             List of roles
 Role name |                         Attributes                         
-----------+------------------------------------------------------------
 pg164     | Superuser, Create role, Create DB, Replication, Bypass RLS

postgres=# select * from pg_sequences;
 schemaname | sequencename | sequenceowner | data_type | start_value | min_value |      max_value      | increment_by | cycle | cache_size | last_value 
------------+--------------+---------------+-----------+-------------+-----------+---------------------+--------------+-------+------------+------------
 public     | seq_test     | pg164         | bigint    |           1 |         1 | 9223372036854775807 |            1 | f     |          1 |           
 schemalei  | seq_test     | pg164         | bigint    |           1 |         1 | 9223372036854775807 |            1 | f     |          1 |           
(2 rows)

postgres=# select setval('seq_test',111);
 setval 
--------
    111
(1 row)

postgres=# select setval('schemalei.seq_test',222);
 setval 
--------
    222
(1 row)

postgres=# select * from pg_sequences;
 schemaname | sequencename | sequenceowner | data_type | start_value | min_value |      max_value      | increment_by | cycle | cache_size | last_value 
------------+--------------+---------------+-----------+-------------+-----------+---------------------+--------------+-------+------------+------------
 public     | seq_test     | pg164         | bigint    |           1 |         1 | 9223372036854775807 |            1 | f     |          1 |        111
 schemalei  | seq_test     | pg164         | bigint    |           1 |         1 | 9223372036854775807 |            1 | f     |          1 |        222
(2 rows)

postgres=# select version();
                                   version                                   
-----------------------------------------------------------------------------
 PostgreSQL 16.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.0, 64-bit
(1 row)

postgres=# 
On Wednesday, August 21, 2024, yanliang lei <msdnchina@163.com> wrote:


in the following example;
select setval('seq_test',111);
---this setval('seq_test',111) :  update   the current schema's seq_test   last_value =111

select setval('schemalei.seq_test',222);
---this setval('seq_test',111) :  update   the  schemalei schema's seq_test  last_value =222

but in the setval document(https://www.postgresql.org/docs/current/functions-sequence.html), there is no description about it.


You haven’t indicated what you want done, but the interpretation of the string literal as a regclass value is discussed in the regclass type definition.  This page rightly does not need to go into detail explaining how types work, there is other documentation for that.  Even the lack of a link to that page isn’t a problem IMO, though the rarity of the reg* types could warrant an exception.

David J.

What I want to express is:
   There is no description in the document that the schema name can be included before the sequence name






在 2024-08-21 21:01:35,"David G. Johnston" <david.g.johnston@gmail.com> 写道:

On Wednesday, August 21, 2024, yanliang lei <msdnchina@163.com> wrote:


in the following example;
select setval('seq_test',111);
---this setval('seq_test',111) :  update   the current schema's seq_test   last_value =111

select setval('schemalei.seq_test',222);
---this setval('seq_test',111) :  update   the  schemalei schema's seq_test  last_value =222

but in the setval document(https://www.postgresql.org/docs/current/functions-sequence.html), there is no description about it.


You haven’t indicated what you want done, but the interpretation of the string literal as a regclass value is discussed in the regclass type definition.  This page rightly does not need to go into detail explaining how types work, there is other documentation for that.  Even the lack of a link to that page isn’t a problem IMO, though the rarity of the reg* types could warrant an exception.

David J.

On Wed, Aug 21, 2024 at 4:41 PM yanliang lei <msdnchina@163.com> wrote:

What I want to express is:
   There is no description in the document that the schema name can be included before the sequence name



Correct.  It is here:

All of the OID alias types for objects that are grouped by namespace accept schema-qualified names, and will display schema-qualified names on output if the object would not be found in the current search path without being qualified. For example, myschema.mytable is acceptable input for regclass (if there is such a table). That value might be output as myschema.mytable, or just mytable, depending on the current search path.


David J.

On Thu, 22 Aug 2024 at 11:41, yanliang lei <msdnchina@163.com> wrote:
> What I want to express is:
>    There is no description in the document that the schema name can be included before the sequence name

I'm not to sure there is any problem here. If you look at the final
paragraph in: https://www.postgresql.org/docs/current/functions-sequence.html
you'll see:

"The sequence to be operated on by a sequence function is specified by
a regclass argument, which is simply the OID of the sequence in the
pg_class system catalog. You do not have to look up the OID by hand,
however, since the regclass data type's input converter will do the
work for you. See Section 8.19 for details."

If you navigate to Section 8.19, you'll see:

"All of the OID alias types for objects that are grouped by namespace
accept schema-qualified names, and will display schema-qualified names
on output if the object would not be found in the current search path
without being qualified. For example, myschema.mytable is acceptable
input for regclass (if there is such a table)."

I don't think it would be a good idea to repeat this information each
time we document a function which has a regclass parameter, (or any
other reg* type). Doing so would bloat the documentation quite a lot.
In any case, if we were to do that, why would we limit it to reg*
types? Could someone come along and argue that we didn't reiterate
what an INT type was each time we document a function with an INT
parameter?

I think once the reader learns what a regclass type is, they'll be
quite irritated if we repeatedly tell them what it is again and again.

David