溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PostgreSQL的插件pg_variables有什么作用

發(fā)布時間:2021-11-08 14:44:34 來源:億速云 閱讀:145 作者:iii 欄目:關(guān)系型數(shù)據(jù)庫

本篇內(nèi)容主要講解“PostgreSQL的插件pg_variables有什么作用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“PostgreSQL的插件pg_variables有什么作用”吧!

安裝
使用git下載源碼,編譯安裝

[pg12@localhost contrib]$ git clone https://github.com/postgrespro/pg_variables.git
Cloning into 'pg_variables'...
remote: Enumerating objects: 585, done.
remote: Total 585 (delta 0), reused 0 (delta 0), pack-reused 585
Receiving objects: 100% (585/585), 328.79 KiB | 75.00 KiB/s, done.
Resolving deltas: 100% (404/404), done.
[pg12@localhost contrib]$ cd pg_variables/
[pg12@localhost pg_variables]$ make USE_PGXS=1
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -O0 -DOPTIMIZER_DEBUG -g3 -gdwarf-2 -fPIC -I. -I./ -I/appdb/pg12/pg12.0/include/postgresql/server -I/appdb/pg12/pg12.0/include/postgresql/internal  -D_GNU_SOURCE -I/usr/include/libxml2   -c -o pg_variables.o pg_variables.c -MMD -MP -MF .deps/pg_variables.Po
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -O0 -DOPTIMIZER_DEBUG -g3 -gdwarf-2 -fPIC -I. -I./ -I/appdb/pg12/pg12.0/include/postgresql/server -I/appdb/pg12/pg12.0/include/postgresql/internal  -D_GNU_SOURCE -I/usr/include/libxml2   -c -o pg_variables_record.o pg_variables_record.c -MMD -MP -MF .deps/pg_variables_record.Po
gcc -std=gnu99 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -O0 -DOPTIMIZER_DEBUG -g3 -gdwarf-2 -fPIC -shared -o pg_variables.so pg_variables.o pg_variables_record.o  -L/appdb/pg12/pg12.0/lib    -Wl,--as-needed -Wl,-rpath,'/appdb/pg12/pg12.0/lib',--enable-new-dtags  
cat pg_variables--1.0.sql pg_variables--1.0--1.1.sql pg_variables--1.1--1.2.sql > pg_variables--1.2.sql
[pg12@localhost pg_variables]$ make USE_PGXS=1 install
/bin/mkdir -p '/appdb/pg12/pg12.0/lib/postgresql'
/bin/mkdir -p '/appdb/pg12/pg12.0/share/postgresql/extension'
/bin/mkdir -p '/appdb/pg12/pg12.0/share/postgresql/extension'
/bin/install -c -m 755  pg_variables.so '/appdb/pg12/pg12.0/lib/postgresql/pg_variables.so'
/bin/install -c -m 644 .//pg_variables.control '/appdb/pg12/pg12.0/share/postgresql/extension/'
/bin/install -c -m 644 .//pg_variables--1.0.sql .//pg_variables--1.0--1.1.sql .//pg_variables--1.1--1.2.sql pg_variables--1.2.sql '/appdb/pg12/pg12.0/share/postgresql/extension/'
[pg12@localhost pg_variables]$ make USE_PGXS=1 installcheck
/appdb/pg12/pg12.0/lib/postgresql/pgxs/src/makefiles/../../src/test/regress/pg_regress --inputdir=./ --bindir='/appdb/pg12/pg12.0/bin'    --dbname=contrib_regression pg_variables pg_variables_any pg_variables_trans
(using postmaster on Unix socket, default port)
============== dropping database "contrib_regression" ==============
psql: error: could not connect to server: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
command failed: "/appdb/pg12/pg12.0/bin/psql" -X -c "DROP DATABASE IF EXISTS \"contrib_regression\"" "postgres"
make: *** [installcheck] Error 2
[pg12@localhost pg_variables]$ pg_ctl start
waiting for server to start....2019-11-18 14:43:59.175 CST [2254] LOG:  starting PostgreSQL 12.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
2019-11-18 14:43:59.175 CST [2254] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2019-11-18 14:43:59.175 CST [2254] LOG:  listening on IPv6 address "::", port 5432
2019-11-18 14:43:59.176 CST [2254] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-11-18 14:43:59.769 CST [2254] LOG:  redirecting log output to logging collector process
2019-11-18 14:43:59.769 CST [2254] HINT:  Future log output will appear in directory "pg_log".
 done
server started
[pg12@localhost pg_variables]$ make USE_PGXS=1 installcheck
/appdb/pg12/pg12.0/lib/postgresql/pgxs/src/makefiles/../../src/test/regress/pg_regress --inputdir=./ --bindir='/appdb/pg12/pg12.0/bin'    --dbname=contrib_regression pg_variables pg_variables_any pg_variables_trans
(using postmaster on Unix socket, default port)
============== dropping database "contrib_regression" ==============
NOTICE:  database "contrib_regression" does not exist, skipping
DROP DATABASE
============== creating database "contrib_regression" ==============
CREATE DATABASE
ALTER DATABASE
============== running regression test queries        ==============
test pg_variables                 ... ok          161 ms
test pg_variables_any             ... ok           47 ms
test pg_variables_trans           ... ok          128 ms
=====================
 All 3 tests passed. 
=====================
[pg12@localhost pg_variables]$

簡單使用
創(chuàng)建擴展

[local]:5432 pg12@testdb=# create extension pg_variables;
CREATE EXTENSION
[local]:5432 pg12@testdb=#

pg_variables中包含了多個函數(shù)

[local]:5432 pg12@testdb=# \df pgv*
                                                                                          List of functions
 Schema |        Name         |                     Result data type                     |                                        Argument data types                                         | Type 
--------+---------------------+----------------------------------------------------------+----------------------------------------------------------------------------------------------------+------
 public | pgv_delete          | boolean                                                  | package text, name text, value anynonarray                                                         | func
 public | pgv_exists          | boolean                                                  | package text                                                                                       | func
 public | pgv_exists          | boolean                                                  | package text, name text                                                                            | func
 public | pgv_free            | void                                                     |                                                                                                    | func
 public | pgv_get             | anyarray                                                 | package text, name text, var_type anyarray, strict boolean DEFAULT true                            | func
 public | pgv_get             | anynonarray                                              | package text, name text, var_type anynonarray, strict boolean DEFAULT true                         | func
 public | pgv_get_date        | date                                                     | package text, name text, strict boolean DEFAULT true                                               | func
 public | pgv_get_int         | integer                                                  | package text, name text, strict boolean DEFAULT true                                               | func
 public | pgv_get_jsonb       | jsonb                                                    | package text, name text, strict boolean DEFAULT true                                               | func
 public | pgv_get_numeric     | numeric                                                  | package text, name text, strict boolean DEFAULT true                                               | func
 public | pgv_get_text        | text                                                     | package text, name text, strict boolean DEFAULT true                                               | func
 public | pgv_get_timestamp   | timestamp without time zone                              | package text, name text, strict boolean DEFAULT true                                               | func
 public | pgv_get_timestamptz | timestamp with time zone                                 | package text, name text, strict boolean DEFAULT true                                               | func
 public | pgv_insert          | void                                                     | package text, name text, r record, is_transactional boolean DEFAULT false                          | func
 public | pgv_list            | TABLE(package text, name text, is_transactional boolean) |                                                                                                    | func
 public | pgv_remove          | void                                                     | package text                                                                                       | func
 public | pgv_remove          | void                                                     | package text, name text                                                                            | func
 public | pgv_select          | SETOF record                                             | package text, name text                                                                            | func
 public | pgv_select          | SETOF record                                             | package text, name text, value anyarray                                                            | func
 public | pgv_select          | record                                                   | package text, name text, value anynonarray                                                         | func
 public | pgv_set             | void                                                     | package text, name text, value anyarray, is_transactional boolean DEFAULT false                    | func
 public | pgv_set             | void                                                     | package text, name text, value anynonarray, is_transactional boolean DEFAULT false                 | func
 public | pgv_set_date        | void                                                     | package text, name text, value date, is_transactional boolean DEFAULT false                        | func
 public | pgv_set_int         | void                                                     | package text, name text, value integer, is_transactional boolean DEFAULT false                     | func
 public | pgv_set_jsonb       | void                                                     | package text, name text, value jsonb, is_transactional boolean DEFAULT false                       | func
 public | pgv_set_numeric     | void                                                     | package text, name text, value numeric, is_transactional boolean DEFAULT false                     | func
 public | pgv_set_text        | void                                                     | package text, name text, value text, is_transactional boolean DEFAULT false                        | func
 public | pgv_set_timestamp   | void                                                     | package text, name text, value timestamp without time zone, is_transactional boolean DEFAULT false | func
 public | pgv_set_timestamptz | void                                                     | package text, name text, value timestamp with time zone, is_transactional boolean DEFAULT false    | func
 public | pgv_stats           | TABLE(package text, allocated_memory bigint)             |                                                                                                    | func
 public | pgv_update          | boolean                                                  | package text, name text, r record                                                                  | func
(31 rows)

可以看到,該插件支持常規(guī)的數(shù)據(jù)類型,而對于復(fù)雜數(shù)據(jù)類型,則使用jsonb。
上述函數(shù)中,其中重要的函數(shù)主要有兩個:pgv_set和pgv_get

[local]:5432 pg12@testdb=# \df pgv_get
                                                    List of functions
 Schema |  Name   | Result data type |                            Argument data types                             | Type 
--------+---------+------------------+----------------------------------------------------------------------------+------
 public | pgv_get | anyarray         | package text, name text, var_type anyarray, strict boolean DEFAULT true    | func
 public | pgv_get | anynonarray      | package text, name text, var_type anynonarray, strict boolean DEFAULT true | func
(2 rows)
[local]:5432 pg12@testdb=# \df pgv_set
                                                        List of functions
 Schema |  Name   | Result data type |                                Argument data types                                 | Type 
--------+---------+------------------+------------------------------------------------------------------------------------+------
 public | pgv_set | void             | package text, name text, value anyarray, is_transactional boolean DEFAULT false    | func
 public | pgv_set | void             | package text, name text, value anynonarray, is_transactional boolean DEFAULT false | func
(2 rows)
[local]:5432 pg12@testdb=# select pgv_set('pk1','pk1_var1',1);
 pgv_set 
---------
(1 row)
[local]:5432 pg12@testdb=# select pgv_get('pk1','pk1_var1',null::int);
 pgv_get 
---------
       1
(1 row)
[local]:5432 pg12@testdb=# select pgv_set('pk1','pk1_var1',101);
 pgv_set 
---------
(1 row)
[local]:5432 pg12@testdb=# select pgv_get('pk1','pk1_var1',null::int);
 pgv_get 
---------
     101
(1 row)
[local]:5432 pg12@testdb=#

而且變量的作用域只在當(dāng)前session中有效

[root@localhost ~]# su - pg12
Last login: Mon Nov 18 14:39:19 CST 2019 on pts/0
[pg12@localhost ~]$ psql -d testdb
Expanded display is used automatically.
psql (12.0)
Type "help" for help.
[local]:5432 pg12@testdb=# select pgv_get('pk1','pk1_var1',null::int);
ERROR:  unrecognized package "pk1"
[local]:5432 pg12@testdb=#

到此,相信大家對“PostgreSQL的插件pg_variables有什么作用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI