Home Explore Blog CI



postgresql

52th chunk of `doc/src/sgml/spi.sgml`
a0e9bbf48bb279864b17cb42c1ea0e55a8074f11beb2be060000000100000c51
 tuptable->vals[j];
            int i;

            for (i = 1, buf[0] = 0; i <= tupdesc->natts; i++)
                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %s%s",
                        SPI_getvalue(tuple, tupdesc, i),
                        (i == tupdesc->natts) ? " " : " |");
            elog(INFO, "EXECQ: %s", buf);
        }
    }

    SPI_finish();
    pfree(command);

    PG_RETURN_INT64(proc);
}
</programlisting>

  <para>
   This is how you declare the function after having compiled it into
   a shared library (details are in <xref linkend="dfunc"/>.):

<programlisting>
CREATE FUNCTION execq(text, integer) RETURNS int8
    AS '<replaceable>filename</replaceable>'
    LANGUAGE C STRICT;
</programlisting>
  </para>

  <para>
   Here is a sample session:

<programlisting>
=&gt; SELECT execq('CREATE TABLE a (x integer)', 0);
 execq
-------
     0
(1 row)

=&gt; INSERT INTO a VALUES (execq('INSERT INTO a VALUES (0)', 0));
INSERT 0 1
=&gt; SELECT execq('SELECT * FROM a', 0);
INFO:  EXECQ:  0    <lineannotation>-- inserted by execq</lineannotation>
INFO:  EXECQ:  1    <lineannotation>-- returned by execq and inserted by upper INSERT</lineannotation>

 execq
-------
     2
(1 row)

=&gt; SELECT execq('INSERT INTO a SELECT x + 2 FROM a RETURNING *', 1);
INFO:  EXECQ:  2    <lineannotation>-- 0 + 2, then execution was stopped by count</lineannotation>
 execq
-------
     1
(1 row)

=&gt; SELECT execq('SELECT * FROM a', 10);
INFO:  EXECQ:  0
INFO:  EXECQ:  1
INFO:  EXECQ:  2

 execq
-------
     3              <lineannotation>-- 10 is the max value only, 3 is the real number of rows</lineannotation>
(1 row)

=&gt; SELECT execq('INSERT INTO a SELECT x + 10 FROM a', 1);
 execq
-------
     3              <lineannotation>-- all rows processed; count does not stop it, because nothing is returned</lineannotation>
(1 row)

=&gt; SELECT * FROM a;
 x
----
  0
  1
  2
 10
 11
 12
(6 rows)

=&gt; DELETE FROM a;
DELETE 6
=&gt; INSERT INTO a VALUES (execq('SELECT * FROM a', 0) + 1);
INSERT 0 1
=&gt; SELECT * FROM a;
 x
---
 1                  <lineannotation>-- 0 (no rows in a) + 1</lineannotation>
(1 row)

=&gt; INSERT INTO a VALUES (execq('SELECT * FROM a', 0) + 1);
INFO:  EXECQ:  1
INSERT 0 1
=&gt; SELECT * FROM a;
 x
---
 1
 2                  <lineannotation>-- 1 (there was one row in a) + 1</lineannotation>
(2 rows)

<lineannotation>-- This demonstrates the data changes visibility rule.</lineannotation>
<lineannotation>-- execq is called twice and sees different numbers of rows each time:</lineannotation>

=&gt; INSERT INTO a SELECT execq('SELECT * FROM a', 0) * x FROM a;
INFO:  EXECQ:  1    <lineannotation>-- results from first execq</lineannotation>
INFO:  EXECQ:  2
INFO:  EXECQ:  1    <lineannotation>-- results from second execq</lineannotation>
INFO:  EXECQ:  2
INFO:  EXECQ:  2
INSERT 0 2
=&gt; SELECT * FROM a;
 x
---
 1
 2
 2                  <lineannotation>-- 2 rows * 1 (x in first row)</lineannotation>
 6                  <lineannotation>-- 3 rows (2 + 1 just inserted) * 2 (x in second row)</lineannotation>
(4 rows)
</programlisting>
  </para>
 </sect1>
</chapter>

Title: Sample Session Demonstrating `execq` Function Usage
Summary
This section presents a comprehensive sample session that demonstrates the usage of the `execq` function defined in the previous section. It showcases various scenarios, including creating tables, inserting data, selecting data, and using the row count parameter. The session highlights the function's behavior with different SQL commands, data visibility rules, and interactions between multiple calls to `execq`. The sample session helps illustrate the functionality and potential use cases of the `execq` function within a PostgreSQL environment.