@@ -870,115 +870,115 @@ materializeResult(FunctionCallInfo fcinfo, PGconn *conn, PGresult *res)
870870 /* prepTuplestoreResult must have been called previously */
871871 Assert (rsinfo -> returnMode == SFRM_Materialize );
872872
873- if (PQresultStatus (res ) == PGRES_COMMAND_OK )
874- {
875- is_sql_cmd = true;
876-
877- /*
878- * need a tuple descriptor representing one TEXT column to return
879- * the command status string as our result tuple
880- */
881- tupdesc = CreateTemplateTupleDesc (1 );
882- TupleDescInitEntry (tupdesc , (AttrNumber ) 1 , "status" ,
883- TEXTOID , -1 , 0 );
884- ntuples = 1 ;
885- nfields = 1 ;
886- }
887- else
888- {
889- Assert (PQresultStatus (res ) == PGRES_TUPLES_OK );
873+ if (PQresultStatus (res ) == PGRES_COMMAND_OK )
874+ {
875+ is_sql_cmd = true;
890876
891- is_sql_cmd = false;
877+ /*
878+ * need a tuple descriptor representing one TEXT column to return the
879+ * command status string as our result tuple
880+ */
881+ tupdesc = CreateTemplateTupleDesc (1 );
882+ TupleDescInitEntry (tupdesc , (AttrNumber ) 1 , "status" ,
883+ TEXTOID , -1 , 0 );
884+ ntuples = 1 ;
885+ nfields = 1 ;
886+ }
887+ else
888+ {
889+ Assert (PQresultStatus (res ) == PGRES_TUPLES_OK );
892890
893- /* get a tuple descriptor for our result type */
894- switch (get_call_result_type (fcinfo , NULL , & tupdesc ))
895- {
896- case TYPEFUNC_COMPOSITE :
897- /* success */
898- break ;
899- case TYPEFUNC_RECORD :
900- /* failed to determine actual type of RECORD */
901- ereport (ERROR ,
902- (errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
903- errmsg ("function returning record called in context "
904- "that cannot accept type record" )));
905- break ;
906- default :
907- /* result type isn't composite */
908- elog (ERROR , "return type must be a row type" );
909- break ;
910- }
891+ is_sql_cmd = false;
911892
912- /* make sure we have a persistent copy of the tupdesc */
913- tupdesc = CreateTupleDescCopy (tupdesc );
914- ntuples = PQntuples (res );
915- nfields = PQnfields (res );
893+ /* get a tuple descriptor for our result type */
894+ switch (get_call_result_type (fcinfo , NULL , & tupdesc ))
895+ {
896+ case TYPEFUNC_COMPOSITE :
897+ /* success */
898+ break ;
899+ case TYPEFUNC_RECORD :
900+ /* failed to determine actual type of RECORD */
901+ ereport (ERROR ,
902+ (errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
903+ errmsg ("function returning record called in context "
904+ "that cannot accept type record" )));
905+ break ;
906+ default :
907+ /* result type isn't composite */
908+ elog (ERROR , "return type must be a row type" );
909+ break ;
916910 }
917911
918- /*
919- * check result and tuple descriptor have the same number of columns
920- */
921- if (nfields != tupdesc -> natts )
922- ereport (ERROR ,
923- (errcode (ERRCODE_DATATYPE_MISMATCH ),
924- errmsg ("remote query result rowtype does not match "
925- "the specified FROM clause rowtype" )));
912+ /* make sure we have a persistent copy of the tupdesc */
913+ tupdesc = CreateTupleDescCopy (tupdesc );
914+ ntuples = PQntuples (res );
915+ nfields = PQnfields (res );
916+ }
926917
927- if ( ntuples > 0 )
928- {
929- AttInMetadata * attinmeta ;
930- int nestlevel = -1 ;
931- Tuplestorestate * tupstore ;
932- MemoryContext oldcontext ;
933- int row ;
934- char * * values ;
918+ /*
919+ * check result and tuple descriptor have the same number of columns
920+ */
921+ if ( nfields != tupdesc -> natts )
922+ ereport ( ERROR ,
923+ ( errcode ( ERRCODE_DATATYPE_MISMATCH ),
924+ errmsg ( "remote query result rowtype does not match "
925+ "the specified FROM clause rowtype" ))) ;
935926
936- attinmeta = TupleDescGetAttInMetadata (tupdesc );
927+ if (ntuples > 0 )
928+ {
929+ AttInMetadata * attinmeta ;
930+ int nestlevel = -1 ;
931+ Tuplestorestate * tupstore ;
932+ MemoryContext oldcontext ;
933+ int row ;
934+ char * * values ;
937935
938- /* Set GUCs to ensure we read GUC-sensitive data types correctly */
939- if (!is_sql_cmd )
940- nestlevel = applyRemoteGucs (conn );
936+ attinmeta = TupleDescGetAttInMetadata (tupdesc );
941937
942- oldcontext = MemoryContextSwitchTo (rsinfo -> econtext -> ecxt_per_query_memory );
943- tupstore = tuplestore_begin_heap (true, false, work_mem );
944- rsinfo -> setResult = tupstore ;
945- rsinfo -> setDesc = tupdesc ;
946- MemoryContextSwitchTo (oldcontext );
938+ /* Set GUCs to ensure we read GUC-sensitive data types correctly */
939+ if (!is_sql_cmd )
940+ nestlevel = applyRemoteGucs (conn );
947941
948- values = palloc_array (char * , nfields );
942+ oldcontext = MemoryContextSwitchTo (rsinfo -> econtext -> ecxt_per_query_memory );
943+ tupstore = tuplestore_begin_heap (true, false, work_mem );
944+ rsinfo -> setResult = tupstore ;
945+ rsinfo -> setDesc = tupdesc ;
946+ MemoryContextSwitchTo (oldcontext );
949947
950- /* put all tuples into the tuplestore */
951- for (row = 0 ; row < ntuples ; row ++ )
948+ values = palloc_array (char * , nfields );
949+
950+ /* put all tuples into the tuplestore */
951+ for (row = 0 ; row < ntuples ; row ++ )
952+ {
953+ HeapTuple tuple ;
954+
955+ if (!is_sql_cmd )
952956 {
953- HeapTuple tuple ;
957+ int i ;
954958
955- if (!is_sql_cmd )
956- {
957- int i ;
958-
959- for (i = 0 ; i < nfields ; i ++ )
960- {
961- if (PQgetisnull (res , row , i ))
962- values [i ] = NULL ;
963- else
964- values [i ] = PQgetvalue (res , row , i );
965- }
966- }
967- else
959+ for (i = 0 ; i < nfields ; i ++ )
968960 {
969- values [0 ] = PQcmdStatus (res );
961+ if (PQgetisnull (res , row , i ))
962+ values [i ] = NULL ;
963+ else
964+ values [i ] = PQgetvalue (res , row , i );
970965 }
971-
972- /* build the tuple and put it into the tuplestore. */
973- tuple = BuildTupleFromCStrings ( attinmeta , values );
974- tuplestore_puttuple ( tupstore , tuple );
966+ }
967+ else
968+ {
969+ values [ 0 ] = PQcmdStatus ( res );
975970 }
976971
977- /* clean up GUC settings, if we changed any */
978- restoreLocalGucs (nestlevel );
972+ /* build the tuple and put it into the tuplestore. */
973+ tuple = BuildTupleFromCStrings (attinmeta , values );
974+ tuplestore_puttuple (tupstore , tuple );
979975 }
980976
981- PQclear (res );
977+ /* clean up GUC settings, if we changed any */
978+ restoreLocalGucs (nestlevel );
979+ }
980+
981+ PQclear (res );
982982}
983983
984984/*
0 commit comments