I am able to use cursors successfully in my program if I retrieve the CURSOR directly and not through a stored function - for example using the line:
My problem is with using CURSOR returned by stored functions.
Here my modified code to check the being transaction result and to use the name returned by the call to the stored function in the fetch command:
//BEGIN the transaction
queryResult4 = PQexec(connection, "BEGIN");
execStatus = PQresultStatus(queryResult4); //Get the result status
char * beginTransactionErrorMessage = PQresultErrorMessage(queryResult4);
NSLog([NSString stringWithCString:beginTransactionErrorMessage]);
char* cursorCall = "select reffunc('portalName')"; //call stored function to get the cursor
//Call the stored function that returns a CURSOR
queryResult4 = PQexec(connection, cursorCall);
execStatus = PQresultStatus(queryResult4); //Get the result status
char * errorMessage = PQresultErrorMessage(queryResult4);
NSLog([NSString stringWithCString:errorMessage]);
if ((execStatus == PGRES_COMMAND_OK) || (execStatus == PGRES_TUPLES_OK)) { //cursor retrieved succesfully
int numRows; //variable to store number of rows returned in query
int numColumns; //number of columns per row
numRows = PQntuples(queryResult4);
NSLog(@"The stored function call returned %i rows.", numRows);
numColumns = PQnfields(queryResult4); //get number of columns
//get the name of the returned cursor
char * cursorReturned;
cursorReturned = PQgetvalue(queryResult4, 0, 0);
NSString * fetchNextQuery = [NSString stringWithFormat:@"FETCH ALL in %s", cursorReturned];
queryResult4 = PQexec(connection,[fetchNextQuery UTF8String] );
execStatus = PQresultStatus(queryResult4); //Get the result status
char * errorMessage2 = PQresultErrorMessage(queryResult4);
NSLog([NSString stringWithCString:errorMessage2]); ....