@@ -2691,7 +2691,7 @@ are invisible to the query scan. For example, in query
2691
2691
2692
2692
INSERT INTO a SELECT * FROM a
2693
2693
2694
- tuples inserted are invisible for SELECT' scan. In effect, this
2694
+ tuples inserted are invisible for SELECT's scan. In effect, this
2695
2695
duplicates the database table within itself (subject to unique index
2696
2696
rules, of course) without recursing.
2697
2697
</Para>
@@ -2708,7 +2708,7 @@ of Q) or after Q is done.
2708
2708
2709
2709
<Para>
2710
2710
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
2712
2712
in contrib/spi.
2713
2713
</Para>
2714
2714
@@ -2719,47 +2719,54 @@ query using SPI_exec and returns the number of tuples for which the query
2719
2719
executed:
2720
2720
2721
2721
<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 */
2723
2723
2724
2724
int execq(text *sql, int cnt);
2725
2725
2726
2726
int
2727
2727
execq(text *sql, int cnt)
2728
2728
{
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);
2763
2770
}
2764
2771
</ProgramListing>
2765
2772
</Para>
0 commit comments