Skip to content

Commit 39b8843

Browse files
committed
Implement remaining fields of information_schema.sequences view
Add new function pg_sequence_parameters that returns a sequence's start, minimum, maximum, increment, and cycle values, and use that in the view. (bug #5662; design suggestion by Tom Lane) Also slightly adjust the view's column order and permissions after review of SQL standard.
1 parent e657b55 commit 39b8843

File tree

8 files changed

+92
-14
lines changed

8 files changed

+92
-14
lines changed

doc/src/sgml/information_schema.sgml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4139,31 +4139,42 @@ ORDER BY c.ordinal_position;
41394139
</row>
41404140

41414141
<row>
4142-
<entry><literal>maximum_value</literal></entry>
4143-
<entry><type>cardinal_number</type></entry>
4144-
<entry>Not yet implemented</entry>
4142+
<entry><literal>start_value</literal></entry>
4143+
<entry><type>character_data</type></entry>
4144+
<entry>The start value of the sequence</entry>
41454145
</row>
41464146

41474147
<row>
41484148
<entry><literal>minimum_value</literal></entry>
4149-
<entry><type>cardinal_number</type></entry>
4150-
<entry>Not yet implemented</entry>
4149+
<entry><type>character_data</type></entry>
4150+
<entry>The minimum value of the sequence</entry>
4151+
</row>
4152+
4153+
<row>
4154+
<entry><literal>maximum_value</literal></entry>
4155+
<entry><type>character_data</type></entry>
4156+
<entry>The maximum value of the sequence</entry>
41514157
</row>
41524158

41534159
<row>
41544160
<entry><literal>increment</literal></entry>
4155-
<entry><type>cardinal_number</type></entry>
4156-
<entry>Not yet implemented</entry>
4161+
<entry><type>character_data</type></entry>
4162+
<entry>The increment of the sequence</entry>
41574163
</row>
41584164

41594165
<row>
41604166
<entry><literal>cycle_option</literal></entry>
41614167
<entry><type>yes_or_no</type></entry>
4162-
<entry>Not yet implemented</entry>
4168+
<entry><literal>YES</literal> if the sequence cycles, else <literal>NO</literal></entry>
41634169
</row>
41644170
</tbody>
41654171
</tgroup>
41664172
</table>
4173+
4174+
<para>
4175+
Note that in accordance with the SQL standard, the start, minimum,
4176+
maximum, and increment values are returned as character strings.
4177+
</para>
41674178
</sect1>
41684179

41694180
<sect1 id="infoschema-sql-features">

src/backend/catalog/information_schema.sql

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,16 +1430,18 @@ CREATE VIEW sequences AS
14301430
CAST(64 AS cardinal_number) AS numeric_precision,
14311431
CAST(2 AS cardinal_number) AS numeric_precision_radix,
14321432
CAST(0 AS cardinal_number) AS numeric_scale,
1433-
CAST(null AS cardinal_number) AS maximum_value, -- FIXME
1434-
CAST(null AS cardinal_number) AS minimum_value, -- FIXME
1435-
CAST(null AS cardinal_number) AS increment, -- FIXME
1436-
CAST(null AS yes_or_no) AS cycle_option -- FIXME
1433+
-- XXX: The following could be improved if we had LATERAL.
1434+
CAST((pg_sequence_parameters(c.oid)).start_value AS character_data) AS start_value,
1435+
CAST((pg_sequence_parameters(c.oid)).minimum_value AS character_data) AS minimum_value,
1436+
CAST((pg_sequence_parameters(c.oid)).maximum_value AS character_data) AS maximum_value,
1437+
CAST((pg_sequence_parameters(c.oid)).increment AS character_data) AS increment,
1438+
CAST(CASE WHEN (pg_sequence_parameters(c.oid)).cycle_option THEN 'YES' ELSE 'NO' END AS yes_or_no) AS cycle_option
14371439
FROM pg_namespace nc, pg_class c
14381440
WHERE c.relnamespace = nc.oid
14391441
AND c.relkind = 'S'
14401442
AND (NOT pg_is_other_temp_schema(nc.oid))
14411443
AND (pg_has_role(c.relowner, 'USAGE')
1442-
OR has_table_privilege(c.oid, 'SELECT, UPDATE') );
1444+
OR has_sequence_privilege(c.oid, 'SELECT, UPDATE, USAGE') );
14431445

14441446
GRANT SELECT ON sequences TO PUBLIC;
14451447

src/backend/commands/sequence.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "commands/defrem.h"
2525
#include "commands/sequence.h"
2626
#include "commands/tablecmds.h"
27+
#include "funcapi.h"
2728
#include "miscadmin.h"
2829
#include "nodes/makefuncs.h"
2930
#include "storage/bufmgr.h"
@@ -1420,6 +1421,56 @@ process_owned_by(Relation seqrel, List *owned_by)
14201421
}
14211422

14221423

1424+
/*
1425+
* Return sequence parameters, for use by information schema
1426+
*/
1427+
Datum
1428+
pg_sequence_parameters(PG_FUNCTION_ARGS)
1429+
{
1430+
Oid relid = PG_GETARG_OID(0);
1431+
TupleDesc tupdesc;
1432+
Datum values[5];
1433+
bool isnull[5];
1434+
SeqTable elm;
1435+
Relation seqrel;
1436+
Buffer buf;
1437+
Form_pg_sequence seq;
1438+
1439+
/* open and AccessShareLock sequence */
1440+
init_sequence(relid, &elm, &seqrel);
1441+
1442+
if (pg_class_aclcheck(relid, GetUserId(), ACL_SELECT | ACL_UPDATE | ACL_USAGE) != ACLCHECK_OK)
1443+
ereport(ERROR,
1444+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1445+
errmsg("permission denied for sequence %s",
1446+
RelationGetRelationName(seqrel))));
1447+
1448+
tupdesc = CreateTemplateTupleDesc(5, false);
1449+
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "start_value", INT8OID, -1, 0);
1450+
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "minimum_value", INT8OID, -1, 0);
1451+
TupleDescInitEntry(tupdesc, (AttrNumber) 3, "maximum_value", INT8OID, -1, 0);
1452+
TupleDescInitEntry(tupdesc, (AttrNumber) 4, "increment", INT8OID, -1, 0);
1453+
TupleDescInitEntry(tupdesc, (AttrNumber) 5, "cycle_option", BOOLOID, -1, 0);
1454+
1455+
BlessTupleDesc(tupdesc);
1456+
1457+
memset(isnull, 0, sizeof(isnull));
1458+
1459+
seq = read_info(elm, seqrel, &buf);
1460+
1461+
values[0] = Int64GetDatum(seq->start_value);
1462+
values[1] = Int64GetDatum(seq->min_value);
1463+
values[2] = Int64GetDatum(seq->max_value);
1464+
values[3] = Int64GetDatum(seq->increment_by);
1465+
values[4] = BoolGetDatum(seq->is_cycled);
1466+
1467+
UnlockReleaseBuffer(buf);
1468+
relation_close(seqrel, NoLock);
1469+
1470+
return HeapTupleGetDatum(heap_form_tuple(tupdesc, values, isnull));
1471+
}
1472+
1473+
14231474
void
14241475
seq_redo(XLogRecPtr lsn, XLogRecord *record)
14251476
{

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201101011
56+
#define CATALOG_VERSION_NO 201101021
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,8 @@ DATA(insert OID = 1576 ( setval PGNSP PGUID 12 1 0 0 f f f t f v 2 0 20 "2205
21202120
DESCR("set sequence value");
21212121
DATA(insert OID = 1765 ( setval PGNSP PGUID 12 1 0 0 f f f t f v 3 0 20 "2205 20 16" _null_ _null_ _null_ _null_ setval3_oid _null_ _null_ _null_ ));
21222122
DESCR("set sequence value and iscalled status");
2123+
DATA(insert OID = 3078 ( pg_sequence_parameters PGNSP PGUID 12 1 0 0 f f f t f s 1 0 2249 "26" "{23,20,20,20,20,16}" "{i,o,o,o,o,o}" "{sequence_oid,start_value,minimum_value,maximum_value,increment,cycle_option}" _null_ pg_sequence_parameters _null_ _null_ _null_));
2124+
DESCR("sequence parameters, for use by information schema");
21232125

21242126
DATA(insert OID = 1579 ( varbit_in PGNSP PGUID 12 1 0 0 f f f t f i 3 0 1562 "2275 26 23" _null_ _null_ _null_ _null_ varbit_in _null_ _null_ _null_ ));
21252127
DESCR("I/O");

src/include/commands/sequence.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ extern Datum setval_oid(PG_FUNCTION_ARGS);
6969
extern Datum setval3_oid(PG_FUNCTION_ARGS);
7070
extern Datum lastval(PG_FUNCTION_ARGS);
7171

72+
extern Datum pg_sequence_parameters(PG_FUNCTION_ARGS);
73+
7274
extern void DefineSequence(CreateSeqStmt *stmt);
7375
extern void AlterSequence(AlterSeqStmt *stmt);
7476
extern void ResetSequence(Oid seq_relid);

src/test/regress/expected/sequence.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@ SELECT nextval('sequence_test2');
220220
5
221221
(1 row)
222222

223+
-- Information schema
224+
SELECT * FROM information_schema.sequences WHERE sequence_name IN ('sequence_test2');
225+
sequence_catalog | sequence_schema | sequence_name | data_type | numeric_precision | numeric_precision_radix | numeric_scale | start_value | minimum_value | maximum_value | increment | cycle_option
226+
------------------+-----------------+----------------+-----------+-------------------+-------------------------+---------------+-------------+---------------+---------------+-----------+--------------
227+
regression | public | sequence_test2 | bigint | 64 | 2 | 0 | 32 | 5 | 36 | 4 | YES
228+
(1 row)
229+
223230
-- Test comments
224231
COMMENT ON SEQUENCE asdf IS 'won''t work';
225232
ERROR: relation "asdf" does not exist

src/test/regress/sql/sequence.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ SELECT nextval('sequence_test2');
8585
SELECT nextval('sequence_test2');
8686
SELECT nextval('sequence_test2');
8787

88+
-- Information schema
89+
SELECT * FROM information_schema.sequences WHERE sequence_name IN ('sequence_test2');
90+
8891
-- Test comments
8992
COMMENT ON SEQUENCE asdf IS 'won''t work';
9093
COMMENT ON SEQUENCE sequence_test2 IS 'will work';

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy