Skip to content

Commit 1d1bfe2

Browse files
committed
Merge branch 'master' into stable
2 parents 89c120c + b01b0f9 commit 1d1bfe2

9 files changed

+1036
-444
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ lib*.pc
3838
/pgsql.sln.cache
3939
/Debug/
4040
/Release/
41+
/log/
4142
/tmp_install/
4243
Dockerfile
4344
pg_variables--1.1.sql

README.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ SELECT pgv_get('vars', 'trans_int', NULL::int);
3838
101
3939
```
4040

41+
You can aggregate variables into packages. This is done to be able to have
42+
variables with different names or to quickly remove the whole batch of
43+
variables. If the package becomes empty, it is automatically deleted.
44+
4145
## License
4246

4347
This module available under the [license](LICENSE) similar to
@@ -56,7 +60,7 @@ Typical installation procedure may look like this:
5660
## Module functions
5761

5862
The functions provided by the **pg_variables** module are shown in the tables
59-
below. The module supports the following scalar and record types.
63+
below.
6064

6165
To use **pgv_get()** function required package and variable must exists. It is
6266
necessary to set variable with **pgv_set()** function to use **pgv_get()**
@@ -98,6 +102,28 @@ Function | Returns
98102
`pgv_set(package text, name text, value anyarray, is_transactional bool default false)` | `void`
99103
`pgv_get(package text, name text, var_type anyarray, strict bool default true)` | `anyarray`
100104

105+
`pgv_set` arguments:
106+
- `package` - name of the package, it will be created if it doesn't exist.
107+
- `name` - name of the variable, it will be created if it doesn't exist.
108+
`pgv_set` fails if the variable already exists and its transactionality doesn't
109+
match `is_transactional` argument.
110+
- `value` - new value for the variable. `pgv_set` fails if the variable already
111+
exists and its type doesn't match new value's type.
112+
- `is_transactional` - transactionality of the newly created variable, by
113+
default it is false.
114+
115+
`pgv_get` arguments:
116+
- `package` - name of the existing package. If the package doesn't exist result
117+
depends on `strict` argument: if it is false then `pgv_get` returns NULL
118+
otherwise it fails.
119+
- `name` - name of the the existing variable. If the variable doesn't exist
120+
result depends on `strict` argument: if it is false then `pgv_get` returns NULL
121+
otherwise it fails.
122+
- `var_type` - type of the existing variable. It is necessary to pass it to get
123+
correct return type.
124+
- `strict` - pass false if `pgv_get` shouldn't raise an error if a variable or a
125+
package didn't created before, by default it is true.
126+
101127
## **Deprecated** scalar variables functions
102128

103129
### Integer variables

expected/pg_variables.out

+178-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
CREATE EXTENSION pg_variables;
2+
-- Test packages - sanity checks
3+
SELECT pgv_free();
4+
pgv_free
5+
----------
6+
7+
(1 row)
8+
9+
SELECT pgv_exists(NULL); -- fail
10+
ERROR: package name can not be NULL
11+
SELECT pgv_remove(NULL); -- fail
12+
ERROR: package name can not be NULL
13+
SELECT pgv_remove('vars'); -- fail
14+
ERROR: unrecognized package "vars"
15+
SELECT pgv_exists('vars111111111111111111111111111111111111111111111111111111111111'); -- fail
16+
ERROR: name "vars111111111111111111111111111111111111111111111111111111111111" is too long
217
-- Integer variables
318
SELECT pgv_get_int('vars', 'int1');
419
ERROR: unrecognized package "vars"
@@ -553,20 +568,44 @@ SELECT pgv_insert('vars3', 'r1', row(1, 1));
553568
ERROR: new record structure differs from variable "r1" structure
554569
SELECT pgv_insert('vars3', 'r1', row('str1', 'str1'));
555570
ERROR: new record structure differs from variable "r1" structure
556-
SELECT pgv_select('vars3', 'r1') LIMIT 2;
557-
pgv_select
571+
SELECT pgv_select('vars3', 'r1', ARRAY[[1,2]]); -- fail
572+
ERROR: searching for elements in multidimensional arrays is not supported
573+
-- Test variables caching
574+
SELECT pgv_insert('vars3', 'r2', row(1, 'str1', 'str2'));
575+
pgv_insert
558576
------------
559-
(,strNULL)
560-
(1,str11)
561-
(2 rows)
577+
578+
(1 row)
562579

563-
SELECT pgv_select('vars3', 'r1') LIMIT 2 OFFSET 2;
564-
pgv_select
580+
SELECT pgv_update('vars3', 'r1', row(3, 'str22'::varchar));
581+
pgv_update
565582
------------
566-
(2,)
567-
(0,str00)
568-
(2 rows)
583+
f
584+
(1 row)
569585

586+
SELECT pgv_update('vars4', 'r1', row(3, 'str22'::varchar)); -- fail
587+
ERROR: unrecognized package "vars4"
588+
select pgv_delete('vars3', 'r2', NULL::int);
589+
pgv_delete
590+
------------
591+
f
592+
(1 row)
593+
594+
select pgv_delete('vars4', 'r2', NULL::int); -- fail
595+
ERROR: unrecognized package "vars4"
596+
-- Test NULL values
597+
SELECT pgv_insert('vars3', 'r2', NULL); -- fail
598+
ERROR: record argument can not be NULL
599+
SELECT pgv_update('vars3', 'r2', NULL); -- fail
600+
ERROR: record argument can not be NULL
601+
select pgv_delete('vars3', 'r2', NULL::int);
602+
pgv_delete
603+
------------
604+
f
605+
(1 row)
606+
607+
SELECT pgv_select('vars3', 'r1', NULL::int[]); -- fail
608+
ERROR: array argument can not be NULL
570609
SELECT pgv_select('vars3', 'r1');
571610
pgv_select
572611
------------
@@ -582,6 +621,8 @@ SELECT pgv_select('vars3', 'r1', 1);
582621
(1,str11)
583622
(1 row)
584623

624+
SELECT pgv_select('vars3', 'r1', 1::float); -- fail
625+
ERROR: requested value type differs from variable "r1" key type
585626
SELECT pgv_select('vars3', 'r1', 0);
586627
pgv_select
587628
------------
@@ -612,6 +653,12 @@ SELECT pgv_update('vars3', 'r1', tab) FROM tab;
612653
t
613654
(4 rows)
614655

656+
SELECT pgv_update('vars3', 'r1', row(4, 'str44'::varchar));
657+
pgv_update
658+
------------
659+
f
660+
(1 row)
661+
615662
SELECT pgv_select('vars3', 'r1');
616663
pgv_select
617664
------------
@@ -657,6 +704,119 @@ SELECT pgv_exists('vars3', 'r1');
657704

658705
SELECT pgv_select('vars2', 'j1');
659706
ERROR: variable "j1" requires "jsonb" value
707+
-- PGPRO-2601 - Test pgv_select() on TupleDesc of dropped table
708+
DROP TABLE tab;
709+
SELECT pgv_select('vars3', 'r1');
710+
pgv_select
711+
------------
712+
(,strNULL)
713+
(2,)
714+
(0,str00)
715+
(3 rows)
716+
717+
-- Tests for SRF's sequential scan of an internal hash table
718+
DO
719+
$$BEGIN
720+
PERFORM pgv_select('vars3', 'r1') LIMIT 2 OFFSET 2;
721+
PERFORM pgv_select('vars3', 'r3');
722+
END$$;
723+
ERROR: unrecognized variable "r3"
724+
CONTEXT: SQL statement "SELECT pgv_select('vars3', 'r3')"
725+
PL/pgSQL function inline_code_block line 3 at PERFORM
726+
-- Check that the hash table was cleaned up after rollback
727+
SET client_min_messages to 'ERROR';
728+
SELECT pgv_select('vars3', 'r1', 1);
729+
pgv_select
730+
------------
731+
732+
(1 row)
733+
734+
SELECT pgv_select('vars3', 'r1') LIMIT 2; -- warning
735+
pgv_select
736+
------------
737+
(,strNULL)
738+
(2,)
739+
(2 rows)
740+
741+
SELECT pgv_select('vars3', 'r1') LIMIT 2 OFFSET 2;
742+
pgv_select
743+
------------
744+
(0,str00)
745+
(1 row)
746+
747+
-- PGPRO-2601 - Test a cursor with the hash table
748+
BEGIN;
749+
DECLARE r1_cur CURSOR FOR SELECT pgv_select('vars3', 'r1');
750+
FETCH 1 in r1_cur;
751+
pgv_select
752+
------------
753+
(,strNULL)
754+
(1 row)
755+
756+
SELECT pgv_select('vars3', 'r1');
757+
pgv_select
758+
------------
759+
(,strNULL)
760+
(2,)
761+
(0,str00)
762+
(3 rows)
763+
764+
FETCH 1 in r1_cur;
765+
pgv_select
766+
------------
767+
(2,)
768+
(1 row)
769+
770+
CLOSE r1_cur;
771+
COMMIT; -- warning
772+
RESET client_min_messages;
773+
-- Clean memory after unsuccessful creation of a variable
774+
SELECT pgv_insert('vars4', 'r1', row('str1', 'str1')); -- fail
775+
ERROR: could not identify a hash function for type unknown
776+
SELECT package FROM pgv_stats() WHERE package = 'vars4';
777+
package
778+
---------
779+
(0 rows)
780+
781+
-- Remove package if it is empty
782+
SELECT pgv_insert('vars4', 'r2', row(1, 'str1', 'str2'));
783+
pgv_insert
784+
------------
785+
786+
(1 row)
787+
788+
SELECT pgv_remove('vars4', 'r2');
789+
pgv_remove
790+
------------
791+
792+
(1 row)
793+
794+
SELECT package FROM pgv_stats() WHERE package = 'vars4';
795+
package
796+
---------
797+
(0 rows)
798+
799+
-- Record variables as scalar
800+
SELECT pgv_set('vars5', 'r1', row(1, 'str11'));
801+
pgv_set
802+
---------
803+
804+
(1 row)
805+
806+
SELECT pgv_get('vars5', 'r1', NULL::record);
807+
pgv_get
808+
-----------
809+
(1,str11)
810+
(1 row)
811+
812+
SELECT pgv_set('vars5', 'r1', row(1, 'str11'), true); -- fail
813+
ERROR: variable "r1" already created as NOT TRANSACTIONAL
814+
SELECT pgv_insert('vars5', 'r1', row(1, 'str11')); -- fail
815+
ERROR: "r1" isn't a record variable
816+
SELECT pgv_select('vars5', 'r1'); -- fail
817+
ERROR: "r1" isn't a record variable
818+
SELECT pgv_get('vars3', 'r1', NULL::record); -- fail
819+
ERROR: "r1" isn't a scalar variable
660820
-- Manipulate variables
661821
SELECT * FROM pgv_list() order by package, name;
662822
package | name | is_transactional
@@ -683,15 +843,18 @@ SELECT * FROM pgv_list() order by package, name;
683843
vars2 | j1 | f
684844
vars2 | j2 | f
685845
vars3 | r1 | f
686-
(22 rows)
846+
vars3 | r2 | f
847+
vars5 | r1 | f
848+
(24 rows)
687849

688850
SELECT package FROM pgv_stats() order by package;
689851
package
690852
---------
691853
vars
692854
vars2
693855
vars3
694-
(3 rows)
856+
vars5
857+
(4 rows)
695858

696859
SELECT pgv_remove('vars', 'int3');
697860
ERROR: unrecognized variable "int3"
@@ -745,7 +908,9 @@ SELECT * FROM pgv_list() order by package, name;
745908
vars | tstz2 | f
746909
vars | tstzNULL | f
747910
vars3 | r1 | f
748-
(19 rows)
911+
vars3 | r2 | f
912+
vars5 | r1 | f
913+
(21 rows)
749914

750915
SELECT pgv_free();
751916
pgv_free

0 commit comments

Comments
 (0)