|
| 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