Skip to content

Commit

Permalink
Add pg_sequence system catalog
Browse files Browse the repository at this point in the history
Move sequence metadata (start, increment, etc.) into a proper system
catalog instead of storing it in the sequence heap object.  This
separates the metadata from the sequence data.  Sequence metadata is now
operated on transactionally by DDL commands, whereas previously
rollbacks of sequence-related DDL commands would be ignored.

Reviewed-by: Andreas Karlsson <[email protected]>
  • Loading branch information
petere committed Dec 20, 2016
1 parent db80acf commit 1753b1b
Show file tree
Hide file tree
Showing 19 changed files with 490 additions and 269 deletions.
88 changes: 87 additions & 1 deletion doc/src/sgml/catalogs.sgml
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@
<entry>security labels on database objects</entry>
</row>

<row>
<entry><link linkend="catalog-pg-sequence"><structname>pg_sequence</structname></link></entry>
<entry>information about sequences</entry>
</row>

<row>
<entry><link linkend="catalog-pg-shdepend"><structname>pg_shdepend</structname></link></entry>
<entry>dependencies on shared objects</entry>
Expand Down Expand Up @@ -1546,7 +1551,8 @@
The catalog <structname>pg_class</structname> catalogs tables and most
everything else that has columns or is otherwise similar to a
table. This includes indexes (but see also
<structname>pg_index</structname>), sequences, views, materialized
<structname>pg_index</structname>), sequences (but see also
<structname>pg_sequence</structname>), views, materialized
views, composite types, and TOAST tables; see <structfield>relkind</>.
Below, when we mean all of these
kinds of objects we speak of <quote>relations</quote>. Not all
Expand Down Expand Up @@ -5587,6 +5593,86 @@
</table>
</sect1>

<sect1 id="catalog-pg-sequence">
<title><structname>pg_sequence</structname></title>

<indexterm zone="catalog-pg-sequence">
<primary>pg_sequence</primary>
</indexterm>

<para>
The catalog <structname>pg_sequence</structname> contains information about
sequences. Some of the information about sequences, such as the name and
the schema, is in <structname>pg_class</structname>.
</para>

<table>
<title><structname>pg_sequence</> Columns</title>

<tgroup cols="4">
<thead>
<row>
<entry>Name</entry>
<entry>Type</entry>
<entry>References</entry>
<entry>Description</entry>
</row>
</thead>

<tbody>
<row>
<entry><structfield>seqrelid</structfield></entry>
<entry><type>oid</type></entry>
<entry><literal><link linkend="catalog-pg-class"><structname>pg_class</structname></link>.oid</literal></entry>
<entry>The OID of the <structname>pg_class</> entry for this sequence</entry>
</row>

<row>
<entry><structfield>seqstart</structfield></entry>
<entry><type>int8</type></entry>
<entry></entry>
<entry>Start value of the sequence</entry>
</row>

<row>
<entry><structfield>seqincrement</structfield></entry>
<entry><type>int8</type></entry>
<entry></entry>
<entry>Increment value of the sequence</entry>
</row>

<row>
<entry><structfield>seqmax</structfield></entry>
<entry><type>int8</type></entry>
<entry></entry>
<entry>Maximum value of the sequence</entry>
</row>

<row>
<entry><structfield>seqmin</structfield></entry>
<entry><type>int8</type></entry>
<entry></entry>
<entry>Minimum value of the sequence</entry>
</row>

<row>
<entry><structfield>seqcache</structfield></entry>
<entry><type>int8</type></entry>
<entry></entry>
<entry>Cache size of the sequence</entry>
</row>

<row>
<entry><structfield>seqcycle</structfield></entry>
<entry><type>bool</type></entry>
<entry></entry>
<entry>Whether the sequence cycles</entry>
</row>
</tbody>
</tgroup>
</table>
</sect1>

<sect1 id="catalog-pg-shdepend">
<title><structname>pg_shdepend</structname></title>

Expand Down
1 change: 1 addition & 0 deletions src/backend/catalog/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ POSTGRES_BKI_SRCS = $(addprefix $(top_srcdir)/src/include/catalog/,\
pg_foreign_table.h pg_policy.h pg_replication_origin.h \
pg_default_acl.h pg_init_privs.h pg_seclabel.h pg_shseclabel.h \
pg_collation.h pg_partitioned_table.h pg_range.h pg_transform.h \
pg_sequence.h \
toasting.h indexing.h \
)

Expand Down
6 changes: 6 additions & 0 deletions src/backend/catalog/dependency.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
#include "commands/proclang.h"
#include "commands/schemacmds.h"
#include "commands/seclabel.h"
#include "commands/sequence.h"
#include "commands/trigger.h"
#include "commands/typecmds.h"
#include "nodes/nodeFuncs.h"
Expand Down Expand Up @@ -1114,6 +1115,11 @@ doDeletion(const ObjectAddress *object, int flags)
else
heap_drop_with_catalog(object->objectId);
}

/* for a sequence, in addition to dropping the heap, also
* delete pg_sequence tuple */
if (relKind == RELKIND_SEQUENCE)
DeleteSequenceTuple(object->objectId);
break;
}

Expand Down
13 changes: 7 additions & 6 deletions src/backend/catalog/information_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1535,15 +1535,16 @@ CREATE VIEW sequences AS
CAST(64 AS cardinal_number) AS numeric_precision,
CAST(2 AS cardinal_number) AS numeric_precision_radix,
CAST(0 AS cardinal_number) AS numeric_scale,
CAST(p.start_value AS character_data) AS start_value,
CAST(p.minimum_value AS character_data) AS minimum_value,
CAST(p.maximum_value AS character_data) AS maximum_value,
CAST(p.increment AS character_data) AS increment,
CAST(CASE WHEN p.cycle_option THEN 'YES' ELSE 'NO' END AS yes_or_no) AS cycle_option
FROM pg_namespace nc, pg_class c, LATERAL pg_sequence_parameters(c.oid) p
CAST(s.seqstart AS character_data) AS start_value,
CAST(s.seqmin AS character_data) AS minimum_value,
CAST(s.seqmax AS character_data) AS maximum_value,
CAST(s.seqincrement AS character_data) AS increment,
CAST(CASE WHEN s.seqcycle THEN 'YES' ELSE 'NO' END AS yes_or_no) AS cycle_option
FROM pg_namespace nc, pg_class c, pg_sequence s
WHERE c.relnamespace = nc.oid
AND c.relkind = 'S'
AND (NOT pg_is_other_temp_schema(nc.oid))
AND c.oid = s.seqrelid
AND (pg_has_role(c.relowner, 'USAGE')
OR has_sequence_privilege(c.oid, 'SELECT, UPDATE, USAGE') );

Expand Down
16 changes: 8 additions & 8 deletions src/backend/catalog/system_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ CREATE OR REPLACE VIEW pg_sequences AS
N.nspname AS schemaname,
C.relname AS sequencename,
pg_get_userbyid(C.relowner) AS sequenceowner,
p.start_value AS start_value,
p.minimum_value AS min_value,
p.maximum_value AS max_value,
p.increment AS increment_by,
p.cycle_option AS cycle,
p.cache_size AS cache_size,
S.seqstart AS start_value,
S.seqmin AS min_value,
S.seqmax AS max_value,
S.seqincrement AS increment_by,
S.seqcycle AS cycle,
S.seqcache AS cache_size,
pg_sequence_last_value(C.oid) AS last_value
FROM pg_class C LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace),
LATERAL pg_sequence_parameters(C.oid) p
FROM pg_sequence S JOIN pg_class C ON (C.oid = S.seqrelid)
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE NOT pg_is_other_temp_schema(N.oid)
AND relkind = 'S';

Expand Down
Loading

0 comments on commit 1753b1b

Please sign in to comment.