@@ -2691,7 +2691,7 @@ are invisible to the query scan. For example, in query
26912691
26922692 INSERT INTO a SELECT * FROM a
26932693
2694- tuples inserted are invisible for SELECT' scan. In effect, this
2694+ tuples inserted are invisible for SELECT's scan. In effect, this
26952695duplicates the database table within itself (subject to unique index
26962696rules, of course) without recursing.
26972697</Para>
@@ -2708,7 +2708,7 @@ of Q) or after Q is done.
27082708
27092709<Para>
27102710 This example of SPI usage demonstrates the visibility rule.
2711- There are more complex examples in in src/test/regress/regress.c and
2711+ There are more complex examples in src/test/regress/regress.c and
27122712in contrib/spi.
27132713</Para>
27142714
@@ -2719,47 +2719,54 @@ query using SPI_exec and returns the number of tuples for which the query
27192719executed:
27202720
27212721<ProgramListing>
2722- #include "executor/spi.h" /* this is what you need to work with SPI */
2722+ #include "executor/spi.h" /* this is what you need to work with SPI */
27232723
27242724int execq(text *sql, int cnt);
27252725
27262726int
27272727execq(text *sql, int cnt)
27282728{
2729- int ret;
2730- int proc = 0;
2731-
2732- SPI_connect();
2733-
2734- ret = SPI_exec(textout(sql), cnt);
2735-
2736- proc = SPI_processed;
2737- /*
2738- * If this is SELECT and some tuple(s) fetched -
2739- * returns tuples to the caller via elog (NOTICE).
2740- */
2741- if ( ret == SPI_OK_SELECT && SPI_processed > 0 )
2742- {
2743- TupleDesc tupdesc = SPI_tuptable->tupdesc;
2744- SPITupleTable *tuptable = SPI_tuptable;
2745- char buf[8192];
2746- int i;
2747-
2748- for (ret = 0; ret < proc; ret++)
2749- {
2750- HeapTuple tuple = tuptable->vals[ret];
2751-
2752- for (i = 1, buf[0] = 0; i <= tupdesc->natts; i++)
2753- sprintf(buf + strlen (buf), " %s%s",
2754- SPI_getvalue(tuple, tupdesc, i),
2755- (i == tupdesc->natts) ? " " : " |");
2756- elog (NOTICE, "EXECQ: %s", buf);
2757- }
2758- }
2759-
2760- SPI_finish();
2761-
2762- return (proc);
2729+ char *query;
2730+ int ret;
2731+ int proc;
2732+
2733+ /* Convert given TEXT object to a C string */
2734+ query = DatumGetCString(DirectFunctionCall1(textout,
2735+ PointerGetDatum(sql)));
2736+
2737+ SPI_connect();
2738+
2739+ ret = SPI_exec(query, cnt);
2740+
2741+ proc = SPI_processed;
2742+ /*
2743+ * If this is SELECT and some tuple(s) fetched -
2744+ * returns tuples to the caller via elog (NOTICE).
2745+ */
2746+ if ( ret == SPI_OK_SELECT && SPI_processed > 0 )
2747+ {
2748+ TupleDesc tupdesc = SPI_tuptable->tupdesc;
2749+ SPITupleTable *tuptable = SPI_tuptable;
2750+ char buf[8192];
2751+ int i,j;
2752+
2753+ for (j = 0; j < proc; j++)
2754+ {
2755+ HeapTuple tuple = tuptable->vals[j];
2756+
2757+ for (i = 1, buf[0] = 0; i <= tupdesc->natts; i++)
2758+ sprintf(buf + strlen (buf), " %s%s",
2759+ SPI_getvalue(tuple, tupdesc, i),
2760+ (i == tupdesc->natts) ? " " : " |");
2761+ elog (NOTICE, "EXECQ: %s", buf);
2762+ }
2763+ }
2764+
2765+ SPI_finish();
2766+
2767+ pfree(query);
2768+
2769+ return (proc);
27632770}
27642771</ProgramListing>
27652772</Para>
0 commit comments