Home Explore Blog CI



postgresql

12th chunk of `doc/src/sgml/fdwhandler.sgml`
48ce9f2376ddcf11aa095f8a52a856b21d627d76d114b54d0000000100000fa6
 rely on attribute numbers of the
     foreign table to index into <literal>planSlot</literal>.
     Also, <literal>planSlot</literal> typically contains
     additional <quote>junk</quote> columns.  In particular, any junk columns
     that were requested by <function>AddForeignUpdateTargets</function> will
     be available from this slot.
    </para>

    <para>
     The return value is either a slot containing the row as it was actually
     updated (this might differ from the data supplied, for example as a
     result of trigger actions), or NULL if no row was actually updated
     (again, typically as a result of triggers).  The passed-in
     <literal>slot</literal> can be re-used for this purpose.
    </para>

    <para>
     The data in the returned slot is used only if the <command>UPDATE</command>
     statement has a <literal>RETURNING</literal> clause or involves a view
     <literal>WITH CHECK OPTION</literal>; or if the foreign table has
     an <literal>AFTER ROW</literal> trigger.  Triggers require all columns,
     but the FDW could choose to optimize away returning some or all columns
     depending on the contents of the <literal>RETURNING</literal> clause or
     <literal>WITH CHECK OPTION</literal> constraints.  Regardless, some slot
     must be returned to indicate success, or the query's reported row count
     will be wrong.
    </para>

    <para>
     If the <function>ExecForeignUpdate</function> pointer is set to
     <literal>NULL</literal>, attempts to update the foreign table will fail
     with an error message.
    </para>

    <para>
<programlisting>
TupleTableSlot *
ExecForeignDelete(EState *estate,
                  ResultRelInfo *rinfo,
                  TupleTableSlot *slot,
                  TupleTableSlot *planSlot);
</programlisting>

     Delete one tuple from the foreign table.
     <literal>estate</literal> is global execution state for the query.
     <literal>rinfo</literal> is the <structname>ResultRelInfo</structname> struct describing
     the target foreign table.
     <literal>slot</literal> contains nothing useful upon call, but can be used to
     hold the returned tuple.
     <literal>planSlot</literal> contains the tuple that was generated by the
     <structname>ModifyTable</structname> plan node's subplan; in particular, it will
     carry any junk columns that were requested by
     <function>AddForeignUpdateTargets</function>.  The junk column(s) must be used
     to identify the tuple to be deleted.
    </para>

    <para>
     The return value is either a slot containing the row that was deleted,
     or NULL if no row was deleted (typically as a result of triggers).  The
     passed-in <literal>slot</literal> can be used to hold the tuple to be returned.
    </para>

    <para>
     The data in the returned slot is used only if the <command>DELETE</command>
     query has a <literal>RETURNING</literal> clause or the foreign table has
     an <literal>AFTER ROW</literal> trigger.  Triggers require all columns, but the
     FDW could choose to optimize away returning some or all columns depending
     on the contents of the <literal>RETURNING</literal> clause.  Regardless, some
     slot must be returned to indicate success, or the query's reported row
     count will be wrong.
    </para>

    <para>
     If the <function>ExecForeignDelete</function> pointer is set to
     <literal>NULL</literal>, attempts to delete from the foreign table will fail
     with an error message.
    </para>

    <para>
<programlisting>
void
EndForeignModify(EState *estate,
                 ResultRelInfo *rinfo);
</programlisting>

     End the table update and release resources.  It is normally not important
     to release palloc'd memory, but for example open files and connections
     to remote servers should be cleaned up.
    </para>

    <para>
     If the <function>EndForeignModify</function> pointer is set to
     <literal>NULL</literal>, no action is taken during executor shutdown.

Title: Foreign Table Update and Delete Routines: ExecForeignUpdate, ExecForeignDelete, and EndForeignModify
Summary
This section explains the usage and return values of `ExecForeignUpdate` and `ExecForeignDelete`, focusing on the scenarios where the returned data is used (e.g., `RETURNING` clause, `AFTER ROW` triggers) and the importance of returning a slot to ensure correct row counts. It notes that `ExecForeignUpdate` will fail if its pointer is NULL. It then introduces `ExecForeignDelete`, explaining that the `planSlot` must be used to identify the tuple to be deleted. Lastly, it describes `EndForeignModify`, which releases resources after the table update, and notes that if its pointer is NULL, no action is taken during executor shutdown.