mirror of
https://github.com/postgres/postgres.git
synced 2025-07-07 00:36:50 +03:00
TABLESAMPLE, SQL Standard and extensible
Add a TABLESAMPLE clause to SELECT statements that allows user to specify random BERNOULLI sampling or block level SYSTEM sampling. Implementation allows for extensible sampling functions to be written, using a standard API. Basic version follows SQLStandard exactly. Usable concrete use cases for the sampling API follow in later commits. Petr Jelinek Reviewed by Michael Paquier and Simon Riggs
This commit is contained in:
@ -71,6 +71,10 @@ static void set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel,
|
||||
RangeTblEntry *rte);
|
||||
static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
|
||||
RangeTblEntry *rte);
|
||||
static void set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel,
|
||||
RangeTblEntry *rte);
|
||||
static void set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
|
||||
RangeTblEntry *rte);
|
||||
static void set_foreign_size(PlannerInfo *root, RelOptInfo *rel,
|
||||
RangeTblEntry *rte);
|
||||
static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel,
|
||||
@ -265,6 +269,11 @@ set_rel_size(PlannerInfo *root, RelOptInfo *rel,
|
||||
/* Foreign table */
|
||||
set_foreign_size(root, rel, rte);
|
||||
}
|
||||
else if (rte->tablesample != NULL)
|
||||
{
|
||||
/* Sampled relation */
|
||||
set_tablesample_rel_size(root, rel, rte);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Plain relation */
|
||||
@ -332,6 +341,11 @@ set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
|
||||
/* Foreign table */
|
||||
set_foreign_pathlist(root, rel, rte);
|
||||
}
|
||||
else if (rte->tablesample != NULL)
|
||||
{
|
||||
/* Build sample scan on relation */
|
||||
set_tablesample_rel_pathlist(root, rel, rte);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Plain relation */
|
||||
@ -417,6 +431,41 @@ set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
|
||||
create_tidscan_paths(root, rel);
|
||||
}
|
||||
|
||||
/*
|
||||
* set_tablesample_rel_size
|
||||
* Set size estimates for a sampled relation.
|
||||
*/
|
||||
static void
|
||||
set_tablesample_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
|
||||
{
|
||||
/* Mark rel with estimated output rows, width, etc */
|
||||
set_baserel_size_estimates(root, rel);
|
||||
}
|
||||
|
||||
/*
|
||||
* set_tablesample_rel_pathlist
|
||||
* Build access paths for a sampled relation
|
||||
*
|
||||
* There is only one possible path - sampling scan
|
||||
*/
|
||||
static void
|
||||
set_tablesample_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
|
||||
{
|
||||
Relids required_outer;
|
||||
Path *path;
|
||||
|
||||
/*
|
||||
* We don't support pushing join clauses into the quals of a seqscan, but
|
||||
* it could still have required parameterization due to LATERAL refs in
|
||||
* its tlist.
|
||||
*/
|
||||
required_outer = rel->lateral_relids;
|
||||
|
||||
/* We only do sample scan if it was requested */
|
||||
path = create_samplescan_path(root, rel, required_outer);
|
||||
rel->pathlist = list_make1(path);
|
||||
}
|
||||
|
||||
/*
|
||||
* set_foreign_size
|
||||
* Set size estimates for a foreign table RTE
|
||||
|
@ -219,6 +219,73 @@ cost_seqscan(Path *path, PlannerInfo *root,
|
||||
path->total_cost = startup_cost + run_cost;
|
||||
}
|
||||
|
||||
/*
|
||||
* cost_samplescan
|
||||
* Determines and returns the cost of scanning a relation using sampling.
|
||||
*
|
||||
* From planner/optimizer perspective, we don't care all that much about cost
|
||||
* itself since there is always only one scan path to consider when sampling
|
||||
* scan is present, but number of rows estimation is still important.
|
||||
*
|
||||
* 'baserel' is the relation to be scanned
|
||||
* 'param_info' is the ParamPathInfo if this is a parameterized path, else NULL
|
||||
*/
|
||||
void
|
||||
cost_samplescan(Path *path, PlannerInfo *root, RelOptInfo *baserel)
|
||||
{
|
||||
Cost startup_cost = 0;
|
||||
Cost run_cost = 0;
|
||||
double spc_seq_page_cost,
|
||||
spc_random_page_cost,
|
||||
spc_page_cost;
|
||||
QualCost qpqual_cost;
|
||||
Cost cpu_per_tuple;
|
||||
BlockNumber pages;
|
||||
double tuples;
|
||||
RangeTblEntry *rte = planner_rt_fetch(baserel->relid, root);
|
||||
TableSampleClause *tablesample = rte->tablesample;
|
||||
|
||||
/* Should only be applied to base relations */
|
||||
Assert(baserel->relid > 0);
|
||||
Assert(baserel->rtekind == RTE_RELATION);
|
||||
|
||||
/* Mark the path with the correct row estimate */
|
||||
if (path->param_info)
|
||||
path->rows = path->param_info->ppi_rows;
|
||||
else
|
||||
path->rows = baserel->rows;
|
||||
|
||||
/* Call the sampling method's costing function. */
|
||||
OidFunctionCall6(tablesample->tsmcost, PointerGetDatum(root),
|
||||
PointerGetDatum(path), PointerGetDatum(baserel),
|
||||
PointerGetDatum(tablesample->args),
|
||||
PointerGetDatum(&pages), PointerGetDatum(&tuples));
|
||||
|
||||
/* fetch estimated page cost for tablespace containing table */
|
||||
get_tablespace_page_costs(baserel->reltablespace,
|
||||
&spc_random_page_cost,
|
||||
&spc_seq_page_cost);
|
||||
|
||||
|
||||
spc_page_cost = tablesample->tsmseqscan ? spc_seq_page_cost :
|
||||
spc_random_page_cost;
|
||||
|
||||
/*
|
||||
* disk costs
|
||||
*/
|
||||
run_cost += spc_page_cost * pages;
|
||||
|
||||
/* CPU costs */
|
||||
get_restriction_qual_cost(root, baserel, path->param_info, &qpqual_cost);
|
||||
|
||||
startup_cost += qpqual_cost.startup;
|
||||
cpu_per_tuple = cpu_tuple_cost + qpqual_cost.per_tuple;
|
||||
run_cost += cpu_per_tuple * tuples;
|
||||
|
||||
path->startup_cost = startup_cost;
|
||||
path->total_cost = startup_cost + run_cost;
|
||||
}
|
||||
|
||||
/*
|
||||
* cost_index
|
||||
* Determines and returns the cost of scanning a relation using an index.
|
||||
|
Reference in New Issue
Block a user