summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Conway2006-01-03 23:48:42 +0000
committerJoe Conway2006-01-03 23:48:42 +0000
commit49e807f1c4b0b1b9a235c8d36df313aa60b2ce10 (patch)
tree8e6734e756011cebbdc454f719c2c7d6079c5f91
parentfadae13661aa772fc41785c710052948302d1d78 (diff)
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.
-rw-r--r--contrib/dblink/README.dblink2
-rw-r--r--contrib/dblink/dblink.c48
-rw-r--r--contrib/dblink/dblink.h2
-rw-r--r--contrib/dblink/doc/cursor7
4 files changed, 40 insertions, 19 deletions
diff --git a/contrib/dblink/README.dblink b/contrib/dblink/README.dblink
index 22db350491..9e1bdba6cb 100644
--- a/contrib/dblink/README.dblink
+++ b/contrib/dblink/README.dblink
@@ -8,7 +8,7 @@
* Darko Prenosil <[email protected]>
* Shridhar Daithankar <[email protected]>
*
- * Copyright (c) 2001-2005, PostgreSQL Global Development Group
+ * Copyright (c) 2001-2006, PostgreSQL Global Development Group
* ALL RIGHTS RESERVED;
*
* Permission to use, copy, modify, and distribute this software and its
diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c
index 4da66a4b4d..4fd17c3227 100644
--- a/contrib/dblink/dblink.c
+++ b/contrib/dblink/dblink.c
@@ -8,7 +8,7 @@
* Darko Prenosil <[email protected]>
* Shridhar Daithankar <[email protected]>
*
- * Copyright (c) 2001-2005, PostgreSQL Global Development Group
+ * Copyright (c) 2001-2006, PostgreSQL Global Development Group
* ALL RIGHTS RESERVED;
*
* Permission to use, copy, modify, and distribute this software and its
@@ -579,14 +579,6 @@ dblink_fetch(PG_FUNCTION_ARGS)
/* got results, keep track of them */
funcctx->user_fctx = res;
- /* fast track when no results */
- if (funcctx->max_calls < 1)
- {
- if (res)
- PQclear(res);
- SRF_RETURN_DONE(funcctx);
- }
-
/* get a tuple descriptor for our result type */
switch (get_call_result_type(fcinfo, NULL, &tupdesc))
{
@@ -609,6 +601,21 @@ dblink_fetch(PG_FUNCTION_ARGS)
/* make sure we have a persistent copy of the tupdesc */
tupdesc = CreateTupleDescCopy(tupdesc);
+ /* check result and tuple descriptor have the same number of columns */
+ if (PQnfields(res) != tupdesc->natts)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("remote query result rowtype does not match "
+ "the specified FROM clause rowtype")));
+
+ /* fast track when no results */
+ if (funcctx->max_calls < 1)
+ {
+ if (res)
+ PQclear(res);
+ SRF_RETURN_DONE(funcctx);
+ }
+
/* store needed metadata for subsequent calls */
attinmeta = TupleDescGetAttInMetadata(tupdesc);
funcctx->attinmeta = attinmeta;
@@ -778,14 +785,6 @@ dblink_record(PG_FUNCTION_ARGS)
if (freeconn)
PQfinish(conn);
- /* fast track when no results */
- if (funcctx->max_calls < 1)
- {
- if (res)
- PQclear(res);
- SRF_RETURN_DONE(funcctx);
- }
-
if (!is_sql_cmd)
{
/* get a tuple descriptor for our result type */
@@ -811,6 +810,21 @@ dblink_record(PG_FUNCTION_ARGS)
tupdesc = CreateTupleDescCopy(tupdesc);
}
+ /* check result and tuple descriptor have the same number of columns */
+ if (PQnfields(res) != tupdesc->natts)
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("remote query result rowtype does not match "
+ "the specified FROM clause rowtype")));
+
+ /* fast track when no results */
+ if (funcctx->max_calls < 1)
+ {
+ if (res)
+ PQclear(res);
+ SRF_RETURN_DONE(funcctx);
+ }
+
/* store needed metadata for subsequent calls */
attinmeta = TupleDescGetAttInMetadata(tupdesc);
funcctx->attinmeta = attinmeta;
diff --git a/contrib/dblink/dblink.h b/contrib/dblink/dblink.h
index 63d6d525db..8f2c9ff4c1 100644
--- a/contrib/dblink/dblink.h
+++ b/contrib/dblink/dblink.h
@@ -8,7 +8,7 @@
* Darko Prenosil <[email protected]>
* Shridhar Daithankar <[email protected]>
*
- * Copyright (c) 2001-2005, PostgreSQL Global Development Group
+ * Copyright (c) 2001-2006, PostgreSQL Global Development Group
* ALL RIGHTS RESERVED;
*
* Permission to use, copy, modify, and distribute this software and its
diff --git a/contrib/dblink/doc/cursor b/contrib/dblink/doc/cursor
index b989fcb69c..321c823e21 100644
--- a/contrib/dblink/doc/cursor
+++ b/contrib/dblink/doc/cursor
@@ -92,6 +92,13 @@ Outputs
Returns setof record
+Note
+
+ On a mismatch between the number of return fields as specified in the FROM
+ clause, and the actual number of fields returned by the remote cursor, an
+ ERROR will be thrown. In this event, the remote cursor is still advanced
+ by as many rows as it would have been if the ERROR had not occurred.
+
Example usage
test=# select dblink_connect('dbname=postgres');