Add PL/PgSQL FOUND and GET DIAGNOSTICS support for RETURN QUERY
authorBruce Momjian <[email protected]>
Thu, 5 Feb 2009 15:25:49 +0000 (15:25 +0000)
committerBruce Momjian <[email protected]>
Thu, 5 Feb 2009 15:25:49 +0000 (15:25 +0000)
statement

Pavel Stehule

doc/src/sgml/plpgsql.sgml
src/pl/plpgsql/src/pl_exec.c
src/test/regress/expected/plpgsql.out
src/test/regress/sql/plpgsql.sql

index 8b8a1114595ce287c42ac8b43bb11f33e0dddbab..9d66dd980dc6fecbcffde8330a28d57b04f43175 100644 (file)
@@ -1356,6 +1356,14 @@ GET DIAGNOSTICS integer_var = ROW_COUNT;
             execution of other statements within the loop body.
            </para>
           </listitem>
+          <listitem>
+           <para>
+            A <command>RETURN QUERY</command> and <command>RETURN QUERY
+            EXECUTE</command> statements set <literal>FOUND</literal>
+            true if the query returns at least one row, false if no row
+            is returned.
+           </para>
+          </listitem>
          </itemizedlist>
 
      <literal>FOUND</literal> is a local variable within each
index a3bde05886b563dffaf5fed7ecdabdccc82d68f4..5e6dd5f08b54d4eda6c5a639cd65d25212f9ea0d 100644 (file)
@@ -2286,6 +2286,7 @@ exec_stmt_return_query(PLpgSQL_execstate *estate,
                                           PLpgSQL_stmt_return_query *stmt)
 {
        Portal          portal;
+       uint32                  processed = 0;
 
        if (!estate->retisset)
                ereport(ERROR,
@@ -2327,6 +2328,7 @@ exec_stmt_return_query(PLpgSQL_execstate *estate,
                        HeapTuple       tuple = SPI_tuptable->vals[i];
 
                        tuplestore_puttuple(estate->tuple_store, tuple);
+                       processed++;
                }
                MemoryContextSwitchTo(old_cxt);
 
@@ -2336,6 +2338,9 @@ exec_stmt_return_query(PLpgSQL_execstate *estate,
        SPI_freetuptable(SPI_tuptable);
        SPI_cursor_close(portal);
 
+       estate->eval_processed = processed;
+       exec_set_found(estate, processed != 0);
+
        return PLPGSQL_RC_OK;
 }
 
index 94a485f46b30aad38f57c6106e3b23cbaa1d00d6..b6f333c787db86605e79ad8a997a66ec9c988d2c 100644 (file)
@@ -3666,3 +3666,35 @@ select * from tftest(10);
 (2 rows)
 
 drop function tftest(int);
+create or replace function rttest()
+returns setof int as $$
+declare rc int;
+begin
+  return query values(10),(20);
+  get diagnostics rc = row_count;
+  raise notice '% %', found, rc;
+  return query select * from (values(10),(20)) f(a) where false;
+  get diagnostics rc = row_count;
+  raise notice '% %', found, rc;
+  return query execute 'values(10),(20)';
+  get diagnostics rc = row_count;
+  raise notice '% %', found, rc;
+  return query execute 'select * from (values(10),(20)) f(a) where false';
+  get diagnostics rc = row_count;
+  raise notice '% %', found, rc;
+end;
+$$ language plpgsql;
+select * from rttest();
+NOTICE:  t 2
+NOTICE:  f 0
+NOTICE:  t 2
+NOTICE:  f 0
+ rttest 
+--------
+     10
+     20
+     10
+     20
+(4 rows)
+
+drop function rttest();
index 9ebe2b5f33248b738eb1688d9d4d6ebcc85114d9..1e261747f46815ba05c11813d320482690b0526f 100644 (file)
@@ -2948,3 +2948,27 @@ $$ language plpgsql immutable strict;
 select * from tftest(10);
 
 drop function tftest(int);
+
+create or replace function rttest()
+returns setof int as $$
+declare rc int;
+begin
+  return query values(10),(20);
+  get diagnostics rc = row_count;
+  raise notice '% %', found, rc;
+  return query select * from (values(10),(20)) f(a) where false;
+  get diagnostics rc = row_count;
+  raise notice '% %', found, rc;
+  return query execute 'values(10),(20)';
+  get diagnostics rc = row_count;
+  raise notice '% %', found, rc;
+  return query execute 'select * from (values(10),(20)) f(a) where false';
+  get diagnostics rc = row_count;
+  raise notice '% %', found, rc;
+end;
+$$ language plpgsql;
+
+select * from rttest();
+
+drop function rttest();
+