当前位置: 首页 > news >正文

PostgreSQL源码分析——物化视图

我们前面分析完视图后,这里再继续分析一下物化视图,其实现原理是不相同的,需要注意,物化视图等于是将返回的结果集缓存起来,而视图是查询重写,结果需要重新进行计算。

create materialized view matvt1 as select * from t1

语法解析部分

主流程如下:

exec_simple_query
--> pg_parse_query--> raw_parser--> base_yyparse
--> pg_analyze_and_rewrite--> parse_analyze--> transformStmt--> transformCreateTableAsStmt--> transformStmt   // 对查询语句进行语义分析,将其转换为查询树Query--> pg_rewrite_query
--> pg_plan_queries

定义物化视图的语法如下:

/*******************************************************************************		QUERY :*				CREATE MATERIALIZED VIEW relname AS SelectStmt******************************************************************************/
CreateMatViewStmt:CREATE OptNoLog MATERIALIZED VIEW create_mv_target AS SelectStmt opt_with_data{CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);  ctas->query = $7;ctas->into = $5;ctas->relkind = OBJECT_MATVIEW;ctas->is_select_into = false;ctas->if_not_exists = false;/* cram additional flags into the IntoClause */$5->rel->relpersistence = $2;$5->skipData = !($8);$$ = (Node *) ctas;}

创建物化视图的语法,抽象语法树表示CreateTableAsStmt,创建物化视图的流程与CREATE TABLE AS 相同,等于新创建一个表(UNLOGGED TABLE),保存查询到的结果集。可以看到SELECT INTO,与CREATE TABLE AS也是用此进行表示。

/* ----------------------*		CREATE TABLE AS Statement (a/k/a SELECT INTO)** A query written as CREATE TABLE AS will produce this node type natively.* A query written as SELECT ... INTO will be transformed to this form during* parse analysis.* A query written as CREATE MATERIALIZED view will produce this node type,* during parse analysis, since it needs all the same data.** The "query" field is handled similarly to EXPLAIN, though note that it* can be a SELECT or an EXECUTE, but not other DML statements.* ----------------------*/
typedef struct CreateTableAsStmt
{NodeTag		type;Node	   *query;			/* the query (see comments above) */IntoClause *into;			/* destination table */ObjectType	relkind;		/* OBJECT_TABLE or OBJECT_MATVIEW */bool		is_select_into; /* it was written as SELECT INTO */bool		if_not_exists;	/* just do nothing if it already exists? */
} CreateTableAsStmt;/** IntoClause - target information for SELECT INTO, CREATE TABLE AS, and* CREATE MATERIALIZED VIEW** For CREATE MATERIALIZED VIEW, viewQuery is the parsed-but-not-rewritten* SELECT Query for the view; otherwise it's NULL.  (Although it's actually* Query*, we declare it as Node* to avoid a forward reference.)*/
typedef struct IntoClause
{NodeTag		type;RangeVar   *rel;			/* target relation name */List	   *colNames;		/* column names to assign, or NIL */char	   *accessMethod;	/* table access method */List	   *options;		/* options from WITH clause */OnCommitAction onCommit;	/* what do we do at COMMIT? */char	   *tableSpaceName; /* table space to use, or NULL */Node	   *viewQuery;		/* materialized view's SELECT query */bool		skipData;		/* true for WITH NO DATA */
} IntoClause;

执行部分——创建物化视图

主流程如下:

exec_simple_query
--> pg_parse_query
--> pg_analyze_and_rewrite
--> pg_plan_queries
--> PortalStart
--> PortalRun--> PortalRunUtility    // Execute a utility statement inside a portal.--> ProcessUtility--> standard_ProcessUtility--> ProcessUtilitySlow/** 执行步骤:*         1. 创建表,准备存储结果集*         2. 查询重写(物化视图中的查询语句)*         3. 生成查询的执行计划*         4. 执行获取查询语句的结果集*/--> ExecCreateTableAs  // Create the tuple receiver object and insert info it will need-->  CreateIntoRelDestReceiver  // 结果集输入到IntoRel中,新建的表中--> QueryRewrite--> pg_plan_query--> standard_planner--> subquery_planner--> grouping_planner--> query_planner--> make_one_rel--> create_plan--> create_scan_plan--> CreateQueryDesc  /* Create a QueryDesc, redirecting output to our tuple receiver */--> ExecutorStart--> ExecutorRun--> standard_ExecutorRun// 1. 建表--> intorel_startup--> create_ctas_internal    //Actually create the target table--> DefineRelation // 建表--> heap_create_with_catalog--> heap_create--> StoreViewQuery  // Use the rules system to store the query for the view.--> UpdateRangeTableOfViewParse --> DefineViewRules--> DefineQueryRewrite  // Set up the ON SELECT rule.--> InsertRule // 插入的规则,重写为新的物化表,并不是源表--> SetMatViewPopulatedState// 2. 执行查询语句,结果集存入物化的表中--> ExecutePlan--> ExecScan    // 扫描获取tuple--> ExecScanFetch--> SeqNext--> table_beginscan--> intorel_receive  // receive one tuple--> table_tuple_insert  // 将查询到的tuple slot插入到创建的表中--> heapam_tuple_insert--> ExecFetchSlotHeapTuple--> tts_buffer_heap_materialize--> heap_copytuple--> tts_buffer_heap_get_heap_tuple--> heap_insert // 插入到表中,找到指定的page,插入tuple。--> heap_prepare_insert--> RelationPutHeapTuple    --> ExecutorEnd--> PortalDrop

对于结果集中如何存入物化的新表中,可查看dest.c、createas.c等源码,查询到的结果可以按照需求发送到不同的地方,可查看下面的枚举,可以看到有个DestIntoRel的值,即使将结果send to relation

/* ----------------*		CommandDest is a simplistic means of identifying the desired*		destination.  Someday this will probably need to be improved.** Note: only the values DestNone, DestDebug, DestRemote are legal for the* global variable whereToSendOutput.   The other values may be used* as the destination for individual commands.* ----------------*/
typedef enum
{DestNone,					/* results are discarded */DestDebug,					/* results go to debugging output */DestRemote,					/* results sent to frontend process */DestRemoteExecute,			/* sent to frontend, in Execute command */DestRemoteSimple,			/* sent to frontend, w/no catalog access */DestSPI,					/* results sent to SPI manager */DestTuplestore,				/* results sent to Tuplestore */DestIntoRel,				/* results sent to relation (SELECT INTO) */DestCopyOut,				/* results sent to COPY TO code */DestSQLFunction,			/* results sent to SQL-language func mgr */DestTransientRel,			/* results sent to transient relation */DestTupleQueue				/* results sent to tuple queue */
} CommandDest;

其中还有一个非常重要的函数需要列出来CreateIntoRelDestReceiver,查询返回的结果输入到IntoClause节点指定的表中。

/** CreateIntoRelDestReceiver -- create a suitable DestReceiver object** intoClause will be NULL if called from CreateDestReceiver(), in which* case it has to be provided later.  However, it is convenient to allow* self->into to be filled in immediately for other callers.*/
DestReceiver *
CreateIntoRelDestReceiver(IntoClause *intoClause)
{DR_intorel *self = (DR_intorel *) palloc0(sizeof(DR_intorel));self->pub.receiveSlot = intorel_receive;self->pub.rStartup = intorel_startup;self->pub.rShutdown = intorel_shutdown;self->pub.rDestroy = intorel_destroy;self->pub.mydest = DestIntoRel;self->into = intoClause;/* other private fields will be set during intorel_startup */return (DestReceiver *) self;
}typedef struct
{DestReceiver pub;			/* publicly-known function pointers */IntoClause *into;			/* target relation specification *//* These fields are filled by intorel_startup: */Relation	rel;			/* relation to write to */ObjectAddress reladdr;		/* address of rel, for ExecCreateTableAs */CommandId	output_cid;		/* cmin to insert in output tuples */int			ti_options;		/* table_tuple_insert performance options */BulkInsertState bistate;	/* bulk insert state */
} DR_intorel;

最后我们看一下系统表pg_class、pg_rewrite中的相关信息:

-- 物化视图matvt1
postgres@postgres=# select oid,relname,relkind,relhasrules from pg_class where relname='matvt1';
-[ RECORD 1 ]-------
oid         | 16391
relname     | matvt1
relkind     | m
relhasrules | t
-- 表t1
postgres@postgres=# select oid,relname,relkind,relhasrules,relrewrite from pg_class where relname='t1';
-[ RECORD 1 ]------
oid         | 16384     -- 表OID
relname     | t1        -- 表名
relkind     | r         -- 表示是普通表
relhasrules | f         -- 表是否定义了规则
relrewrite  | 0
-- 查看系统表pg_rewrite,查看插入的规则
postgres@postgres=# select * from pg_rewrite order by oid desc limit 1;
-[ RECORD 1 ]
oid        | 16394
rulename   | _RETURN
ev_class   | 16391
ev_type    | 1
ev_enabled | O
is_instead | t
ev_qual    | <>
ev_action  | ({QUERY :commandType 1 :querySource 0 :canSetTag true :utilityStmt <> :resultRelation 0 :hasAggs false :hasWindowFuncs false :hasTargetSRFs false :hasSubLinks false :hasDistinctOn false :hasRecursive false :hasModifyingCTE false :hasForUpdate false :hasRowSecurity false :cteList <> :rtable ({RTE :alias {ALIAS :aliasname old :colnames <>} :eref {ALIAS :aliasname old :colnames ("a" "b")} :rtekind 0 :relid 16391 :relkind m :rellockmode 1 :tablesample <> :lateral false :inh false :inFromCl false :requiredPerms 0 :checkAsUser 0 :selectedCols (b) :insertedCols (b) :updatedCols (b) :extraUpdatedCols (b) :securityQuals <>} {RTE :alias {ALIAS :aliasname new :colnames <>} :eref {ALIAS :aliasname new :colnames ("a" "b")} :rtekind 0 :relid 16391 :relkind m :rellockmode 1 :tablesample <> :lateral false :inh false :inFromCl false :requiredPerms 0 :checkAsUser 0 :selectedCols (b) :insertedCols (b) :updatedCols (b) :extraUpdatedCols (b) :securityQuals <>} {RTE :alias <> :eref {ALIAS :aliasname t1 :colnames ("a" "b")} :rtekind 0 :relid 16384 :relkind r :rellockmode 1 :tablesample <> :lateral false :inh true :inFromCl true :requiredPerms 2 :checkAsUser 0 :selectedCols (b 8 9) :insertedCols (b) :updatedCols (b) :extraUpdatedCols (b) :securityQuals <>}) :jointree {FROMEXPR :fromlist ({RANGETBLREF :rtindex 3}) :quals <>} :targetList ({TARGETENTRY :expr {VAR :varno 3 :varattno 1 :vartype 23 :vartypmod -1 :varcollid 0 :varlevelsup 0 :varnosyn 3 :varattnosyn 1 :location 42} :resno 1 :resname a :ressortgroupref 0 :resorigtbl 16384 :resorigcol 1 :resjunk false} {TARGETENTRY :expr {VAR :varno 3 :varattno 2 :vartype 23 :vartypmod -1 :varcollid 0 :varlevelsup 0 :varnosyn 3 :varattnosyn 2 :location 42} :resno 2 :resname b :ressortgroupref 0 :resorigtbl 16384 :resorigcol 2 :resjunk false}) :override 0 :onConflict <> :returningList <> :groupClause <> :groupingSets <> :havingQual <> :windowClause <> :distinctClause <> :sortClause <> :limitOffset <> :limitCount <> :limitOption 0 :rowMarks <> :setOperations <> :constraintDeps <> :withCheckOptions <>})

物化视图与普通视图不同的地方在于,创建物化视图时,要建立一张物理表存储查询语句的结果集。

相关文章:

PostgreSQL源码分析——物化视图

我们前面分析完视图后&#xff0c;这里再继续分析一下物化视图&#xff0c;其实现原理是不相同的&#xff0c;需要注意&#xff0c;物化视图等于是将返回的结果集缓存起来&#xff0c;而视图是查询重写&#xff0c;结果需要重新进行计算。 create materialized view matvt1 as…...

操作系统入门系列-MIT6.828(操作系统工程)学习笔记(七)---- 系统调用函数与GDB(Lab: system calls)

系列文章目录 操作系统入门系列-MIT6.828&#xff08;操作系统工程&#xff09;学习笔记&#xff08;一&#xff09;---- 操作系统介绍与接口示例 操作系统入门系列-MIT6.828&#xff08;操作系统工程&#xff09;学习笔记&#xff08;二&#xff09;---- 课程实验环境搭建&am…...

ORA-12560: TNS:协议适配器错误

项目场景&#xff1a; 由于最近一直没有连接oracle&#xff0c;然后之前windows也是正常可以启动oracle&#xff0c;正常连接。无论是SQL Developer还是SQL PLUS命令&#xff0c;都能正常连接和操作。 问题描述 这两天刚好用SQL Developer工具连接&#xff0c;然后报错&#…...

不容小觑的“白纸黑字”:银行重空凭证的风险与防控

一、定义与重要性 定义&#xff1a; 银行重空凭证&#xff0c;也称为重要空白凭证&#xff0c;是银行专业术语&#xff0c;指银行印制的无面额、经银行或单位填写金额并签章后&#xff0c;即具有支取款项效力的空白凭证。 重要性&#xff1a; 它是银行资金支付的重要工具&a…...

30v-180V降3.3V100mA恒压WT5107

30v-180V降3.3V100mA恒压WT5107 WT5107是一款恒压单片机供电芯片&#xff0c;它可以30V-180V直流电转换成稳定的3.3V直流电&#xff08;最大输出电流300mA&#xff09;&#xff0c;为各种单片机供电。WT5107的应用也非常广泛。它可以用于智能家居、LED照明、电子玩具等领域。比…...

Spring Boot 和 Spring Cloud 的区别及选型

Spring Boot 和 Spring Cloud 是现代 Java 开发中非常流行的两个框架&#xff0c;它们分别解决了不同层次的问题。本文将详细介绍 Spring Boot 和 Spring Cloud 的区别&#xff0c;以及在不同场景下如何选择合适的技术。 Spring Boot 什么是 Spring Boot Spring Boot 是一个…...

【神经网络】图像的数字视角

文章目录 图像的数字视角引言直观感受内在剖析图像常用函数图像三维层次 经验总结 图像的数字视角 引言 在机器视觉和目标识别领域&#xff0c;需要处理的对象都是图像&#xff0c;但这些领域的模型都是针对数值进行训练的&#xff0c;那么图像和数值之间是什么关系呢?答案是…...

ChatGPT的问题与回复的内容导出(Chorme)

我给出两种方式&#xff0c;第一种方式无使用要求&#xff0c;第二种方式必须安装Chorme 个人更推荐第二种方式 第一种方式&#xff1a;使用chatgpt自带的数据导出 缺点&#xff1a;会将当前未归档的所有聊天记录导出&#xff0c;发送到你的电子邮箱中 第二种方式&#xff1a…...

游戏开发中的坑之十四 photoshop的javascript脚本批量修改分辨率

原因&#xff1a;美术提交大量2048x2048的贴图&#xff0c;导致工程臃肿。 方案&#xff1a;使用photoshop的javascript脚本批量把指定的文件夹以及所有子文件夹的贴图进行压缩。 脚本中指定针对2048x2048的贴图进行处理。 // Photoshop JavaScript to resize TGA images with…...

leetcode打卡#day45 携带研究材料(第七期模拟笔试)、518. 零钱兑换 II、377. 组合总和 Ⅳ、爬楼梯(第八期模拟笔试)

携带研究材料&#xff08;第七期模拟笔试&#xff09; #include<iostream> #include<algorithm> #include<vector>using namespace std;int main() {int N, V;cin >> N >> V;vector<int> weights(N1);vector<int> values(V1);int w…...

Vite+Vue3安装且自动按需引入Element Plus组件库

一&#xff0c;安装Element Plus npm install element-plus //node环境16二&#xff0c;安装插件 npm install unplugin-auto-import unplugin-vue-components -D三&#xff0c;配置vite.config.ts文件 //按需引入element-plus组件 import AutoImport from unplugin-auto-i…...

敬酒词大全绝对实用 万能敬酒词

举杯共饮&#xff0c;友情初识&#xff1b;再续一杯&#xff0c;情深似海&#xff0c;朋友相伴人生路更宽。酒逢知己千杯少&#xff0c;一饮而尽显真意&#xff0c;浅尝则留情&#xff0c;深情则尽欢。友情到深处&#xff0c;千杯不倒&#xff0c;若情浅则饮少&#xff0c;醉卧…...

【Java】已解决com.mysql.cj.jdbc.exceptions.CommunicationsException异常

文章目录 一、分析问题背景二、可能出错的原因三、错误代码示例四、正确代码示例五、注意事项 已解决com.mysql.cj.jdbc.exceptions.CommunicationsException异常 一、分析问题背景 com.mysql.cj.jdbc.exceptions.CommunicationsException是Java程序在使用MySQL Connector/J与…...

Leetcode 76. 最小覆盖子串

76. 最小覆盖子串 - 力扣&#xff08;LeetCode&#xff09; class Solution {/**也是滑动窗口&#xff0c;思路简单&#xff0c;但实现起来容易出错。一个tmap记录目标串t的各个字符出现的次数&#xff1b;一个smap记录原串的某个滑动窗口里字符出现次数。两个指针left&#x…...

JAVAWEB--Mybatis03

Mybatis映射 什么是映射器 MyBatis的映射器就是用来解决这一问题&#xff0c;映射器其实是一个Interface接口,我们通过编写简单的映射器接口&#xff0c;就可以将我们之前在Dao中做的重复的&#xff0c;看起来比较低级的代码给替换掉。也就是说我们以后不用向之前那样写代码&…...

论文学习_Fuzz4All: Universal Fuzzing with Large Language Models

论文名称发表时间发表期刊期刊等级研究单位Fuzz4All: Universal Fuzzing with Large Language Models2024年arXiv-伊利诺伊大学 0.摘要 研究背景模糊测试再发现各种软件系统中的错误和漏洞方面取得了巨大的成功。以编程或形式语言作为输入的被测系统&#xff08;SUT&#xff…...

元数据相关资料整理 metadata

目录 定义和特点 关注点 流程 使用场景 元数据影响分析 元数据冷热度分析 元数据关联度分析 血缘分析 数据地图 元数据接口 相关产品的架构图 定义和特点 元数据&#xff08;Metadata&#xff09;是指关于数据的数据&#xff0c;或者说是描述数据的数据。它提供了一…...

【Android面试八股文】谈一谈你对http和https的关系理解

文章目录 HTTPHTTPSSSL/TLS协议HTTPS加密、解密流程HTTP 和 HTTPS 的关系具体的差异实际应用总结扩展阅读HTTP(HyperText Transfer Protocol)和HTTPS(HyperText Transfer Protocol Secure)是用于在网络上进行通信的两种协议。 它们在很多方面是相似的,但关键的区别在于安全…...

Vue3 中 setup 函数与 script setup 用法总结

在 Vue3 中&#xff0c;setup 函数和 script setup 是两种新引入的编写组件的方式&#xff0c;它们都是 Composition API 的一部分。 setup 函数: setup 函数是一个新的组件选项&#xff0c;它作为在组件内使用 Composition API 的入口。在 setup 函数中&#xff0c;我们可以定…...

Springboot 开发之任务调度框架(一)Quartz 简介

一、引言 常见的定时任务框架有 Quartz、elastic-job、xxl-job等等&#xff0c;本文主要介绍 Spirng Boot 集成 Quartz 定时任务框架。 二、Quartz 简介 Quartz 是一个功能强大且灵活的开源作业调度库&#xff0c;广泛用于 Java 应用中。它允许开发者创建复杂的调度任务&…...

SCAU期末笔记 - 数据分析与数据挖掘题库解析

这门怎么题库答案不全啊日 来简单学一下子来 一、选择题&#xff08;可多选&#xff09; 将原始数据进行集成、变换、维度规约、数值规约是在以下哪个步骤的任务?(C) A. 频繁模式挖掘 B.分类和预测 C.数据预处理 D.数据流挖掘 A. 频繁模式挖掘&#xff1a;专注于发现数据中…...

UDP(Echoserver)

网络命令 Ping 命令 检测网络是否连通 使用方法: ping -c 次数 网址ping -c 3 www.baidu.comnetstat 命令 netstat 是一个用来查看网络状态的重要工具. 语法&#xff1a;netstat [选项] 功能&#xff1a;查看网络状态 常用选项&#xff1a; n 拒绝显示别名&#…...

JDK 17 新特性

#JDK 17 新特性 /**************** 文本块 *****************/ python/scala中早就支持&#xff0c;不稀奇 String json “”" { “name”: “Java”, “version”: 17 } “”"; /**************** Switch 语句 -> 表达式 *****************/ 挺好的&#xff…...

C++八股 —— 单例模式

文章目录 1. 基本概念2. 设计要点3. 实现方式4. 详解懒汉模式 1. 基本概念 线程安全&#xff08;Thread Safety&#xff09; 线程安全是指在多线程环境下&#xff0c;某个函数、类或代码片段能够被多个线程同时调用时&#xff0c;仍能保证数据的一致性和逻辑的正确性&#xf…...

在Ubuntu24上采用Wine打开SourceInsight

1. 安装wine sudo apt install wine 2. 安装32位库支持,SourceInsight是32位程序 sudo dpkg --add-architecture i386 sudo apt update sudo apt install wine32:i386 3. 验证安装 wine --version 4. 安装必要的字体和库(解决显示问题) sudo apt install fonts-wqy…...

[免费]微信小程序问卷调查系统(SpringBoot后端+Vue管理端)【论文+源码+SQL脚本】

大家好&#xff0c;我是java1234_小锋老师&#xff0c;看到一个不错的微信小程序问卷调查系统(SpringBoot后端Vue管理端)【论文源码SQL脚本】&#xff0c;分享下哈。 项目视频演示 【免费】微信小程序问卷调查系统(SpringBoot后端Vue管理端) Java毕业设计_哔哩哔哩_bilibili 项…...

STM32HAL库USART源代码解析及应用

STM32HAL库USART源代码解析 前言STM32CubeIDE配置串口USART和UART的选择使用模式参数设置GPIO配置DMA配置中断配置硬件流控制使能生成代码解析和使用方法串口初始化__UART_HandleTypeDef结构体浅析HAL库代码实际使用方法使用轮询方式发送使用轮询方式接收使用中断方式发送使用中…...

PHP 8.5 即将发布:管道操作符、强力调试

前不久&#xff0c;PHP宣布了即将在 2025 年 11 月 20 日 正式发布的 PHP 8.5&#xff01;作为 PHP 语言的又一次重要迭代&#xff0c;PHP 8.5 承诺带来一系列旨在提升代码可读性、健壮性以及开发者效率的改进。而更令人兴奋的是&#xff0c;借助强大的本地开发环境 ServBay&am…...

Spring AI Chat Memory 实战指南:Local 与 JDBC 存储集成

一个面向 Java 开发者的 Sring-Ai 示例工程项目&#xff0c;该项目是一个 Spring AI 快速入门的样例工程项目&#xff0c;旨在通过一些小的案例展示 Spring AI 框架的核心功能和使用方法。 项目采用模块化设计&#xff0c;每个模块都专注于特定的功能领域&#xff0c;便于学习和…...

提升移动端网页调试效率:WebDebugX 与常见工具组合实践

在日常移动端开发中&#xff0c;网页调试始终是一个高频但又极具挑战的环节。尤其在面对 iOS 与 Android 的混合技术栈、各种设备差异化行为时&#xff0c;开发者迫切需要一套高效、可靠且跨平台的调试方案。过去&#xff0c;我们或多或少使用过 Chrome DevTools、Remote Debug…...