云贝教育 |【PostgreSQL PGCA】pg15安装pg_hint_plan扩展包

news/2024/7/9 21:41:01 标签: postgresql, 数据库

pg15安装pg_hint_plan扩展包

pg当前是支持HINT固定执行计划,需要通过扩展包pg_hint_plan来实现

一、扩展包下载:

Releases · ossc-db/pg_hint_plan · GitHub

二、选择v15版本
pg_hint_plan15 1.5.1 is released
pg_hint_plan15 1.5.1 is released. This version only supports PostgreSQL 15.
Some changes are made in this release:
    Fix hint stack corruption on ERROR when setting GUCs from Set hints (Michael Paquier: 61a3a55)
    Fix handling of unavailable indexes in Scan hints (Sami Imseih: 33adb40)
    Reset more aggressively hints for queries executed via extended query protocol (tanujnay112: de709e6)
    Add EXPLAIN (COSTS false) to some tests (Masahiro Ikeda: ffd7f62)
    Bootstrap a new documentation (Julien Rouhaud: 287e9b5, and more). This removes the HTML documentation, switching to a set of markdown files with support for multiple languages possible.
    Fix and improve documentation (Michael Paquier: ea8616b)

下载源码包(任选其一)

图片

三、上传postgres用户下
[postgres@ora19c02 ~]$ ll -d pg_hint_plan-REL15_1_5_1.zip 
-rw-r--r-- 1 postgres postgres 227951 Nov  1 15:21 pg_hint_plan-REL15_1_5_1.zip

四、解压包
unzip pg_hint_plan-REL15_1_5_1.zip

五、进入解压目录,并编绎
[postgres@ora19c02 pg_hint_plan-REL15_1_5_1]$ make 
[postgres@ora19c02 pg_hint_plan-REL15_1_5_1]$ make install

编绎可能会碰到的问题:

1)make时提示命令找不到

/bin/sh: rpmbuild: command not found

解决
  yum install rpm-build

2)权限不足

[postgres@ora19c02 pg_hint_plan-REL15_1_5_1]$ make install
/usr/bin/mkdir -p '/usr/local/postgres/share/extension'
/usr/bin/mkdir -p '/usr/local/postgres/share/extension'
/usr/bin/mkdir -p '/usr/local/postgres/lib'
/usr/bin/install -c -m 644 .//pg_hint_plan.control '/usr/local/postgres/share/extension/'
/usr/bin/install: cannot create regular file ‘/usr/local/postgres/share/extension/pg_hint_plan.control’: Permission denied
make: *** [install] Error 1

授权解决

[root@ora19c02 ]# chmod 777 /usr/local/postgres -R

六、验证安装
[postgres@ora19c02 pg_hint_plan-REL15_1_5_1]$ ls -l /usr/local/postgres/lib/ | grep hint
-rwxr-xr-x 1 postgres postgres  357016 Nov  1 15:31 pg_hint_plan.so

[postgres@ora19c02 pg_hint_plan-REL15_1_5_1]$ ls -l /usr/local/postgres/share/extension/ | grep hint
-rw-r--r-- 1 postgres postgres   437 Nov  1 15:31 pg_hint_plan--1.3.0--1.3.1.sql
-rw-r--r-- 1 postgres postgres   684 Nov  1 15:31 pg_hint_plan--1.3.0.sql
-rw-r--r-- 1 postgres postgres   437 Nov  1 15:31 pg_hint_plan--1.3.1--1.3.2.sql
-rw-r--r-- 1 postgres postgres   437 Nov  1 15:31 pg_hint_plan--1.3.2--1.3.3.sql
-rw-r--r-- 1 postgres postgres   437 Nov  1 15:31 pg_hint_plan--1.3.3--1.3.4.sql
-rw-r--r-- 1 postgres postgres   437 Nov  1 15:31 pg_hint_plan--1.3.4--1.3.5.sql
-rw-r--r-- 1 postgres postgres   437 Nov  1 15:31 pg_hint_plan--1.3.5--1.3.6.sql
-rw-r--r-- 1 postgres postgres   437 Nov  1 15:31 pg_hint_plan--1.3.6--1.3.7.sql
-rw-r--r-- 1 postgres postgres   437 Nov  1 15:31 pg_hint_plan--1.3.7--1.3.8.sql
-rw-r--r-- 1 postgres postgres   437 Nov  1 15:31 pg_hint_plan--1.3.8--1.3.9.sql
-rw-r--r-- 1 postgres postgres   433 Nov  1 15:31 pg_hint_plan--1.3.9--1.4.sql
-rw-r--r-- 1 postgres postgres   437 Nov  1 15:31 pg_hint_plan--1.4.1--1.4.2.sql
-rw-r--r-- 1 postgres postgres   435 Nov  1 15:31 pg_hint_plan--1.4--1.4.1.sql
-rw-r--r-- 1 postgres postgres   434 Nov  1 15:31 pg_hint_plan--1.4.2--1.5.sql
-rw-r--r-- 1 postgres postgres   436 Nov  1 15:31 pg_hint_plan--1.5--1.5.1.sql
-rw-r--r-- 1 postgres postgres   104 Nov  1 15:31 pg_hint_plan.control

七、设置插件

7.1 会话级别设置

postgres=# LOAD 'pg_hint_plan';
LOAD

如果报错,在template1数据库下执行以下SQL

testdb=# \c template1
template1=# create extension pg_hint_plan;
CREATE EXTENSION

7.2 用户级别设置

postgres=# alter user postgres set session_preload_libraries='pg_hint_plan';
ALTER ROLE

7.3 数据库级别设置

postgres=# alter database postgres set session_preload_libraries='pg_hint_plan';
ALTER DATABASE

7.4 集群级别设置

修改参数文件

[postgres@ora19c02 data]$ vi postgresql.conf 
shared_preload_libraries = 'pg_hint_plan'       # (change requires restart)

重启数据库

pg_ctl restart

7.5 重置配置

配置错了的话就连不上数据库,如果配置错了,连接template1库执行

alter database postgres reset session_preload_libraries;
alter user postgres reset session_preload_libraries;

八、测试HINT功能

8.1 查看参数

testdb=# show session_preload_libraries;
 session_preload_libraries 
---------------------------
 pg_hint_plan

8.2 、模拟数据

CREATE TABLE
IF NOT EXISTS dept (
    -- 部门编号
    deptno serial PRIMARY KEY,
    -- 部门名称
    dname VARCHAR (15),
    -- 部门所在位置
    loc VARCHAR (50)
);
CREATE TABLE
IF NOT EXISTS emp (
    -- 雇员编号
    empno serial,
    -- 雇员姓名
    ename VARCHAR (15),
    -- 雇员职位
    job VARCHAR (10),
    -- 雇员对应的领导的编号
    mgr INT,
    -- 雇员的雇佣日期
    hiredate DATE,
    -- 雇员的基本工资
    sal DECIMAL (7, 2),
    -- 奖金
    comm DECIMAL (7, 2),
    -- 所在部门
    deptno INT,
    FOREIGN KEY (deptno) REFERENCES dept (deptno)
);
-- dept表中的数据
INSERT INTO dept VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO dept VALUES (20,'RESEARCH','DALLAS');
INSERT INTO dept VALUES (30,'SALES','CHICAGO');
INSERT INTO dept VALUES (40,'OPERATIONS','BOSTON');
-- emp表中的数据
INSERT INTO emp VALUES (7369,'SMITH','CLERK',7902,to_date('1980-12-17','yyyy-mm-dd'),800,NULL,20);
INSERT INTO emp VALUES (7499,'ALLEN','SALESMAN',7698,to_date('1981-2-20','yyyy-mm-dd'),1600,300,30);
INSERT INTO emp VALUES (7521,'WARD','SALESMAN',7698,to_date('1981-2-22','yyyy-mm-dd'),1250,500,30);
INSERT INTO emp VALUES (7566,'JONES','MANAGER',7839,to_date('1981-4-2','yyyy-mm-dd'),2975,NULL,20);
INSERT INTO emp VALUES (7654,'MARTIN','SALESMAN',7698,to_date('1981-9-28','yyyy-mm-dd'),1250,1400,30);
INSERT INTO emp VALUES (7698,'BLAKE','MANAGER',7839,to_date('1981-5-1','yyyy-mm-dd'),2850,NULL,30);
INSERT INTO emp VALUES (7782,'CLARK','MANAGER',7839,to_date('1981-6-9','yyyy-mm-dd'),2450,NULL,10);
INSERT INTO emp VALUES (7788,'SCOTT','ANALYST',7566,to_date('87-7-13','yyyy-mm-dd'),3000,NULL,20);
INSERT INTO emp VALUES (7839,'KING','PRESIDENT',NULL,to_date('1981-11-17','yyyy-mm-dd'),5000,NULL,10);
INSERT INTO emp VALUES (7844,'TURNER','SALESMAN',7698,to_date('1981-9-8','yyyy-mm-dd'),1500,0,30);
INSERT INTO emp VALUES (7876,'ADAMS','CLERK',7788,to_date('87-7-13','yyyy-mm-dd'),1100,NULL,20);
INSERT INTO emp VALUES (7900,'JAMES','CLERK',7698,to_date('1981-12-3','yyyy-mm-dd'),950,NULL,30);
INSERT INTO emp VALUES (7902,'FORD','ANALYST',7566,to_date('1981-12-3','yyyy-mm-dd'),3000,NULL,20);
INSERT INTO emp VALUES (7934,'MILLER','CLERK',7782,to_date('1982-1-23','yyyy-mm-dd'),1300,NULL,10);

8.3 执行SQL,查看默认执行计划

testdb=# explain analyze select * from emp a,dept b where a.deptno=b.deptno;
                                                   QUERY PLAN                                    
                
-----------------------------------------------------------------------------------------------------------------
 Hash Join  (cost=19.23..35.67 rows=510 width=300) (actual time=0.088..0.098 rows=14 loops=1)
   Hash Cond: (a.deptno = b.deptno)
   ->  Seq Scan on emp a  (cost=0.00..15.10 rows=510 width=130) (actual time=0.012..0.015 rows=14
 loops=1)
   ->  Hash  (cost=14.10..14.10 rows=410 width=170) (actual time=0.016..0.017 rows=4 loops=1)
         Buckets: 1024  Batches: 1  Memory Usage: 9kB
         ->  Seq Scan on dept b  (cost=0.00..14.10 rows=410 width=170) (actual time=0.009..0.009 
rows=4 loops=1)
 Planning Time: 1.149 ms
 Execution Time: 0.182 ms
(8 rows)

以上输出可以看到,默认的执行计划走HJ

8.4 模拟NL

testdb=# explain analyze select /*+ nestloop(a b)*/* from emp a,dept b where a.deptno=b.deptno;
                                                        QUERY PLAN                               
                          
---------------------------------------------------------------------------------------------------------------------------
 Nested Loop  (cost=0.15..147.14 rows=510 width=300) (actual time=0.040..0.056 rows=14 loops=1)
   ->  Seq Scan on emp a  (cost=0.00..15.10 rows=510 width=130) (actual time=0.009..0.011 rows=14
 loops=1)
   ->  Index Scan using dept_pkey on dept b  (cost=0.15..0.26 rows=1 width=170) (actual time=0.00
2..0.002 rows=1 loops=14)
         Index Cond: (deptno = a.deptno)
 Planning Time: 0.166 ms
 Execution Time: 0.113 ms
(6 rows)

8.5 模拟MJ

testdb=# explain analyze select /*+ mergejoin(a b)*/* from emp a,dept b where a.deptno=b.deptno;
                                                   QUERY PLAN                                    
                
-----------------------------------------------------------------------------------------------------------------
 Merge Join  (cost=69.93..79.63 rows=510 width=300) (actual time=0.168..0.178 rows=14 loops=1)
   Merge Cond: (a.deptno = b.deptno)
   ->  Sort  (cost=38.04..39.31 rows=510 width=130) (actual time=0.142..0.146 rows=14 loops=1)
         Sort Key: a.deptno
         Sort Method: quicksort  Memory: 26kB
         ->  Seq Scan on emp a  (cost=0.00..15.10 rows=510 width=130) (actual time=0.014..0.019 r
ows=14 loops=1)
   ->  Sort  (cost=31.89..32.92 rows=410 width=170) (actual time=0.020..0.020 rows=3 loops=1)
         Sort Key: b.deptno
         Sort Method: quicksort  Memory: 25kB
         ->  Seq Scan on dept b  (cost=0.00..14.10 rows=410 width=170) (actual time=0.010..0.011 
rows=4 loops=1)
 Planning Time: 1.826 ms
 Execution Time: 0.417 ms
(12 rows)

图片


http://www.niftyadmin.cn/n/5163466.html

相关文章

资源分享(课设、说明书、资源)不定期更新

2023年11月8日 自己在大学四年的资源,也有借鉴别人的,希望对大家有帮助。 可以支持一下的,可以在csdn上支持一下,也可以白嫖的,这里提供阿里云 也可以领取200G粉丝福利码:zjlongUidvc 200G粉丝福利码&…

Packet Tracer路由器连接终端设备怎么配置?

在Packet Tracer中配置一台路由器和三台终端设备可以帮助你建立一个简单的局域网,以下是配置的基本步骤: 打开Packet Tracer,从左侧设备栏中拖拽一个路由器和三个终端设备到工作区。 连接设备:使用网线将路由器的端口与每台终端设…

springboot中如何同时操作同一功能

问题描述 测试阶段,由于存在某一功能的同时操作,该功能还是入库逻辑,此时若不进行处理,会造成插入表中多条重复数据,为此该问题需要修复。 解决办法 在接口开始进行对是否存在某个key值的判断,若不存在&…

高性能网络编程 - The C10K problem 以及 网络编程技术角度的解决思路

文章目录 C10KC10K的由来C10K问题在技术层面的典型体现C10K问题的本质C10K解决思路思路一:每个进程/线程处理一个连接思路二:每个进程/线程同时处理多个连接(IO多路复用)● 实现方式1:直接循环处理多个连接● 实现方式…

高德地图 web js端 出现 INVALID_USER_SCODE 10008 MD5安全码未通过验证

图片意思就是在引入 高德js和css 链接前 引入 <script type"text/javascript">window._AMapSecurityConfig {securityJsCode:您申请的安全密钥,} </script> 到这里就完美结束了

目标检测回归损失函数(看情况补...)

文章目录 L1 loss-平均绝对误差(Mean Absolute Error——MAE)L2 loss-均方误差(Mean Square Error——MSE)Smooth L1 LossMAE、MSE、Smooth L1对比IoU LossGIoU LossDIoU Loss、CIoU LossE-IoU Loss、Focal E-IoU LossReferenceL1 loss-平均绝对误差(Mean Absolute Error——…

Qt http get请求数据阻塞和非阻塞实现源码

一、阻塞方式 #include <QObject> #include <QNetworkAccessManager> #include <QNetworkReply> #include <QNetworkRequest> #include <QJsonParseError> #include <QJsonObject> #include <QTime> #include <QEventLoop> #…

C++之vector的常见函数介绍

C之vector的常见函数介绍 初始化成员函数 初始化 1&#xff09;使用空的vector并逐个添加元素&#xff1a; std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3);2&#xff09;使用指定数量的默认元素初始化vector&#xff1a; std::vector<…