gooderp18绿色标准版
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

163 linhas
11KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>57.1. Sampling Method Support Functions</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="tablesample-method.html" title="Chapter 57. Writing a Table Sampling Method" /><link rel="next" href="custom-scan.html" title="Chapter 58. Writing a Custom Scan Provider" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">57.1. Sampling Method Support Functions</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="tablesample-method.html" title="Chapter 57. Writing a Table Sampling Method">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="tablesample-method.html" title="Chapter 57. Writing a Table Sampling Method">Up</a></td><th width="60%" align="center">Chapter 57. Writing a Table Sampling Method</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="custom-scan.html" title="Chapter 58. Writing a Custom Scan Provider">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="TABLESAMPLE-SUPPORT-FUNCTIONS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">57.1. Sampling Method Support Functions</h2></div></div></div><p>
  3. The TSM handler function returns a palloc'd <code class="type">TsmRoutine</code> struct
  4. containing pointers to the support functions described below. Most of
  5. the functions are required, but some are optional, and those pointers can
  6. be NULL.
  7. </p><p>
  8. </p><pre class="programlisting">
  9. void
  10. SampleScanGetSampleSize (PlannerInfo *root,
  11. RelOptInfo *baserel,
  12. List *paramexprs,
  13. BlockNumber *pages,
  14. double *tuples);
  15. </pre><p>
  16. This function is called during planning. It must estimate the number of
  17. relation pages that will be read during a sample scan, and the number of
  18. tuples that will be selected by the scan. (For example, these might be
  19. determined by estimating the sampling fraction, and then multiplying
  20. the <code class="literal">baserel-&gt;pages</code> and <code class="literal">baserel-&gt;tuples</code>
  21. numbers by that, being sure to round the results to integral values.)
  22. The <code class="literal">paramexprs</code> list holds the expression(s) that are
  23. parameters to the <code class="literal">TABLESAMPLE</code> clause. It is recommended to
  24. use <code class="function">estimate_expression_value()</code> to try to reduce these
  25. expressions to constants, if their values are needed for estimation
  26. purposes; but the function must provide size estimates even if they cannot
  27. be reduced, and it should not fail even if the values appear invalid
  28. (remember that they're only estimates of what the run-time values will be).
  29. The <code class="literal">pages</code> and <code class="literal">tuples</code> parameters are outputs.
  30. </p><p>
  31. </p><pre class="programlisting">
  32. void
  33. InitSampleScan (SampleScanState *node,
  34. int eflags);
  35. </pre><p>
  36. Initialize for execution of a SampleScan plan node.
  37. This is called during executor startup.
  38. It should perform any initialization needed before processing can start.
  39. The <code class="structname">SampleScanState</code> node has already been created, but
  40. its <code class="structfield">tsm_state</code> field is NULL.
  41. The <code class="function">InitSampleScan</code> function can palloc whatever internal
  42. state data is needed by the sampling method, and store a pointer to
  43. it in <code class="literal">node-&gt;tsm_state</code>.
  44. Information about the table to scan is accessible through other fields
  45. of the <code class="structname">SampleScanState</code> node (but note that the
  46. <code class="literal">node-&gt;ss.ss_currentScanDesc</code> scan descriptor is not set
  47. up yet).
  48. <code class="literal">eflags</code> contains flag bits describing the executor's
  49. operating mode for this plan node.
  50. </p><p>
  51. When <code class="literal">(eflags &amp; EXEC_FLAG_EXPLAIN_ONLY)</code> is true,
  52. the scan will not actually be performed, so this function should only do
  53. the minimum required to make the node state valid for <code class="command">EXPLAIN</code>
  54. and <code class="function">EndSampleScan</code>.
  55. </p><p>
  56. This function can be omitted (set the pointer to NULL), in which case
  57. <code class="function">BeginSampleScan</code> must perform all initialization needed
  58. by the sampling method.
  59. </p><p>
  60. </p><pre class="programlisting">
  61. void
  62. BeginSampleScan (SampleScanState *node,
  63. Datum *params,
  64. int nparams,
  65. uint32 seed);
  66. </pre><p>
  67. Begin execution of a sampling scan.
  68. This is called just before the first attempt to fetch a tuple, and
  69. may be called again if the scan needs to be restarted.
  70. Information about the table to scan is accessible through fields
  71. of the <code class="structname">SampleScanState</code> node (but note that the
  72. <code class="literal">node-&gt;ss.ss_currentScanDesc</code> scan descriptor is not set
  73. up yet).
  74. The <code class="literal">params</code> array, of length <code class="literal">nparams</code>, contains the
  75. values of the parameters supplied in the <code class="literal">TABLESAMPLE</code> clause.
  76. These will have the number and types specified in the sampling
  77. method's <code class="literal">parameterTypes</code> list, and have been checked
  78. to not be null.
  79. <code class="literal">seed</code> contains a seed to use for any random numbers generated
  80. within the sampling method; it is either a hash derived from the
  81. <code class="literal">REPEATABLE</code> value if one was given, or the result
  82. of <code class="literal">random()</code> if not.
  83. </p><p>
  84. This function may adjust the fields <code class="literal">node-&gt;use_bulkread</code>
  85. and <code class="literal">node-&gt;use_pagemode</code>.
  86. If <code class="literal">node-&gt;use_bulkread</code> is <code class="literal">true</code>, which it is by
  87. default, the scan will use a buffer access strategy that encourages
  88. recycling buffers after use. It might be reasonable to set this
  89. to <code class="literal">false</code> if the scan will visit only a small fraction of the
  90. table's pages.
  91. If <code class="literal">node-&gt;use_pagemode</code> is <code class="literal">true</code>, which it is by
  92. default, the scan will perform visibility checking in a single pass for
  93. all tuples on each visited page. It might be reasonable to set this
  94. to <code class="literal">false</code> if the scan will select only a small fraction of the
  95. tuples on each visited page. That will result in fewer tuple visibility
  96. checks being performed, though each one will be more expensive because it
  97. will require more locking.
  98. </p><p>
  99. If the sampling method is
  100. marked <code class="literal">repeatable_across_scans</code>, it must be able to
  101. select the same set of tuples during a rescan as it did originally, that is
  102. a fresh call of <code class="function">BeginSampleScan</code> must lead to selecting the
  103. same tuples as before (if the <code class="literal">TABLESAMPLE</code> parameters
  104. and seed don't change).
  105. </p><p>
  106. </p><pre class="programlisting">
  107. BlockNumber
  108. NextSampleBlock (SampleScanState *node, BlockNumber nblocks);
  109. </pre><p>
  110. Returns the block number of the next page to be scanned, or
  111. <code class="literal">InvalidBlockNumber</code> if no pages remain to be scanned.
  112. </p><p>
  113. This function can be omitted (set the pointer to NULL), in which case
  114. the core code will perform a sequential scan of the entire relation.
  115. Such a scan can use synchronized scanning, so that the sampling method
  116. cannot assume that the relation pages are visited in the same order on
  117. each scan.
  118. </p><p>
  119. </p><pre class="programlisting">
  120. OffsetNumber
  121. NextSampleTuple (SampleScanState *node,
  122. BlockNumber blockno,
  123. OffsetNumber maxoffset);
  124. </pre><p>
  125. Returns the offset number of the next tuple to be sampled on the
  126. specified page, or <code class="literal">InvalidOffsetNumber</code> if no tuples remain to
  127. be sampled. <code class="literal">maxoffset</code> is the largest offset number in use
  128. on the page.
  129. </p><div class="note"><h3 class="title">Note</h3><p>
  130. <code class="function">NextSampleTuple</code> is not explicitly told which of the offset
  131. numbers in the range <code class="literal">1 .. maxoffset</code> actually contain valid
  132. tuples. This is not normally a problem since the core code ignores
  133. requests to sample missing or invisible tuples; that should not result in
  134. any bias in the sample. However, if necessary, the function can use
  135. <code class="literal">node-&gt;donetuples</code> to examine how many of the tuples
  136. it returned were valid and visible.
  137. </p></div><div class="note"><h3 class="title">Note</h3><p>
  138. <code class="function">NextSampleTuple</code> must <span class="emphasis"><em>not</em></span> assume
  139. that <code class="literal">blockno</code> is the same page number returned by the most
  140. recent <code class="function">NextSampleBlock</code> call. It was returned by some
  141. previous <code class="function">NextSampleBlock</code> call, but the core code is allowed
  142. to call <code class="function">NextSampleBlock</code> in advance of actually scanning
  143. pages, so as to support prefetching. It is OK to assume that once
  144. sampling of a given page begins, successive <code class="function">NextSampleTuple</code>
  145. calls all refer to the same page until <code class="literal">InvalidOffsetNumber</code> is
  146. returned.
  147. </p></div><p>
  148. </p><pre class="programlisting">
  149. void
  150. EndSampleScan (SampleScanState *node);
  151. </pre><p>
  152. End the scan and release resources. It is normally not important
  153. to release palloc'd memory, but any externally-visible resources
  154. should be cleaned up.
  155. This function can be omitted (set the pointer to NULL) in the common
  156. case where no such resources exist.
  157. </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="tablesample-method.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="tablesample-method.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="custom-scan.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 57. Writing a Table Sampling Method </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Chapter 58. Writing a Custom Scan Provider</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1