本文主要是介绍postgres源码解析54 Brin Index--1,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 */
}
这篇关于postgres源码解析54 Brin Index--1的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!