PostgreSQL Source Code  git master
system.c File Reference
#include "postgres.h"
#include <math.h>
#include "access/relscan.h"
#include "access/tsmapi.h"
#include "catalog/pg_type.h"
#include "optimizer/optimizer.h"
#include "utils/builtins.h"
#include "utils/hashutils.h"
Include dependency graph for system.c:

Go to the source code of this file.

Data Structures

struct  SystemSamplerData
 

Functions

static void system_samplescangetsamplesize (PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
 
static void system_initsamplescan (SampleScanState *node, int eflags)
 
static void system_beginsamplescan (SampleScanState *node, Datum *params, int nparams, uint32 seed)
 
static BlockNumber system_nextsampleblock (SampleScanState *node, BlockNumber nblocks)
 
static OffsetNumber system_nextsampletuple (SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
 
Datum tsm_system_handler (PG_FUNCTION_ARGS)
 

Function Documentation

◆ system_beginsamplescan()

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

Definition at line 140 of file system.c.

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

Referenced by tsm_system_handler().

144 {
145  SystemSamplerData *sampler = (SystemSamplerData *) node->tsm_state;
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->nextblock = 0;
163  sampler->lt = InvalidOffsetNumber;
164 
165  /*
166  * Bulkread buffer access strategy probably makes sense unless we're
167  * scanning a very small fraction of the table. The 1% cutoff here is a
168  * guess. We should use pagemode visibility checking, since we scan all
169  * tuples on each selected page.
170  */
171  node->use_bulkread = (percent >= 1);
172  node->use_pagemode = true;
173 }
void * tsm_state
Definition: execnodes.h:1358
int errcode(int sqlerrcode)
Definition: elog.c:570
#define PG_UINT32_MAX
Definition: c.h:442
#define ERROR
Definition: elog.h:43
double rint(double x)
Definition: rint.c:21
#define ereport(elevel, rest)
Definition: elog.h:141
OffsetNumber lt
Definition: system.c:43
uint64 cutoff
Definition: system.c:40
#define InvalidOffsetNumber
Definition: off.h:26
uint32 seed
Definition: system.c:41
#define DatumGetFloat4(X)
Definition: postgres.h:680
BlockNumber nextblock
Definition: system.c:42
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ system_initsamplescan()

static void system_initsamplescan ( SampleScanState node,
int  eflags 
)
static

Definition at line 131 of file system.c.

References palloc0(), and SampleScanState::tsm_state.

Referenced by tsm_system_handler().

132 {
133  node->tsm_state = palloc0(sizeof(SystemSamplerData));
134 }
void * tsm_state
Definition: execnodes.h:1358
void * palloc0(Size size)
Definition: mcxt.c:980

◆ system_nextsampleblock()

static BlockNumber system_nextsampleblock ( SampleScanState node,
BlockNumber  nblocks 
)
static

Definition at line 179 of file system.c.

References DatumGetUInt32, hash(), hash_any(), InvalidBlockNumber, SystemSamplerData::nextblock, SystemSamplerData::seed, and SampleScanState::tsm_state.

Referenced by tsm_system_handler().

180 {
181  SystemSamplerData *sampler = (SystemSamplerData *) node->tsm_state;
182  BlockNumber nextblock = sampler->nextblock;
183  uint32 hashinput[2];
184 
185  /*
186  * We compute the hash by applying hash_any to an array of 2 uint32's
187  * containing the block number and seed. This is efficient to set up, and
188  * with the current implementation of hash_any, it gives
189  * machine-independent results, which is a nice property for regression
190  * testing.
191  *
192  * These words in the hash input are the same throughout the block:
193  */
194  hashinput[1] = sampler->seed;
195 
196  /*
197  * Loop over block numbers until finding suitable block or reaching end of
198  * relation.
199  */
200  for (; nextblock < nblocks; nextblock++)
201  {
202  uint32 hash;
203 
204  hashinput[0] = nextblock;
205 
206  hash = DatumGetUInt32(hash_any((const unsigned char *) hashinput,
207  (int) sizeof(hashinput)));
208  if (hash < sampler->cutoff)
209  break;
210  }
211 
212  if (nextblock < nblocks)
213  {
214  /* Found a suitable block; remember where we should start next time */
215  sampler->nextblock = nextblock + 1;
216  return nextblock;
217  }
218 
219  /* Done, but let's reset nextblock to 0 for safety. */
220  sampler->nextblock = 0;
221  return InvalidBlockNumber;
222 }
#define DatumGetUInt32(X)
Definition: postgres.h:486
Datum hash_any(const unsigned char *k, int keylen)
Definition: hashfn.c:148
void * tsm_state
Definition: execnodes.h:1358
uint32 BlockNumber
Definition: block.h:31
unsigned int uint32
Definition: c.h:358
uint32 seed
Definition: system.c:41
#define InvalidBlockNumber
Definition: block.h:33
BlockNumber nextblock
Definition: system.c:42
static unsigned hash(unsigned *uv, int n)
Definition: rege_dfa.c:541

◆ system_nextsampletuple()

static OffsetNumber system_nextsampletuple ( SampleScanState node,
BlockNumber  blockno,
OffsetNumber  maxoffset 
)
static

Definition at line 237 of file system.c.

References FirstOffsetNumber, InvalidOffsetNumber, SystemSamplerData::lt, and SampleScanState::tsm_state.

Referenced by tsm_system_handler().

240 {
241  SystemSamplerData *sampler = (SystemSamplerData *) node->tsm_state;
242  OffsetNumber tupoffset = sampler->lt;
243 
244  /* Advance to next possible offset on page */
245  if (tupoffset == InvalidOffsetNumber)
246  tupoffset = FirstOffsetNumber;
247  else
248  tupoffset++;
249 
250  /* Done? */
251  if (tupoffset > maxoffset)
252  tupoffset = InvalidOffsetNumber;
253 
254  sampler->lt = tupoffset;
255 
256  return tupoffset;
257 }
void * tsm_state
Definition: execnodes.h:1358
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
OffsetNumber lt
Definition: system.c:43
#define InvalidOffsetNumber
Definition: off.h:26

◆ system_samplescangetsamplesize()

static void system_samplescangetsamplesize ( PlannerInfo root,
RelOptInfo baserel,
List paramexprs,
BlockNumber pages,
double *  tuples 
)
static

Definition at line 89 of file system.c.

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

Referenced by tsm_system_handler().

94 {
95  Node *pctnode;
96  float4 samplefract;
97 
98  /* Try to extract an estimate for the sample percentage */
99  pctnode = (Node *) linitial(paramexprs);
100  pctnode = estimate_expression_value(root, pctnode);
101 
102  if (IsA(pctnode, Const) &&
103  !((Const *) pctnode)->constisnull)
104  {
105  samplefract = DatumGetFloat4(((Const *) pctnode)->constvalue);
106  if (samplefract >= 0 && samplefract <= 100 && !isnan(samplefract))
107  samplefract /= 100.0f;
108  else
109  {
110  /* Default samplefract if the value is bogus */
111  samplefract = 0.1f;
112  }
113  }
114  else
115  {
116  /* Default samplefract if we didn't obtain a non-null Const */
117  samplefract = 0.1f;
118  }
119 
120  /* We'll visit a sample of the pages ... */
121  *pages = clamp_row_est(baserel->pages * samplefract);
122 
123  /* ... and hopefully get a representative number of tuples from them */
124  *tuples = clamp_row_est(baserel->tuples * samplefract);
125 }
#define IsA(nodeptr, _type_)
Definition: nodes.h:576
Node * estimate_expression_value(PlannerInfo *root, Node *node)
Definition: clauses.c:2286
double tuples
Definition: pathnodes.h:681
Definition: nodes.h:525
#define linitial(l)
Definition: pg_list.h:195
float float4
Definition: c.h:490
BlockNumber pages
Definition: pathnodes.h:680
#define DatumGetFloat4(X)
Definition: postgres.h:680
double clamp_row_est(double nrows)
Definition: costsize.c:187

◆ tsm_system_handler()

Datum tsm_system_handler ( PG_FUNCTION_ARGS  )

Definition at line 68 of file system.c.

References TsmRoutine::BeginSampleScan, TsmRoutine::EndSampleScan, TsmRoutine::InitSampleScan, list_make1_oid, makeNode, TsmRoutine::NextSampleBlock, TsmRoutine::NextSampleTuple, TsmRoutine::parameterTypes, PG_RETURN_POINTER, TsmRoutine::repeatable_across_queries, TsmRoutine::repeatable_across_scans, TsmRoutine::SampleScanGetSampleSize, system_beginsamplescan(), system_initsamplescan(), system_nextsampleblock(), system_nextsampletuple(), and system_samplescangetsamplesize().

69 {
71 
72  tsm->parameterTypes = list_make1_oid(FLOAT4OID);
73  tsm->repeatable_across_queries = true;
74  tsm->repeatable_across_scans = true;
80  tsm->EndSampleScan = NULL;
81 
82  PG_RETURN_POINTER(tsm);
83 }
InitSampleScan_function InitSampleScan
Definition: tsmapi.h:71
#define PG_RETURN_POINTER(x)
Definition: fmgr.h:351
static OffsetNumber system_nextsampletuple(SampleScanState *node, BlockNumber blockno, OffsetNumber maxoffset)
Definition: system.c:237
bool repeatable_across_queries
Definition: tsmapi.h:64
static void system_initsamplescan(SampleScanState *node, int eflags)
Definition: system.c:131
NextSampleTuple_function NextSampleTuple
Definition: tsmapi.h:74
NextSampleBlock_function NextSampleBlock
Definition: tsmapi.h:73
SampleScanGetSampleSize_function SampleScanGetSampleSize
Definition: tsmapi.h:68
#define list_make1_oid(x1)
Definition: pg_list.h:249
static BlockNumber system_nextsampleblock(SampleScanState *node, BlockNumber nblocks)
Definition: system.c:179
#define makeNode(_type_)
Definition: nodes.h:573
BeginSampleScan_function BeginSampleScan
Definition: tsmapi.h:72
bool repeatable_across_scans
Definition: tsmapi.h:65
static void system_beginsamplescan(SampleScanState *node, Datum *params, int nparams, uint32 seed)
Definition: system.c:140
List * parameterTypes
Definition: tsmapi.h:61
static void system_samplescangetsamplesize(PlannerInfo *root, RelOptInfo *baserel, List *paramexprs, BlockNumber *pages, double *tuples)
Definition: system.c:89
EndSampleScan_function EndSampleScan
Definition: tsmapi.h:75