Home Explore Blog CI



postgresql

50th chunk of `doc/src/sgml/xfunc.sgml`
1c61516344094d345ecce5fc625b88c449ae06ada4d8d6b30000000100000fa0
 including the backends started after
     <literal>InjectionPointAttach</literal> is called. It remains attached
     while the server is running or until the injection point is detached
     using <literal>InjectionPointDetach</literal>.
    </para>

    <para>
     An example can be found in
     <filename>src/test/modules/injection_points</filename> in the PostgreSQL
     source tree.
    </para>

    <para>
     Enabling injections points requires
     <option>--enable-injection-points</option> with
     <command>configure</command> or <option>-Dinjection_points=true</option>
     with <application>Meson</application>.
    </para>
   </sect2>

   <sect2 id="xfunc-addin-custom-cumulative-statistics">
    <title>Custom Cumulative Statistics</title>

    <para>
     It is possible for add-ins written in C-language to use custom types
     of cumulative statistics registered in the
     <link linkend="monitoring-stats-setup">Cumulative Statistics System</link>.
    </para>

    <para>
     First, define a <literal>PgStat_KindInfo</literal> that includes all
     the information related to the custom type registered. For example:
<programlisting>
static const PgStat_KindInfo custom_stats = {
    .name = "custom_stats",
    .fixed_amount = false,
    .shared_size = sizeof(PgStatShared_Custom),
    .shared_data_off = offsetof(PgStatShared_Custom, stats),
    .shared_data_len = sizeof(((PgStatShared_Custom *) 0)->stats),
    .pending_size = sizeof(PgStat_StatCustomEntry),
}
</programlisting>

     Then, each backend that needs to use this custom type needs to register
     it with <literal>pgstat_register_kind</literal> and a unique ID used to
     store the entries related to this type of statistics:
<programlisting>
extern PgStat_Kind pgstat_register_kind(PgStat_Kind kind,
                                        const PgStat_KindInfo *kind_info);
</programlisting>
     While developing a new extension, use
     <literal>PGSTAT_KIND_EXPERIMENTAL</literal> for
     <parameter>kind</parameter>. When you are ready to release the extension
     to users, reserve a kind ID at the
     <ulink url="https://wiki.postgresql.org/wiki/CustomCumulativeStats">
     Custom Cumulative Statistics</ulink> page.
    </para>

    <para>
     The details of the API for <literal>PgStat_KindInfo</literal> can
     be found in <filename>src/include/utils/pgstat_internal.h</filename>.
    </para>

    <para>
     The type of statistics registered is associated with a name and a unique
     ID shared across the server in shared memory. Each backend using a
     custom type of statistics maintains a local cache storing the information
     of each custom <literal>PgStat_KindInfo</literal>.
    </para>

    <para>
     Place the extension module implementing the custom cumulative statistics
     type in <xref linkend="guc-shared-preload-libraries"/> so that it will
     be loaded early during <productname>PostgreSQL</productname> startup.
    </para>

    <para>
     An example describing how to register and use custom statistics can be
     found in <filename>src/test/modules/injection_points</filename>.
    </para>
   </sect2>

   <sect2 id="extend-cpp">
    <title>Using C++ for Extensibility</title>

    <indexterm zone="extend-cpp">
     <primary>C++</primary>
    </indexterm>

    <para>
     Although the <productname>PostgreSQL</productname> backend is written in
     C, it is possible to write extensions in C++ if these guidelines are
     followed:

     <itemizedlist>
      <listitem>
       <para>
         All functions accessed by the backend must present a C interface
         to the backend;  these C functions can then call C++ functions.
         For example, <literal>extern C</literal> linkage is required for
         backend-accessed functions.  This is also necessary for any
         functions that are passed as pointers between the backend and
         C++ code.
       </para>
      </listitem>
      <listitem>
       <para>
        Free

Title: Custom Cumulative Statistics and Using C++ for Extensibility in PostgreSQL
Summary
This section discusses how C-language add-ins can use custom cumulative statistics registered in the Cumulative Statistics System. It details defining a PgStat_KindInfo, registering with pgstat_register_kind, and providing a unique ID. The section also covers using C++ for extensibility, requiring C interfaces for backend-accessed functions and careful memory management to avoid leaks.