|
| 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_int('vars','int1',101); |
| 13 | +BEGIN; |
| 14 | +SELECT pgv_set_int('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_()** functions required package and variable must exists. It is |
| 46 | +necessary to set variable with**pgv_set_()** functions to use**pgv_get_()** |
| 47 | +functions. |
| 48 | + |
| 49 | +If a package does not exists you will get the following error: |
| 50 | + |
| 51 | +```sql |
| 52 | +SELECT pgv_get_int('vars','int1'); |
| 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_int('vars','int1'); |
| 60 | +ERROR: unrecognized variable"int1" |
| 61 | +``` |
| 62 | + |
| 63 | +**pgv_get_()** functions 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_text('vars','int1'); |
| 68 | +ERROR: variable"int1" requires"integer" value |
| 69 | +``` |
| 70 | + |
| 71 | +###Integer variables |
| 72 | + |
| 73 | +Function | Returns |
| 74 | +-------- | ------- |
| 75 | +`pgv_set_int(package text, name text, value int)` |`void` |
| 76 | +`pgv_get_int(package text, name text, strict bool default true)` |`int` |
| 77 | + |
| 78 | +###Text variables |
| 79 | + |
| 80 | +Function | Returns |
| 81 | +-------- | ------- |
| 82 | +`pgv_set_text(package text, name text, value text)` |`void` |
| 83 | +`pgv_get_text(package text, name text, strict bool default true)` |`text` |
| 84 | + |
| 85 | +###Numeric variables |
| 86 | + |
| 87 | +Function | Returns |
| 88 | +-------- | ------- |
| 89 | +`pgv_set_numeric(package text, name text, value numeric)` |`void` |
| 90 | +`pgv_get_numeric(package text, name text, strict bool default true)` |`numeric` |
| 91 | + |
| 92 | +###Timestamp variables |
| 93 | + |
| 94 | +Function | Returns |
| 95 | +-------- | ------- |
| 96 | +`pgv_set_timestamp(package text, name text, value timestamp)` |`void` |
| 97 | +`pgv_get_timestamp(package text, name text, strict bool default true)` |`timestamp` |
| 98 | + |
| 99 | +###Timestamp with timezone variables |
| 100 | + |
| 101 | +Function | Returns |
| 102 | +-------- | ------- |
| 103 | +`pgv_set_timestamptz(package text, name text, value timestamptz)` |`void` |
| 104 | +`pgv_get_timestamptz(package text, name text, strict bool default true)` |`timestamptz` |
| 105 | + |
| 106 | +###Date variables |
| 107 | + |
| 108 | +Function | Returns |
| 109 | +-------- | ------- |
| 110 | +`pgv_set_date(package text, name text, value date)` |`void` |
| 111 | +`pgv_get_date(package text, name text, strict bool default true)` |`date` |
| 112 | + |
| 113 | +###Jsonb variables |
| 114 | + |
| 115 | +Function | Returns |
| 116 | +-------- | ------- |
| 117 | +`pgv_set_jsonb(package text, name text, value jsonb)` |`void` |
| 118 | +`pgv_get_jsonb(package text, name text, strict bool default true)` |`jsonb` |
| 119 | + |
| 120 | +###Records |
| 121 | + |
| 122 | +The following functions are provided by the module to work with collections of |
| 123 | +record types. |
| 124 | + |
| 125 | +To use**pgv_update()**,**pgv_delete()** and**pgv_select()** functions |
| 126 | +required package and variable must exists. Otherwise the error will be raised. |
| 127 | +It is necessary to set variable with**pgv_insert()** function to use these |
| 128 | +functions. |
| 129 | + |
| 130 | +**pgv_update()**,**pgv_delete()** and**pgv_select()** functions check the |
| 131 | +variable type. If the variable type does not**record** type the error will be |
| 132 | +raised. |
| 133 | + |
| 134 | +Function | Returns | Description |
| 135 | +-------- | ------- | ----------- |
| 136 | +`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. |
| 137 | +`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. |
| 138 | +`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. |
| 139 | +`pgv_select(package text, name text)` |`set of record` | Returns the variable collection records. |
| 140 | +`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). |
| 141 | +`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). |
| 142 | + |
| 143 | +###Miscellaneous functions |
| 144 | + |
| 145 | +Function | Returns | Description |
| 146 | +-------- | ------- | ----------- |
| 147 | +`pgv_exists(package text, name text)` |`bool` | Returns**true** if package and variable exists. |
| 148 | +`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. |
| 149 | +`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. |
| 150 | +`pgv_free()` |`void` | Removes all packages and variables. |
| 151 | +`pgv_list()` |`table(package text, name text)` | Returns set of records of assigned packages and variables. |
| 152 | +`pgv_stats()` |`table(package text, used_memory bigint)` | Returns list of assigned packages and used memory in bytes. |
| 153 | + |
| 154 | +Note that**pgv_stats()** works only with the PostgreSQL 9.6 and newer. |
| 155 | + |
| 156 | +##Examples |
| 157 | + |
| 158 | +It is easy to use functions to work with scalar variables: |
| 159 | + |
| 160 | +```sql |
| 161 | +SELECT pgv_set_int('vars','int1',101); |
| 162 | +SELECT pgv_set_int('vars','int2',102); |
| 163 | + |
| 164 | +SELECT pgv_get_int('vars','int1'); |
| 165 | + pgv_get_int |
| 166 | +------------- |
| 167 | +101 |
| 168 | +(1 row) |
| 169 | + |
| 170 | +SELECT pgv_get_int('vars','int2'); |
| 171 | + pgv_get_int |
| 172 | +------------- |
| 173 | +102 |
| 174 | +(1 row) |
| 175 | +``` |
| 176 | + |
| 177 | +Let's assume we have a**tab** table: |
| 178 | + |
| 179 | +```sql |
| 180 | +CREATETABLEtab (idint, tvarchar); |
| 181 | +INSERT INTO tabVALUES (0,'str00'), (1,'str11'); |
| 182 | +``` |
| 183 | + |
| 184 | +Then you can use functions to work with record variables: |
| 185 | + |
| 186 | +```sql |
| 187 | +SELECT pgv_insert('vars','r1', tab)FROM tab; |
| 188 | + |
| 189 | +SELECT pgv_select('vars','r1'); |
| 190 | + pgv_select |
| 191 | +------------ |
| 192 | + (1,str11) |
| 193 | + (0,str00) |
| 194 | +(2 rows) |
| 195 | + |
| 196 | +SELECT pgv_select('vars','r1',1); |
| 197 | + pgv_select |
| 198 | +------------ |
| 199 | + (1,str11) |
| 200 | +(1 row) |
| 201 | + |
| 202 | +SELECT pgv_select('vars','r1',0); |
| 203 | + pgv_select |
| 204 | +------------ |
| 205 | + (0,str00) |
| 206 | +(1 row) |
| 207 | + |
| 208 | +SELECT pgv_select('vars','r1', ARRAY[1,0]); |
| 209 | + pgv_select |
| 210 | +------------ |
| 211 | + (1,str11) |
| 212 | + (0,str00) |
| 213 | +(2 rows) |
| 214 | + |
| 215 | +SELECT pgv_delete('vars','r1',1); |
| 216 | + |
| 217 | +SELECT pgv_select('vars','r1'); |
| 218 | + pgv_select |
| 219 | +------------ |
| 220 | + (0,str00) |
| 221 | +(1 row) |
| 222 | +``` |
| 223 | + |
| 224 | +You can list packages and variables: |
| 225 | + |
| 226 | +```sql |
| 227 | +SELECT*FROM pgv_list()order by package, name; |
| 228 | + package | name |
| 229 | +---------+------ |
| 230 | + vars | int1 |
| 231 | + vars | int2 |
| 232 | + vars | r1 |
| 233 | +(3 rows) |
| 234 | +``` |
| 235 | + |
| 236 | +And get used memory in bytes: |
| 237 | + |
| 238 | +```sql |
| 239 | +SELECT*FROM pgv_stats()order by package; |
| 240 | + package | used_memory |
| 241 | +---------+------------- |
| 242 | + vars |16736 |
| 243 | +(1 row) |
| 244 | +``` |
| 245 | + |
| 246 | +You can delete variables or hole packages: |
| 247 | + |
| 248 | +```sql |
| 249 | +SELECT pgv_remove('vars','int1'); |
| 250 | +SELECT pgv_remove('vars'); |
| 251 | +``` |
| 252 | + |
| 253 | +You can delete all packages and variables: |
| 254 | +```sql |
| 255 | +SELECT pgv_free(); |
| 256 | +``` |