Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
F.52. pg_variables — functions for working with variables of various types
Prev UpAppendix F. Additional Supplied Modules and Extensions Shipped inpostgrespro-std-16-contribHome Next

F.52. pg_variables — functions for working with variables of various types#

Thepg_variables module provides functions for working with variables of various types. The created variables are only available in the current user session.

F.52.1. Installation#

Thepg_variables extension is included into Postgres Pro. Once you have Postgres Pro installed, you must execute theCREATE EXTENSION command to enablepg_variables, as follows:

CREATE EXTENSION pg_variables;

F.52.2. Usage#

Thepg_variables module provides several functions for creating, reading, and managing variables of scalar, record, and array types. See the following sections for function descriptions and syntax:

For detailed usage examples, seeSection F.52.5.

F.52.2.1. Using Transactional Variables#

By default, the created variables are non-transactional. Once successfully set, a variable exists for the whole session, regardless of rollbacks, if any. For example:

SELECT pgv_set('vars', 'int1', 101);BEGIN;SELECT pgv_set('vars', 'int2', 102);ROLLBACK;SELECT * FROM pgv_list() order by package, name; package | name | is_transactional---------+------+------------------ vars    | int1 | f vars    | int2 | f

If you would like to use variables that support transactions and savepoints, pass the optionalis_transactional flag as the last parameter when creating this variable:

BEGIN;SELECT pgv_set('vars', 'trans_int', 101, true);SAVEPOINT sp1;SELECT pgv_set('vars', 'trans_int', 102, true);ROLLBACK TO sp1;COMMIT;SELECT pgv_get('vars', 'trans_int', NULL::int); pgv_get---------     101

You must use theis_transactional flag every time you change the value of a transactional variable usingpgv_set() orpgv_insert() functions. Otherwise, an error occurs. Other functions do not require this flag.

SELECT pgv_insert('pack', 'var_record', row(123::int, 'text'::text), true);SELECT pgv_insert('pack', 'var_record', row(456::int, 'another text'::text));ERROR:  variable "var_record" already created as TRANSACTIONALSELECT pgv_delete('pack', 'var_record', 123::int);

If thepgv_free() orpgv_remove() function calls are rolled back, the affected transactional variables will be restored, unlike non-transactional variables, which are removed permanently. For example:

SELECT pgv_set('pack', 'var_reg', 123);SELECT pgv_set('pack', 'var_trans', 456, true);BEGIN;SELECT pgv_free();ROLLBACK;SELECT * FROM pgv_list(); package |   name    | is_transactional ---------+-----------+------------------ pack    | var_trans | t

F.52.3. Functions#

F.52.3.1. Scalar Variables#

The following functions support scalar variables:

Function Returns
pgv_set(package text, name text, value anynonarray, is_transactional bool default false)void
pgv_get(package text, name text, var_type anynonarray, strict bool default true)anynonarray

To use thepgv_get() function, you must first create a package and a variable using thepgv_set() function. If the specified package or variable does not exist, an error occurs:

SELECT pgv_get('vars', 'int1', NULL::int);ERROR:  unrecognized package "vars"
SELECT pgv_get('vars', 'int1', NULL::int);ERROR:  unrecognized variable "int1"

pgv_get() function checks the variable type. If the specified type does not match the type of the variable, an error is raised:

SELECT pgv_get('vars', 'int1', NULL::text);ERROR:  variable "int1" requires "integer" value

F.52.3.2. Collections of Records#

The following functions support collections of record variables:

Function Returns Description
pgv_insert(package text, name text, r record, is_transactional bool default false)void Inserts a record into a collection variable for the specified package. If the package or variable does not exist, it is created automatically. The first column ofr is the primary key. If a record with the same primary key already exists or this collection variable has a different structure, an error is raised.
pgv_update(package text, name text, r record)boolean Updates a record with the corresponding primary key (the first column ofr is the primary key). Returnstrue if the record was found. If this collection variable has a different structure, an error is raised.
pgv_delete(package text, name text, value anynonarray)boolean Deletes a record with the corresponding primary key (the first column ofr is the primary key). Returnstrue if the record was found andfalse otherwise.
pgv_select(package text, name text)set of records Returns the collection variable records.
pgv_select(package text, name text, value anynonarray)record Returns the record with the corresponding primary key (the first column ofr is a primary key).
pgv_select(package text, name text, value anyarray)set of records Returns the collection variable records with the corresponding primary keys (the first column ofr is a primary key).

To usepgv_update(),pgv_delete() andpgv_select() functions, you must first create a package and a variable using thepgv_insert() function. The variable type and the record type must be the same; otherwise, an error occurs.

F.52.3.3. Arrays#

The following functions support array variables:

Function Returns
pgv_set(package text, name text, value anyarray, is_transactional bool default false)void
pgv_get(package text, name text, var_type anyarray, strict bool default true)anyarray

Usage instructions for these functions are the same as those provided inSection F.52.3.1 for scalar variables.

F.52.3.4. General Collections#

The following functions support general collection variables:

Function Returns Description

pgv_set_elem(package text, name text, key int, value anyelement, is_transactional bool default false)

pgv_set_elem(package text, name text, key text, value anyelement, is_transactional bool default false)

void Sets a value for the element with the keykey of the collection variablename in the packagepackage. If the package or variable does not exist, it is created automatically. Inside one collection, only keys of the same type are allowed. Thekey argument can have eitherint ortext type. If an element with the specified key already exists, its value is set to the new one.is_transactional shows whether the new variable is transactional and equalsfalse by default. So if the variable already exists, it must be transactional/non-transactional as indicated byis_transactional, otherwise an error is raised. If the collection already exists and its value type does not match the type of the new value, an error is also raised.

pgv_get_elem(package text, name text, key int, val_type anyelement)

pgv_get_elem(package text, name text, key text, val_type anyelement)

anyelement Returns the value of the element with the keykey of the collection variablename in the packagepackage. If there is no element with the specified key in this collection, returns NULL. Thekey argument can have eitherint ortext type. If the package or variable does not exist, an error is raised. If the specified variable is not a collection, an error is also raised. Theval_type argument is required to properly determine the return type.

pgv_exists_elem(package text, name text, key int)

pgv_exists_elem(package text, name text, key text)

bool Returnstrue if an element with the keykey exists in the collection variablename in the packagepackage andfalse otherwise. If the package or variable does not exist, also returnsfalse. Thekey argument can have eitherint ortext type. If the specified variable is not a collection, an error is raised.

pgv_remove_elem(package text, name text, key int)

pgv_remove_elem(package text, name text, key text)

void Removes the element with the keykey from the collection variablename in the packagepackage. If there is no element with the specified key in this collection, does nothing. Thekey argument can have eitherint ortext type. If the package or variable does not exist, an error is raised. If the specified variable is not a collection, an error is also raised.

Important

Collections initialized withpgv_set_elem() and withpgv_insert() are not considered compatible.

F.52.3.5. Iterators#

The following functions are provided to use iterators to traverse collection variables. These functions work with collections initialized with bothpgv_set_elem() andpgv_insert().

Function Returns Description
pgv_first(package text, name text, key_type anyelement)anyelement Returns the first key from the collection variable. Collections are sorted by the key in ascending order.key_type is required to determine the return value. If thename passed is the name of a non-collection variable, an error is raised.
pgv_last(package text, name text, key_type anyelement)anyelement Returns the last key from the collection variable. Collections are sorted by the key in ascending order.key_type is required to determine the return value. If thename passed is the name of a non-collection variable, an error is raised.
pgv_next(package text, name text, key anyelement)anyelement Returns the next key from the collection variable. Thekey passed may not exist in the collection. Returns NULL if used for the last key in the collection. Collections are sorted by the key in ascending order. If thename passed is the name of a non-collection variable, an error is raised.
pgv_prior(package text, name text, key anyelement)anyelement Returns the previous key from the collection variable. Thekey passed may not exist in the collection. Returns NULL if used for the first key in the collection. Collections are sorted by the key in ascending order. If thename passed is the name of a non-collection variable, an error is raised.
pgv_count(package text, name text)integer Returns the number of elements in the collection. If thename passed is the name of a non-collection variable, an error is raised.

F.52.3.6. Miscellaneous Functions#

Function Returns Description
pgv_exists(package text, name text)bool Returnstrue if the specified package and variable exist andfalse otherwise.
pgv_exists(package text)bool Returnstrue if the specified package exists andfalse otherwise.
pgv_remove(package text, name text)void Removes the variable with the specified name. The specified package and variable must exist; otherwise, an error is raised.
pgv_remove(package text)void Removes the specified package and all the corresponding variables. The specified package must exist; otherwise, an error is raised.
pgv_free()void Removes all packages and variables.
pgv_list()table(package text, name text, is_transactional bool) Displays all the available variables and the corresponding packages, as well as whether each variable is transactional.
pgv_stats()table(package text, allocated_memory bigint) Returns the list of assigned packages and the amount of memory used by variables, in bytes. If you are using transactional variables, this list also includes all deleted packages that still may be restored by aROLLBACK. This function only supports Postgres Pro 9.6 or higher.

F.52.3.7. Deprecated Functions#

F.52.3.7.1. Integer Variables#

The following functions are deprecated. Use generic functions forscalar variables instead.

Function Returns
pgv_set_int(package text, name text, value int, is_transactional bool default false)void
pgv_get_int(package text, name text, strict bool default true)int
F.52.3.7.2. Text Variables#

The following functions are deprecated. Use generic functions forscalar variables instead.

Function Returns
pgv_set_text(package text, name text, value text, is_transactional bool default false)void
pgv_get_text(package text, name text, strict bool default true)text
F.52.3.7.3. Numeric Variables#

The following functions are deprecated. Use generic functions forscalar variables instead.

Function Returns
pgv_set_numeric(package text, name text, value numeric, is_transactional bool default false)void
pgv_get_numeric(package text, name text, strict bool default true)numeric
F.52.3.7.4. Timestamp Variables#

The following functions are deprecated. Use generic functions forscalar variables instead.

Function Returns
pgv_set_timestamp(package text, name text, value timestamp, is_transactional bool default false)void
pgv_get_timestamp(package text, name text, strict bool default true)timestamp
F.52.3.7.5. Timestamp with timezone Variables#

The following functions are deprecated. Use generic functions forscalar variables instead.

Function Returns
pgv_set_timestamptz(package text, name text, value timestamptz, is_transactional bool default false)void
pgv_get_timestamptz(package text, name text, strict bool default true)timestamptz
F.52.3.7.6. Date Variables#

The following functions are deprecated. Use generic functions forscalar variables instead.

Function Returns
pgv_set_date(package text, name text, value date, is_transactional bool default false)void
pgv_get_date(package text, name text, strict bool default true)date
F.52.3.7.7. Jsonb Variables#

The following functions are deprecated. Use generic functions forscalar variables instead.

Function Returns
pgv_set_jsonb(package text, name text, value jsonb, is_transactional bool default false)void
pgv_get_jsonb(package text, name text, strict bool default true)jsonb

F.52.4. Important Notes#

F.52.4.1. Collations in Collections#

Collections are stored in ascending order. In the case oftext keys, you need a collation to determine the order of elements. If no collation is specified when inserting the first element of a collection, the default collation is used. Otherwise, the specified collation is used.

F.52.4.2. Cursors for Set-Returning Functions#

All set-returning functions exceptpgv_select(package, variable) fix their return results at firstFETCH from the cursor and are not affected by further data manipulation.

The results ofpgv_select(package, variable) are received dynamically and are affected by transactions/changes in the collection. Forpgv_select() called for a transactional collection, cursors look at the snapshot of the collection when the firstFETCH was executed, but consider changes that were made in that transaction and in committed subtransactions.

F.52.5. Examples#

Define scalar variables using thepgv_set() function, and then return their values using thepgv_get() function:

SELECT pgv_set('vars', 'int1', 101);SELECT pgv_set('vars', 'int2', 102);SELECT pgv_set('vars', 'text1', 'text variable'::text);SELECT pgv_get('vars', 'int1', NULL::int); pgv_get-------------         101SELECT pgv_get('vars', 'int2', NULL::int); pgv_get-------------         102SELECT pgv_get('vars', 'text1', NULL::text); pgv_get--------------- text variable

Let's assume we have thetab table and examine several examples of using record variables:

CREATE TABLE tab (id int, t varchar);INSERT INTO tab VALUES (0, 'str00'), (1, 'str11');

You can use the following functions to work with record variables:

SELECT pgv_insert('vars', 'r1', tab) FROM tab;SELECT pgv_select('vars', 'r1'); pgv_select------------ (1,str11) (0,str00)SELECT pgv_select('vars', 'r1', 1); pgv_select------------ (1,str11)SELECT pgv_select('vars', 'r1', 0); pgv_select------------ (0,str00)SELECT pgv_select('vars', 'r1', ARRAY[1, 0]); pgv_select------------ (1,str11) (0,str00)SELECT pgv_delete('vars', 'r1', 1);SELECT pgv_select('vars', 'r1'); pgv_select------------ (0,str00)

Consider the behavior of a transactional variablevar_text when changed before and after savepoints:

SELECT pgv_set('pack', 'var_text', 'before transaction block'::text, true);BEGIN;SELECT pgv_set('pack', 'var_text', 'before savepoint'::text, true);SAVEPOINT sp1;SELECT pgv_set('pack', 'var_text', 'savepoint sp1'::text, true);SAVEPOINT sp2;SELECT pgv_set('pack', 'var_text', 'savepoint sp2'::text, true);RELEASE sp2;SELECT pgv_get('pack', 'var_text', NULL::text);    pgv_get--------------- savepoint sp2ROLLBACK TO sp1;SELECT pgv_get('pack', 'var_text', NULL::text);     pgv_get------------------ before savepointROLLBACK;SELECT pgv_get('pack', 'var_text', NULL::text);         pgv_get-------------------------- before transaction block

If you create a variable afterBEGIN orSAVEPOINT statements and than rollback to the previous state, the transactional variable is removed:

BEGIN;SAVEPOINT sp1;SAVEPOINT sp2;SELECT pgv_set('pack', 'var_int', 122, true);RELEASE SAVEPOINT sp2;SELECT pgv_get('pack', 'var_int', NULL::int);pgv_get---------     122ROLLBACK TO sp1;SELECT pgv_get('pack','var_int', NULL::int);ERROR:  unrecognized variable "var_int"COMMIT;

List the available packages and variables:

SELECT * FROM pgv_list() ORDER BY package, name; package |   name   | is_transactional ---------+----------+------------------ pack    | var_text | t vars    | int1     | f vars    | int2     | f vars    | r1       | f vars    | text1    | f

Get the amount of memory used by variables, in bytes:

SELECT * FROM pgv_stats() ORDER BY package; package | allocated_memory ---------+------------------ pack    |            16384 vars    |            32768

Delete the specified variables or packages:

SELECT pgv_remove('vars', 'int1');SELECT pgv_remove('vars');

Delete all packages and variables:

SELECT pgv_free();

These examples show usage of collection variables and iterator functions:

sqlSELECT pgv_set_elem('pack', 'var', 1, 1);SELECT pgv_set_elem('pack', 'var', 5, 5);SELECT pgv_set_elem('pack', 'var', 10, 10);SELECT pgv_first('pack', 'var', NULL::int); pgv_first-----------         1SELECT pgv_last('pack', 'var', NULL::int); pgv_last----------       10SELECT pgv_next('pack', 'var', pgv_first('pack', 'var', NULL::int)); pgv_next----------        5SELECT pgv_prior('pack', 'var', pgv_last('pack', 'var', NULL::int)); pgv_prior-----------         5SELECT pgv_prior('pack', 'var', pgv_first('pack', 'var', NULL::int)); pgv_prior-----------SELECT pgv_next('pack', 'var', pgv_last('pack', 'var', NULL::int)); pgv_prior-----------SELECT pgv_next('pack', 'var', 3); pgv_next----------        5SELECT pgv_prior('pack', 'var', 3); pgv_prior-----------         1SELECT pgv_get_elem('pack', 'var', pgv_last('pack', 'var', NULL::int), NULL::int); pgv_get_elem--------------           10SELECT pgv_remove_elem('pack', 'var', pgv_last('pack', 'var', NULL::int)); pgv_remove_elem-----------------SELECT pgv_get_elem('pack', 'var', pgv_last('pack', 'var', NULL::int), NULL::int); pgv_get_elem--------------           5(1 row)

These examples show how the collation affects the order of elements in a collection. They also show how to iterate over a whole collection withPL/pgSQL loops:

sqlSELECT pgv_set_elem('pack', 'var1', 'а' COLLATE "ru_RU", 'а'::text);SELECT pgv_set_elem('pack', 'var1', 'д' COLLATE "ru_RU", 'д'::text);SELECT pgv_set_elem('pack', 'var1', 'е' COLLATE "ru_RU", 'е'::text);SELECT pgv_set_elem('pack', 'var1', 'ё' COLLATE "ru_RU", 'ё'::text);SELECT pgv_set_elem('pack', 'var1', 'ж' COLLATE "ru_RU", 'ж'::text);SELECT pgv_set_elem('pack', 'var1', 'я' COLLATE "ru_RU", 'я'::text);DO$$DECLARE        iter text;BEGIN        iter := pgv_first('pack', 'var1', NULL::text);        WHILE iter IS NOT NULL LOOP          RAISE NOTICE '%', pgv_get_elem('pack', 'var1', iter, NULL::text);          iter := pgv_next('pack', 'var1', iter);        END LOOP;END;$$;NOTICE:  аNOTICE:  дNOTICE:  еNOTICE:  ёNOTICE:  жNOTICE:  яSELECT pgv_set_elem('pack', 'var2', 'а' COLLATE "C", 'а'::text);SELECT pgv_set_elem('pack', 'var2', 'д' COLLATE "C", 'д'::text);SELECT pgv_set_elem('pack', 'var2', 'е' COLLATE "C", 'е'::text);SELECT pgv_set_elem('pack', 'var2', 'ё' COLLATE "C", 'ё'::text);SELECT pgv_set_elem('pack', 'var2', 'ж' COLLATE "C", 'ж'::text);SELECT pgv_set_elem('pack', 'var2', 'я' COLLATE "C", 'я'::text);DO$$DECLARE        iter text;BEGIN        iter := pgv_first('pack', 'var2', NULL::text);        WHILE iter IS NOT NULL LOOP          RAISE NOTICE '%', pgv_get_elem('pack', 'var2', iter, NULL::text);          iter := pgv_next('pack', 'var2', iter);        END LOOP;END;$$;NOTICE:  аNOTICE:  дNOTICE:  еNOTICE:  жNOTICE:  яNOTICE:  ё

F.52.6. Authors#

Postgres Professional, Moscow, Russia


Prev Up Next
F.51. pg_tsparser — an extension for text search Home F.53. pg_visibility — visibility map information and utilities
pdfepub
Go to Postgres Pro Standard 16
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp