Skip to content

Commit 9259526

Browse files
committed
When the remote query result has a different number of columns
than the local query specifies (e.g. in the FROM clause), throw an ERROR (instead of crashing). Fix for bug #2129 reported by Akio Iwaasa.
1 parent 71ad8e2 commit 9259526

File tree

4 files changed

+40
-19
lines changed

4 files changed

+40
-19
lines changed

contrib/dblink/README.dblink

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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

contrib/dblink/dblink.c

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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;

contrib/dblink/dblink.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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

contrib/dblink/doc/cursor

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ Outputs
9292

9393
Returns setof record
9494

95+
Note
96+
97+
On a mismatch between the number of return fields as specified in the FROM
98+
clause, and the actual number of fields returned by the remote cursor, an
99+
ERROR will be thrown. In this event, the remote cursor is still advanced
100+
by as many rows as it would have been if the ERROR had not occurred.
101+
95102
Example usage
96103

97104
test=# select dblink_connect('dbname=postgres');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy