<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Executing Custom Scans</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 9.6.17 Documentation" HREF="index.html"><LINK REL="UP" TITLE="Writing A Custom Scan Provider" HREF="custom-scan.html"><LINK REL="PREVIOUS" TITLE="Creating Custom Scan Plans" HREF="custom-scan-plan.html"><LINK REL="NEXT" TITLE="Genetic Query Optimizer" HREF="geqo.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2020-02-15T12:30:38"></HEAD ><BODY CLASS="SECT1" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="4" ALIGN="center" VALIGN="bottom" ><A HREF="index.html" >PostgreSQL 9.6.17 Documentation</A ></TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A TITLE="Creating Custom Scan Plans" HREF="custom-scan-plan.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="custom-scan.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" >Chapter 57. Writing A Custom Scan Provider</TD ><TD WIDTH="20%" ALIGN="right" VALIGN="top" ><A TITLE="Genetic Query Optimizer" HREF="geqo.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="CUSTOM-SCAN-EXECUTION" >57.3. Executing Custom Scans</A ></H1 ><P > When a <TT CLASS="STRUCTFIELD" >CustomScan</TT > is executed, its execution state is represented by a <TT CLASS="STRUCTFIELD" >CustomScanState</TT >, which is declared as follows: </P><PRE CLASS="PROGRAMLISTING" >typedef struct CustomScanState { ScanState ss; uint32 flags; const CustomExecMethods *methods; } CustomScanState;</PRE ><P> </P ><P > <TT CLASS="STRUCTFIELD" >ss</TT > is initialized as for any other scan state, except that if the scan is for a join rather than a base relation, <TT CLASS="LITERAL" >ss.ss_currentRelation</TT > is left NULL. <TT CLASS="STRUCTFIELD" >flags</TT > is a bit mask with the same meaning as in <TT CLASS="STRUCTNAME" >CustomPath</TT > and <TT CLASS="STRUCTNAME" >CustomScan</TT >. <TT CLASS="STRUCTFIELD" >methods</TT > must point to a (usually statically allocated) object implementing the required custom scan state methods, which are further detailed below. Typically, a <TT CLASS="STRUCTNAME" >CustomScanState</TT >, which need not support <CODE CLASS="FUNCTION" >copyObject</CODE >, will actually be a larger structure embedding the above as its first member. </P ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="CUSTOM-SCAN-EXECUTION-CALLBACKS" >57.3.1. Custom Scan Execution Callbacks</A ></H2 ><P ></P><PRE CLASS="PROGRAMLISTING" >void (*BeginCustomScan) (CustomScanState *node, EState *estate, int eflags);</PRE ><P> Complete initialization of the supplied <TT CLASS="STRUCTNAME" >CustomScanState</TT >. Standard fields have been initialized by <CODE CLASS="FUNCTION" >ExecInitCustomScan</CODE >, but any private fields should be initialized here. </P ><P ></P><PRE CLASS="PROGRAMLISTING" >TupleTableSlot *(*ExecCustomScan) (CustomScanState *node);</PRE ><P> Fetch the next scan tuple. If any tuples remain, it should fill <TT CLASS="LITERAL" >ps_ResultTupleSlot</TT > with the next tuple in the current scan direction, and then return the tuple slot. If not, <TT CLASS="LITERAL" >NULL</TT > or an empty slot should be returned. </P ><P ></P><PRE CLASS="PROGRAMLISTING" >void (*EndCustomScan) (CustomScanState *node);</PRE ><P> Clean up any private data associated with the <TT CLASS="LITERAL" >CustomScanState</TT >. This method is required, but it does not need to do anything if there is no associated data or it will be cleaned up automatically. </P ><P ></P><PRE CLASS="PROGRAMLISTING" >void (*ReScanCustomScan) (CustomScanState *node);</PRE ><P> Rewind the current scan to the beginning and prepare to rescan the relation. </P ><P ></P><PRE CLASS="PROGRAMLISTING" >void (*MarkPosCustomScan) (CustomScanState *node);</PRE ><P> Save the current scan position so that it can subsequently be restored by the <CODE CLASS="FUNCTION" >RestrPosCustomScan</CODE > callback. This callback is optional, and need only be supplied if the <TT CLASS="LITERAL" >CUSTOMPATH_SUPPORT_MARK_RESTORE</TT > flag is set. </P ><P ></P><PRE CLASS="PROGRAMLISTING" >void (*RestrPosCustomScan) (CustomScanState *node);</PRE ><P> Restore the previous scan position as saved by the <CODE CLASS="FUNCTION" >MarkPosCustomScan</CODE > callback. This callback is optional, and need only be supplied if the <TT CLASS="LITERAL" >CUSTOMPATH_SUPPORT_MARK_RESTORE</TT > flag is set. </P ><P ></P><PRE CLASS="PROGRAMLISTING" >Size (*EstimateDSMCustomScan) (CustomScanState *node, ParallelContext *pcxt);</PRE ><P> Estimate the amount of dynamic shared memory that will be required for parallel operation. This may be higher than the amount that will actually be used, but it must not be lower. The return value is in bytes. This callback is optional, and need only be supplied if this custom scan provider supports parallel execution. </P ><P ></P><PRE CLASS="PROGRAMLISTING" >void (*InitializeDSMCustomScan) (CustomScanState *node, ParallelContext *pcxt, void *coordinate);</PRE ><P> Initialize the dynamic shared memory that will be required for parallel operation; <TT CLASS="LITERAL" >coordinate</TT > points to an amount of allocated space equal to the return value of <CODE CLASS="FUNCTION" >EstimateDSMCustomScan</CODE >. This callback is optional, and need only be supplied if this custom scan provider supports parallel execution. </P ><P ></P><PRE CLASS="PROGRAMLISTING" >void (*InitializeWorkerCustomScan) (CustomScanState *node, shm_toc *toc, void *coordinate);</PRE ><P> Initialize a parallel worker's custom state based on the shared state set up in the leader by <TT CLASS="LITERAL" >InitializeDSMCustomScan</TT >. This callback is optional, and needs only be supplied if this custom path supports parallel execution. </P ><P ></P><PRE CLASS="PROGRAMLISTING" >void (*ExplainCustomScan) (CustomScanState *node, List *ancestors, ExplainState *es);</PRE ><P> Output additional information for <TT CLASS="COMMAND" >EXPLAIN</TT > of a custom-scan plan node. This callback is optional. Common data stored in the <TT CLASS="STRUCTNAME" >ScanState</TT >, such as the target list and scan relation, will be shown even without this callback, but the callback allows the display of additional, private state. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="custom-scan-plan.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="geqo.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Creating Custom Scan Plans</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="custom-scan.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Genetic Query Optimizer</TD ></TR ></TABLE ></DIV ></BODY ></HTML >