BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
/*
|
|
|
|
* brin_revmap.c
|
|
|
|
* Range map for BRIN indexes
|
|
|
|
*
|
|
|
|
* The range map (revmap) is a translation structure for BRIN indexes: for each
|
|
|
|
* page range there is one summary tuple, and its location is tracked by the
|
|
|
|
* revmap. Whenever a new tuple is inserted into a table that violates the
|
|
|
|
* previously recorded summary values, a new tuple is inserted into the index
|
|
|
|
* and the revmap is updated to point to it.
|
|
|
|
*
|
|
|
|
* The revmap is stored in the first pages of the index, immediately following
|
|
|
|
* the metapage. When the revmap needs to be expanded, all tuples on the
|
|
|
|
* regular BRIN page at that block (if any) are moved out of the way.
|
|
|
|
*
|
2019-01-02 18:44:25 +01:00
|
|
|
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
|
|
|
* src/backend/access/brin/brin_revmap.c
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/brin_page.h"
|
|
|
|
#include "access/brin_pageops.h"
|
|
|
|
#include "access/brin_revmap.h"
|
|
|
|
#include "access/brin_tuple.h"
|
|
|
|
#include "access/brin_xlog.h"
|
|
|
|
#include "access/rmgr.h"
|
|
|
|
#include "access/xloginsert.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "storage/bufmgr.h"
|
|
|
|
#include "storage/lmgr.h"
|
|
|
|
#include "utils/rel.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In revmap pages, each item stores an ItemPointerData. These defines let one
|
|
|
|
* find the logical revmap page number and index number of the revmap item for
|
|
|
|
* the given heap block number.
|
|
|
|
*/
|
|
|
|
#define HEAPBLK_TO_REVMAP_BLK(pagesPerRange, heapBlk) \
|
|
|
|
((heapBlk / pagesPerRange) / REVMAP_PAGE_MAXITEMS)
|
|
|
|
#define HEAPBLK_TO_REVMAP_INDEX(pagesPerRange, heapBlk) \
|
|
|
|
((heapBlk / pagesPerRange) % REVMAP_PAGE_MAXITEMS)
|
|
|
|
|
|
|
|
|
|
|
|
struct BrinRevmap
|
|
|
|
{
|
|
|
|
Relation rm_irel;
|
|
|
|
BlockNumber rm_pagesPerRange;
|
Phase 2 of pgindent updates.
Change pg_bsd_indent to follow upstream rules for placement of comments
to the right of code, and remove pgindent hack that caused comments
following #endif to not obey the general rule.
Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using
the published version of pg_bsd_indent, but a hacked-up version that
tried to minimize the amount of movement of comments to the right of
code. The situation of interest is where such a comment has to be
moved to the right of its default placement at column 33 because there's
code there. BSD indent has always moved right in units of tab stops
in such cases --- but in the previous incarnation, indent was working
in 8-space tab stops, while now it knows we use 4-space tabs. So the
net result is that in about half the cases, such comments are placed
one tab stop left of before. This is better all around: it leaves
more room on the line for comment text, and it means that in such
cases the comment uniformly starts at the next 4-space tab stop after
the code, rather than sometimes one and sometimes two tabs after.
Also, ensure that comments following #endif are indented the same
as comments following other preprocessor commands such as #else.
That inconsistency turns out to have been self-inflicted damage
from a poorly-thought-through post-indent "fixup" in pgindent.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
|
|
|
BlockNumber rm_lastRevmapPage; /* cached from the metapage */
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
Buffer rm_metaBuf;
|
|
|
|
Buffer rm_currBuf;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* typedef appears in brin_revmap.h */
|
|
|
|
|
|
|
|
|
|
|
|
static BlockNumber revmap_get_blkno(BrinRevmap *revmap,
|
2015-05-24 03:35:49 +02:00
|
|
|
BlockNumber heapBlk);
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
static Buffer revmap_get_buffer(BrinRevmap *revmap, BlockNumber heapBlk);
|
|
|
|
static BlockNumber revmap_extend_and_get_blkno(BrinRevmap *revmap,
|
|
|
|
BlockNumber heapBlk);
|
|
|
|
static void revmap_physical_extend(BrinRevmap *revmap);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize an access object for a range map. This must be freed by
|
|
|
|
* brinRevmapTerminate when caller is done with it.
|
|
|
|
*/
|
|
|
|
BrinRevmap *
|
2016-04-08 21:36:30 +02:00
|
|
|
brinRevmapInitialize(Relation idxrel, BlockNumber *pagesPerRange,
|
|
|
|
Snapshot snapshot)
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
{
|
|
|
|
BrinRevmap *revmap;
|
|
|
|
Buffer meta;
|
|
|
|
BrinMetaPageData *metadata;
|
2016-04-08 21:30:10 +02:00
|
|
|
Page page;
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
|
|
|
|
meta = ReadBuffer(idxrel, BRIN_METAPAGE_BLKNO);
|
|
|
|
LockBuffer(meta, BUFFER_LOCK_SHARE);
|
2016-04-20 15:31:19 +02:00
|
|
|
page = BufferGetPage(meta);
|
|
|
|
TestForOldSnapshot(snapshot, idxrel, page);
|
2016-04-08 21:30:10 +02:00
|
|
|
metadata = (BrinMetaPageData *) PageGetContents(page);
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
|
|
|
|
revmap = palloc(sizeof(BrinRevmap));
|
|
|
|
revmap->rm_irel = idxrel;
|
|
|
|
revmap->rm_pagesPerRange = metadata->pagesPerRange;
|
|
|
|
revmap->rm_lastRevmapPage = metadata->lastRevmapPage;
|
|
|
|
revmap->rm_metaBuf = meta;
|
|
|
|
revmap->rm_currBuf = InvalidBuffer;
|
|
|
|
|
|
|
|
*pagesPerRange = metadata->pagesPerRange;
|
|
|
|
|
|
|
|
LockBuffer(meta, BUFFER_LOCK_UNLOCK);
|
|
|
|
|
|
|
|
return revmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Release resources associated with a revmap access object.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
brinRevmapTerminate(BrinRevmap *revmap)
|
|
|
|
{
|
|
|
|
ReleaseBuffer(revmap->rm_metaBuf);
|
|
|
|
if (revmap->rm_currBuf != InvalidBuffer)
|
|
|
|
ReleaseBuffer(revmap->rm_currBuf);
|
|
|
|
pfree(revmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extend the revmap to cover the given heap block number.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
brinRevmapExtend(BrinRevmap *revmap, BlockNumber heapBlk)
|
|
|
|
{
|
2015-05-24 03:35:49 +02:00
|
|
|
BlockNumber mapBlk PG_USED_FOR_ASSERTS_ONLY;
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
|
|
|
|
mapBlk = revmap_extend_and_get_blkno(revmap, heapBlk);
|
|
|
|
|
|
|
|
/* Ensure the buffer we got is in the expected range */
|
|
|
|
Assert(mapBlk != InvalidBlockNumber &&
|
|
|
|
mapBlk != BRIN_METAPAGE_BLKNO &&
|
|
|
|
mapBlk <= revmap->rm_lastRevmapPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare to insert an entry into the revmap; the revmap buffer in which the
|
|
|
|
* entry is to reside is locked and returned. Most callers should call
|
|
|
|
* brinRevmapExtend beforehand, as this routine does not extend the revmap if
|
|
|
|
* it's not long enough.
|
|
|
|
*
|
|
|
|
* The returned buffer is also recorded in the revmap struct; finishing that
|
2015-09-05 10:35:49 +02:00
|
|
|
* releases the buffer, therefore the caller needn't do it explicitly.
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
*/
|
|
|
|
Buffer
|
|
|
|
brinLockRevmapPageForUpdate(BrinRevmap *revmap, BlockNumber heapBlk)
|
|
|
|
{
|
|
|
|
Buffer rmBuf;
|
|
|
|
|
|
|
|
rmBuf = revmap_get_buffer(revmap, heapBlk);
|
|
|
|
LockBuffer(rmBuf, BUFFER_LOCK_EXCLUSIVE);
|
|
|
|
|
|
|
|
return rmBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In the given revmap buffer (locked appropriately by caller), which is used
|
|
|
|
* in a BRIN index of pagesPerRange pages per range, set the element
|
|
|
|
* corresponding to heap block number heapBlk to the given TID.
|
|
|
|
*
|
|
|
|
* Once the operation is complete, the caller must update the LSN on the
|
|
|
|
* returned buffer.
|
|
|
|
*
|
|
|
|
* This is used both in regular operation and during WAL replay.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
brinSetHeapBlockItemptr(Buffer buf, BlockNumber pagesPerRange,
|
|
|
|
BlockNumber heapBlk, ItemPointerData tid)
|
|
|
|
{
|
|
|
|
RevmapContents *contents;
|
|
|
|
ItemPointerData *iptr;
|
|
|
|
Page page;
|
|
|
|
|
|
|
|
/* The correct page should already be pinned and locked */
|
2016-04-20 15:31:19 +02:00
|
|
|
page = BufferGetPage(buf);
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
contents = (RevmapContents *) PageGetContents(page);
|
|
|
|
iptr = (ItemPointerData *) contents->rm_tids;
|
|
|
|
iptr += HEAPBLK_TO_REVMAP_INDEX(pagesPerRange, heapBlk);
|
|
|
|
|
2017-04-01 21:10:04 +02:00
|
|
|
if (ItemPointerIsValid(&tid))
|
|
|
|
ItemPointerSet(iptr,
|
|
|
|
ItemPointerGetBlockNumber(&tid),
|
|
|
|
ItemPointerGetOffsetNumber(&tid));
|
|
|
|
else
|
|
|
|
ItemPointerSetInvalid(iptr);
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch the BrinTuple for a given heap block.
|
|
|
|
*
|
2017-05-30 23:32:53 +02:00
|
|
|
* The buffer containing the tuple is locked, and returned in *buf. The
|
|
|
|
* returned tuple points to the shared buffer and must not be freed; if caller
|
|
|
|
* wants to use it after releasing the buffer lock, it must create its own
|
|
|
|
* palloc'ed copy. As an optimization, the caller can pass a pinned buffer
|
|
|
|
* *buf on entry, which will avoid a pin-unpin cycle when the next tuple is on
|
|
|
|
* the same page as a previous one.
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
*
|
|
|
|
* If no tuple is found for the given heap range, returns NULL. In that case,
|
2017-05-30 23:32:53 +02:00
|
|
|
* *buf might still be updated (and pin must be released by caller), but it's
|
|
|
|
* not locked.
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
*
|
|
|
|
* The output tuple offset within the buffer is returned in *off, and its size
|
|
|
|
* is returned in *size.
|
|
|
|
*/
|
|
|
|
BrinTuple *
|
|
|
|
brinGetTupleForHeapBlock(BrinRevmap *revmap, BlockNumber heapBlk,
|
2016-04-08 21:36:30 +02:00
|
|
|
Buffer *buf, OffsetNumber *off, Size *size, int mode,
|
|
|
|
Snapshot snapshot)
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
{
|
|
|
|
Relation idxRel = revmap->rm_irel;
|
|
|
|
BlockNumber mapBlk;
|
|
|
|
RevmapContents *contents;
|
|
|
|
ItemPointerData *iptr;
|
|
|
|
BlockNumber blk;
|
|
|
|
Page page;
|
|
|
|
ItemId lp;
|
|
|
|
BrinTuple *tup;
|
|
|
|
ItemPointerData previptr;
|
|
|
|
|
|
|
|
/* normalize the heap block number to be the first page in the range */
|
|
|
|
heapBlk = (heapBlk / revmap->rm_pagesPerRange) * revmap->rm_pagesPerRange;
|
|
|
|
|
BRIN auto-summarization
Previously, only VACUUM would cause a page range to get initially
summarized by BRIN indexes, which for some use cases takes too much time
since the inserts occur. To avoid the delay, have brininsert request a
summarization run for the previous range as soon as the first tuple is
inserted into the first page of the next range. Autovacuum is in charge
of processing these requests, after doing all the regular vacuuming/
analyzing work on tables.
This doesn't impose any new tasks on autovacuum, because autovacuum was
already in charge of doing summarizations. The only actual effect is to
change the timing, i.e. that it occurs earlier. For this reason, we
don't go any great lengths to record these requests very robustly; if
they are lost because of a server crash or restart, they will happen at
a later time anyway.
Most of the new code here is in autovacuum, which can now be told about
"work items" to process. This can be used for other things such as GIN
pending list cleaning, perhaps visibility map bit setting, both of which
are currently invoked during vacuum, but do not really depend on vacuum
taking place.
The requests are at the page range level, a granularity for which we did
not have SQL-level access; we only had index-level summarization
requests via brin_summarize_new_values(). It seems reasonable to add
SQL-level access to range-level summarization too, so add a function
brin_summarize_range() to do that.
Authors: Álvaro Herrera, based on sketch from Simon Riggs.
Reviewed-by: Thomas Munro.
Discussion: https://postgr.es/m/20170301045823.vneqdqkmsd4as4ds@alvherre.pgsql
2017-04-01 19:00:53 +02:00
|
|
|
/*
|
|
|
|
* Compute the revmap page number we need. If Invalid is returned (i.e.,
|
|
|
|
* the revmap page hasn't been created yet), the requested page range is
|
|
|
|
* not summarized.
|
|
|
|
*/
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
mapBlk = revmap_get_blkno(revmap, heapBlk);
|
|
|
|
if (mapBlk == InvalidBlockNumber)
|
|
|
|
{
|
|
|
|
*off = InvalidOffsetNumber;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ItemPointerSetInvalid(&previptr);
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
|
|
|
|
if (revmap->rm_currBuf == InvalidBuffer ||
|
|
|
|
BufferGetBlockNumber(revmap->rm_currBuf) != mapBlk)
|
|
|
|
{
|
|
|
|
if (revmap->rm_currBuf != InvalidBuffer)
|
|
|
|
ReleaseBuffer(revmap->rm_currBuf);
|
|
|
|
|
|
|
|
Assert(mapBlk != InvalidBlockNumber);
|
|
|
|
revmap->rm_currBuf = ReadBuffer(revmap->rm_irel, mapBlk);
|
|
|
|
}
|
|
|
|
|
|
|
|
LockBuffer(revmap->rm_currBuf, BUFFER_LOCK_SHARE);
|
|
|
|
|
|
|
|
contents = (RevmapContents *)
|
2016-04-20 15:31:19 +02:00
|
|
|
PageGetContents(BufferGetPage(revmap->rm_currBuf));
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
iptr = contents->rm_tids;
|
|
|
|
iptr += HEAPBLK_TO_REVMAP_INDEX(revmap->rm_pagesPerRange, heapBlk);
|
|
|
|
|
|
|
|
if (!ItemPointerIsValid(iptr))
|
|
|
|
{
|
|
|
|
LockBuffer(revmap->rm_currBuf, BUFFER_LOCK_UNLOCK);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the TID we got in a previous iteration, if any, and save the
|
|
|
|
* current TID we got from the revmap; if we loop, we can sanity-check
|
|
|
|
* that the next one we get is different. Otherwise we might be stuck
|
|
|
|
* looping forever if the revmap is somehow badly broken.
|
|
|
|
*/
|
|
|
|
if (ItemPointerIsValid(&previptr) && ItemPointerEquals(&previptr, iptr))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INDEX_CORRUPTED),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
errmsg_internal("corrupted BRIN index: inconsistent range map")));
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
previptr = *iptr;
|
|
|
|
|
|
|
|
blk = ItemPointerGetBlockNumber(iptr);
|
|
|
|
*off = ItemPointerGetOffsetNumber(iptr);
|
|
|
|
|
|
|
|
LockBuffer(revmap->rm_currBuf, BUFFER_LOCK_UNLOCK);
|
|
|
|
|
|
|
|
/* Ok, got a pointer to where the BrinTuple should be. Fetch it. */
|
|
|
|
if (!BufferIsValid(*buf) || BufferGetBlockNumber(*buf) != blk)
|
|
|
|
{
|
|
|
|
if (BufferIsValid(*buf))
|
|
|
|
ReleaseBuffer(*buf);
|
|
|
|
*buf = ReadBuffer(idxRel, blk);
|
|
|
|
}
|
|
|
|
LockBuffer(*buf, mode);
|
2016-04-20 15:31:19 +02:00
|
|
|
page = BufferGetPage(*buf);
|
|
|
|
TestForOldSnapshot(snapshot, idxRel, page);
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
|
|
|
|
/* If we land on a revmap page, start over */
|
|
|
|
if (BRIN_IS_REGULAR_PAGE(page))
|
|
|
|
{
|
Fix PageAddItem BRIN bug
BRIN was relying on the ability to remove a tuple from an index page,
then putting another tuple in the same line pointer. But PageAddItem
refuses to add a tuple beyond the first free item past the last used
item, and in particular, it rejects an attempt to add an item to an
empty page anywhere other than the first line pointer. PageAddItem
issues a WARNING and indicates to the caller that it failed, which in
turn causes the BRIN calling code to issue a PANIC, so the whole
sequence looks like this:
WARNING: specified item offset is too large
PANIC: failed to add BRIN tuple
To fix, create a new function PageAddItemExtended which is like
PageAddItem except that the two boolean arguments become a flags bitmap;
the "overwrite" and "is_heap" boolean flags in PageAddItem become
PAI_OVERWITE and PAI_IS_HEAP flags in the new function, and a new flag
PAI_ALLOW_FAR_OFFSET enables the behavior required by BRIN.
PageAddItem() retains its original signature, for compatibility with
third-party modules (other callers in core code are not modified,
either).
Also, in the belt-and-suspenders spirit, I added a new sanity check in
brinGetTupleForHeapBlock to raise an error if an TID found in the revmap
is not marked as live by the page header. This causes it to react with
"ERROR: corrupted BRIN index" to the bug at hand, rather than a hard
crash.
Backpatch to 9.5.
Bug reported by Andreas Seltenreich as detected by his handy sqlsmith
fuzzer.
Discussion: https://www.postgresql.org/message-id/87mvni77jh.fsf@elite.ansel.ydns.eu
2016-05-30 20:47:22 +02:00
|
|
|
if (*off > PageGetMaxOffsetNumber(page))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INDEX_CORRUPTED),
|
|
|
|
errmsg_internal("corrupted BRIN index: inconsistent range map")));
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
lp = PageGetItemId(page, *off);
|
|
|
|
if (ItemIdIsUsed(lp))
|
|
|
|
{
|
|
|
|
tup = (BrinTuple *) PageGetItem(page, lp);
|
|
|
|
|
|
|
|
if (tup->bt_blkno == heapBlk)
|
|
|
|
{
|
|
|
|
if (size)
|
|
|
|
*size = ItemIdGetLength(lp);
|
|
|
|
/* found it! */
|
|
|
|
return tup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No luck. Assume that the revmap was updated concurrently.
|
|
|
|
*/
|
|
|
|
LockBuffer(*buf, BUFFER_LOCK_UNLOCK);
|
|
|
|
}
|
|
|
|
/* not reached, but keep compiler quiet */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-04-01 21:10:04 +02:00
|
|
|
/*
|
|
|
|
* Delete an index tuple, marking a page range as unsummarized.
|
|
|
|
*
|
|
|
|
* Index must be locked in ShareUpdateExclusiveLock mode.
|
|
|
|
*
|
2017-08-16 06:22:32 +02:00
|
|
|
* Return false if caller should retry.
|
2017-04-01 21:10:04 +02:00
|
|
|
*/
|
|
|
|
bool
|
|
|
|
brinRevmapDesummarizeRange(Relation idxrel, BlockNumber heapBlk)
|
|
|
|
{
|
|
|
|
BrinRevmap *revmap;
|
2017-05-17 22:31:56 +02:00
|
|
|
BlockNumber pagesPerRange;
|
2017-04-01 21:10:04 +02:00
|
|
|
RevmapContents *contents;
|
|
|
|
ItemPointerData *iptr;
|
2017-05-17 22:31:56 +02:00
|
|
|
ItemPointerData invalidIptr;
|
|
|
|
BlockNumber revmapBlk;
|
2017-04-01 21:10:04 +02:00
|
|
|
Buffer revmapBuf;
|
|
|
|
Buffer regBuf;
|
|
|
|
Page revmapPg;
|
|
|
|
Page regPg;
|
|
|
|
OffsetNumber revmapOffset;
|
|
|
|
OffsetNumber regOffset;
|
|
|
|
ItemId lp;
|
|
|
|
BrinTuple *tup;
|
|
|
|
|
|
|
|
revmap = brinRevmapInitialize(idxrel, &pagesPerRange, NULL);
|
|
|
|
|
|
|
|
revmapBlk = revmap_get_blkno(revmap, heapBlk);
|
|
|
|
if (!BlockNumberIsValid(revmapBlk))
|
|
|
|
{
|
|
|
|
/* revmap page doesn't exist: range not summarized, we're done */
|
|
|
|
brinRevmapTerminate(revmap);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lock the revmap page, obtain the index tuple pointer from it */
|
|
|
|
revmapBuf = brinLockRevmapPageForUpdate(revmap, heapBlk);
|
|
|
|
revmapPg = BufferGetPage(revmapBuf);
|
|
|
|
revmapOffset = HEAPBLK_TO_REVMAP_INDEX(revmap->rm_pagesPerRange, heapBlk);
|
|
|
|
|
|
|
|
contents = (RevmapContents *) PageGetContents(revmapPg);
|
|
|
|
iptr = contents->rm_tids;
|
|
|
|
iptr += revmapOffset;
|
|
|
|
|
|
|
|
if (!ItemPointerIsValid(iptr))
|
|
|
|
{
|
|
|
|
/* no index tuple: range not summarized, we're done */
|
|
|
|
LockBuffer(revmapBuf, BUFFER_LOCK_UNLOCK);
|
|
|
|
brinRevmapTerminate(revmap);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
regBuf = ReadBuffer(idxrel, ItemPointerGetBlockNumber(iptr));
|
|
|
|
LockBuffer(regBuf, BUFFER_LOCK_EXCLUSIVE);
|
|
|
|
regPg = BufferGetPage(regBuf);
|
|
|
|
|
|
|
|
/* if this is no longer a regular page, tell caller to start over */
|
|
|
|
if (!BRIN_IS_REGULAR_PAGE(regPg))
|
|
|
|
{
|
|
|
|
LockBuffer(revmapBuf, BUFFER_LOCK_UNLOCK);
|
|
|
|
LockBuffer(regBuf, BUFFER_LOCK_UNLOCK);
|
|
|
|
brinRevmapTerminate(revmap);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
regOffset = ItemPointerGetOffsetNumber(iptr);
|
|
|
|
if (regOffset > PageGetMaxOffsetNumber(regPg))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INDEX_CORRUPTED),
|
|
|
|
errmsg("corrupted BRIN index: inconsistent range map")));
|
|
|
|
|
|
|
|
lp = PageGetItemId(regPg, regOffset);
|
|
|
|
if (!ItemIdIsUsed(lp))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INDEX_CORRUPTED),
|
|
|
|
errmsg("corrupted BRIN index: inconsistent range map")));
|
|
|
|
tup = (BrinTuple *) PageGetItem(regPg, lp);
|
|
|
|
/* XXX apply sanity checks? Might as well delete a bogus tuple ... */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We're only removing data, not reading it, so there's no need to
|
|
|
|
* TestForOldSnapshot here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Because of SUE lock, this function shouldn't run concurrently with
|
|
|
|
* summarization. Placeholder tuples can only exist as leftovers from
|
|
|
|
* crashed summarization, so if we detect any, we complain but proceed.
|
|
|
|
*/
|
|
|
|
if (BrinTupleIsPlaceholder(tup))
|
|
|
|
ereport(WARNING,
|
|
|
|
(errmsg("leftover placeholder tuple detected in BRIN index \"%s\", deleting",
|
|
|
|
RelationGetRelationName(idxrel))));
|
|
|
|
|
|
|
|
START_CRIT_SECTION();
|
|
|
|
|
|
|
|
ItemPointerSetInvalid(&invalidIptr);
|
|
|
|
brinSetHeapBlockItemptr(revmapBuf, revmap->rm_pagesPerRange, heapBlk,
|
|
|
|
invalidIptr);
|
|
|
|
PageIndexTupleDeleteNoCompact(regPg, regOffset);
|
|
|
|
/* XXX record free space in FSM? */
|
|
|
|
|
|
|
|
MarkBufferDirty(regBuf);
|
|
|
|
MarkBufferDirty(revmapBuf);
|
|
|
|
|
|
|
|
if (RelationNeedsWAL(idxrel))
|
|
|
|
{
|
|
|
|
xl_brin_desummarize xlrec;
|
2017-05-17 22:31:56 +02:00
|
|
|
XLogRecPtr recptr;
|
2017-04-01 21:10:04 +02:00
|
|
|
|
2017-04-07 21:58:54 +02:00
|
|
|
xlrec.pagesPerRange = revmap->rm_pagesPerRange;
|
2017-04-01 21:10:04 +02:00
|
|
|
xlrec.heapBlk = heapBlk;
|
|
|
|
xlrec.regOffset = regOffset;
|
|
|
|
|
|
|
|
XLogBeginInsert();
|
|
|
|
XLogRegisterData((char *) &xlrec, SizeOfBrinDesummarize);
|
|
|
|
XLogRegisterBuffer(0, revmapBuf, 0);
|
|
|
|
XLogRegisterBuffer(1, regBuf, REGBUF_STANDARD);
|
|
|
|
recptr = XLogInsert(RM_BRIN_ID, XLOG_BRIN_DESUMMARIZE);
|
|
|
|
PageSetLSN(revmapPg, recptr);
|
|
|
|
PageSetLSN(regPg, recptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
END_CRIT_SECTION();
|
|
|
|
|
|
|
|
UnlockReleaseBuffer(regBuf);
|
|
|
|
LockBuffer(revmapBuf, BUFFER_LOCK_UNLOCK);
|
|
|
|
brinRevmapTerminate(revmap);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
/*
|
|
|
|
* Given a heap block number, find the corresponding physical revmap block
|
|
|
|
* number and return it. If the revmap page hasn't been allocated yet, return
|
|
|
|
* InvalidBlockNumber.
|
|
|
|
*/
|
|
|
|
static BlockNumber
|
|
|
|
revmap_get_blkno(BrinRevmap *revmap, BlockNumber heapBlk)
|
|
|
|
{
|
|
|
|
BlockNumber targetblk;
|
|
|
|
|
|
|
|
/* obtain revmap block number, skip 1 for metapage block */
|
|
|
|
targetblk = HEAPBLK_TO_REVMAP_BLK(revmap->rm_pagesPerRange, heapBlk) + 1;
|
|
|
|
|
|
|
|
/* Normal case: the revmap page is already allocated */
|
|
|
|
if (targetblk <= revmap->rm_lastRevmapPage)
|
|
|
|
return targetblk;
|
|
|
|
|
|
|
|
return InvalidBlockNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Obtain and return a buffer containing the revmap page for the given heap
|
|
|
|
* page. The revmap must have been previously extended to cover that page.
|
|
|
|
* The returned buffer is also recorded in the revmap struct; finishing that
|
2015-09-05 10:35:49 +02:00
|
|
|
* releases the buffer, therefore the caller needn't do it explicitly.
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
*/
|
|
|
|
static Buffer
|
|
|
|
revmap_get_buffer(BrinRevmap *revmap, BlockNumber heapBlk)
|
|
|
|
{
|
|
|
|
BlockNumber mapBlk;
|
|
|
|
|
|
|
|
/* Translate the heap block number to physical index location. */
|
|
|
|
mapBlk = revmap_get_blkno(revmap, heapBlk);
|
|
|
|
|
|
|
|
if (mapBlk == InvalidBlockNumber)
|
|
|
|
elog(ERROR, "revmap does not cover heap block %u", heapBlk);
|
|
|
|
|
|
|
|
/* Ensure the buffer we got is in the expected range */
|
|
|
|
Assert(mapBlk != BRIN_METAPAGE_BLKNO &&
|
|
|
|
mapBlk <= revmap->rm_lastRevmapPage);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Obtain the buffer from which we need to read. If we already have the
|
|
|
|
* correct buffer in our access struct, use that; otherwise, release that,
|
|
|
|
* (if valid) and read the one we need.
|
|
|
|
*/
|
|
|
|
if (revmap->rm_currBuf == InvalidBuffer ||
|
|
|
|
mapBlk != BufferGetBlockNumber(revmap->rm_currBuf))
|
|
|
|
{
|
|
|
|
if (revmap->rm_currBuf != InvalidBuffer)
|
|
|
|
ReleaseBuffer(revmap->rm_currBuf);
|
|
|
|
|
|
|
|
revmap->rm_currBuf = ReadBuffer(revmap->rm_irel, mapBlk);
|
|
|
|
}
|
|
|
|
|
|
|
|
return revmap->rm_currBuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a heap block number, find the corresponding physical revmap block
|
|
|
|
* number and return it. If the revmap page hasn't been allocated yet, extend
|
|
|
|
* the revmap until it is.
|
|
|
|
*/
|
|
|
|
static BlockNumber
|
|
|
|
revmap_extend_and_get_blkno(BrinRevmap *revmap, BlockNumber heapBlk)
|
|
|
|
{
|
2015-05-24 03:35:49 +02:00
|
|
|
BlockNumber targetblk;
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
|
|
|
|
/* obtain revmap block number, skip 1 for metapage block */
|
|
|
|
targetblk = HEAPBLK_TO_REVMAP_BLK(revmap->rm_pagesPerRange, heapBlk) + 1;
|
|
|
|
|
|
|
|
/* Extend the revmap, if necessary */
|
|
|
|
while (targetblk > revmap->rm_lastRevmapPage)
|
|
|
|
{
|
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
revmap_physical_extend(revmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
return targetblk;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to extend the revmap by one page. This might not happen for a number of
|
|
|
|
* reasons; caller is expected to retry until the expected outcome is obtained.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
revmap_physical_extend(BrinRevmap *revmap)
|
|
|
|
{
|
|
|
|
Buffer buf;
|
|
|
|
Page page;
|
|
|
|
Page metapage;
|
|
|
|
BrinMetaPageData *metadata;
|
|
|
|
BlockNumber mapBlk;
|
|
|
|
BlockNumber nblocks;
|
|
|
|
Relation irel = revmap->rm_irel;
|
|
|
|
bool needLock = !RELATION_IS_LOCAL(irel);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lock the metapage. This locks out concurrent extensions of the revmap,
|
|
|
|
* but note that we still need to grab the relation extension lock because
|
|
|
|
* another backend can extend the index with regular BRIN pages.
|
|
|
|
*/
|
|
|
|
LockBuffer(revmap->rm_metaBuf, BUFFER_LOCK_EXCLUSIVE);
|
2016-04-20 15:31:19 +02:00
|
|
|
metapage = BufferGetPage(revmap->rm_metaBuf);
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
metadata = (BrinMetaPageData *) PageGetContents(metapage);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that our cached lastRevmapPage value was up-to-date; if it
|
|
|
|
* wasn't, update the cached copy and have caller start over.
|
|
|
|
*/
|
|
|
|
if (metadata->lastRevmapPage != revmap->rm_lastRevmapPage)
|
|
|
|
{
|
|
|
|
revmap->rm_lastRevmapPage = metadata->lastRevmapPage;
|
|
|
|
LockBuffer(revmap->rm_metaBuf, BUFFER_LOCK_UNLOCK);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mapBlk = metadata->lastRevmapPage + 1;
|
|
|
|
|
|
|
|
nblocks = RelationGetNumberOfBlocks(irel);
|
|
|
|
if (mapBlk < nblocks)
|
|
|
|
{
|
|
|
|
buf = ReadBuffer(irel, mapBlk);
|
|
|
|
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
|
2016-04-20 15:31:19 +02:00
|
|
|
page = BufferGetPage(buf);
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (needLock)
|
|
|
|
LockRelationForExtension(irel, ExclusiveLock);
|
|
|
|
|
|
|
|
buf = ReadBuffer(irel, P_NEW);
|
|
|
|
if (BufferGetBlockNumber(buf) != mapBlk)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Very rare corner case: somebody extended the relation
|
|
|
|
* concurrently after we read its length. If this happens, give
|
|
|
|
* up and have caller start over. We will have to evacuate that
|
|
|
|
* page from under whoever is using it.
|
|
|
|
*/
|
|
|
|
if (needLock)
|
|
|
|
UnlockRelationForExtension(irel, ExclusiveLock);
|
|
|
|
LockBuffer(revmap->rm_metaBuf, BUFFER_LOCK_UNLOCK);
|
2015-09-11 20:29:46 +02:00
|
|
|
ReleaseBuffer(buf);
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
|
2016-04-20 15:31:19 +02:00
|
|
|
page = BufferGetPage(buf);
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
|
|
|
|
if (needLock)
|
|
|
|
UnlockRelationForExtension(irel, ExclusiveLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that it's a regular block (or an empty page) */
|
|
|
|
if (!PageIsNew(page) && !BRIN_IS_REGULAR_PAGE(page))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INDEX_CORRUPTED),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
errmsg("unexpected page type 0x%04X in BRIN index \"%s\" block %u",
|
|
|
|
BrinPageType(page),
|
|
|
|
RelationGetRelationName(irel),
|
|
|
|
BufferGetBlockNumber(buf))));
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
|
|
|
|
/* If the page is in use, evacuate it and restart */
|
|
|
|
if (brin_start_evacuating_page(irel, buf))
|
|
|
|
{
|
|
|
|
LockBuffer(revmap->rm_metaBuf, BUFFER_LOCK_UNLOCK);
|
|
|
|
brin_evacuate_page(irel, revmap->rm_pagesPerRange, revmap, buf);
|
|
|
|
|
|
|
|
/* have caller start over */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ok, we have now locked the metapage and the target block. Re-initialize
|
Set the metapage's pd_lower correctly in brin, gin, and spgist indexes.
Previously, these index types left the pd_lower field set to the default
SizeOfPageHeaderData, which is really a lie because it ought to point past
whatever space is being used for metadata. The coding accidentally failed
to fail because we never told xlog.c that the metapage is of standard
format --- but that's not very good, because it impedes WAL consistency
checking, and in some cases prevents compression of full-page images.
To fix, ensure that we set pd_lower correctly, not only when creating a
metapage but whenever we write it out (these apparently redundant steps are
needed to cope with pg_upgrade'd indexes that don't yet contain the right
value). This allows telling xlog.c that the page is of standard format.
The WAL consistency check mask functions are made to mask only if pd_lower
appears valid, which I think is likely unnecessary complication, since
any metapage appearing in a v11 WAL stream should contain valid pd_lower.
But it doesn't cost much to be paranoid.
Amit Langote, reviewed by Michael Paquier and Amit Kapila
Discussion: https://postgr.es/m/0d273805-0e9e-ec1a-cb84-d4da400b8f85@lab.ntt.co.jp
2017-11-02 22:22:08 +01:00
|
|
|
* the target block as a revmap page, and update the metapage.
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
*/
|
|
|
|
START_CRIT_SECTION();
|
|
|
|
|
|
|
|
/* the rm_tids array is initialized to all invalid by PageInit */
|
|
|
|
brin_page_init(page, BRIN_PAGETYPE_REVMAP);
|
|
|
|
MarkBufferDirty(buf);
|
|
|
|
|
|
|
|
metadata->lastRevmapPage = mapBlk;
|
Set the metapage's pd_lower correctly in brin, gin, and spgist indexes.
Previously, these index types left the pd_lower field set to the default
SizeOfPageHeaderData, which is really a lie because it ought to point past
whatever space is being used for metadata. The coding accidentally failed
to fail because we never told xlog.c that the metapage is of standard
format --- but that's not very good, because it impedes WAL consistency
checking, and in some cases prevents compression of full-page images.
To fix, ensure that we set pd_lower correctly, not only when creating a
metapage but whenever we write it out (these apparently redundant steps are
needed to cope with pg_upgrade'd indexes that don't yet contain the right
value). This allows telling xlog.c that the page is of standard format.
The WAL consistency check mask functions are made to mask only if pd_lower
appears valid, which I think is likely unnecessary complication, since
any metapage appearing in a v11 WAL stream should contain valid pd_lower.
But it doesn't cost much to be paranoid.
Amit Langote, reviewed by Michael Paquier and Amit Kapila
Discussion: https://postgr.es/m/0d273805-0e9e-ec1a-cb84-d4da400b8f85@lab.ntt.co.jp
2017-11-02 22:22:08 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set pd_lower just past the end of the metadata. This is essential,
|
|
|
|
* because without doing so, metadata will be lost if xlog.c compresses
|
|
|
|
* the page. (We must do this here because pre-v11 versions of PG did not
|
|
|
|
* set the metapage's pd_lower correctly, so a pg_upgraded index might
|
|
|
|
* contain the wrong value.)
|
|
|
|
*/
|
|
|
|
((PageHeader) metapage)->pd_lower =
|
|
|
|
((char *) metadata + sizeof(BrinMetaPageData)) - (char *) metapage;
|
|
|
|
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
MarkBufferDirty(revmap->rm_metaBuf);
|
|
|
|
|
|
|
|
if (RelationNeedsWAL(revmap->rm_irel))
|
|
|
|
{
|
|
|
|
xl_brin_revmap_extend xlrec;
|
|
|
|
XLogRecPtr recptr;
|
|
|
|
|
|
|
|
xlrec.targetBlk = mapBlk;
|
Revamp the WAL record format.
Each WAL record now carries information about the modified relation and
block(s) in a standardized format. That makes it easier to write tools that
need that information, like pg_rewind, prefetching the blocks to speed up
recovery, etc.
There's a whole new API for building WAL records, replacing the XLogRecData
chains used previously. The new API consists of XLogRegister* functions,
which are called for each buffer and chunk of data that is added to the
record. The new API also gives more control over when a full-page image is
written, by passing flags to the XLogRegisterBuffer function.
This also simplifies the XLogReadBufferForRedo() calls. The function can dig
the relation and block number from the WAL record, so they no longer need to
be passed as arguments.
For the convenience of redo routines, XLogReader now disects each WAL record
after reading it, copying the main data part and the per-block data into
MAXALIGNed buffers. The data chunks are not aligned within the WAL record,
but the redo routines can assume that the pointers returned by XLogRecGet*
functions are. Redo routines are now passed the XLogReaderState, which
contains the record in the already-disected format, instead of the plain
XLogRecord.
The new record format also makes the fixed size XLogRecord header smaller,
by removing the xl_len field. The length of the "main data" portion is now
stored at the end of the WAL record, and there's a separate header after
XLogRecord for it. The alignment padding at the end of XLogRecord is also
removed. This compansates for the fact that the new format would otherwise
be more bulky than the old format.
Reviewed by Andres Freund, Amit Kapila, Michael Paquier, Alvaro Herrera,
Fujii Masao.
2014-11-20 16:56:26 +01:00
|
|
|
|
|
|
|
XLogBeginInsert();
|
|
|
|
XLogRegisterData((char *) &xlrec, SizeOfBrinRevmapExtend);
|
Set the metapage's pd_lower correctly in brin, gin, and spgist indexes.
Previously, these index types left the pd_lower field set to the default
SizeOfPageHeaderData, which is really a lie because it ought to point past
whatever space is being used for metadata. The coding accidentally failed
to fail because we never told xlog.c that the metapage is of standard
format --- but that's not very good, because it impedes WAL consistency
checking, and in some cases prevents compression of full-page images.
To fix, ensure that we set pd_lower correctly, not only when creating a
metapage but whenever we write it out (these apparently redundant steps are
needed to cope with pg_upgrade'd indexes that don't yet contain the right
value). This allows telling xlog.c that the page is of standard format.
The WAL consistency check mask functions are made to mask only if pd_lower
appears valid, which I think is likely unnecessary complication, since
any metapage appearing in a v11 WAL stream should contain valid pd_lower.
But it doesn't cost much to be paranoid.
Amit Langote, reviewed by Michael Paquier and Amit Kapila
Discussion: https://postgr.es/m/0d273805-0e9e-ec1a-cb84-d4da400b8f85@lab.ntt.co.jp
2017-11-02 22:22:08 +01:00
|
|
|
XLogRegisterBuffer(0, revmap->rm_metaBuf, REGBUF_STANDARD);
|
Revamp the WAL record format.
Each WAL record now carries information about the modified relation and
block(s) in a standardized format. That makes it easier to write tools that
need that information, like pg_rewind, prefetching the blocks to speed up
recovery, etc.
There's a whole new API for building WAL records, replacing the XLogRecData
chains used previously. The new API consists of XLogRegister* functions,
which are called for each buffer and chunk of data that is added to the
record. The new API also gives more control over when a full-page image is
written, by passing flags to the XLogRegisterBuffer function.
This also simplifies the XLogReadBufferForRedo() calls. The function can dig
the relation and block number from the WAL record, so they no longer need to
be passed as arguments.
For the convenience of redo routines, XLogReader now disects each WAL record
after reading it, copying the main data part and the per-block data into
MAXALIGNed buffers. The data chunks are not aligned within the WAL record,
but the redo routines can assume that the pointers returned by XLogRecGet*
functions are. Redo routines are now passed the XLogReaderState, which
contains the record in the already-disected format, instead of the plain
XLogRecord.
The new record format also makes the fixed size XLogRecord header smaller,
by removing the xl_len field. The length of the "main data" portion is now
stored at the end of the WAL record, and there's a separate header after
XLogRecord for it. The alignment padding at the end of XLogRecord is also
removed. This compansates for the fact that the new format would otherwise
be more bulky than the old format.
Reviewed by Andres Freund, Amit Kapila, Michael Paquier, Alvaro Herrera,
Fujii Masao.
2014-11-20 16:56:26 +01:00
|
|
|
|
|
|
|
XLogRegisterBuffer(1, buf, REGBUF_WILL_INIT);
|
|
|
|
|
|
|
|
recptr = XLogInsert(RM_BRIN_ID, XLOG_BRIN_REVMAP_EXTEND);
|
BRIN: Block Range Indexes
BRIN is a new index access method intended to accelerate scans of very
large tables, without the maintenance overhead of btrees or other
traditional indexes. They work by maintaining "summary" data about
block ranges. Bitmap index scans work by reading each summary tuple and
comparing them with the query quals; all pages in the range are returned
in a lossy TID bitmap if the quals are consistent with the values in the
summary tuple, otherwise not. Normal index scans are not supported
because these indexes do not store TIDs.
As new tuples are added into the index, the summary information is
updated (if the block range in which the tuple is added is already
summarized) or not; in the latter case, a subsequent pass of VACUUM or
the brin_summarize_new_values() function will create the summary
information.
For data types with natural 1-D sort orders, the summary info consists
of the maximum and the minimum values of each indexed column within each
page range. This type of operator class we call "Minmax", and we
supply a bunch of them for most data types with B-tree opclasses.
Since the BRIN code is generalized, other approaches are possible for
things such as arrays, geometric types, ranges, etc; even for things
such as enum types we could do something different than minmax with
better results. In this commit I only include minmax.
Catalog version bumped due to new builtin catalog entries.
There's more that could be done here, but this is a good step forwards.
Loosely based on ideas from Simon Riggs; code mostly by Álvaro Herrera,
with contribution by Heikki Linnakangas.
Patch reviewed by: Amit Kapila, Heikki Linnakangas, Robert Haas.
Testing help from Jeff Janes, Erik Rijkers, Emanuel Calvo.
PS:
The research leading to these results has received funding from the
European Union's Seventh Framework Programme (FP7/2007-2013) under
grant agreement n° 318633.
2014-11-07 20:38:14 +01:00
|
|
|
PageSetLSN(metapage, recptr);
|
|
|
|
PageSetLSN(page, recptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
END_CRIT_SECTION();
|
|
|
|
|
|
|
|
LockBuffer(revmap->rm_metaBuf, BUFFER_LOCK_UNLOCK);
|
|
|
|
|
|
|
|
UnlockReleaseBuffer(buf);
|
|
|
|
}
|