88 * Darko Prenosil <Darko.Prenosil@finteh.hr>
99 * Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
1010 *
11- * Copyright (c) 2001-2005 , PostgreSQL Global Development Group
11+ * Copyright (c) 2001-2006 , PostgreSQL Global Development Group
1212 * ALL RIGHTS RESERVED;
1313 *
1414 * Permission to use, copy, modify, and distribute this software and its
@@ -579,14 +579,6 @@ dblink_fetch(PG_FUNCTION_ARGS)
579579 /* got results, keep track of them */
580580 funcctx -> user_fctx = res ;
581581
582- /* fast track when no results */
583- if (funcctx -> max_calls < 1 )
584- {
585- if (res )
586- PQclear (res );
587- SRF_RETURN_DONE (funcctx );
588- }
589-
590582 /* get a tuple descriptor for our result type */
591583 switch (get_call_result_type (fcinfo , NULL , & tupdesc ))
592584 {
@@ -609,6 +601,21 @@ dblink_fetch(PG_FUNCTION_ARGS)
609601 /* make sure we have a persistent copy of the tupdesc */
610602 tupdesc = CreateTupleDescCopy (tupdesc );
611603
604+ /* check result and tuple descriptor have the same number of columns */
605+ if (PQnfields (res ) != tupdesc -> natts )
606+ ereport (ERROR ,
607+ (errcode (ERRCODE_DATATYPE_MISMATCH ),
608+ errmsg ("remote query result rowtype does not match "
609+ "the specified FROM clause rowtype" )));
610+
611+ /* fast track when no results */
612+ if (funcctx -> max_calls < 1 )
613+ {
614+ if (res )
615+ PQclear (res );
616+ SRF_RETURN_DONE (funcctx );
617+ }
618+
612619 /* store needed metadata for subsequent calls */
613620 attinmeta = TupleDescGetAttInMetadata (tupdesc );
614621 funcctx -> attinmeta = attinmeta ;
@@ -778,14 +785,6 @@ dblink_record(PG_FUNCTION_ARGS)
778785 if (freeconn )
779786 PQfinish (conn );
780787
781- /* fast track when no results */
782- if (funcctx -> max_calls < 1 )
783- {
784- if (res )
785- PQclear (res );
786- SRF_RETURN_DONE (funcctx );
787- }
788-
789788 if (!is_sql_cmd )
790789 {
791790 /* get a tuple descriptor for our result type */
@@ -811,6 +810,21 @@ dblink_record(PG_FUNCTION_ARGS)
811810 tupdesc = CreateTupleDescCopy (tupdesc );
812811 }
813812
813+ /* check result and tuple descriptor have the same number of columns */
814+ if (PQnfields (res ) != tupdesc -> natts )
815+ ereport (ERROR ,
816+ (errcode (ERRCODE_DATATYPE_MISMATCH ),
817+ errmsg ("remote query result rowtype does not match "
818+ "the specified FROM clause rowtype" )));
819+
820+ /* fast track when no results */
821+ if (funcctx -> max_calls < 1 )
822+ {
823+ if (res )
824+ PQclear (res );
825+ SRF_RETURN_DONE (funcctx );
826+ }
827+
814828 /* store needed metadata for subsequent calls */
815829 attinmeta = TupleDescGetAttInMetadata (tupdesc );
816830 funcctx -> attinmeta = attinmeta ;
0 commit comments