2024-05-08 postgres-調試及分析-記錄

摘要:

2024-05-08 postgres-調試及分析-記錄

DDL:

創建庫表及插入數據:


create database d1;\c d1;create table t1( a int, b int );
create table t2( a int, b int );insert into t1(a,b) values(3,4);
insert into t1(a,b) values(5,6);insert into t2(a,b) values(3,1);
insert into t2(a,b) values(7,2);

準備查詢SQL:

SELECT * FROM  t1 LEFT JOIN t2 ON t2.a = t1.a;

一. 客戶端連接pg服務

參考:?2024-05-08 postgres-編譯初始化及運行-記錄-CSDN博客

二. 找到客戶端連接的pg的后端進程的pid

命令:

ps -ef | grep postgres | grep -v grep | grep -v psql

執行結果:

[root@192 trunk]# ps -ef | grep postgres | grep -v grep | grep -v psql
kevin      41716       1  0 02:00 ?        00:00:00 /usr/local/pgsql/bin/postgres
kevin      41718   41716  0 02:00 ?        00:00:00 postgres: checkpointer 
kevin      41719   41716  0 02:00 ?        00:00:00 postgres: background writer 
kevin      41720   41716  0 02:00 ?        00:00:00 postgres: walwriter 
kevin      41721   41716  0 02:00 ?        00:00:00 postgres: autovacuum launcher 
kevin      41722   41716  0 02:00 ?        00:00:00 postgres: stats collector 
kevin      41723   41716  0 02:00 ?        00:00:00 postgres: logical replication launcher 
kevin      41772   41716  0 02:18 ?        00:00:00 postgres: kevin d1 [local] idle

客戶端連接的pg后端進程pid:?

kevin      41772   41716  0 02:18 ?        00:00:00 postgres: kevin d1 [local] idle

三. gdb掛在pg后端進程并打斷點

gdb掛載進程:

[root@192 trunk]# gdb -p 41772
GNU gdb (GDB) Red Hat Enterprise Linux 8.2-20.el8
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:<http://www.gnu.org/software/gdb/documentation/>.For help, type "help".
Type "apropos word" to search for commands related to "word".
Attaching to process 41772
Reading symbols from /usr/local/pgsql/bin/postgres...done.
Reading symbols from /lib64/libpthread.so.0...(no debugging symbols found)...done.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Reading symbols from /lib64/librt.so.1...(no debugging symbols found)...done.
Reading symbols from /lib64/libdl.so.2...(no debugging symbols found)...done.
Reading symbols from /lib64/libm.so.6...Reading symbols from .gnu_debugdata for /lib64/libm.so.6...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Reading symbols from /lib64/ld-linux-x86-64.so.2...done.
Reading symbols from /lib64/libnss_files.so.2...Reading symbols from .gnu_debugdata for /lib64/libnss_files.so.2...(no debugging symbols found)...done.
(no debugging symbols found)...done.
0x00007fa384b8e2cb in epoll_wait () from /lib64/libc.so.6
Missing separate debuginfos, use: yum debuginfo-install glibc-2.28-251.el8.x86_64
(gdb) 

為核心函數打斷點:

例如:?create_scan_plan

(gdb) 
(gdb) b create_scan_plan
Breakpoint 1 at 0x7dbe25: file createplan.c, line 558.
(gdb) c
Continuing.

四. 客戶端執行查詢,查看gdb的堆棧

客戶端執行查詢:

使用空值拒絕的外連接轉內連接的規則的sql

SELECT * FROM  t1 LEFT JOIN t2 ON t2.a = t1.a WHERE t2.b < 5;

gdb中的函數調用堆棧:

(gdb) c
Continuing.Breakpoint 1, create_scan_plan (root=0x2ce5808, best_path=0x2cfb508, flags=2) at createplan.c:558
558		RelOptInfo *rel = best_path->parent;

(gdb) bt
#0  create_scan_plan (root=0x2ce5808, best_path=0x2cfb508, flags=2) at createplan.c:558
#1  0x00000000007dbaa4 in create_plan_recurse (root=0x2ce5808, best_path=0x2cfb508, flags=2) at createplan.c:410
#2  0x00000000007e2777 in create_mergejoin_plan (root=0x2ce5808, best_path=0x2cfbee8) at createplan.c:4376
#3  0x00000000007dc862 in create_join_plan (root=0x2ce5808, best_path=0x2cfbee8) at createplan.c:1067
#4  0x00000000007dbac0 in create_plan_recurse (root=0x2ce5808, best_path=0x2cfbee8, flags=1) at createplan.c:415
#5  0x00000000007db9ba in create_plan (root=0x2ce5808, best_path=0x2cfbee8) at createplan.c:346
#6  0x00000000007ecfbf in standard_planner (parse=0x2c16948, query_string=0x2c15718 "SELECT * FROM  t1 LEFT JOIN t2 ON t2.a = t1.a;", cursorOptions=2048, boundParams=0x0)at planner.c:407
#7  0x00000000007ecd2a in planner (parse=0x2c16948, query_string=0x2c15718 "SELECT * FROM  t1 LEFT JOIN t2 ON t2.a = t1.a;", cursorOptions=2048, boundParams=0x0) at planner.c:271
#8  0x000000000090572b in pg_plan_query (querytree=0x2c16948, query_string=0x2c15718 "SELECT * FROM  t1 LEFT JOIN t2 ON t2.a = t1.a;", cursorOptions=2048, boundParams=0x0)at postgres.c:847
#9  0x0000000000905869 in pg_plan_queries (querytrees=0x2ce5728, query_string=0x2c15718 "SELECT * FROM  t1 LEFT JOIN t2 ON t2.a = t1.a;", cursorOptions=2048, boundParams=0x0)at postgres.c:939
#10 0x0000000000905bc0 in exec_simple_query (query_string=0x2c15718 "SELECT * FROM  t1 LEFT JOIN t2 ON t2.a = t1.a;") at postgres.c:1133
#11 0x000000000090a0ef in PostgresMain (argc=1, argv=0x7fff70e94eb0, dbname=0x2c42428 "d1", username=0x2c10a58 "kevin") at postgres.c:4496
#12 0x0000000000857a54 in BackendRun (port=0x2c39e30) at postmaster.c:4530
#13 0x00000000008573c1 in BackendStartup (port=0x2c39e30) at postmaster.c:4252
#14 0x0000000000853b10 in ServerLoop () at postmaster.c:1745
#15 0x00000000008533c9 in PostmasterMain (argc=1, argv=0x2c0ea10) at postmaster.c:1417
#16 0x0000000000760270 in main (argc=1, argv=0x2c0ea10) at main.c:209

查詢優化后, 查詢執行的調用堆棧:

#0  ExecScanFetch (node=0x14328d8, accessMtd=0x730efe <SeqNext>, recheckMtd=0x730fa8 <SeqRecheck>) at execScan.c:39
#1  0x00000000006f86b3 in ExecScan (node=0x14328d8, accessMtd=0x730efe <SeqNext>, recheckMtd=0x730fa8 <SeqRecheck>) at execScan.c:199
#2  0x0000000000730ff3 in ExecSeqScan (pstate=0x14328d8) at nodeSeqscan.c:112
#3  0x00000000006f4ca9 in ExecProcNodeFirst (node=0x14328d8) at execProcnode.c:463
#4  0x0000000000732343 in ExecProcNode (node=0x14328d8) at ../../../src/include/executor/executor.h:257
#5  0x000000000073248a in ExecSort (pstate=0x14326c8) at nodeSort.c:108
#6  0x00000000006f4ca9 in ExecProcNodeFirst (node=0x14326c8) at execProcnode.c:463
#7  0x0000000000726e97 in ExecProcNode (node=0x14326c8) at ../../../src/include/executor/executor.h:257
#8  0x0000000000727af0 in ExecMergeJoin (pstate=0x14322b8) at nodeMergejoin.c:656
#9  0x00000000006f4ca9 in ExecProcNodeFirst (node=0x14322b8) at execProcnode.c:463
#10 0x00000000006ea204 in ExecProcNode (node=0x14322b8) at ../../../src/include/executor/executor.h:257
#11 0x00000000006ec6bb in ExecutePlan (estate=0x1432078, planstate=0x14322b8, use_parallel_mode=false, operation=CMD_SELECT, sendTuples=true, numberTuples=0, direction=ForwardScanDirection, dest=0x1423f98, execute_once=true) at execMain.c:1551
#12 0x00000000006ea76a in standard_ExecutorRun (queryDesc=0x136dfc8, direction=ForwardScanDirection, count=0, execute_once=true) at execMain.c:361
#13 0x00000000006ea602 in ExecutorRun (queryDesc=0x136dfc8, direction=ForwardScanDirection, count=0, execute_once=true) at execMain.c:305
#14 0x000000000090c03e in PortalRunSelect (portal=0x13ad5d8, forward=true, count=0, dest=0x1423f98) at pquery.c:921
#15 0x000000000090bd2d in PortalRun (portal=0x13ad5d8, count=9223372036854775807, isTopLevel=true, run_once=true, dest=0x1423f98, altdest=0x1423f98, qc=0x7ffff3ea58b0)at pquery.c:765
#16 0x0000000000905d39 in exec_simple_query (query_string=0x134a598 "SELECT * FROM  t1 LEFT JOIN t2 ON t2.a = t1.a WHERE t2.b < 5;") at postgres.c:1214
#17 0x000000000090a0ef in PostgresMain (argc=1, argv=0x7ffff3ea5b40, dbname=0x13775d8 "d1", username=0x1345a48 "kevin") at postgres.c:4496
#18 0x0000000000857a54 in BackendRun (port=0x136f010) at postmaster.c:4530
#19 0x00000000008573c1 in BackendStartup (port=0x136f010) at postmaster.c:4252
#20 0x0000000000853b10 in ServerLoop () at postmaster.c:1745
#21 0x00000000008533c9 in PostmasterMain (argc=1, argv=0x1343a00) at postmaster.c:1417
#22 0x0000000000760270 in main (argc=1, argv=0x1343a00) at main.c:209

五. 查詢計劃分析

PostgreSQL: Documentation: 16: 14.1.?Using EXPLAIN

explain參數:

explain [ ( option [,...] ) ] statement
explain [ analyze ] [ verbose ] statementoption選項有:
analyze [ boolean ]                     //會實際執行SQL,并返回SQL實際執行的相關統計信息
verbose [ boolean ]                     //顯示執行計劃的附加信息
costs [ boolean ]                       //默認開啟,顯示每個計劃節點的啟動成本、總成本,預計返回行數,預估返回結果集每行平均寬度
buffers [ boolean ]                     //顯示緩沖區使用信息
format [ text | xml | json | yaml ]     //執行計劃執行輸出格式

例子:

d1=# EXPLAIN ANALYZE VERBOSE      
d1-# SELECT * FROM  t1 LEFT JOIN t2 ON t2.a = t1.a WHERE t2.b < 5;
***(Single step mode: verify command)*******************************************
explain analyze verbose    
SELECT * FROM  t1 LEFT JOIN t2 ON t2.a = t1.a WHERE t2.b < 5;
***(press return to proceed or enter x and return to cancel)********************QUERY PLAN                                                     
-------------------------------------------------------------------------------------------------------------------Merge Join  (cost=232.74..364.14 rows=8509 width=16) (actual time=0.027..0.030 rows=1 loops=1)Output: t1.a, t1.b, t2.a, t2.bMerge Cond: (t2.a = t1.a)->  Sort  (cost=74.23..76.11 rows=753 width=8) (actual time=0.018..0.018 rows=2 loops=1)Output: t2.a, t2.bSort Key: t2.aSort Method: quicksort  Memory: 25kB->  Seq Scan on public.t2  (cost=0.00..38.25 rows=753 width=8) (actual time=0.009..0.011 rows=2 loops=1)Output: t2.a, t2.bFilter: (t2.b < 5)->  Sort  (cost=158.51..164.16 rows=2260 width=8) (actual time=0.005..0.006 rows=2 loops=1)Output: t1.a, t1.bSort Key: t1.aSort Method: quicksort  Memory: 25kB->  Seq Scan on public.t1  (cost=0.00..32.60 rows=2260 width=8) (actual time=0.002..0.003 rows=2 loops=1)Output: t1.a, t1.bPlanning Time: 0.177 msExecution Time: 0.064 ms
(18 rows)

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/10209.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/10209.shtml
英文地址,請注明出處:http://en.pswp.cn/web/10209.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

MongoDB聚合運算符:$trim

MongoDB聚合運算符&#xff1a;$trim 文章目錄 MongoDB聚合運算符&#xff1a;$trim語法使用空白字符 舉例 $trim用來刪除字符串開頭和結尾的空白字符&#xff08;包括空值&#xff09;或指定字符。 語法 { $trim: { input: <string>, chars: <string> } }input&…

react經驗15:拖拽排序組件dnd-kit的使用經驗

應用場景 列表中的成員可鼠標拖拽改變順序 實施步驟 前置引入 import type { DragEndEvent } from dnd-kit/core import { DndContext } from dnd-kit/core import {arrayMove,/*垂直列表使用verticalListSortingStrategy,橫向列表使用horizontalListSortingStrategy*/vert…

springboot引入security,測試接口報Unauthorized

1、報錯截圖 2、當前項目pom文件引入security <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-security</artifactId><version>2.2.2.RELEASE</version> </dependency> 3、解決…

數據結構之圖——探索圖論的奧秘

前言 在這篇文章中&#xff0c;我們一起來看看我們生活中都會用到&#xff0c;但卻不那么熟悉的數據結構——圖&#xff08;英語&#xff1a;graph&#xff09;。我們看下百科定義&#xff1a; 在計算機科學中&#xff0c;圖&#xff08;英語&#xff1a;graph&#xff09;是一…

計算機畢業設計 | vue+springboot汽車銷售管理系統(附源碼)

1&#xff0c;項目介紹 本項目基于spring boot以及Vue開發&#xff0c;前端實現基于PanJiaChen所提供的開源后臺項目vue-element-admin改造。 針對汽車銷售提供客戶信息、車輛信息、訂單信息、銷售人員管理、財務報表等功能&#xff0c;提供經理和銷售兩種角色進行管理。 2&…

某MBTI性格測試系統后臺Getshell

在淘寶購買了性格測試系統源代碼進行環境部署,后進行滲透測試 淘寶源碼鏈接:https://item.taobao.com/item.htm?ftt&id790798788255 (自己學習(代碼審計、算法、環境搭建)知識技能提升) 環境準備 集成環境選的是小皮 phpstudy 創建網站,將源代碼放入網站根目錄配置好數據…

Doris【部署 01】Linux部署MPP數據庫Doris穩定版(下載+安裝+連接+測試)

本次安裝測試的為穩定版2.0.8官方文檔 https://doris.apache.org/zh-CN/docs/2.0/get-starting/quick-start 這個簡短的指南將告訴你如何下載 Doris 最新穩定版本&#xff0c;在單節點上安裝并運行它&#xff0c;包括創建數據庫、數據表、導入數據及查詢等。 Linux部署穩定版Do…

ElasticSearch的python api以及dev tool方式的基本操作

一、環境要求 根據es服務器版本&#xff0c;下載es的python api包&#xff0c;我們這里的環境為&#xff1a; python3.8, 下載的elastic search版本為7.6.0&#xff0c;安裝方式&#xff1a; pip install elasticsearch7.6.0二、es操作及python代碼 1、獲取es實例&#xff0…

LeetCode 每日一題 2024/5/6-2024/5/12

記錄了初步解題思路 以及本地實現代碼&#xff1b;并不一定為最優 也希望大家能一起探討 一起進步 目錄 5/6 741. 摘櫻桃5/7 1463. 摘櫻桃 II5/8 2079. 給植物澆水5/9 2105. 給植物澆水 II5/10 2960. 統計已測試設備5/11 2391. 收集垃圾的最少總時間5/12 5/6 741. 摘櫻桃 從起點…

當下是風口的熱門兼職副業,月入3萬問題不大,附保姆教程!

近年來&#xff0c;短視頻行業呈現出迅猛的發展勢頭&#xff0c;已經成為當下最受歡迎的一種形式。甚至連曾經的電商巨頭京東也開始積極布局這一領域&#xff0c;投入巨資20億元進行深入耕耘。 周周近財&#xff1a;讓網絡小白少花冤枉錢&#xff0c;賺取第一桶金 不知道您是…

第 8 章 機器人底盤Arduino端入口(自學二刷筆記)

重要參考&#xff1a; 課程鏈接:https://www.bilibili.com/video/BV1Ci4y1L7ZZ 講義鏈接:Introduction Autolabor-ROS機器人入門課程《ROS理論與實踐》零基礎教程 8.4.2 底盤實現_01Arduino端入口 ros_arduino_bridge/ros_arduino_firmware/src/libraries/ROSArduinoBridge…

Android APP讀寫外置SD卡無權限 java.io.IOException: Permission denied

在物聯網應用里&#xff0c;app需要對掛載SD卡讀寫文件&#xff0c;從 Android 4.4&#xff08;KitKat&#xff09;版本開始&#xff0c;Google 引入了一項名為 "Storage Access Framework" 的新功能&#xff0c;該功能限制了應用對外部存儲的直接讀寫權限,要不然就是…

引入Minio

前置條件 官網&#xff1a;https://www.minio.org.cn/download.shtml#/kubernetes 命令 # 查看系統上的網絡連接和監聽端口信息 netstat -tpnl # 檢查系統的指定端口占用情況 sudo netstat -tuln | grep 9000systemctl status firewalld # 臨時關閉 systemctl stop firewall…

生信人寫程序1. Perl語言模板及配置

生物信息領域常用語言 個人認為&#xff1a;是否能熟悉使用Shell(項目流程搭建)R(數據統計與可視化)Perl/Python/Java…(膠水語言&#xff0c;數據格式轉換&#xff0c;軟件間銜接)三門語言是一位合格生物信息工程師的標準。 生物信息常用語言非常廣泛&#xff0c;我常用的有…

在macOS中開發的Django項目部署到局域網的Win10服務器上

由于windows10是日常辦公電腦&#xff0c;沒有服務器基本環境&#xff0c;部署工程耗費不少時間&#xff0c;記錄一下。 1、安裝Python 訪問Python官方下載頁面&#xff1a;Python Downloads&#xff0c;下載適用于Windows的安裝程序并按照提示進行安裝。開發環境python版本是…

Python可以自學但是千萬不要亂學,避免“埋頭苦學”的陷阱!

前言 Python可以自學但是千萬不要亂學&#xff01; 歸根結底因為學習是個反人性的過程&#xff01; 復盤沒學下去的網課&#xff0c;都有以下特點&#xff1a; &#x1f605; 臣妾聽不懂啊&#xff01; 初次接觸編程遇到太多抽象高深的概念&#xff0c;不了解老師口中的一個…

基于51單片機的二氧化碳檢測及調節系統仿真

基于51單片機的二氧化碳檢測及調節系統 &#xff08;仿真&#xff0b;程序&#xff09; 功能介紹 具體功能&#xff1a; 1.二氧化碳傳感器測得二氧化碳數據后經過單片機處理。 2.LCD1602實時顯示&#xff0c;第一行顯示測得的濃度值&#xff0c;第二行顯示報警閾值。 3.測…

棱鏡七彩參編《網絡安全技術 軟件供應鏈安全要求》國家標準發布

據全國標準信息公共服務平臺消息顯示&#xff0c;《網絡安全技術 軟件供應鏈安全要求》&#xff08;GB/T 43698-2024&#xff09;國家標準已于2024年4月25日正式發布&#xff0c;并將于2024年11月1日正式實施。棱鏡七彩作為主要編制單位之一參與該國家標準的編制&#xff0c;為…

Taro 快速開始

大家好我是蘇麟 , 今天聊聊Trao. 官網 : Taro 介紹 | Taro 文檔 (jd.com) 點擊快速開始 全局安裝 CLI 初始化一個項目 選擇配置 : 根據自己需求選擇 安裝失敗先不用管 , 用前端工具打開項目 npm install 安裝 , 顯示安裝失敗 怎么解決 ? : 查看報錯信息 百度 , 問 AI 工具 運…

算法練習第六十天|84. 柱狀圖中最大的矩形

84. 柱狀圖中最大的矩形 柱狀圖中最大的矩形 class Solution {public int largestRectangleArea(int[] heights) {int[] newHeight new int[heights.length 2];System.arraycopy(heights, 0, newHeight, 1, heights.length);newHeight[heights.length1] 0;newHeight[0] 0;…