系統平臺:
Linux x86-64 Red Hat Enterprise Linux 7
版本:
4.5
1.配置hg_job的相關引數
①
修改配置檔案
vim /opt/HighGo4.5.6-see/data/postgresql.conf
修改前:
#shared_preload_libraries = 'hg_job' # (change requires restart)
修改後:
shared_preload_libraries = 'hg_job' # (change requires restart)
hg.job_database = 'mvdb' # (設定為建立定時任務的資料庫名)
hg.job_queue_processes = 0 #併發數先設定為0
hg.job_queue_interval = 60 #掃描間隔為60秒 |
儲存退出
重啟資料庫服務,pg_ctl restart
②在資料庫的data目錄下建立hg_job目錄
mkdir /opt/HighGo4.5.6-see/data/hg_job
2.建立測試使用者和測試庫
①psql命令或者客戶端連線資料庫
psql -U sysdba -d highgo
②執行下面建立語句
create user mvuds with password 'xxxxxx';
create database mvdb owner mvuds;
③切換使用者和資料庫
\c mvdb mvuds
④建立測試表
create schema mvuds;
create table mvuds.t1(id int,sname text);
⑤建立物化檢視
create materialized view mvuds.mv_t1 as select * from mvuds.t1;
⑥
建立免密檔案,密碼設定為實際的密碼
如果pgpass檔案中有內容,在最後空白行中新增
vim ~/.pgpass
#hostname:port:database:username:password
localhost:5866:mvdb:sysdba:xxxxxx
儲存退出,然後修改檔案許可權:
chmod 0600 ~/.pgpass |
3.在測試庫中建立hg_job擴充套件
①關閉三權
psql -U syssso -d mvdb
select set_secure_param('hg_sepofpowers','off');
\q
pg_ctl restart
②建立hg_job擴充套件
psql -U sysdba -d mvdb
create extension hg_job;
\q
③建立擴充套件成功後,開啟三權
psql -U syssso -d mvdb
select set_secure_param('hg_sepofpowers','on');
\q
pg_ctl restart |
4.建立定時任務
①設定為每隔2分鐘執行一次
psql -U sysdba -d mvdb
select hgjob.job_create(
$body$
do $do$
begin
refresh materialized view mvuds.mv_t1;
end;
$do$;
$body$
,$$now() + interval '2 minute'$$,now()); |
②配置併發數大於0的值
vim /opt/HighGo4.5.6-see/data/postgresql.conf
修改前:
hg.job_queue_processes = 0
修改後:
hg.job_queue_processes = 5 |
③重啟資料庫
pg_ctl restart
④檢視建立的定時任務
mvdb=# select * from hgjob.job;
jobid | jobenabled | jobwhat | jobinterval | jobnextrun | jobstartrun
| joblastrun | jobuser | jobcount
-------+------------+----------------------------------------------------+-----------------------------+-------------------------------+---------------------------
----+-------------------------------+---------+----------
2 | t | +| now() + interval '2 minute' | 2022-01-12 15:09:38.431608+08 | 2022-01-12 15:07:03.982518
+08 | 2022-01-12 15:07:38.431608+08 | sysdba | 1
| | do $do$ +| | |
| | |
| | begin +| | |
| | |
| | refresh materialized view mvuds.mv_t1;+| | |
| | |
| | end; +| | |
| | |
| | $do$; +| | |
| | |
| | | | |
| | |
(1 行記錄) |
5.測試
①測試資料準備
mvdb=> select * from mv_t1;
id | sname
----+-------
1 | zhao
2 | qian
3 | sun
4 | li
(4 行記錄)
mvdb=> select * from t1;
id | sname
----+-------
1 | zhao
2 | qian
3 | sun
4 | li
(4 行記錄) |
②t1表中插入2條記錄
mvdb=> insert into t1 values(5,'zhou');
INSERT 0 1
mvdb=> insert into t1 values(6,'wu');
INSERT 0 1 |
③查詢t1表和物化檢視mv_t1中的資料
mvdb=> select * from t1;
id | sname
----+-------
1 | zhao
2 | qian
3 | sun
4 | li
5 | zhou
6 | wu
(6 行記錄)
mvdb=> select * from mv_t1;
id | sname
----+-------
1 | zhao
2 | qian
3 | sun
4 | li
(4 行記錄) |
④間隔2分鐘之後,檢視定時任務執行情況
mvdb=# select * from hgjob.job;
jobid | jobenabled | jobwhat | jobinterval | jobnextrun | jobstartrun
| joblastrun | jobuser | jobcount
-------+------------+----------------------------------------------------+-----------------------------+-------------------------------+---------------------------
----+-------------------------------+---------+----------
2 | t | +| now() + interval '2 minute' | 2022-01-12 15:11:38.606462+08 | 2022-01-12 15:07:03.982518
+08 | 2022-01-12 15:09:38.606462+08 | sysdba | 2
| | do $do$ +| | |
| | |
| | begin +| | |
| | |
| | refresh materialized view mvuds.mv_t1;+| | |
| | |
| | end; +| | |
| | |
| | $do$; +| | |
| | |
| | | | |
| | |
(1 行記錄) |
⑤再次查詢物化檢視mv_t1的資料,已經重新整理了
mvdb=> select * from mv_t1;
id | sname
----+-------
1 | zhao
2 | qian
3 | sun
4 | li
5 | zhou
6 | wu
(6 行記錄) |
至此,結束。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69994931/viewspace-2861830/,如需轉載,請註明出處,否則將追究法律責任。