Skip to content

Commit

Permalink
Added unlogged option while creating a sequence. #6376
Browse files Browse the repository at this point in the history
  • Loading branch information
pravesh-sharma committed Jul 24, 2023
1 parent 52643a5 commit aa9e5ca
Show file tree
Hide file tree
Showing 22 changed files with 1,035 additions and 6 deletions.
Binary file modified docs/en_US/images/sequence_definition.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion docs/en_US/sequence_dialog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ Use the fields in the *Definition* tab to define the sequence:
to be preallocated and stored in memory for faster access. The minimum value
is 1 (only one value can be generated at a time, i.e., no cache), and this is
also the default.
* Move the *Cycled* switch to the *Yes* position to allow the sequence to wrap
* Move the switch next to *Cycled* towards the *right position* to allow the sequence to wrap
around when the MAXVALUE or the MINVALUE has been reached by an ascending or
descending sequence respectively. If the limit is reached, the next number
generated will be the MINVALUE or MAXVALUE, respectively. The default is *No*.
* Move the switch next to *Unlogged?* towards the *right position* to make the sequence Unlogged.
The default is *No*. This option is available only on PostgreSQL 15 and above.
* The *OWNED BY* option causes the sequence to be associated with a specific
table column, such that if that column (or its whole table) is dropped, the
sequence will be automatically dropped as well. The specified table must have
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export default class SequenceSchema extends BaseUISchema {
maximum: undefined,
cache: undefined,
cycled: undefined,
relpersistence: undefined,
relacl: [],
securities: [],
...initValues,
Expand Down Expand Up @@ -154,6 +155,10 @@ export default class SequenceSchema extends BaseUISchema {
}, {
id: 'cycled', label: gettext('Cycled'), type: 'switch',
mode: ['properties', 'create', 'edit'], group: gettext('Definition'),
}, {
id: 'relpersistence', label: gettext('Unlogged?'), type: 'switch',
mode: ['properties', 'create', 'edit'], group: gettext('Definition'),
min_version: 150000,
}, {
type: 'nested-fieldset', label: gettext('Owned By'), group: gettext('Definition'),
schema: this.ownedSchemaObj,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CREATE {% if data.relpersistence %}UNLOGGED {% endif %}SEQUENCE{% if add_not_exists_clause %} IF NOT EXISTS{% endif %} {{ conn|qtIdent(data.schema, data.name) }}{% if data.increment is defined and data.cycled %}

CYCLE{% endif %}{% if data.increment is defined %}

INCREMENT {{data.increment|int}}{% endif %}{% if data.start is defined %}

START {{data.start|int}}{% elif data.current_value is defined %}

START {{data.current_value|int}}{% endif %}{% if data.minimum is defined %}

MINVALUE {{data.minimum|int}}{% endif %}{% if data.maximum is defined %}

MAXVALUE {{data.maximum|int}}{% endif %}{% if data.cache is defined and data.cache|int(-1) > -1%}

CACHE {{data.cache|int}}{% endif %}{% if data.owned_table is defined and data.owned_table != None and data.owned_column is defined and data.owned_column != None %}

OWNED BY {{ conn|qtIdent(data.owned_table) }}.{{ conn|qtIdent(data.owned_column) }}{% endif %};

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% if scid %}
SELECT
cl.oid as oid,
cl.relname as name,
nsp.nspname as schema,
pg_catalog.pg_get_userbyid(cl.relowner) AS seqowner,
description as comment,
pg_catalog.array_to_string(cl.relacl::text[], ', ') as acl,
(SELECT pg_catalog.array_agg(provider || '=' || label) FROM pg_catalog.pg_seclabels sl1 WHERE sl1.objoid=cl.oid) AS securities,
depcl.relname AS owned_table,
att.attname AS owned_column,
(CASE WHEN cl.relpersistence = 'u' THEN true ELSE false END) AS relpersistence
FROM pg_catalog.pg_class cl
LEFT OUTER JOIN pg_catalog.pg_namespace nsp ON cl.relnamespace = nsp.oid
LEFT OUTER JOIN pg_catalog.pg_description des ON (des.objoid=cl.oid
AND des.classoid='pg_class'::regclass)
LEFT OUTER JOIN pg_catalog.pg_depend dep ON (dep.objid=cl.oid and deptype = 'a')
LEFT JOIN pg_catalog.pg_attribute att ON dep.refobjid=att.attrelid AND dep.refobjsubid=att.attnum
LEFT JOIN pg_catalog.pg_class depcl ON depcl.oid = att.attrelid
WHERE cl.relkind = 'S' AND cl.relnamespace = {{scid}}::oid
{% if seid %}AND cl.oid = {{seid}}::oid {% endif %}
ORDER BY cl.relname
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{% import 'macros/schemas/security.macros' as SECLABEL %}
{% import 'macros/schemas/privilege.macros' as PRIVILEGE %}
{% if data %}
{% if data.name != o_data.name %}
ALTER SEQUENCE IF EXISTS {{ conn|qtIdent(o_data.schema, o_data.name) }}
RENAME TO {{ conn|qtIdent(data.name) }};

{% endif %}
{% if data.seqowner and data.seqowner != o_data.seqowner %}
ALTER SEQUENCE IF EXISTS {{ conn|qtIdent(o_data.schema, data.name) }}
OWNER TO {{ conn|qtIdent(data.seqowner) }};

{% endif %}
{% if (data.owned_table == None) and (data.owned_column == None) %}
ALTER SEQUENCE IF EXISTS {{ conn|qtIdent(o_data.schema, data.name) }}
OWNED BY NONE;
{% elif (data.owned_table is defined or data.owned_column is defined) and (data.owned_table != o_data.owned_table or data.owned_column != o_data.owned_column) %}
ALTER SEQUENCE IF EXISTS {{ conn|qtIdent(o_data.schema, data.name) }}
OWNED BY {% if data.owned_table is defined %}{{ conn|qtIdent(data.owned_table) }}{% else %}{{ conn|qtIdent(o_data.owned_table) }}{% endif %}.{% if data.owned_column is defined %}{{ conn|qtIdent(data.owned_column) }}{% else %}{{ conn|qtIdent(o_data.owned_column) }}{% endif %};
{% endif %}
{% if data.current_value is defined %}
{% set seqname = conn|qtIdent(o_data.schema, data.name) %}
SELECT setval({{ seqname|qtLiteral(conn) }}, {{ data.current_value }}, true);

{% endif %}
{% if data.relpersistence in [True, False] and data.relpersistence != o_data.relpersistence %}
ALTER SEQUENCE IF EXISTS {{ conn|qtIdent(o_data.schema, data.name) }}
SET {% if data.relpersistence %}UNLOGGED{% else %}LOGGED{% endif %};

{% endif %}
{% set defquery = '' %}
{% if data.increment is defined %}
{% set defquery = defquery+'\n INCREMENT '+data.increment|string %}
{% endif %}
{% if data.start is defined %}
{% set defquery = defquery+'\n START '+data.start|string %}
{% endif %}
{% if data.minimum is defined %}
{% set defquery = defquery+'\n MINVALUE '+data.minimum|string %}
{% endif %}
{% if data.maximum is defined %}
{% set defquery = defquery+'\n MAXVALUE '+data.maximum|string %}
{% endif %}
{% if data.cache is defined %}
{% set defquery = defquery+'\n CACHE '+data.cache|string %}
{% endif %}
{% if data.cycled == True %}
{% set defquery = defquery+'\n CYCLE' %}
{% elif data.cycled == False %}
{% set defquery = defquery+'\n NO CYCLE' %}
{% endif %}
{% if defquery and defquery != '' %}
ALTER SEQUENCE IF EXISTS {{ conn|qtIdent(o_data.schema, data.name) }}{{ defquery }};

{% endif %}
{% if data.schema and data.schema != o_data.schema %}
ALTER SEQUENCE IF EXISTS {{ conn|qtIdent(o_data.schema, data.name) }}
SET SCHEMA {{ conn|qtIdent(data.schema) }};

{% set seqname = conn|qtIdent(data.schema, data.name) %}
{% set schema = data.schema %}
{% else %}
{% set seqname = conn|qtIdent(o_data.schema, data.name) %}
{% set schema = o_data.schema %}
{% endif %}
{% if data.comment is defined and data.comment != o_data.comment %}
COMMENT ON SEQUENCE {{ seqname }}
IS {{ data.comment|qtLiteral(conn) }};

{% endif %}
{% if data.securities and data.securities|length > 0 %}

{% set seclabels = data.securities %}
{% if 'deleted' in seclabels and seclabels.deleted|length > 0 %}
{% for r in seclabels.deleted %}
{{ SECLABEL.UNSET(conn, 'SEQUENCE', data.name, r.provider, schema) }}
{% endfor %}
{% endif %}
{% if 'added' in seclabels and seclabels.added|length > 0 %}
{% for r in seclabels.added %}
{{ SECLABEL.SET(conn, 'SEQUENCE', data.name, r.provider, r.label, schema) }}
{% endfor %}
{% endif %}
{% if 'changed' in seclabels and seclabels.changed|length > 0 %}
{% for r in seclabels.changed %}
{{ SECLABEL.SET(conn, 'SEQUENCE', data.name, r.provider, r.label, schema) }}
{% endfor %}
{% endif %}
{% endif %}
{% if data.relacl %}

{% if 'deleted' in data.relacl %}
{% for priv in data.relacl.deleted %}
{{ PRIVILEGE.UNSETALL(conn, 'SEQUENCE', priv.grantee, data.name, schema) }}
{% endfor %}
{% endif %}
{% if 'changed' in data.relacl %}
{% for priv in data.relacl.changed %}
{% if priv.grantee != priv.old_grantee %}
{{ PRIVILEGE.UNSETALL(conn, 'SEQUENCE', priv.old_grantee, data.name, schema) }}
{% else %}
{{ PRIVILEGE.UNSETALL(conn, 'SEQUENCE', priv.grantee, data.name, schema) }}
{% endif %}
{{ PRIVILEGE.SET(conn, 'SEQUENCE', priv.grantee, data.name, priv.without_grant, priv.with_grant, schema) }}
{% endfor %}
{% endif %}
{% if 'added' in data.relacl %}
{% for priv in data.relacl.added %}
{{ PRIVILEGE.SET(conn, 'SEQUENCE', priv.grantee, data.name, priv.without_grant, priv.with_grant, schema) }}
{% endfor %}
{% endif %}
{% endif %}
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- SEQUENCE: public.Seq1_$%{}[]()&*^!@"'`\/#

-- DROP SEQUENCE IF EXISTS public."Seq1_$%{}[]()&*^!@""'`\/#";

CREATE SEQUENCE IF NOT EXISTS public."Seq1_$%{}[]()&*^!@""'`\/#"
INCREMENT 5
START 5
MINVALUE 5
MAXVALUE 999
CACHE 1;

ALTER SEQUENCE public."Seq1_$%{}[]()&*^!@""'`\/#"
OWNER TO postgres;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER SEQUENCE IF EXISTS public."Seq1_$%{}[]()&*^!@""'`\/#"
SET LOGGED;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- SEQUENCE: public.Seq1_$%{}[]()&*^!@"'`\/#

-- DROP SEQUENCE IF EXISTS public."Seq1_$%{}[]()&*^!@""'`\/#";

CREATE UNLOGGED SEQUENCE IF NOT EXISTS public."Seq1_$%{}[]()&*^!@""'`\/#"
INCREMENT 5
START 5
MINVALUE 5
MAXVALUE 999
CACHE 1;

ALTER SEQUENCE public."Seq1_$%{}[]()&*^!@""'`\/#"
OWNER TO postgres;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER SEQUENCE IF EXISTS public."Seq1_$%{}[]()&*^!@""'`\/#"
SET UNLOGGED;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- SEQUENCE: public.Seq1_$%{}[]()&*^!@"'`\/#

-- DROP SEQUENCE IF EXISTS public."Seq1_$%{}[]()&*^!@""'`\/#";

CREATE UNLOGGED SEQUENCE IF NOT EXISTS public."Seq1_$%{}[]()&*^!@""'`\/#"
INCREMENT 5
START 5
MINVALUE 5
MAXVALUE 999
CACHE 1;

ALTER SEQUENCE public."Seq1_$%{}[]()&*^!@""'`\/#"
OWNER TO postgres;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE UNLOGGED SEQUENCE public."Seq1_$%{}[]()&*^!@""'`\/#"
INCREMENT 5
START 5
MINVALUE 5
MAXVALUE 999
CACHE 1;

ALTER SEQUENCE public."Seq1_$%{}[]()&*^!@""'`\/#"
OWNER TO postgres;
Loading

0 comments on commit aa9e5ca

Please sign in to comment.