summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/tablesample-method.html
blob: c61f25f9719e0d7b600fdbebe555a9425e85f592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!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>Chapter 60. Writing a Table Sampling Method</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 Vsnapshot" /><link rel="prev" href="fdw-row-locking.html" title="59.5. Row Locking in Foreign Data Wrappers" /><link rel="next" href="tablesample-support-functions.html" title="60.1. Sampling Method Support Functions" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">Chapter 60. Writing a Table Sampling Method</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="fdw-row-locking.html" title="59.5. Row Locking in Foreign Data Wrappers">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="internals.html" title="Part VII. Internals">Up</a></td><th width="60%" align="center">Part VII. Internals</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 15.6 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="tablesample-support-functions.html" title="60.1. Sampling Method Support Functions">Next</a></td></tr></table><hr /></div><div class="chapter" id="TABLESAMPLE-METHOD"><div class="titlepage"><div><div><h2 class="title">Chapter 60. Writing a Table Sampling Method</h2></div></div></div><div class="toc"><p><strong>Table of Contents</strong></p><dl class="toc"><dt><span class="sect1"><a href="tablesample-support-functions.html">60.1. Sampling Method Support Functions</a></span></dt></dl></div><a id="id-1.10.11.2" class="indexterm"></a><a id="id-1.10.11.3" class="indexterm"></a><p>
  <span class="productname">PostgreSQL</span>'s implementation of the <code class="literal">TABLESAMPLE</code>
  clause supports custom table sampling methods, in addition to
  the <code class="literal">BERNOULLI</code> and <code class="literal">SYSTEM</code> methods that are required
  by the SQL standard.  The sampling method determines which rows of the
  table will be selected when the <code class="literal">TABLESAMPLE</code> clause is used.
 </p><p>
  At the SQL level, a table sampling method is represented by a single SQL
  function, typically implemented in C, having the signature
</p><pre class="programlisting">
method_name(internal) RETURNS tsm_handler
</pre><p>
  The name of the function is the same method name appearing in the
  <code class="literal">TABLESAMPLE</code> clause.  The <code class="type">internal</code> argument is a dummy
  (always having value zero) that simply serves to prevent this function from
  being called directly from an SQL command.
  The result of the function must be a palloc'd struct of
  type <code class="type">TsmRoutine</code>, which contains pointers to support functions for
  the sampling method.  These support functions are plain C functions and
  are not visible or callable at the SQL level.  The support functions are
  described in <a class="xref" href="tablesample-support-functions.html" title="60.1. Sampling Method Support Functions">Section 60.1</a>.
 </p><p>
  In addition to function pointers, the <code class="type">TsmRoutine</code> struct must
  provide these additional fields:
 </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">List *parameterTypes</code></span></dt><dd><p>
     This is an OID list containing the data type OIDs of the parameter(s)
     that will be accepted by the <code class="literal">TABLESAMPLE</code> clause when this
     sampling method is used.  For example, for the built-in methods, this
     list contains a single item with value <code class="literal">FLOAT4OID</code>, which
     represents the sampling percentage.  Custom sampling methods can have
     more or different parameters.
    </p></dd><dt><span class="term"><code class="literal">bool repeatable_across_queries</code></span></dt><dd><p>
     If <code class="literal">true</code>, the sampling method can deliver identical samples
     across successive queries, if the same parameters
     and <code class="literal">REPEATABLE</code> seed value are supplied each time and the
     table contents have not changed.  When this is <code class="literal">false</code>,
     the <code class="literal">REPEATABLE</code> clause is not accepted for use with the
     sampling method.
    </p></dd><dt><span class="term"><code class="literal">bool repeatable_across_scans</code></span></dt><dd><p>
     If <code class="literal">true</code>, the sampling method can deliver identical samples
     across successive scans in the same query (assuming unchanging
     parameters, seed value, and snapshot).
     When this is <code class="literal">false</code>, the planner will not select plans that
     would require scanning the sampled table more than once, since that
     might result in inconsistent query output.
    </p></dd></dl></div><p>
  The <code class="type">TsmRoutine</code> struct type is declared
  in <code class="filename">src/include/access/tsmapi.h</code>, which see for additional
  details.
 </p><p>
  The table sampling methods included in the standard distribution are good
  references when trying to write your own.  Look into
  the <code class="filename">src/backend/access/tablesample</code> subdirectory of the source
  tree for the built-in sampling methods, and into the <code class="filename">contrib</code>
  subdirectory for add-on methods.
 </p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="fdw-row-locking.html" title="59.5. Row Locking in Foreign Data Wrappers">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="internals.html" title="Part VII. Internals">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="tablesample-support-functions.html" title="60.1. Sampling Method Support Functions">Next</a></td></tr><tr><td width="40%" align="left" valign="top">59.5. Row Locking in Foreign Data Wrappers </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.6 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 60.1. Sampling Method Support Functions</td></tr></table></div></body></html>