PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
bernoulli.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/hash.h"
#include "access/tsmapi.h"
#include "catalog/pg_type.h"
#include "optimizer/clauses.h"
#include "optimizer/cost.h"
#include "utils/builtins.h"
Include dependency graph for bernoulli.c:

Go to the source code of this file.

Data Structures

struct  BernoulliSamplerData
 

Functions

static void bernoulli_samplescangetsamplesize (PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
 
static void bernoulli_initsamplescan (SampleScanState *node, int eflags)
 
static void bernoulli_beginsamplescan (SampleScanState *node, Datum *params, int nparams, uint32 seed)
 
static OffsetNumber bernoulli_nextsampletuple (SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
 
Datum tsm_bernoulli_handler (PG_FUNCTION_ARGS)
 

Function Documentation

static void bernoulli_beginsamplescan ( SampleScanState node,
Datum params,
int  nparams,
uint32  seed 
)
static

Definition at line 140 of file bernoulli.c.

References BernoulliSamplerData::cutoff, DatumGetFloat4, ereport, errcode(), errmsg(), ERROR, InvalidOffsetNumber, BernoulliSamplerData::lt, PG_UINT32_MAX, rint(), BernoulliSamplerData::seed, SampleScanState::tsm_state, SampleScanState::use_bulkread, and SampleScanState::use_pagemode.

Referenced by tsm_bernoulli_handler().

144 {
146  double percent = DatumGetFloat4(params[0]);
147  double dcutoff;
148 
149  if (percent < 0 || percent > 100 || isnan(percent))
150  ereport(ERROR,
151  (errcode(ERRCODE_INVALID_TABLESAMPLE_ARGUMENT),
152  errmsg("sample percentage must be between 0 and 100")));
153 
154  /*
155  * The cutoff is sample probability times (PG_UINT32_MAX + 1); we have to
156  * store that as a uint64, of course. Note that this gives strictly
157  * correct behavior at the limits of zero or one probability.
158  */
159  dcutoff = rint(((double) PG_UINT32_MAX + 1) * percent / 100);
160  sampler->cutoff = (uint64) dcutoff;
161  sampler->seed = seed;
162  sampler->lt = InvalidOffsetNumber;
163 
164  /*
165  * Use bulkread, since we're scanning all pages. But pagemode visibility
166  * checking is a win only at larger sampling fractions. The 25% cutoff
167  * here is based on very limited experimentation.
168  */
169  node->use_bulkread = true;
170  node->use_pagemode = (percent >= 25);
171 }
void * tsm_state
Definition: execnodes.h:1106
int errcode(int sqlerrcode)
Definition: elog.c:575
#define PG_UINT32_MAX
Definition: c.h:341
#define ERROR
Definition: elog.h:43
double rint(double x)
Definition: rint.c:22
#define ereport(elevel, rest)
Definition: elog.h:122
OffsetNumber lt
Definition: bernoulli.c:45
#define InvalidOffsetNumber
Definition: off.h:26
#define DatumGetFloat4(X)
Definition: postgres.h:686
int errmsg(const char *fmt,...)
Definition: elog.c:797
static void bernoulli_initsamplescan ( SampleScanState node,
int  eflags 
)
static

Definition at line 131 of file bernoulli.c.

References palloc0(), and SampleScanState::tsm_state.

Referenced by tsm_bernoulli_handler().

132 {
133  node->tsm_state = palloc0(sizeof(BernoulliSamplerData));
134 }
void * tsm_state
Definition: execnodes.h:1106
void * palloc0(Size size)
Definition: mcxt.c:878
static OffsetNumber bernoulli_nextsampletuple ( SampleScanState node,
BlockNumber  blockno,
OffsetNumber  maxoffset 
)
static

Definition at line 185 of file bernoulli.c.

References DatumGetUInt32, FirstOffsetNumber, hash(), hash_any(), InvalidOffsetNumber, BernoulliSamplerData::lt, BernoulliSamplerData::seed, and SampleScanState::tsm_state.

Referenced by tsm_bernoulli_handler().

188 {
190  OffsetNumber tupoffset = sampler->lt;
191  uint32 hashinput[3];
192 
193  /* Advance to first/next tuple in block */
194  if (tupoffset == InvalidOffsetNumber)
195  tupoffset = FirstOffsetNumber;
196  else
197  tupoffset++;
198 
199  /*
200  * We compute the hash by applying hash_any to an array of 3 uint32's
201  * containing the block, offset, and seed. This is efficient to set up,
202  * and with the current implementation of hash_any, it gives
203  * machine-independent results, which is a nice property for regression
204  * testing.
205  *
206  * These words in the hash input are the same throughout the block:
207  */
208  hashinput[0] = blockno;
209  hashinput[2] = sampler->seed;
210 
211  /*
212  * Loop over tuple offsets until finding suitable TID or reaching end of
213  * block.
214  */
215  for (; tupoffset <= maxoffset; tupoffset++)
216  {
217  uint32 hash;
218 
219  hashinput[1] = tupoffset;
220 
221  hash = DatumGetUInt32(hash_any((const unsigned char *) hashinput,
222  (int) sizeof(hashinput)));
223  if (hash < sampler->cutoff)
224  break;
225  }
226 
227  if (tupoffset > maxoffset)
228  tupoffset = InvalidOffsetNumber;
229 
230  sampler->lt = tupoffset;
231 
232  return tupoffset;
233 }
#define DatumGetUInt32(X)
Definition: postgres.h:492
void * tsm_state
Definition: execnodes.h:1106
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
unsigned int uint32
Definition: c.h:268
OffsetNumber lt
Definition: bernoulli.c:45
#define InvalidOffsetNumber
Definition: off.h:26
Datum hash_any(register const unsigned char *k, register int keylen)
Definition: hashfunc.c:307
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541
static void bernoulli_samplescangetsamplesize ( PlannerInfo root,
RelOptInfo baserel,
List paramexprs,
BlockNumber pages,
double *  tuples 
)
static

Definition at line 90 of file bernoulli.c.

References clamp_row_est(), DatumGetFloat4, estimate_expression_value(), IsA, linitial, RelOptInfo::pages, and RelOptInfo::tuples.

Referenced by tsm_bernoulli_handler().

95 {
96  Node *pctnode;
97  float4 samplefract;
98 
99  /* Try to extract an estimate for the sample percentage */
100  pctnode = (Node *) linitial(paramexprs);
101  pctnode = estimate_expression_value(root, pctnode);
102 
103  if (IsA(pctnode, Const) &&
104  !((Const *) pctnode)->constisnull)
105  {
106  samplefract = DatumGetFloat4(((Const *) pctnode)->constvalue);
107  if (samplefract >= 0 && samplefract <= 100 && !isnan(samplefract))
108  samplefract /= 100.0f;
109  else
110  {
111  /* Default samplefract if the value is bogus */
112  samplefract = 0.1f;
113  }
114  }
115  else
116  {
117  /* Default samplefract if we didn't obtain a non-null Const */
118  samplefract = 0.1f;
119  }
120 
121  /* We'll visit all pages of the baserel */
122  *pages = baserel->pages;
123 
124  *tuples = clamp_row_est(baserel->tuples * samplefract);
125 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:560
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2454
double tuples
Definition: relation.h:565
Definition: nodes.h:509
#define linitial(l)
Definition: pg_list.h:111
float float4
Definition: c.h:380
BlockNumber pages
Definition: relation.h:564
#define DatumGetFloat4(X)
Definition: postgres.h:686
double clamp_row_est(double nrows)
Definition: costsize.c:173
Datum tsm_bernoulli_handler ( PG_FUNCTION_ARGS  )

Definition at line 69 of file bernoulli.c.

References TsmRoutine::BeginSampleScan, bernoulli_beginsamplescan(), bernoulli_initsamplescan(), bernoulli_nextsampletuple(), bernoulli_samplescangetsamplesize(), TsmRoutine::EndSampleScan, FLOAT4OID, TsmRoutine::InitSampleScan, list_make1_oid, makeNode, TsmRoutine::NextSampleBlock, TsmRoutine::NextSampleTuple, NULL, TsmRoutine::parameterTypes, PG_RETURN_POINTER, TsmRoutine::repeatable_across_queries, TsmRoutine::repeatable_across_scans, and TsmRoutine::SampleScanGetSampleSize.

70 {
72 
74  tsm->repeatable_across_queries = true;
75  tsm->repeatable_across_scans = true;
79  tsm->NextSampleBlock = NULL;
81  tsm->EndSampleScan = NULL;
82 
83  PG_RETURN_POINTER(tsm);
84 }
InitSampleScan_function InitSampleScan
Definition: tsmapi.h:70
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:321
bool repeatable_across_queries
Definition: tsmapi.h:63
static void bernoulli_beginsamplescan(SampleScanState *node, Datum *params, int nparams, uint32 seed)
Definition: bernoulli.c:140
NextSampleTuple_function NextSampleTuple
Definition: tsmapi.h:73
NextSampleBlock_function NextSampleBlock
Definition: tsmapi.h:72
SampleScanGetSampleSize_function SampleScanGetSampleSize
Definition: tsmapi.h:67
#define FLOAT4OID
Definition: pg_type.h:416
#define list_make1_oid(x1)
Definition: pg_list.h:151
static void bernoulli_initsamplescan(SampleScanState *node, int eflags)
Definition: bernoulli.c:131
#define makeNode(_type_)
Definition: nodes.h:557
#define NULL
Definition: c.h:229
static OffsetNumber bernoulli_nextsampletuple(SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
Definition: bernoulli.c:185
BeginSampleScan_function BeginSampleScan
Definition: tsmapi.h:71
bool repeatable_across_scans
Definition: tsmapi.h:64
static void bernoulli_samplescangetsamplesize(PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
Definition: bernoulli.c:90
List * parameterTypes
Definition: tsmapi.h:60
EndSampleScan_function EndSampleScan
Definition: tsmapi.h:74