mirror of
https://github.com/postgres/postgres.git
synced 2025-11-10 17:42:29 +03:00
Add equalimage B-Tree support functions.
Invent the concept of a B-Tree equalimage ("equality implies image
equality") support function, registered as support function 4. This
indicates whether it is safe (or not safe) to apply optimizations that
assume that any two datums considered equal by an operator class's order
method must be interchangeable without any loss of semantic information.
This is static information about an operator class and a collation.
Register an equalimage routine for almost all of the existing B-Tree
opclasses. We only need two trivial routines for all of the opclasses
that are included with the core distribution. There is one routine for
opclasses that index non-collatable types (which returns 'true'
unconditionally), plus another routine for collatable types (which
returns 'true' when the collation is a deterministic collation).
This patch is infrastructure for an upcoming patch that adds B-Tree
deduplication.
Author: Peter Geoghegan, Anastasia Lubennikova
Discussion: https://postgr.es/m/CAH2-Wzn3Ee49Gmxb7V1VJ3-AC8fWn-Fr8pfWQebHe8rYRxt5OQ@mail.gmail.com
This commit is contained in:
@@ -20,6 +20,7 @@
|
||||
#include "access/nbtree.h"
|
||||
#include "access/reloptions.h"
|
||||
#include "access/relscan.h"
|
||||
#include "catalog/catalog.h"
|
||||
#include "commands/progress.h"
|
||||
#include "lib/qunique.h"
|
||||
#include "miscadmin.h"
|
||||
@@ -2566,3 +2567,75 @@ _bt_check_third_page(Relation rel, Relation heap, bool needheaptidspace,
|
||||
"or use full text indexing."),
|
||||
errtableconstraint(heap, RelationGetRelationName(rel))));
|
||||
}
|
||||
|
||||
/*
|
||||
* Are all attributes in rel "equality is image equality" attributes?
|
||||
*
|
||||
* We use each attribute's BTEQUALIMAGE_PROC opclass procedure. If any
|
||||
* opclass either lacks a BTEQUALIMAGE_PROC procedure or returns false, we
|
||||
* return false; otherwise we return true.
|
||||
*
|
||||
* Returned boolean value is stored in index metapage during index builds.
|
||||
* Deduplication can only be used when we return true.
|
||||
*/
|
||||
bool
|
||||
_bt_allequalimage(Relation rel, bool debugmessage)
|
||||
{
|
||||
bool allequalimage = true;
|
||||
|
||||
/* INCLUDE indexes don't support deduplication */
|
||||
if (IndexRelationGetNumberOfAttributes(rel) !=
|
||||
IndexRelationGetNumberOfKeyAttributes(rel))
|
||||
return false;
|
||||
|
||||
/*
|
||||
* There is no special reason why deduplication cannot work with system
|
||||
* relations (i.e. with system catalog indexes and TOAST indexes). We
|
||||
* deem deduplication unsafe for these indexes all the same, since the
|
||||
* alternative is to force users to always use deduplication, without
|
||||
* being able to opt out. (ALTER INDEX is not supported with system
|
||||
* indexes, so users would have no way to set the deduplicate_items
|
||||
* storage parameter to 'off'.)
|
||||
*/
|
||||
if (IsSystemRelation(rel))
|
||||
return false;
|
||||
|
||||
for (int i = 0; i < IndexRelationGetNumberOfKeyAttributes(rel); i++)
|
||||
{
|
||||
Oid opfamily = rel->rd_opfamily[i];
|
||||
Oid opcintype = rel->rd_opcintype[i];
|
||||
Oid collation = rel->rd_indcollation[i];
|
||||
Oid equalimageproc;
|
||||
|
||||
equalimageproc = get_opfamily_proc(opfamily, opcintype, opcintype,
|
||||
BTEQUALIMAGE_PROC);
|
||||
|
||||
/*
|
||||
* If there is no BTEQUALIMAGE_PROC then deduplication is assumed to
|
||||
* be unsafe. Otherwise, actually call proc and see what it says.
|
||||
*/
|
||||
if (!OidIsValid(equalimageproc) ||
|
||||
!DatumGetBool(OidFunctionCall1Coll(equalimageproc, collation,
|
||||
ObjectIdGetDatum(opcintype))))
|
||||
{
|
||||
allequalimage = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Don't elog() until here to avoid reporting on a system relation index
|
||||
* or an INCLUDE index
|
||||
*/
|
||||
if (debugmessage)
|
||||
{
|
||||
if (allequalimage)
|
||||
elog(DEBUG1, "index \"%s\" can safely use deduplication",
|
||||
RelationGetRelationName(rel));
|
||||
else
|
||||
elog(DEBUG1, "index \"%s\" cannot use deduplication",
|
||||
RelationGetRelationName(rel));
|
||||
}
|
||||
|
||||
return allequalimage;
|
||||
}
|
||||
|
||||
@@ -104,6 +104,10 @@ btvalidate(Oid opclassoid)
|
||||
procform->amprocrighttype,
|
||||
BOOLOID, BOOLOID);
|
||||
break;
|
||||
case BTEQUALIMAGE_PROC:
|
||||
ok = check_amproc_signature(procform->amproc, BOOLOID, true,
|
||||
1, 1, OIDOID);
|
||||
break;
|
||||
default:
|
||||
ereport(INFO,
|
||||
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
|
||||
@@ -211,8 +215,8 @@ btvalidate(Oid opclassoid)
|
||||
|
||||
/*
|
||||
* Complain if there seems to be an incomplete set of either operators
|
||||
* or support functions for this datatype pair. The only things
|
||||
* considered optional are the sortsupport and in_range functions.
|
||||
* or support functions for this datatype pair. The sortsupport,
|
||||
* in_range, and equalimage functions are considered optional.
|
||||
*/
|
||||
if (thisgroup->operatorset !=
|
||||
((1 << BTLessStrategyNumber) |
|
||||
|
||||
Reference in New Issue
Block a user