postgres源码解析54 Brin Index--1

news/2024/7/9 20:31:20 标签: 数据库, 数据库架构, C语言, postgresql, 索引

Brin Index简介

brin index是Block range Index的缩写,顾名思义该索引是指块范围索引,该索引适合在超大表中进行过滤性扫描。基本的思路是追踪heap页域的最大值与最小值,用于过滤不符合条件的数据块。以下图为例,右边的堆表包含三个block,第一个block中有4个tuple,字段上的值分别是1、3、5、7。因此,与这个block相对应的Brin的元组就记录了 block的最小值1,最大值7。同理,2、4、6、8的最小值是2,最大值是8;12、11、14、13的最小值是10,最大值是14。这样就获得了一个 Brin index。
在这里插入图片描述
在执行带有条件的查询时,就可以利用Brin记录的这些信息,找到相应的block,同时滤掉大部分block,从而起到Index的作用。Brin Index是一种非常直观简单的Index。

关键数据结构

在这里插入图片描述

brin索引创建对应的handler,定义索引处理函数和属性

/*
 * BRIN handler function: return IndexAmRoutine with access method parameters
 * and callbacks.
 */
Datum
brinhandler(PG_FUNCTION_ARGS)
{
	IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);

	amroutine->amstrategies = 0;
	amroutine->amsupport = BRIN_LAST_OPTIONAL_PROCNUM;
	amroutine->amcanorder = false;
	amroutine->amcanorderbyop = false;
	amroutine->amcanbackward = false;
	amroutine->amcanunique = false;
	amroutine->amcanmulticol = true;
	amroutine->amoptionalkey = true;
	amroutine->amsearcharray = false;
	amroutine->amsearchnulls = true;
	amroutine->amstorage = true;
	amroutine->amclusterable = false;
	amroutine->ampredlocks = false;
	amroutine->amcanparallel = false;
	amroutine->amcaninclude = false;
	amroutine->amkeytype = InvalidOid;

	amroutine->ambuild = brinbuild;
	amroutine->ambuildempty = brinbuildempty;
	amroutine->aminsert = brininsert;
	amroutine->ambulkdelete = brinbulkdelete;
	amroutine->amvacuumcleanup = brinvacuumcleanup;
	amroutine->amcanreturn = NULL;
	amroutine->amcostestimate = brincostestimate;
	amroutine->amoptions = brinoptions;
	amroutine->amproperty = NULL;
	amroutine->ambuildphasename = NULL;
	amroutine->amvalidate = brinvalidate;
	amroutine->ambeginscan = brinbeginscan;
	amroutine->amrescan = brinrescan;
	amroutine->amgettuple = NULL;
	amroutine->amgetbitmap = bringetbitmap;
	amroutine->amendscan = brinendscan;
	amroutine->ammarkpos = NULL;
	amroutine->amrestrpos = NULL;
	amroutine->amestimateparallelscan = NULL;
	amroutine->aminitparallelscan = NULL;
	amroutine->amparallelrescan = NULL;

	PG_RETURN_POINTER(amroutine);
}

Brin Index页格式

brin文件页有如下三种类型:元数据页,映射页与常规页类型
/* special space on all BRIN pages stores a "type" identifier */
#define		BRIN_PAGETYPE_META			0xF091
#define		BRIN_PAGETYPE_REVMAP		0xF092
#define		BRIN_PAGETYPE_REGULAR		0xF093

每个brin文件页含有一段特殊的空间BrinSpecialSpace,用于记录类型与标识信息
/*
 * Special area of BRIN pages.
 *
 * We define it in this odd way so that it always occupies the last
 * MAXALIGN-sized element of each page.
 */
typedef struct BrinSpecialSpace
{
	uint16		vector[MAXALIGN(1) / sizeof(uint16)];
} BrinSpecialSpace;

/* flags for BrinSpecialSpace */
#define		BRIN_EVACUATE_PAGE			(1 << 0)

通过如下宏定义从BrinSpecialSpace空间中获取页类型与标识信息
#define BrinPageType(page)		\
	(((BrinSpecialSpace *)		\
	  PageGetSpecialPointer(page))->vector[MAXALIGN(1) / sizeof(uint16) - 1])

#define BrinPageFlags(page)		\
	(((BrinSpecialSpace *)		\
	  PageGetSpecialPointer(page))->vector[MAXALIGN(1) / sizeof(uint16) - 2])

brin页类型的判断可通过调用如下宏实现:
#define BRIN_IS_META_PAGE(page) (BrinPageType(page) == BRIN_PAGETYPE_META)
#define BRIN_IS_REVMAP_PAGE(page) (BrinPageType(page) == BRIN_PAGETYPE_REVMAP)
#define BRIN_IS_REGULAR_PAGE(page) (BrinPageType(page) == BRIN_PAGETYPE_REGULAR)

在这里插入图片描述
在这里插入图片描述

void
brin_page_init(Page page, uint16 type)
{
	PageInit(page, BLCKSZ, sizeof(BrinSpecialSpace));
	BrinPageType(page) = type;
}

------------------------------------------------------------------------------------------------------------
void
PageInit(Page page, Size pageSize, Size specialSize)
{
	PageHeader	p = (PageHeader) page;

	specialSize = MAXALIGN(specialSize);

	Assert(pageSize == BLCKSZ);
	Assert(pageSize > specialSize + SizeOfPageHeaderData);

	/* Make sure all fields of page are zero, as well as unused space */
	MemSet(p, 0, pageSize);

	p->pd_flags = 0;
	p->pd_lower = SizeOfPageHeaderData;
	p->pd_upper = pageSize - specialSize;
	p->pd_special = pageSize - specialSize;
	PageSetPageSizeAndVersion(page, pageSize, PG_PAGE_LAYOUT_VERSION);
	/* p->pd_prune_xid = InvalidTransactionId;		done by above MemSet */
}

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

相关文章

JavaScript 中类的使用(笔记)

1. 类的基本定义 class Person {// 构造函数constructor(name, age) {this.name name; // 成员变量this.age age;}// 类方法printInformation(){console.log(name: ${this.name}; age: ${this.age});} }var person new Person("Micheal", 18); console.log(pers…

SpringCloudAlibaba Gateway(一)简单集成

SpringCloudAlibaba Gateway(一)简单集成 随着服务模块的增加&#xff0c;一定会产生多个接口地址&#xff0c;那么客户端调用多个接口只能使用多个地址&#xff0c;维护多个地址是很不方便的&#xff0c;这个时候就需要统一服务地址。同时也可以进行统一认证鉴权的需求。那么服…

什么是跨域(cross-origin)请求,如何解决跨域问题?

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 跨域请求和跨域问题⭐ 解决跨域问题的方法1. CORS&#xff08;跨域资源共享&#xff09;2. JSONP&#xff08;JSON with Padding&#xff09;3. 代理服务器4. WebSocket5. 使用服务器中继 ⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff…

【C语言每日一题】10. 超级玛丽游戏

题目来源&#xff1a;http://noi.openjudge.cn/ch0101/10/ 10 超级玛丽游戏 总时间限制: 1000ms 内存限制: 65536kB 问题描述 超级玛丽是一个非常经典的游戏。请你用字符画的形式输出超级玛丽中的一个场景。 输入 无。 输出 如样例所示。 样例输入 &#xff08;无&…

MySQL创建用户时报错“Your password does not satisfy the current policy requirements“

MySQL创建用户时报错"Your password does not satisfy the current policy requirements" MySQL是一个流行的关系型数据库管理系统&#xff0c;它提供了许多安全性特性&#xff0c;其中之一是密码策略。在创建或更改用户密码时&#xff0c;MySQL会检查密码是否符合当…

无涯教程-JavaScript - NORMSDIST函数

NORMSDIST函数替代Excel 2010中的NORM.S.DIST函数。 描述 该函数返回标准正态累积分布函数。分布的平均值为0(零),标准偏差为1。使用此功能代替标准法线区域的表格。 语法 NORMSDIST (z)争论 Argument描述Required/OptionalZThe value for which you want the distributio…

Windows BAT脚本入门指南

简介 BAT&#xff08;批处理&#xff09;脚本是一种用于自动化Windows操作系统任务的强大工具。无需深入了解编程&#xff0c;您就可以使用BAT脚本来执行各种任务&#xff0c;从简单的文件操作到复杂的系统管理。本文将向您介绍BAT脚本的基本概念&#xff0c;帮助您入门并开始…