I could use some assistance trying to figure out the root cause of this error scenario.

Here are the details:

1. The query is a dynamically generated SQL statement inside of a plpgsql function that returns the result as a refcursor.
2. The query runs fine when run from PgAdmin, no errors.
3. When called from Java (using JDBC), the error as indicated in the title of this post, occurs.
4. I have narrowed down the culprit in the SQL statement that is causing the error to be thrown (the "what"), but I cannot seem to answer the "why?".

The error:
Code:
Nov 24, 2014 2:55:33 PM aMenu.data.Query call
SEVERE: null
org.postgresql.util.PSQLException: ERROR: cannot open multi-query plan as cursor
  Where: PL/pgSQL function amenu(character varying,character varying,character varying,character varying,character varying) line 123 at OPEN
        at
org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorI
mpl.java:2198)
        at
org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.ja
va:1927)
        at
org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
        at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.j
ava:561)
        at
org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2St
atement.java:419)
        at
org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.j
ava:412)
        at aMenu.data.Query.getResults(Query.java:77)
        at aMenu.data.Query.call(Query.java:98)
        at aMenu.data.Query.call(Query.java:23)
        at javafx.concurrent.Task$TaskCallable.call(Task.java:1259)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.lang.Thread.run(Thread.java:745)
Java Params:

Code:
queryString = "{ ? = call amenu(?, ?, ?, ?, ?) }";
Java:
Code:
    @Override
    protected String call() throws SQLException {

        Connection dbConnection = PGConnect.getConnection();

        ObservableList<ObservableList> data = null;
        try {

            try (CallableStatement statement
                    = dbConnection.prepareCall(queryString)) {

                statement.registerOutParameter(1, Types.OTHER);

                statement.setString(2, p1Context.getp1Obj().getValue());
                statement.setString(3, p2);
                statement.setString(4, p3);
                statement.setString(5, p4);
                statement.setString(6, p5);

                data = getResults(statement);
            }

        } catch (Exception ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        }

        dbConnection.commit();

        return format(data);
    }
Here is the culprit function that runs fine in PgAdmin, but fails with the error through JDBC:
Code:
CREATE OR REPLACE FUNCTION amenu(p_1 character varying, p_2 character varying, p_3 character varying, p_4 character varying, p_5 character varying)
  RETURNS refcursor AS
$BODY$

DECLARE

  -- Other variables
  v_DynamicSQL                    VARCHAR;
  v_DynamicSQL_option             VARCHAR;
  v_places                        VARCHAR[];
  v_place                         VARCHAR;
  v_DynamicSQL_placeTemplate      VARCHAR;
  v_DynamicSQL_placeQuerys        VARCHAR[];

  -- Generic cursor
  RTRN_VAL                     REFCURSOR := 'amenu_cursor';

BEGIN
  p_5 := upper(p_5);
  
  IF p_4='A' THEN
    v_DynamicSQL_option = ' ';
  ELSIF p_4='B' THEN
    v_DynamicSQL_option = 'AND name in('||p_5||') ';
  END IF;

  p_1 = upper(p_1);

  v_places = ARRAY[p_1];

  FOREACH v_place IN ARRAY v_places
  LOOP

    v_DynamicSQL_placeTemplate =  '
      SELECT
        place as "Place",
        TO_CHAR(dt1, ''MM/DD/YYYY'') as "Date", 
        sum(a) as "A Count"        
      FROM table_name_partition_1
      WHERE type='''||upper(p_4)||'''
        AND place='''||v_place||'''
        '||v_DynamicSQL_option||'
      GROUP BY dt1, place';

    -- add the current dynamic query to the 
    v_DynamicSQL_placeQuerys = array_append( v_DynamicSQL_placeQuerys, v_DynamicSQL_placeTemplate );

  END LOOP;

  -- Union all place queries together
  v_DynamicSQL = array_to_string(v_DynamicSQL_placeQuerys, ' UNION ');

  raise notice '%', v_DynamicSQL;
    
     -- Populate and return the cursor
  OPEN RTRN_VAL FOR EXECUTE v_DynamicSQL;

  RETURN RTRN_VAL;

END;$BODY$
  LANGUAGE plpgsql VOLATILE;
If I run this from PgAdmin, it works fine:

Code:
select amenu('A', '20141101', '20141105', 'B', '''ABC''' ) ; 
FETCH ALL from amenu_cursor;
Now, if I remove the line (in bold and red below) from the dynamically generated SQL, the error goes away.

Code:
    v_DynamicSQL_placeTemplate =  '
      SELECT
        place as "Place",
        TO_CHAR(dt1, ''MM/DD/YYYY'') as "Date", 
        sum(a) as "A Count"        
      FROM table_name_partition_1
      WHERE type='''||upper(p_4)||'''
        AND place='''||v_place||'''
        '||v_DynamicSQL_option||'
      GROUP BY dt1, place';
The raise notice clearly shows the dynamic SQL is properly formatted prior to the execution in the function, so I am at a loss to explain why this particular scenario is causing this error. I don't have enough knowledge of the inner workings of PostgreSQL or the JDBC driver to figure this out.

I am hoping someone in the community with more knowledge and experience will have more insight into this issue.

Thanks in advance for any assistance.