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