postgres.git / summary / log / commit / refs

commit    6c2fa4888594d1ff7fd357f2bd00aa1e1f169088
Author:   Joe Conway <mail@joeconway.com>
Date:     Tue Jan 03 23:47:24 2006 +0000
First released in: 8.2.0, 7.3.13, 7.4.11, 8.0.6, 8.1.2

    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.


contrib/dblink/README.dblink | 2 +- contrib/dblink/dblink.c | 48 ++++++++++++++++++++++++++++---------------- contrib/dblink/dblink.h | 2 +- contrib/dblink/doc/cursor | 7 +++++++ 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/contrib/dblink/README.dblink b/contrib/dblink/README.dblink index 22db3504913..9e1bdba6cba 100644 --- a/contrib/dblink/README.dblink +++ b/contrib/dblink/README.dblink @@ -8,7 +8,7 @@ * Darko Prenosil <Darko.Prenosil@finteh.hr> * Shridhar Daithankar <shridhar_daithankar@persistent.co.in> * - * 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 5de15144590..313c32f2d00 100644 --- a/contrib/dblink/dblink.c +++ b/contrib/dblink/dblink.c @@ -8,7 +8,7 @@ * Darko Prenosil <Darko.Prenosil@finteh.hr> * Shridhar Daithankar <shridhar_daithankar@persistent.co.in> * - * 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 @@ -547,14 +547,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); - } - /* check typtype to see if we have a predetermined return type */ functypeid = get_func_rettype(funcid); functyptype = get_typtype(functypeid); @@ -577,6 +569,21 @@ dblink_fetch(PG_FUNCTION_ARGS) /* shouldn't happen */ elog(ERROR, "return type must be a row type"); + /* 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; @@ -749,14 +756,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); - } - /* check typtype to see if we have a predetermined return type */ functypeid = get_func_rettype(funcid); functyptype = get_typtype(functypeid); @@ -782,6 +781,21 @@ dblink_record(PG_FUNCTION_ARGS) elog(ERROR, "return type must be a row type"); } + /* 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 63d6d525db9..8f2c9ff4c1f 100644 --- a/contrib/dblink/dblink.h +++ b/contrib/dblink/dblink.h @@ -8,7 +8,7 @@ * Darko Prenosil <Darko.Prenosil@finteh.hr> * Shridhar Daithankar <shridhar_daithankar@persistent.co.in> * - * 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 23a3ce89ba5..1220d85ea32 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=template1'); [parent: a559ef6594ce]