8
8
* Darko Prenosil <Darko.Prenosil@finteh.hr>
9
9
* Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
10
10
*
11
- * Copyright (c) 2001-2005 , PostgreSQL Global Development Group
11
+ * Copyright (c) 2001-2006 , PostgreSQL Global Development Group
12
12
* ALL RIGHTS RESERVED;
13
13
*
14
14
* Permission to use, copy, modify, and distribute this software and its
@@ -579,14 +579,6 @@ dblink_fetch(PG_FUNCTION_ARGS)
579
579
/* got results, keep track of them */
580
580
funcctx -> user_fctx = res ;
581
581
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
-
590
582
/* get a tuple descriptor for our result type */
591
583
switch (get_call_result_type (fcinfo , NULL , & tupdesc ))
592
584
{
@@ -609,6 +601,21 @@ dblink_fetch(PG_FUNCTION_ARGS)
609
601
/* make sure we have a persistent copy of the tupdesc */
610
602
tupdesc = CreateTupleDescCopy (tupdesc );
611
603
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
+
612
619
/* store needed metadata for subsequent calls */
613
620
attinmeta = TupleDescGetAttInMetadata (tupdesc );
614
621
funcctx -> attinmeta = attinmeta ;
@@ -778,14 +785,6 @@ dblink_record(PG_FUNCTION_ARGS)
778
785
if (freeconn )
779
786
PQfinish (conn );
780
787
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
-
789
788
if (!is_sql_cmd )
790
789
{
791
790
/* get a tuple descriptor for our result type */
@@ -811,6 +810,21 @@ dblink_record(PG_FUNCTION_ARGS)
811
810
tupdesc = CreateTupleDescCopy (tupdesc );
812
811
}
813
812
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
+
814
828
/* store needed metadata for subsequent calls */
815
829
attinmeta = TupleDescGetAttInMetadata (tupdesc );
816
830
funcctx -> attinmeta = attinmeta ;
0 commit comments