Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5bbd0a2

Browse files
committed
Add 'contrib/pg_variables/' from commit '8829bc249562295bf212ce1021bf6a41da6fe0c7'
git-subtree-dir: contrib/pg_variables git-subtree-mainline: c5321a8 git-subtree-split: 8829bc2
2 parents c5321a8 + 8829bc2 commit 5bbd0a2

12 files changed

+4193
-0
lines changed

contrib/pg_variables/.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Global excludes across all subdirectories
2+
*.o
3+
*.obj
4+
*.so
5+
*.so.[0-9]
6+
*.so.[0-9].[0-9]
7+
*.sl
8+
*.sl.[0-9]
9+
*.sl.[0-9].[0-9]
10+
*.dylib
11+
*.dll
12+
*.exp
13+
*.a
14+
*.mo
15+
*.pot
16+
objfiles.txt
17+
.deps/
18+
*.gcno
19+
*.gcda
20+
*.gcov
21+
*.gcov.out
22+
lcov.info
23+
coverage/
24+
*.vcproj
25+
*.vcxproj
26+
win32ver.rc
27+
*.exe
28+
lib*dll.def
29+
lib*.pc
30+
31+
# Local excludes in root directory
32+
/GNUmakefile
33+
/config.cache
34+
/config.log
35+
/config.status
36+
/pgsql.sln
37+
/pgsql.sln.cache
38+
/Debug/
39+
/Release/
40+
/tmp_install/

contrib/pg_variables/Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# contrib/pg_variables/Makefile
2+
3+
MODULE_big = pg_variables
4+
OBJS = pg_variables.o pg_variables_record.o $(WIN32RES)
5+
6+
EXTENSION = pg_variables
7+
DATA = pg_variables--1.0.sql
8+
PGFILEDESC = "pg_variables - sessional variables"
9+
10+
REGRESS = pg_variables pg_variables_any
11+
12+
ifdef USE_PGXS
13+
PG_CONFIG = pg_config
14+
PGXS := $(shell $(PG_CONFIG) --pgxs)
15+
include $(PGXS)
16+
else
17+
subdir = contrib/pg_variables
18+
top_builddir = ../..
19+
include $(top_builddir)/src/Makefile.global
20+
include $(top_srcdir)/contrib/contrib-global.mk
21+
endif

contrib/pg_variables/README.md

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
# pg_variables - session variables with various types
2+
3+
## Introduction
4+
5+
The **pg_variables** module provides functions to work with variables of various
6+
types. Created variables live only in the current user session.
7+
8+
Note that the module does **not support transactions and savepoints**. For
9+
example:
10+
11+
```sql
12+
SELECT pgv_set('vars', 'int1', 101);
13+
BEGIN;
14+
SELECT pgv_set('vars', 'int2', 102);
15+
ROLLBACK;
16+
17+
SELECT * FROM pgv_list() order by package, name;
18+
package | name
19+
---------+------
20+
vars | int1
21+
vars | int2
22+
(2 rows)
23+
```
24+
25+
## License
26+
27+
This module available under the same license as
28+
[PostgreSQL](http://www.postgresql.org/about/licence/).
29+
30+
## Installation
31+
32+
Typical installation procedure may look like this:
33+
34+
$ cd pg_variables
35+
$ make USE_PGXS=1
36+
$ sudo make USE_PGXS=1 install
37+
$ make USE_PGXS=1 installcheck
38+
$ psql DB -c "CREATE EXTENSION pg_variables;"
39+
40+
## Module functions
41+
42+
The functions provided by the **pg_variables** module are shown in the tables
43+
below. The module supports the following scalar and record types.
44+
45+
To use **pgv_get()** function required package and variable must exists. It is
46+
necessary to set variable with **pgv_set()** function to use **pgv_get()**
47+
function.
48+
49+
If a package does not exists you will get the following error:
50+
51+
```sql
52+
SELECT pgv_get('vars', 'int1', NULL::int);
53+
ERROR: unrecognized package "vars"
54+
```
55+
56+
If a variable does not exists you will get the following error:
57+
58+
```sql
59+
SELECT pgv_get('vars', 'int1', NULL::int);
60+
ERROR: unrecognized variable "int1"
61+
```
62+
63+
**pgv_get()** function check the variable type. If the variable type does not
64+
match with the function type the error will be raised:
65+
66+
```sql
67+
SELECT pgv_get('vars', 'int1', NULL::text);
68+
ERROR: variable "int1" requires "integer" value
69+
```
70+
71+
## Scalar variables functions
72+
73+
Function | Returns
74+
-------- | -------
75+
`pgv_set(package text, name text, value anynonarray)` | `void`
76+
`pgv_get(package text, name text, var_type anynonarray, strict bool default true)` | `anynonarray`
77+
78+
## **Deprecated** scalar variables functions
79+
80+
### Integer variables
81+
82+
Function | Returns
83+
-------- | -------
84+
`pgv_set_int(package text, name text, value int)` | `void`
85+
`pgv_get_int(package text, name text, strict bool default true)` | `int`
86+
87+
### Text variables
88+
89+
Function | Returns
90+
-------- | -------
91+
`pgv_set_text(package text, name text, value text)` | `void`
92+
`pgv_get_text(package text, name text, strict bool default true)` | `text`
93+
94+
### Numeric variables
95+
96+
Function | Returns
97+
-------- | -------
98+
`pgv_set_numeric(package text, name text, value numeric)` | `void`
99+
`pgv_get_numeric(package text, name text, strict bool default true)` | `numeric`
100+
101+
### Timestamp variables
102+
103+
Function | Returns
104+
-------- | -------
105+
`pgv_set_timestamp(package text, name text, value timestamp)` | `void`
106+
`pgv_get_timestamp(package text, name text, strict bool default true)` | `timestamp`
107+
108+
### Timestamp with timezone variables
109+
110+
Function | Returns
111+
-------- | -------
112+
`pgv_set_timestamptz(package text, name text, value timestamptz)` | `void`
113+
`pgv_get_timestamptz(package text, name text, strict bool default true)` | `timestamptz`
114+
115+
### Date variables
116+
117+
Function | Returns
118+
-------- | -------
119+
`pgv_set_date(package text, name text, value date)` | `void`
120+
`pgv_get_date(package text, name text, strict bool default true)` | `date`
121+
122+
### Jsonb variables
123+
124+
Function | Returns
125+
-------- | -------
126+
`pgv_set_jsonb(package text, name text, value jsonb)` | `void`
127+
`pgv_get_jsonb(package text, name text, strict bool default true)` | `jsonb`
128+
129+
## Record variables functions
130+
131+
The following functions are provided by the module to work with collections of
132+
record types.
133+
134+
To use **pgv_update()**, **pgv_delete()** and **pgv_select()** functions
135+
required package and variable must exists. Otherwise the error will be raised.
136+
It is necessary to set variable with **pgv_insert()** function to use these
137+
functions.
138+
139+
**pgv_update()**, **pgv_delete()** and **pgv_select()** functions check the
140+
variable type. If the variable type does not **record** type the error will be
141+
raised.
142+
143+
Function | Returns | Description
144+
-------- | ------- | -----------
145+
`pgv_insert(package text, name text, r record)` | `void` | Inserts a record to the variable collection. If package and variable do not exists they will be created. The first column of **r** will be a primary key. If exists a record with the same primary key the error will be raised. If this variable collection has other structure the error will be raised.
146+
`pgv_update(package text, name text, r record)` | `boolean` | Updates a record with the corresponding primary key (the first column of **r** is a primary key). Returns **true** if a record was found. If this variable collection has other structure the error will be raised.
147+
`pgv_delete(package text, name text, value anynonarray)` | `boolean` | Deletes a record with the corresponding primary key (the first column of **r** is a primary key). Returns **true** if a record was found.
148+
`pgv_select(package text, name text)` | `set of record` | Returns the variable collection records.
149+
`pgv_select(package text, name text, value anynonarray)` | `record` | Returns the record with the corresponding primary key (the first column of **r** is a primary key).
150+
`pgv_select(package text, name text, value anyarray)` | `set of record` | Returns the variable collection records with the corresponding primary keys (the first column of **r** is a primary key).
151+
152+
### Miscellaneous functions
153+
154+
Function | Returns | Description
155+
-------- | ------- | -----------
156+
`pgv_exists(package text, name text)` | `bool` | Returns **true** if package and variable exists.
157+
`pgv_exists(package text)` | `bool` | Returns **true** if package exists.
158+
`pgv_remove(package text, name text)` | `void` | Removes the variable with the corresponding name. Required package and variable must exists, otherwise the error will be raised.
159+
`pgv_remove(package text)` | `void` | Removes the package and all package variables with the corresponding name. Required package must exists, otherwise the error will be raised.
160+
`pgv_free()` | `void` | Removes all packages and variables.
161+
`pgv_list()` | `table(package text, name text)` | Returns set of records of assigned packages and variables.
162+
`pgv_stats()` | `table(package text, used_memory bigint)` | Returns list of assigned packages and used memory in bytes.
163+
164+
Note that **pgv_stats()** works only with the PostgreSQL 9.6 and newer.
165+
166+
## Examples
167+
168+
It is easy to use functions to work with scalar variables:
169+
170+
```sql
171+
SELECT pgv_set('vars', 'int1', 101);
172+
SELECT pgv_set('vars', 'int2', 102);
173+
174+
SELECT pgv_get('vars', 'int1', NULL::int);
175+
pgv_get_int
176+
-------------
177+
101
178+
(1 row)
179+
180+
SELECT pgv_get('vars', 'int2', NULL::int);
181+
pgv_get_int
182+
-------------
183+
102
184+
(1 row)
185+
```
186+
187+
Let's assume we have a **tab** table:
188+
189+
```sql
190+
CREATE TABLE tab (id int, t varchar);
191+
INSERT INTO tab VALUES (0, 'str00'), (1, 'str11');
192+
```
193+
194+
Then you can use functions to work with record variables:
195+
196+
```sql
197+
SELECT pgv_insert('vars', 'r1', tab) FROM tab;
198+
199+
SELECT pgv_select('vars', 'r1');
200+
pgv_select
201+
------------
202+
(1,str11)
203+
(0,str00)
204+
(2 rows)
205+
206+
SELECT pgv_select('vars', 'r1', 1);
207+
pgv_select
208+
------------
209+
(1,str11)
210+
(1 row)
211+
212+
SELECT pgv_select('vars', 'r1', 0);
213+
pgv_select
214+
------------
215+
(0,str00)
216+
(1 row)
217+
218+
SELECT pgv_select('vars', 'r1', ARRAY[1, 0]);
219+
pgv_select
220+
------------
221+
(1,str11)
222+
(0,str00)
223+
(2 rows)
224+
225+
SELECT pgv_delete('vars', 'r1', 1);
226+
227+
SELECT pgv_select('vars', 'r1');
228+
pgv_select
229+
------------
230+
(0,str00)
231+
(1 row)
232+
```
233+
234+
You can list packages and variables:
235+
236+
```sql
237+
SELECT * FROM pgv_list() order by package, name;
238+
package | name
239+
---------+------
240+
vars | int1
241+
vars | int2
242+
vars | r1
243+
(3 rows)
244+
```
245+
246+
And get used memory in bytes:
247+
248+
```sql
249+
SELECT * FROM pgv_stats() order by package;
250+
package | used_memory
251+
---------+-------------
252+
vars | 16736
253+
(1 row)
254+
```
255+
256+
You can delete variables or hole packages:
257+
258+
```sql
259+
SELECT pgv_remove('vars', 'int1');
260+
SELECT pgv_remove('vars');
261+
```
262+
263+
You can delete all packages and variables:
264+
```sql
265+
SELECT pgv_free();
266+
```

0 commit comments

Comments
 (0)