2001-06-15 00:49:03 +08:00
|
|
|
/*
|
|
|
|
* dblink.c
|
|
|
|
*
|
|
|
|
* Functions returning results from a remote database
|
|
|
|
*
|
2002-09-02 14:13:31 +08:00
|
|
|
* Joe Conway <mail@joeconway.com>
|
2003-06-25 09:10:15 +08:00
|
|
|
* And contributors:
|
|
|
|
* Darko Prenosil <Darko.Prenosil@finteh.hr>
|
|
|
|
* Shridhar Daithankar <shridhar_daithankar@persistent.co.in>
|
2002-09-02 14:13:31 +08:00
|
|
|
*
|
2009-01-02 01:24:05 +08:00
|
|
|
* $PostgreSQL: pgsql/contrib/dblink/dblink.c,v 1.77 2009/01/01 17:23:31 momjian Exp $
|
|
|
|
* Copyright (c) 2001-2009, PostgreSQL Global Development Group
|
2002-04-24 10:28:28 +08:00
|
|
|
* ALL RIGHTS RESERVED;
|
2001-10-25 13:50:21 +08:00
|
|
|
*
|
2001-06-15 00:49:03 +08:00
|
|
|
* Permission to use, copy, modify, and distribute this software and its
|
|
|
|
* documentation for any purpose, without fee, and without a written agreement
|
|
|
|
* is hereby granted, provided that the above copyright notice and this
|
|
|
|
* paragraph and the following two paragraphs appear in all copies.
|
2001-10-25 13:50:21 +08:00
|
|
|
*
|
2001-06-15 00:49:03 +08:00
|
|
|
* IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR
|
|
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
|
|
|
|
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
|
|
|
|
* DOCUMENTATION, EVEN IF THE AUTHOR OR DISTRIBUTORS HAVE BEEN ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2001-10-25 13:50:21 +08:00
|
|
|
*
|
2001-06-15 00:49:03 +08:00
|
|
|
* THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIMS ANY WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
|
|
* ON AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAS NO OBLIGATIONS TO
|
|
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*
|
|
|
|
*/
|
2002-09-02 14:13:31 +08:00
|
|
|
#include "postgres.h"
|
2003-07-25 01:52:50 +08:00
|
|
|
|
2006-03-11 09:19:22 +08:00
|
|
|
#include <limits.h>
|
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
#include "libpq-fe.h"
|
|
|
|
#include "fmgr.h"
|
|
|
|
#include "funcapi.h"
|
2008-01-14 10:49:47 +08:00
|
|
|
#include "access/genam.h"
|
2002-09-02 14:13:31 +08:00
|
|
|
#include "access/heapam.h"
|
2006-07-12 00:35:33 +08:00
|
|
|
#include "access/tupdesc.h"
|
2008-01-14 10:49:47 +08:00
|
|
|
#include "catalog/indexing.h"
|
2002-09-02 14:13:31 +08:00
|
|
|
#include "catalog/namespace.h"
|
|
|
|
#include "catalog/pg_index.h"
|
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "executor/executor.h"
|
|
|
|
#include "executor/spi.h"
|
|
|
|
#include "lib/stringinfo.h"
|
2008-03-27 05:10:39 +08:00
|
|
|
#include "miscadmin.h"
|
2002-09-02 14:13:31 +08:00
|
|
|
#include "nodes/execnodes.h"
|
2006-07-12 00:35:33 +08:00
|
|
|
#include "nodes/nodes.h"
|
2002-09-02 14:13:31 +08:00
|
|
|
#include "nodes/pg_list.h"
|
|
|
|
#include "parser/parse_type.h"
|
2007-08-27 09:24:50 +08:00
|
|
|
#include "utils/acl.h"
|
2006-07-12 00:35:33 +08:00
|
|
|
#include "utils/array.h"
|
2002-09-02 14:13:31 +08:00
|
|
|
#include "utils/builtins.h"
|
2006-07-12 00:35:33 +08:00
|
|
|
#include "utils/dynahash.h"
|
2002-09-02 14:13:31 +08:00
|
|
|
#include "utils/fmgroids.h"
|
2006-07-12 00:35:33 +08:00
|
|
|
#include "utils/hsearch.h"
|
2002-09-02 14:13:31 +08:00
|
|
|
#include "utils/lsyscache.h"
|
2003-06-25 09:10:15 +08:00
|
|
|
#include "utils/memutils.h"
|
2008-03-27 05:10:39 +08:00
|
|
|
#include "utils/syscache.h"
|
|
|
|
#include "utils/tqual.h"
|
2001-06-15 00:49:03 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
#include "dblink.h"
|
2002-05-28 05:59:12 +08:00
|
|
|
|
2006-05-31 06:12:16 +08:00
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
2003-06-25 09:10:15 +08:00
|
|
|
typedef struct remoteConn
|
|
|
|
{
|
2005-11-23 02:17:34 +08:00
|
|
|
PGconn *conn; /* Hold the remote connection */
|
2005-10-18 10:55:49 +08:00
|
|
|
int openCursorCount; /* The number of open cursors */
|
2005-11-23 02:17:34 +08:00
|
|
|
bool newXactForCursor; /* Opened a transaction for a cursor */
|
2003-08-04 08:43:34 +08:00
|
|
|
} remoteConn;
|
2003-06-25 09:10:15 +08:00
|
|
|
|
2002-05-28 05:59:12 +08:00
|
|
|
/*
|
|
|
|
* Internal declarations
|
|
|
|
*/
|
2006-09-03 05:11:15 +08:00
|
|
|
static Datum dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get);
|
2003-06-25 09:10:15 +08:00
|
|
|
static remoteConn *getConnectionByName(const char *name);
|
|
|
|
static HTAB *createConnHash(void);
|
2005-10-15 10:49:52 +08:00
|
|
|
static void createNewConnection(const char *name, remoteConn * rconn);
|
2003-06-25 09:10:15 +08:00
|
|
|
static void deleteConnection(const char *name);
|
2002-05-28 05:59:12 +08:00
|
|
|
static char **get_pkey_attnames(Oid relid, int16 *numatts);
|
2005-11-18 10:38:24 +08:00
|
|
|
static char **get_text_array_contents(ArrayType *array, int *numitems);
|
2005-03-29 08:17:27 +08:00
|
|
|
static char *get_sql_insert(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pkattvals, char **tgt_pkattvals);
|
|
|
|
static char *get_sql_delete(Oid relid, int2vector *pkattnums, int16 pknumatts, char **tgt_pkattvals);
|
|
|
|
static char *get_sql_update(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pkattvals, char **tgt_pkattvals);
|
2002-05-28 05:59:12 +08:00
|
|
|
static char *quote_literal_cstr(char *rawstr);
|
|
|
|
static char *quote_ident_cstr(char *rawstr);
|
2005-03-29 08:17:27 +08:00
|
|
|
static int16 get_attnum_pk_pos(int2vector *pkattnums, int16 pknumatts, int16 key);
|
|
|
|
static HeapTuple get_tuple_of_interest(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pkattvals);
|
2002-09-05 04:31:48 +08:00
|
|
|
static Oid get_relid_from_relname(text *relname_text);
|
2002-11-24 02:59:25 +08:00
|
|
|
static char *generate_relation_name(Oid relid);
|
2008-09-22 21:55:14 +08:00
|
|
|
static void dblink_connstr_check(const char *connstr);
|
2008-01-04 05:27:59 +08:00
|
|
|
static void dblink_security_check(PGconn *conn, remoteConn *rconn);
|
2008-07-03 11:56:57 +08:00
|
|
|
static void dblink_res_error(const char *conname, PGresult *res, const char *dblink_context_msg, bool fail);
|
2002-05-28 05:59:12 +08:00
|
|
|
|
2002-04-24 10:28:28 +08:00
|
|
|
/* Global */
|
2005-11-23 02:17:34 +08:00
|
|
|
static remoteConn *pconn = NULL;
|
|
|
|
static HTAB *remoteConnHash = NULL;
|
2003-06-25 09:10:15 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
/*
|
2005-10-08 20:18:48 +08:00
|
|
|
* Following is list that holds multiple remote connections.
|
|
|
|
* Calling convention of each dblink function changes to accept
|
|
|
|
* connection name as the first parameter. The connection list is
|
|
|
|
* much like ecpg e.g. a mapping between a name and a PGconn object.
|
|
|
|
*/
|
2003-06-25 09:10:15 +08:00
|
|
|
|
|
|
|
typedef struct remoteConnHashEnt
|
|
|
|
{
|
|
|
|
char name[NAMEDATALEN];
|
2005-10-08 20:12:29 +08:00
|
|
|
remoteConn *rconn;
|
2003-08-04 08:43:34 +08:00
|
|
|
} remoteConnHashEnt;
|
2003-06-25 09:10:15 +08:00
|
|
|
|
|
|
|
/* initial number of connection hashes */
|
|
|
|
#define NUMCONN 16
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2003-06-25 09:10:15 +08:00
|
|
|
/* general utility */
|
2002-09-02 14:13:31 +08:00
|
|
|
#define xpfree(var_) \
|
|
|
|
do { \
|
|
|
|
if (var_ != NULL) \
|
|
|
|
{ \
|
|
|
|
pfree(var_); \
|
|
|
|
var_ = NULL; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2005-10-08 19:33:45 +08:00
|
|
|
|
2008-07-03 11:56:57 +08:00
|
|
|
#define xpstrdup(var_c, var_) \
|
2003-06-25 09:10:15 +08:00
|
|
|
do { \
|
2008-07-03 11:56:57 +08:00
|
|
|
if (var_ != NULL) \
|
|
|
|
var_c = pstrdup(var_); \
|
|
|
|
else \
|
|
|
|
var_c = NULL; \
|
2003-06-25 09:10:15 +08:00
|
|
|
} while (0)
|
2005-10-08 19:33:45 +08:00
|
|
|
|
2008-07-03 11:56:57 +08:00
|
|
|
#define DBLINK_RES_INTERNALERROR(p2) \
|
2004-03-07 10:27:00 +08:00
|
|
|
do { \
|
|
|
|
msg = pstrdup(PQerrorMessage(conn)); \
|
|
|
|
if (res) \
|
|
|
|
PQclear(res); \
|
2008-07-03 11:56:57 +08:00
|
|
|
elog(ERROR, "%s: %s", p2, msg); \
|
2004-03-07 10:27:00 +08:00
|
|
|
} while (0)
|
2005-10-08 19:33:45 +08:00
|
|
|
|
2003-07-25 01:52:50 +08:00
|
|
|
#define DBLINK_CONN_NOT_AVAIL \
|
2003-06-25 09:10:15 +08:00
|
|
|
do { \
|
|
|
|
if(conname) \
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR, \
|
|
|
|
(errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST), \
|
|
|
|
errmsg("connection \"%s\" not available", conname))); \
|
2003-06-25 09:10:15 +08:00
|
|
|
else \
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR, \
|
|
|
|
(errcode(ERRCODE_CONNECTION_DOES_NOT_EXIST), \
|
|
|
|
errmsg("connection not available"))); \
|
2003-06-25 09:10:15 +08:00
|
|
|
} while (0)
|
2005-10-08 19:33:45 +08:00
|
|
|
|
2003-07-25 01:52:50 +08:00
|
|
|
#define DBLINK_GET_CONN \
|
2003-06-25 09:10:15 +08:00
|
|
|
do { \
|
2008-03-26 06:42:46 +08:00
|
|
|
char *conname_or_str = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
|
2005-10-08 20:12:29 +08:00
|
|
|
rconn = getConnectionByName(conname_or_str); \
|
|
|
|
if(rconn) \
|
2003-06-25 09:10:15 +08:00
|
|
|
{ \
|
2005-10-08 20:12:29 +08:00
|
|
|
conn = rconn->conn; \
|
2003-06-25 09:10:15 +08:00
|
|
|
} \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
connstr = conname_or_str; \
|
2008-09-22 21:55:14 +08:00
|
|
|
dblink_connstr_check(connstr); \
|
2003-06-25 09:10:15 +08:00
|
|
|
conn = PQconnectdb(connstr); \
|
|
|
|
if (PQstatus(conn) == CONNECTION_BAD) \
|
|
|
|
{ \
|
|
|
|
msg = pstrdup(PQerrorMessage(conn)); \
|
|
|
|
PQfinish(conn); \
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR, \
|
|
|
|
(errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION), \
|
|
|
|
errmsg("could not establish connection"), \
|
|
|
|
errdetail("%s", msg))); \
|
2003-06-25 09:10:15 +08:00
|
|
|
} \
|
2008-01-04 05:27:59 +08:00
|
|
|
dblink_security_check(conn, rconn); \
|
2004-03-07 10:27:00 +08:00
|
|
|
freeconn = true; \
|
2003-06-25 09:10:15 +08:00
|
|
|
} \
|
|
|
|
} while (0)
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2008-01-04 05:27:59 +08:00
|
|
|
#define DBLINK_GET_NAMED_CONN \
|
|
|
|
do { \
|
2008-03-26 06:42:46 +08:00
|
|
|
char *conname = text_to_cstring(PG_GETARG_TEXT_PP(0)); \
|
2008-01-04 05:27:59 +08:00
|
|
|
rconn = getConnectionByName(conname); \
|
|
|
|
if(rconn) \
|
|
|
|
conn = rconn->conn; \
|
|
|
|
else \
|
|
|
|
DBLINK_CONN_NOT_AVAIL; \
|
|
|
|
} while (0)
|
|
|
|
|
2005-10-18 10:55:49 +08:00
|
|
|
#define DBLINK_INIT \
|
|
|
|
do { \
|
|
|
|
if (!pconn) \
|
|
|
|
{ \
|
|
|
|
pconn = (remoteConn *) MemoryContextAlloc(TopMemoryContext, sizeof(remoteConn)); \
|
|
|
|
pconn->conn = NULL; \
|
|
|
|
pconn->openCursorCount = 0; \
|
|
|
|
pconn->newXactForCursor = FALSE; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a persistent connection to another database
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_connect);
|
|
|
|
Datum
|
|
|
|
dblink_connect(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2003-06-25 09:10:15 +08:00
|
|
|
char *connstr = NULL;
|
|
|
|
char *connname = NULL;
|
2002-09-05 04:31:48 +08:00
|
|
|
char *msg;
|
2003-06-25 09:10:15 +08:00
|
|
|
PGconn *conn = NULL;
|
2005-10-08 20:12:29 +08:00
|
|
|
remoteConn *rconn = NULL;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2005-10-18 10:55:49 +08:00
|
|
|
DBLINK_INIT;
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (PG_NARGS() == 2)
|
2003-06-25 09:10:15 +08:00
|
|
|
{
|
2008-03-26 06:42:46 +08:00
|
|
|
connstr = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
|
|
|
connname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
else if (PG_NARGS() == 1)
|
2008-03-26 06:42:46 +08:00
|
|
|
connstr = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (connname)
|
2008-12-01 07:23:52 +08:00
|
|
|
rconn = (remoteConn *) MemoryContextAlloc(TopMemoryContext,
|
|
|
|
sizeof(remoteConn));
|
2008-09-22 21:55:14 +08:00
|
|
|
|
|
|
|
/* check password in connection string if not superuser */
|
|
|
|
dblink_connstr_check(connstr);
|
2003-06-25 09:10:15 +08:00
|
|
|
conn = PQconnectdb(connstr);
|
|
|
|
|
|
|
|
if (PQstatus(conn) == CONNECTION_BAD)
|
2002-09-02 14:13:31 +08:00
|
|
|
{
|
2003-06-25 09:10:15 +08:00
|
|
|
msg = pstrdup(PQerrorMessage(conn));
|
|
|
|
PQfinish(conn);
|
2005-10-08 20:12:29 +08:00
|
|
|
if (rconn)
|
|
|
|
pfree(rconn);
|
2003-07-25 01:52:50 +08:00
|
|
|
|
|
|
|
ereport(ERROR,
|
2005-10-15 10:49:52 +08:00
|
|
|
(errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
|
|
|
|
errmsg("could not establish connection"),
|
|
|
|
errdetail("%s", msg)));
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
|
|
|
|
2008-09-22 21:55:14 +08:00
|
|
|
/* check password actually used if not superuser */
|
2008-01-04 05:27:59 +08:00
|
|
|
dblink_security_check(conn, rconn);
|
2007-07-09 01:12:38 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (connname)
|
2003-06-25 09:10:15 +08:00
|
|
|
{
|
2005-10-08 20:12:29 +08:00
|
|
|
rconn->conn = conn;
|
|
|
|
createNewConnection(connname, rconn);
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
|
|
|
else
|
2005-10-18 10:55:49 +08:00
|
|
|
pconn->conn = conn;
|
2003-06-25 09:10:15 +08:00
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text("OK"));
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear a persistent connection to another database
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_disconnect);
|
|
|
|
Datum
|
|
|
|
dblink_disconnect(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2003-07-25 01:52:50 +08:00
|
|
|
char *conname = NULL;
|
2005-10-08 20:12:29 +08:00
|
|
|
remoteConn *rconn = NULL;
|
2003-06-25 09:10:15 +08:00
|
|
|
PGconn *conn = NULL;
|
|
|
|
|
2005-10-18 10:55:49 +08:00
|
|
|
DBLINK_INIT;
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (PG_NARGS() == 1)
|
2003-06-25 09:10:15 +08:00
|
|
|
{
|
2008-03-26 06:42:46 +08:00
|
|
|
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
2005-10-08 20:12:29 +08:00
|
|
|
rconn = getConnectionByName(conname);
|
|
|
|
if (rconn)
|
|
|
|
conn = rconn->conn;
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
|
|
|
else
|
2005-10-18 10:55:49 +08:00
|
|
|
conn = pconn->conn;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2003-06-25 09:10:15 +08:00
|
|
|
if (!conn)
|
2003-07-25 01:52:50 +08:00
|
|
|
DBLINK_CONN_NOT_AVAIL;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2003-06-25 09:10:15 +08:00
|
|
|
PQfinish(conn);
|
2005-10-08 20:12:29 +08:00
|
|
|
if (rconn)
|
2003-06-25 09:10:15 +08:00
|
|
|
{
|
2003-07-25 01:52:50 +08:00
|
|
|
deleteConnection(conname);
|
2005-10-08 20:12:29 +08:00
|
|
|
pfree(rconn);
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
2003-11-28 13:03:02 +08:00
|
|
|
else
|
2005-10-18 10:55:49 +08:00
|
|
|
pconn->conn = NULL;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text("OK"));
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* opens a cursor using a persistent connection
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_open);
|
|
|
|
Datum
|
|
|
|
dblink_open(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
char *msg;
|
|
|
|
PGresult *res = NULL;
|
|
|
|
PGconn *conn = NULL;
|
2003-06-25 09:10:15 +08:00
|
|
|
char *curname = NULL;
|
|
|
|
char *sql = NULL;
|
|
|
|
char *conname = NULL;
|
2006-03-01 14:51:01 +08:00
|
|
|
StringInfoData buf;
|
2005-10-08 20:12:29 +08:00
|
|
|
remoteConn *rconn = NULL;
|
2004-03-07 10:27:00 +08:00
|
|
|
bool fail = true; /* default to backward compatible behavior */
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2005-10-18 10:55:49 +08:00
|
|
|
DBLINK_INIT;
|
2006-03-01 14:51:01 +08:00
|
|
|
initStringInfo(&buf);
|
2005-10-18 10:55:49 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (PG_NARGS() == 2)
|
2003-06-25 09:10:15 +08:00
|
|
|
{
|
2004-03-07 10:27:00 +08:00
|
|
|
/* text,text */
|
2008-03-26 06:42:46 +08:00
|
|
|
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
|
|
|
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
2005-10-18 10:55:49 +08:00
|
|
|
rconn = pconn;
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
else if (PG_NARGS() == 3)
|
2003-06-25 09:10:15 +08:00
|
|
|
{
|
2004-03-07 10:27:00 +08:00
|
|
|
/* might be text,text,text or text,text,bool */
|
|
|
|
if (get_fn_expr_argtype(fcinfo->flinfo, 2) == BOOLOID)
|
|
|
|
{
|
2008-03-26 06:42:46 +08:00
|
|
|
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
|
|
|
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
2004-03-07 10:27:00 +08:00
|
|
|
fail = PG_GETARG_BOOL(2);
|
2005-10-18 10:55:49 +08:00
|
|
|
rconn = pconn;
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-26 06:42:46 +08:00
|
|
|
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
|
|
|
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
|
|
|
sql = text_to_cstring(PG_GETARG_TEXT_PP(2));
|
2005-10-08 20:12:29 +08:00
|
|
|
rconn = getConnectionByName(conname);
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (PG_NARGS() == 4)
|
|
|
|
{
|
|
|
|
/* text,text,text,bool */
|
2008-03-26 06:42:46 +08:00
|
|
|
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
|
|
|
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
|
|
|
sql = text_to_cstring(PG_GETARG_TEXT_PP(2));
|
2004-03-07 10:27:00 +08:00
|
|
|
fail = PG_GETARG_BOOL(3);
|
2005-10-08 20:12:29 +08:00
|
|
|
rconn = getConnectionByName(conname);
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
|
|
|
|
2005-10-18 10:55:49 +08:00
|
|
|
if (!rconn || !rconn->conn)
|
2003-07-25 01:52:50 +08:00
|
|
|
DBLINK_CONN_NOT_AVAIL;
|
2005-10-18 10:55:49 +08:00
|
|
|
else
|
|
|
|
conn = rconn->conn;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2005-11-23 02:17:34 +08:00
|
|
|
/* If we are not in a transaction, start one */
|
2005-10-18 10:55:49 +08:00
|
|
|
if (PQtransactionStatus(conn) == PQTRANS_IDLE)
|
|
|
|
{
|
|
|
|
res = PQexec(conn, "BEGIN");
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
|
|
DBLINK_RES_INTERNALERROR("begin error");
|
|
|
|
PQclear(res);
|
|
|
|
rconn->newXactForCursor = TRUE;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-06-22 00:43:11 +08:00
|
|
|
/*
|
|
|
|
* Since transaction state was IDLE, we force cursor count to
|
2006-10-04 08:30:14 +08:00
|
|
|
* initially be 0. This is needed as a previous ABORT might have wiped
|
|
|
|
* out our transaction without maintaining the cursor count for us.
|
2006-06-22 00:43:11 +08:00
|
|
|
*/
|
|
|
|
rconn->openCursorCount = 0;
|
2005-10-18 10:55:49 +08:00
|
|
|
}
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2005-10-18 10:55:49 +08:00
|
|
|
/* if we started a transaction, increment cursor count */
|
|
|
|
if (rconn->newXactForCursor)
|
|
|
|
(rconn->openCursorCount)++;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, "DECLARE %s CURSOR FOR %s", curname, sql);
|
|
|
|
res = PQexec(conn, buf.data);
|
2004-03-07 10:27:00 +08:00
|
|
|
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
|
|
{
|
2008-07-03 11:56:57 +08:00
|
|
|
dblink_res_error(conname, res, "could not open cursor", fail);
|
|
|
|
PG_RETURN_TEXT_P(cstring_to_text("ERROR"));
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2003-06-25 09:10:15 +08:00
|
|
|
PQclear(res);
|
2008-03-26 06:42:46 +08:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text("OK"));
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
/*
|
|
|
|
* closes a cursor
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_close);
|
|
|
|
Datum
|
|
|
|
dblink_close(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
PGconn *conn = NULL;
|
|
|
|
PGresult *res = NULL;
|
2003-06-25 09:10:15 +08:00
|
|
|
char *curname = NULL;
|
|
|
|
char *conname = NULL;
|
2006-03-01 14:51:01 +08:00
|
|
|
StringInfoData buf;
|
2002-09-05 04:31:48 +08:00
|
|
|
char *msg;
|
2005-10-08 20:12:29 +08:00
|
|
|
remoteConn *rconn = NULL;
|
2004-03-07 10:27:00 +08:00
|
|
|
bool fail = true; /* default to backward compatible behavior */
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2005-10-18 10:55:49 +08:00
|
|
|
DBLINK_INIT;
|
2006-03-01 14:51:01 +08:00
|
|
|
initStringInfo(&buf);
|
2005-10-18 10:55:49 +08:00
|
|
|
|
2003-06-25 09:10:15 +08:00
|
|
|
if (PG_NARGS() == 1)
|
|
|
|
{
|
2004-03-07 10:27:00 +08:00
|
|
|
/* text */
|
2008-03-26 06:42:46 +08:00
|
|
|
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
2005-10-18 10:55:49 +08:00
|
|
|
rconn = pconn;
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
2003-08-04 08:43:34 +08:00
|
|
|
else if (PG_NARGS() == 2)
|
2003-06-25 09:10:15 +08:00
|
|
|
{
|
2004-03-07 10:27:00 +08:00
|
|
|
/* might be text,text or text,bool */
|
|
|
|
if (get_fn_expr_argtype(fcinfo->flinfo, 1) == BOOLOID)
|
|
|
|
{
|
2008-03-26 06:42:46 +08:00
|
|
|
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
2004-03-07 10:27:00 +08:00
|
|
|
fail = PG_GETARG_BOOL(1);
|
2005-10-18 10:55:49 +08:00
|
|
|
rconn = pconn;
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-26 06:42:46 +08:00
|
|
|
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
|
|
|
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
2005-10-08 20:12:29 +08:00
|
|
|
rconn = getConnectionByName(conname);
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (PG_NARGS() == 3)
|
|
|
|
{
|
|
|
|
/* text,text,bool */
|
2008-03-26 06:42:46 +08:00
|
|
|
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
|
|
|
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
2004-03-07 10:27:00 +08:00
|
|
|
fail = PG_GETARG_BOOL(2);
|
2005-10-08 20:12:29 +08:00
|
|
|
rconn = getConnectionByName(conname);
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
|
|
|
|
2005-10-18 10:55:49 +08:00
|
|
|
if (!rconn || !rconn->conn)
|
2003-07-25 01:52:50 +08:00
|
|
|
DBLINK_CONN_NOT_AVAIL;
|
2005-10-18 10:55:49 +08:00
|
|
|
else
|
|
|
|
conn = rconn->conn;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, "CLOSE %s", curname);
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
/* close the cursor */
|
2006-03-01 14:51:01 +08:00
|
|
|
res = PQexec(conn, buf.data);
|
2002-09-05 04:31:48 +08:00
|
|
|
if (!res || PQresultStatus(res) != PGRES_COMMAND_OK)
|
2004-03-07 10:27:00 +08:00
|
|
|
{
|
2008-07-03 11:56:57 +08:00
|
|
|
dblink_res_error(conname, res, "could not close cursor", fail);
|
|
|
|
PG_RETURN_TEXT_P(cstring_to_text("ERROR"));
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
PQclear(res);
|
|
|
|
|
2005-10-18 10:55:49 +08:00
|
|
|
/* if we started a transaction, decrement cursor count */
|
|
|
|
if (rconn->newXactForCursor)
|
|
|
|
{
|
|
|
|
(rconn->openCursorCount)--;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2005-10-18 10:55:49 +08:00
|
|
|
/* if count is zero, commit the transaction */
|
|
|
|
if (rconn->openCursorCount == 0)
|
|
|
|
{
|
|
|
|
rconn->newXactForCursor = FALSE;
|
|
|
|
|
|
|
|
res = PQexec(conn, "COMMIT");
|
|
|
|
if (PQresultStatus(res) != PGRES_COMMAND_OK)
|
|
|
|
DBLINK_RES_INTERNALERROR("commit error");
|
|
|
|
PQclear(res);
|
|
|
|
}
|
|
|
|
}
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text("OK"));
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch results from an open cursor
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_fetch);
|
|
|
|
Datum
|
|
|
|
dblink_fetch(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
FuncCallContext *funcctx;
|
|
|
|
TupleDesc tupdesc = NULL;
|
|
|
|
int call_cntr;
|
|
|
|
int max_calls;
|
|
|
|
AttInMetadata *attinmeta;
|
|
|
|
PGresult *res = NULL;
|
|
|
|
MemoryContext oldcontext;
|
2003-06-25 09:10:15 +08:00
|
|
|
char *conname = NULL;
|
2005-10-08 20:12:29 +08:00
|
|
|
remoteConn *rconn = NULL;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2005-10-18 10:55:49 +08:00
|
|
|
DBLINK_INIT;
|
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
/* stuff done only on the first call of the function */
|
2002-09-05 04:31:48 +08:00
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
PGconn *conn = NULL;
|
2006-03-01 14:51:01 +08:00
|
|
|
StringInfoData buf;
|
2003-06-25 09:10:15 +08:00
|
|
|
char *curname = NULL;
|
|
|
|
int howmany = 0;
|
2004-03-07 10:27:00 +08:00
|
|
|
bool fail = true; /* default to backward compatible */
|
2003-06-25 09:10:15 +08:00
|
|
|
|
2004-03-07 10:27:00 +08:00
|
|
|
if (PG_NARGS() == 4)
|
2003-06-25 09:10:15 +08:00
|
|
|
{
|
2004-03-07 10:27:00 +08:00
|
|
|
/* text,text,int,bool */
|
2008-03-26 06:42:46 +08:00
|
|
|
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
|
|
|
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
2003-06-25 09:10:15 +08:00
|
|
|
howmany = PG_GETARG_INT32(2);
|
2004-03-07 10:27:00 +08:00
|
|
|
fail = PG_GETARG_BOOL(3);
|
2003-06-25 09:10:15 +08:00
|
|
|
|
2005-10-08 20:12:29 +08:00
|
|
|
rconn = getConnectionByName(conname);
|
|
|
|
if (rconn)
|
|
|
|
conn = rconn->conn;
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
2004-03-07 10:27:00 +08:00
|
|
|
else if (PG_NARGS() == 3)
|
|
|
|
{
|
|
|
|
/* text,text,int or text,int,bool */
|
|
|
|
if (get_fn_expr_argtype(fcinfo->flinfo, 2) == BOOLOID)
|
|
|
|
{
|
2008-03-26 06:42:46 +08:00
|
|
|
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
2004-03-07 10:27:00 +08:00
|
|
|
howmany = PG_GETARG_INT32(1);
|
|
|
|
fail = PG_GETARG_BOOL(2);
|
2005-10-18 10:55:49 +08:00
|
|
|
conn = pconn->conn;
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-26 06:42:46 +08:00
|
|
|
conname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
|
|
|
curname = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
2004-03-07 10:27:00 +08:00
|
|
|
howmany = PG_GETARG_INT32(2);
|
|
|
|
|
2005-10-08 20:12:29 +08:00
|
|
|
rconn = getConnectionByName(conname);
|
|
|
|
if (rconn)
|
|
|
|
conn = rconn->conn;
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
|
|
|
}
|
2003-06-25 09:10:15 +08:00
|
|
|
else if (PG_NARGS() == 2)
|
|
|
|
{
|
2004-03-07 10:27:00 +08:00
|
|
|
/* text,int */
|
2008-03-26 06:42:46 +08:00
|
|
|
curname = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
2003-06-25 09:10:15 +08:00
|
|
|
howmany = PG_GETARG_INT32(1);
|
2005-10-18 10:55:49 +08:00
|
|
|
conn = pconn->conn;
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!conn)
|
2003-07-25 01:52:50 +08:00
|
|
|
DBLINK_CONN_NOT_AVAIL;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
initStringInfo(&buf);
|
|
|
|
appendStringInfo(&buf, "FETCH %d FROM %s", howmany, curname);
|
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
/* create a function context for cross-call persistence */
|
2002-09-05 04:31:48 +08:00
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
/*
|
2008-12-01 07:23:52 +08:00
|
|
|
* Try to execute the query. Note that since libpq uses malloc,
|
|
|
|
* the PGresult will be long-lived even though we are still in
|
|
|
|
* a short-lived memory context.
|
2002-09-05 04:31:48 +08:00
|
|
|
*/
|
2006-03-01 14:51:01 +08:00
|
|
|
res = PQexec(conn, buf.data);
|
2002-09-02 14:13:31 +08:00
|
|
|
if (!res ||
|
|
|
|
(PQresultStatus(res) != PGRES_COMMAND_OK &&
|
|
|
|
PQresultStatus(res) != PGRES_TUPLES_OK))
|
2004-03-07 10:27:00 +08:00
|
|
|
{
|
2008-07-03 11:56:57 +08:00
|
|
|
dblink_res_error(conname, res, "could not fetch from cursor", fail);
|
|
|
|
SRF_RETURN_DONE(funcctx);
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
2002-09-02 14:13:31 +08:00
|
|
|
else if (PQresultStatus(res) == PGRES_COMMAND_OK)
|
|
|
|
{
|
|
|
|
/* cursor does not exist - closed already or bad name */
|
|
|
|
PQclear(res);
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_CURSOR_NAME),
|
|
|
|
errmsg("cursor \"%s\" does not exist", curname)));
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
funcctx->max_calls = PQntuples(res);
|
|
|
|
|
|
|
|
/* got results, keep track of them */
|
|
|
|
funcctx->user_fctx = res;
|
|
|
|
|
2005-05-31 07:09:07 +08:00
|
|
|
/* get a tuple descriptor for our result type */
|
|
|
|
switch (get_call_result_type(fcinfo, NULL, &tupdesc))
|
2004-02-24 14:07:18 +08:00
|
|
|
{
|
2005-05-31 07:09:07 +08:00
|
|
|
case TYPEFUNC_COMPOSITE:
|
|
|
|
/* success */
|
|
|
|
break;
|
|
|
|
case TYPEFUNC_RECORD:
|
|
|
|
/* failed to determine actual type of RECORD */
|
2004-02-24 14:07:18 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2005-05-31 07:09:07 +08:00
|
|
|
errmsg("function returning record called in context "
|
|
|
|
"that cannot accept type record")));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* result type isn't composite */
|
|
|
|
elog(ERROR, "return type must be a row type");
|
|
|
|
break;
|
2004-02-24 14:07:18 +08:00
|
|
|
}
|
2005-05-31 07:09:07 +08:00
|
|
|
|
2006-01-04 07:45:52 +08:00
|
|
|
/* check result and tuple descriptor have the same number of columns */
|
|
|
|
if (PQnfields(res) != tupdesc->natts)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATATYPE_MISMATCH),
|
2006-10-04 08:30:14 +08:00
|
|
|
errmsg("remote query result rowtype does not match "
|
|
|
|
"the specified FROM clause rowtype")));
|
2006-01-04 07:45:52 +08:00
|
|
|
|
2008-12-01 07:23:52 +08:00
|
|
|
/*
|
|
|
|
* fast track when no results. We could exit earlier, but then
|
|
|
|
* we'd not report error if the result tuple type is wrong.
|
|
|
|
*/
|
2006-01-04 07:45:52 +08:00
|
|
|
if (funcctx->max_calls < 1)
|
|
|
|
{
|
2008-12-01 07:23:52 +08:00
|
|
|
PQclear(res);
|
2006-01-04 07:45:52 +08:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
|
|
|
}
|
|
|
|
|
2008-12-01 07:23:52 +08:00
|
|
|
/*
|
|
|
|
* switch to memory context appropriate for multiple function calls,
|
|
|
|
* so we can make long-lived copy of tupdesc etc
|
|
|
|
*/
|
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
|
|
|
/* make sure we have a persistent copy of the tupdesc */
|
|
|
|
tupdesc = CreateTupleDescCopy(tupdesc);
|
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
/* store needed metadata for subsequent calls */
|
|
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc);
|
|
|
|
funcctx->attinmeta = attinmeta;
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
/* stuff done on every call of the function */
|
2002-09-05 04:31:48 +08:00
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize per-call variables
|
|
|
|
*/
|
|
|
|
call_cntr = funcctx->call_cntr;
|
|
|
|
max_calls = funcctx->max_calls;
|
|
|
|
|
|
|
|
res = (PGresult *) funcctx->user_fctx;
|
|
|
|
attinmeta = funcctx->attinmeta;
|
|
|
|
tupdesc = attinmeta->tupdesc;
|
|
|
|
|
|
|
|
if (call_cntr < max_calls) /* do when there is more left to send */
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
2002-09-02 14:13:31 +08:00
|
|
|
char **values;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Datum result;
|
2002-09-05 04:31:48 +08:00
|
|
|
int i;
|
|
|
|
int nfields = PQnfields(res);
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
values = (char **) palloc(nfields * sizeof(char *));
|
|
|
|
for (i = 0; i < nfields; i++)
|
|
|
|
{
|
|
|
|
if (PQgetisnull(res, call_cntr, i) == 0)
|
|
|
|
values[i] = PQgetvalue(res, call_cntr, i);
|
|
|
|
else
|
|
|
|
values[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build the tuple */
|
|
|
|
tuple = BuildTupleFromCStrings(attinmeta, values);
|
|
|
|
|
|
|
|
/* make the tuple into a datum */
|
2004-04-02 05:28:47 +08:00
|
|
|
result = HeapTupleGetDatum(tuple);
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
SRF_RETURN_NEXT(funcctx, result);
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
else
|
2002-09-02 14:13:31 +08:00
|
|
|
{
|
2003-06-25 09:10:15 +08:00
|
|
|
/* do when there is no more left */
|
2002-09-02 14:13:31 +08:00
|
|
|
PQclear(res);
|
2002-09-05 04:31:48 +08:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: this is the new preferred version of dblink
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_record);
|
|
|
|
Datum
|
|
|
|
dblink_record(PG_FUNCTION_ARGS)
|
2006-09-03 05:11:15 +08:00
|
|
|
{
|
|
|
|
return dblink_record_internal(fcinfo, false, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_send_query);
|
|
|
|
Datum
|
|
|
|
dblink_send_query(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return dblink_record_internal(fcinfo, true, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_get_result);
|
|
|
|
Datum
|
|
|
|
dblink_get_result(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return dblink_record_internal(fcinfo, true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Datum
|
|
|
|
dblink_record_internal(FunctionCallInfo fcinfo, bool is_async, bool do_get)
|
2002-09-02 14:13:31 +08:00
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
FuncCallContext *funcctx;
|
|
|
|
TupleDesc tupdesc = NULL;
|
|
|
|
int call_cntr;
|
|
|
|
int max_calls;
|
|
|
|
AttInMetadata *attinmeta;
|
|
|
|
char *msg;
|
|
|
|
PGresult *res = NULL;
|
|
|
|
bool is_sql_cmd = false;
|
|
|
|
char *sql_cmd_status = NULL;
|
|
|
|
MemoryContext oldcontext;
|
2004-03-07 10:27:00 +08:00
|
|
|
bool freeconn = false;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2005-10-18 10:55:49 +08:00
|
|
|
DBLINK_INIT;
|
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
/* stuff done only on the first call of the function */
|
2002-09-05 04:31:48 +08:00
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
PGconn *conn = NULL;
|
|
|
|
char *connstr = NULL;
|
|
|
|
char *sql = NULL;
|
2003-06-25 09:10:15 +08:00
|
|
|
char *conname = NULL;
|
2005-10-08 20:12:29 +08:00
|
|
|
remoteConn *rconn = NULL;
|
2004-03-07 10:27:00 +08:00
|
|
|
bool fail = true; /* default to backward compatible */
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
/* create a function context for cross-call persistence */
|
2002-09-05 04:31:48 +08:00
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* switch to memory context appropriate for multiple function calls
|
2002-09-05 04:31:48 +08:00
|
|
|
*/
|
2002-09-02 14:13:31 +08:00
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
if (!is_async)
|
2002-09-02 14:13:31 +08:00
|
|
|
{
|
2006-09-03 05:11:15 +08:00
|
|
|
if (PG_NARGS() == 3)
|
|
|
|
{
|
|
|
|
/* text,text,bool */
|
|
|
|
DBLINK_GET_CONN;
|
2008-03-26 06:42:46 +08:00
|
|
|
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
2006-09-03 05:11:15 +08:00
|
|
|
fail = PG_GETARG_BOOL(2);
|
|
|
|
}
|
|
|
|
else if (PG_NARGS() == 2)
|
2004-03-07 10:27:00 +08:00
|
|
|
{
|
2006-09-03 05:11:15 +08:00
|
|
|
/* text,text or text,bool */
|
|
|
|
if (get_fn_expr_argtype(fcinfo->flinfo, 1) == BOOLOID)
|
|
|
|
{
|
|
|
|
conn = pconn->conn;
|
2008-03-26 06:42:46 +08:00
|
|
|
sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
2006-09-03 05:11:15 +08:00
|
|
|
fail = PG_GETARG_BOOL(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DBLINK_GET_CONN;
|
2008-03-26 06:42:46 +08:00
|
|
|
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
2006-09-03 05:11:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (PG_NARGS() == 1)
|
|
|
|
{
|
|
|
|
/* text */
|
2005-10-18 10:55:49 +08:00
|
|
|
conn = pconn->conn;
|
2008-03-26 06:42:46 +08:00
|
|
|
sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
|
|
|
else
|
2006-09-03 05:11:15 +08:00
|
|
|
/* shouldn't happen */
|
|
|
|
elog(ERROR, "wrong number of arguments");
|
|
|
|
}
|
|
|
|
else if (is_async && do_get)
|
|
|
|
{
|
|
|
|
/* get async result */
|
|
|
|
if (PG_NARGS() == 2)
|
2004-03-07 10:27:00 +08:00
|
|
|
{
|
2006-09-03 05:11:15 +08:00
|
|
|
/* text,bool */
|
2004-03-07 10:27:00 +08:00
|
|
|
DBLINK_GET_CONN;
|
2006-09-03 05:11:15 +08:00
|
|
|
fail = PG_GETARG_BOOL(2);
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
2006-09-03 05:11:15 +08:00
|
|
|
else if (PG_NARGS() == 1)
|
|
|
|
{
|
|
|
|
/* text */
|
|
|
|
DBLINK_GET_CONN;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
/* shouldn't happen */
|
|
|
|
elog(ERROR, "wrong number of arguments");
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
2006-09-03 05:11:15 +08:00
|
|
|
else
|
2002-09-02 14:13:31 +08:00
|
|
|
{
|
2006-09-03 05:11:15 +08:00
|
|
|
/* send async query */
|
|
|
|
if (PG_NARGS() == 2)
|
|
|
|
{
|
|
|
|
DBLINK_GET_CONN;
|
2008-03-26 06:42:46 +08:00
|
|
|
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
2006-09-03 05:11:15 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
/* shouldn't happen */
|
|
|
|
elog(ERROR, "wrong number of arguments");
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!conn)
|
2003-07-25 01:52:50 +08:00
|
|
|
DBLINK_CONN_NOT_AVAIL;
|
2003-06-25 09:10:15 +08:00
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
if (!is_async || (is_async && do_get))
|
2004-03-07 10:27:00 +08:00
|
|
|
{
|
2006-09-03 05:11:15 +08:00
|
|
|
/* synchronous query, or async result retrieval */
|
|
|
|
if (!is_async)
|
|
|
|
res = PQexec(conn, sql);
|
2004-03-07 10:27:00 +08:00
|
|
|
else
|
|
|
|
{
|
2006-09-03 05:11:15 +08:00
|
|
|
res = PQgetResult(conn);
|
|
|
|
/* NULL means we're all done with the async results */
|
|
|
|
if (!res)
|
2008-12-01 07:23:52 +08:00
|
|
|
{
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2006-09-03 05:11:15 +08:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
2008-12-01 07:23:52 +08:00
|
|
|
}
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
if (!res ||
|
|
|
|
(PQresultStatus(res) != PGRES_COMMAND_OK &&
|
2006-10-04 08:30:14 +08:00
|
|
|
PQresultStatus(res) != PGRES_TUPLES_OK))
|
2004-02-24 14:07:18 +08:00
|
|
|
{
|
2008-07-03 11:56:57 +08:00
|
|
|
dblink_res_error(conname, res, "could not execute query", fail);
|
|
|
|
if (freeconn)
|
|
|
|
PQfinish(conn);
|
2008-12-01 07:23:52 +08:00
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2008-07-03 11:56:57 +08:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
2004-02-24 14:07:18 +08:00
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
if (PQresultStatus(res) == PGRES_COMMAND_OK)
|
|
|
|
{
|
|
|
|
is_sql_cmd = true;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
/* need a tuple descriptor representing one TEXT column */
|
|
|
|
tupdesc = CreateTemplateTupleDesc(1, false);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "status",
|
2006-10-04 08:30:14 +08:00
|
|
|
TEXTOID, -1, 0);
|
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
/*
|
2006-10-04 08:30:14 +08:00
|
|
|
* and save a copy of the command status string to return as
|
|
|
|
* our result tuple
|
|
|
|
*/
|
2006-09-03 05:11:15 +08:00
|
|
|
sql_cmd_status = PQcmdStatus(res);
|
|
|
|
funcctx->max_calls = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
funcctx->max_calls = PQntuples(res);
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
/* got results, keep track of them */
|
|
|
|
funcctx->user_fctx = res;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
/* if needed, close the connection to the database and cleanup */
|
|
|
|
if (freeconn)
|
|
|
|
PQfinish(conn);
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
if (!is_sql_cmd)
|
|
|
|
{
|
|
|
|
/* get a tuple descriptor for our result type */
|
|
|
|
switch (get_call_result_type(fcinfo, NULL, &tupdesc))
|
|
|
|
{
|
|
|
|
case TYPEFUNC_COMPOSITE:
|
|
|
|
/* success */
|
|
|
|
break;
|
|
|
|
case TYPEFUNC_RECORD:
|
|
|
|
/* failed to determine actual type of RECORD */
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
2006-10-04 08:30:14 +08:00
|
|
|
errmsg("function returning record called in context "
|
|
|
|
"that cannot accept type record")));
|
2006-09-03 05:11:15 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* result type isn't composite */
|
|
|
|
elog(ERROR, "return type must be a row type");
|
|
|
|
break;
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
/* make sure we have a persistent copy of the tupdesc */
|
|
|
|
tupdesc = CreateTupleDescCopy(tupdesc);
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check result and tuple descriptor have the same number of
|
|
|
|
* columns
|
|
|
|
*/
|
2006-09-03 05:11:15 +08:00
|
|
|
if (PQnfields(res) != tupdesc->natts)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DATATYPE_MISMATCH),
|
2006-10-04 08:30:14 +08:00
|
|
|
errmsg("remote query result rowtype does not match "
|
|
|
|
"the specified FROM clause rowtype")));
|
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
/* fast track when no results */
|
|
|
|
if (funcctx->max_calls < 1)
|
|
|
|
{
|
|
|
|
if (res)
|
|
|
|
PQclear(res);
|
2008-12-01 07:23:52 +08:00
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2006-09-03 05:11:15 +08:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
|
|
|
}
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
/* store needed metadata for subsequent calls */
|
|
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc);
|
|
|
|
funcctx->attinmeta = attinmeta;
|
2006-10-04 08:30:14 +08:00
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
2006-09-03 05:11:15 +08:00
|
|
|
else
|
2006-01-04 07:45:52 +08:00
|
|
|
{
|
2006-09-03 05:11:15 +08:00
|
|
|
/* async query send */
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
PG_RETURN_INT32(PQsendQuery(conn, sql));
|
2006-01-04 07:45:52 +08:00
|
|
|
}
|
2006-09-03 05:11:15 +08:00
|
|
|
}
|
2006-01-04 07:45:52 +08:00
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
if (is_async && !do_get)
|
|
|
|
{
|
|
|
|
/* async query send -- should not happen */
|
|
|
|
elog(ERROR, "async query send called more than once");
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
/* stuff done on every call of the function */
|
2002-09-05 04:31:48 +08:00
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* initialize per-call variables
|
|
|
|
*/
|
|
|
|
call_cntr = funcctx->call_cntr;
|
|
|
|
max_calls = funcctx->max_calls;
|
|
|
|
|
|
|
|
res = (PGresult *) funcctx->user_fctx;
|
|
|
|
attinmeta = funcctx->attinmeta;
|
|
|
|
tupdesc = attinmeta->tupdesc;
|
|
|
|
|
|
|
|
if (call_cntr < max_calls) /* do when there is more left to send */
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
2002-09-02 14:13:31 +08:00
|
|
|
char **values;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Datum result;
|
|
|
|
|
|
|
|
if (!is_sql_cmd)
|
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
int i;
|
|
|
|
int nfields = PQnfields(res);
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
values = (char **) palloc(nfields * sizeof(char *));
|
|
|
|
for (i = 0; i < nfields; i++)
|
|
|
|
{
|
|
|
|
if (PQgetisnull(res, call_cntr, i) == 0)
|
|
|
|
values[i] = PQgetvalue(res, call_cntr, i);
|
|
|
|
else
|
|
|
|
values[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
values = (char **) palloc(1 * sizeof(char *));
|
|
|
|
values[0] = sql_cmd_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* build the tuple */
|
|
|
|
tuple = BuildTupleFromCStrings(attinmeta, values);
|
|
|
|
|
|
|
|
/* make the tuple into a datum */
|
2004-04-02 05:28:47 +08:00
|
|
|
result = HeapTupleGetDatum(tuple);
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
SRF_RETURN_NEXT(funcctx, result);
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
else
|
2002-09-02 14:13:31 +08:00
|
|
|
{
|
2003-06-25 09:10:15 +08:00
|
|
|
/* do when there is no more left */
|
2002-09-02 14:13:31 +08:00
|
|
|
PQclear(res);
|
2002-09-05 04:31:48 +08:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-03 05:11:15 +08:00
|
|
|
/*
|
|
|
|
* List all open dblink connections by name.
|
|
|
|
* Returns an array of all connection names.
|
|
|
|
* Takes no params
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_get_connections);
|
|
|
|
Datum
|
|
|
|
dblink_get_connections(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
HASH_SEQ_STATUS status;
|
|
|
|
remoteConnHashEnt *hentry;
|
|
|
|
ArrayBuildState *astate = NULL;
|
2006-09-03 05:11:15 +08:00
|
|
|
|
|
|
|
if (remoteConnHash)
|
|
|
|
{
|
|
|
|
hash_seq_init(&status, remoteConnHash);
|
|
|
|
while ((hentry = (remoteConnHashEnt *) hash_seq_search(&status)) != NULL)
|
|
|
|
{
|
|
|
|
/* stash away current value */
|
|
|
|
astate = accumArrayResult(astate,
|
2008-03-26 06:42:46 +08:00
|
|
|
CStringGetTextDatum(hentry->name),
|
2006-09-03 05:11:15 +08:00
|
|
|
false, TEXTOID, CurrentMemoryContext);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (astate)
|
|
|
|
PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate,
|
|
|
|
CurrentMemoryContext));
|
|
|
|
else
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks if a given remote connection is busy
|
|
|
|
*
|
|
|
|
* Returns 1 if the connection is busy, 0 otherwise
|
|
|
|
* Params:
|
2006-10-04 08:30:14 +08:00
|
|
|
* text connection_name - name of the connection to check
|
|
|
|
*
|
2006-09-03 05:11:15 +08:00
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_is_busy);
|
|
|
|
Datum
|
|
|
|
dblink_is_busy(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
PGconn *conn = NULL;
|
|
|
|
remoteConn *rconn = NULL;
|
2006-09-03 05:11:15 +08:00
|
|
|
|
|
|
|
DBLINK_INIT;
|
2008-01-04 05:27:59 +08:00
|
|
|
DBLINK_GET_NAMED_CONN;
|
2006-09-03 05:11:15 +08:00
|
|
|
|
|
|
|
PQconsumeInput(conn);
|
|
|
|
PG_RETURN_INT32(PQisBusy(conn));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cancels a running request on a connection
|
|
|
|
*
|
2006-10-04 08:30:14 +08:00
|
|
|
* Returns text:
|
2006-09-03 05:11:15 +08:00
|
|
|
* "OK" if the cancel request has been sent correctly,
|
2006-10-04 08:30:14 +08:00
|
|
|
* an error message otherwise
|
|
|
|
*
|
2006-09-03 05:11:15 +08:00
|
|
|
* Params:
|
2006-10-04 08:30:14 +08:00
|
|
|
* text connection_name - name of the connection to check
|
|
|
|
*
|
2006-09-03 05:11:15 +08:00
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_cancel_query);
|
|
|
|
Datum
|
|
|
|
dblink_cancel_query(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
int res = 0;
|
|
|
|
PGconn *conn = NULL;
|
|
|
|
remoteConn *rconn = NULL;
|
|
|
|
PGcancel *cancel;
|
|
|
|
char errbuf[256];
|
2006-09-03 05:11:15 +08:00
|
|
|
|
|
|
|
DBLINK_INIT;
|
2008-01-04 05:27:59 +08:00
|
|
|
DBLINK_GET_NAMED_CONN;
|
2006-09-03 05:11:15 +08:00
|
|
|
cancel = PQgetCancel(conn);
|
|
|
|
|
|
|
|
res = PQcancel(cancel, errbuf, 256);
|
|
|
|
PQfreeCancel(cancel);
|
|
|
|
|
2008-01-04 05:27:59 +08:00
|
|
|
if (res == 1)
|
2008-03-26 06:42:46 +08:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text("OK"));
|
2006-09-03 05:11:15 +08:00
|
|
|
else
|
2008-03-26 06:42:46 +08:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(errbuf));
|
2006-09-03 05:11:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get error message from a connection
|
|
|
|
*
|
2006-10-04 08:30:14 +08:00
|
|
|
* Returns text:
|
2006-09-03 05:11:15 +08:00
|
|
|
* "OK" if no error, an error message otherwise
|
2006-10-04 08:30:14 +08:00
|
|
|
*
|
2006-09-03 05:11:15 +08:00
|
|
|
* Params:
|
2006-10-04 08:30:14 +08:00
|
|
|
* text connection_name - name of the connection to check
|
|
|
|
*
|
2006-09-03 05:11:15 +08:00
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_error_message);
|
|
|
|
Datum
|
|
|
|
dblink_error_message(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-10-04 08:30:14 +08:00
|
|
|
char *msg;
|
|
|
|
PGconn *conn = NULL;
|
|
|
|
remoteConn *rconn = NULL;
|
2006-09-03 05:11:15 +08:00
|
|
|
|
|
|
|
DBLINK_INIT;
|
2008-01-04 05:27:59 +08:00
|
|
|
DBLINK_GET_NAMED_CONN;
|
2006-09-03 05:11:15 +08:00
|
|
|
|
|
|
|
msg = PQerrorMessage(conn);
|
2008-01-04 05:27:59 +08:00
|
|
|
if (msg == NULL || msg[0] == '\0')
|
2008-03-26 06:42:46 +08:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text("OK"));
|
2006-09-03 05:11:15 +08:00
|
|
|
else
|
2008-03-26 06:42:46 +08:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(msg));
|
2006-09-03 05:11:15 +08:00
|
|
|
}
|
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
/*
|
|
|
|
* Execute an SQL non-SELECT command
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_exec);
|
|
|
|
Datum
|
|
|
|
dblink_exec(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
char *msg;
|
|
|
|
PGresult *res = NULL;
|
2003-06-25 09:10:15 +08:00
|
|
|
text *sql_cmd_status = NULL;
|
2002-09-05 04:31:48 +08:00
|
|
|
TupleDesc tupdesc = NULL;
|
|
|
|
PGconn *conn = NULL;
|
|
|
|
char *connstr = NULL;
|
|
|
|
char *sql = NULL;
|
2003-06-25 09:10:15 +08:00
|
|
|
char *conname = NULL;
|
2005-10-08 20:12:29 +08:00
|
|
|
remoteConn *rconn = NULL;
|
2004-03-07 10:27:00 +08:00
|
|
|
bool freeconn = false;
|
|
|
|
bool fail = true; /* default to backward compatible behavior */
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2005-10-18 10:55:49 +08:00
|
|
|
DBLINK_INIT;
|
|
|
|
|
2004-03-07 10:27:00 +08:00
|
|
|
if (PG_NARGS() == 3)
|
2002-09-02 14:13:31 +08:00
|
|
|
{
|
2004-03-07 10:27:00 +08:00
|
|
|
/* must be text,text,bool */
|
2003-07-25 01:52:50 +08:00
|
|
|
DBLINK_GET_CONN;
|
2008-03-26 06:42:46 +08:00
|
|
|
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
2004-03-07 10:27:00 +08:00
|
|
|
fail = PG_GETARG_BOOL(2);
|
|
|
|
}
|
|
|
|
else if (PG_NARGS() == 2)
|
|
|
|
{
|
|
|
|
/* might be text,text or text,bool */
|
|
|
|
if (get_fn_expr_argtype(fcinfo->flinfo, 1) == BOOLOID)
|
|
|
|
{
|
2005-10-18 10:55:49 +08:00
|
|
|
conn = pconn->conn;
|
2008-03-26 06:42:46 +08:00
|
|
|
sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
2004-03-07 10:27:00 +08:00
|
|
|
fail = PG_GETARG_BOOL(1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DBLINK_GET_CONN;
|
2008-03-26 06:42:46 +08:00
|
|
|
sql = text_to_cstring(PG_GETARG_TEXT_PP(1));
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
2003-06-25 09:10:15 +08:00
|
|
|
else if (PG_NARGS() == 1)
|
2002-09-02 14:13:31 +08:00
|
|
|
{
|
2004-03-07 10:27:00 +08:00
|
|
|
/* must be single text argument */
|
2005-10-18 10:55:49 +08:00
|
|
|
conn = pconn->conn;
|
2008-03-26 06:42:46 +08:00
|
|
|
sql = text_to_cstring(PG_GETARG_TEXT_PP(0));
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
|
|
|
else
|
2003-07-25 01:52:50 +08:00
|
|
|
/* shouldn't happen */
|
|
|
|
elog(ERROR, "wrong number of arguments");
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!conn)
|
2003-07-25 01:52:50 +08:00
|
|
|
DBLINK_CONN_NOT_AVAIL;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
res = PQexec(conn, sql);
|
2003-06-25 09:10:15 +08:00
|
|
|
if (!res ||
|
|
|
|
(PQresultStatus(res) != PGRES_COMMAND_OK &&
|
|
|
|
PQresultStatus(res) != PGRES_TUPLES_OK))
|
2004-03-07 10:27:00 +08:00
|
|
|
{
|
2008-07-03 11:56:57 +08:00
|
|
|
dblink_res_error(conname, res, "could not execute command", fail);
|
2003-06-25 09:10:15 +08:00
|
|
|
|
2004-03-07 10:27:00 +08:00
|
|
|
/* need a tuple descriptor representing one TEXT column */
|
|
|
|
tupdesc = CreateTemplateTupleDesc(1, false);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "status",
|
2004-04-02 05:28:47 +08:00
|
|
|
TEXTOID, -1, 0);
|
2004-03-07 10:27:00 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* and save a copy of the command status string to return as our
|
|
|
|
* result tuple
|
|
|
|
*/
|
2008-03-26 06:42:46 +08:00
|
|
|
sql_cmd_status = cstring_to_text("ERROR");
|
2004-03-07 10:27:00 +08:00
|
|
|
}
|
|
|
|
else if (PQresultStatus(res) == PGRES_COMMAND_OK)
|
2002-09-02 14:13:31 +08:00
|
|
|
{
|
2003-06-25 09:10:15 +08:00
|
|
|
/* need a tuple descriptor representing one TEXT column */
|
|
|
|
tupdesc = CreateTemplateTupleDesc(1, false);
|
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "status",
|
2004-04-02 05:28:47 +08:00
|
|
|
TEXTOID, -1, 0);
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2003-06-25 09:10:15 +08:00
|
|
|
/*
|
2003-08-04 08:43:34 +08:00
|
|
|
* and save a copy of the command status string to return as our
|
|
|
|
* result tuple
|
2003-06-25 09:10:15 +08:00
|
|
|
*/
|
2008-03-26 06:42:46 +08:00
|
|
|
sql_cmd_status = cstring_to_text(PQcmdStatus(res));
|
2004-09-28 08:49:04 +08:00
|
|
|
PQclear(res);
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
|
|
|
else
|
2004-09-28 08:49:04 +08:00
|
|
|
{
|
|
|
|
PQclear(res);
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
2005-10-15 10:49:52 +08:00
|
|
|
(errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
|
|
|
|
errmsg("statement returning results not allowed")));
|
2004-09-28 08:49:04 +08:00
|
|
|
}
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
/* if needed, close the connection to the database and cleanup */
|
2004-03-07 10:27:00 +08:00
|
|
|
if (freeconn)
|
2002-09-02 14:13:31 +08:00
|
|
|
PQfinish(conn);
|
|
|
|
|
2003-06-25 09:10:15 +08:00
|
|
|
PG_RETURN_TEXT_P(sql_cmd_status);
|
2002-09-02 14:13:31 +08:00
|
|
|
}
|
|
|
|
|
2001-06-19 03:09:50 +08:00
|
|
|
|
2002-04-24 10:28:28 +08:00
|
|
|
/*
|
|
|
|
* dblink_get_pkey
|
2002-09-05 04:31:48 +08:00
|
|
|
*
|
2002-09-02 14:13:31 +08:00
|
|
|
* Return list of primary key fields for the supplied relation,
|
2002-04-24 10:28:28 +08:00
|
|
|
* or NULL if none exists.
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_get_pkey);
|
|
|
|
Datum
|
|
|
|
dblink_get_pkey(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
int16 numatts;
|
|
|
|
Oid relid;
|
|
|
|
char **results;
|
|
|
|
FuncCallContext *funcctx;
|
|
|
|
int32 call_cntr;
|
|
|
|
int32 max_calls;
|
|
|
|
AttInMetadata *attinmeta;
|
|
|
|
MemoryContext oldcontext;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
/* stuff done only on the first call of the function */
|
2002-09-05 04:31:48 +08:00
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
TupleDesc tupdesc = NULL;
|
2002-09-02 14:13:31 +08:00
|
|
|
|
|
|
|
/* create a function context for cross-call persistence */
|
2002-09-05 04:31:48 +08:00
|
|
|
funcctx = SRF_FIRSTCALL_INIT();
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* switch to memory context appropriate for multiple function calls
|
2002-09-05 04:31:48 +08:00
|
|
|
*/
|
2002-09-02 14:13:31 +08:00
|
|
|
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
|
|
|
|
|
|
|
/* convert relname to rel Oid */
|
|
|
|
relid = get_relid_from_relname(PG_GETARG_TEXT_P(0));
|
|
|
|
if (!OidIsValid(relid))
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_TABLE),
|
|
|
|
errmsg("relation \"%s\" does not exist",
|
2008-03-26 06:42:46 +08:00
|
|
|
text_to_cstring(PG_GETARG_TEXT_PP(0)))));
|
2003-08-04 08:43:34 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* need a tuple descriptor representing one INT and one TEXT column
|
2002-09-05 04:31:48 +08:00
|
|
|
*/
|
2002-09-03 12:00:37 +08:00
|
|
|
tupdesc = CreateTemplateTupleDesc(2, false);
|
2002-09-02 14:13:31 +08:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "position",
|
2004-04-02 05:28:47 +08:00
|
|
|
INT4OID, -1, 0);
|
2002-09-02 14:13:31 +08:00
|
|
|
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "colname",
|
2004-04-02 05:28:47 +08:00
|
|
|
TEXTOID, -1, 0);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* Generate attribute metadata needed later to produce tuples from raw
|
|
|
|
* C strings
|
2002-04-24 10:28:28 +08:00
|
|
|
*/
|
2002-09-02 14:13:31 +08:00
|
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc);
|
|
|
|
funcctx->attinmeta = attinmeta;
|
|
|
|
|
|
|
|
/* get an array of attnums */
|
|
|
|
results = get_pkey_attnames(relid, &numatts);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
if ((results != NULL) && (numatts > 0))
|
2002-04-24 10:28:28 +08:00
|
|
|
{
|
2002-09-02 14:13:31 +08:00
|
|
|
funcctx->max_calls = numatts;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
/* got results, keep track of them */
|
|
|
|
funcctx->user_fctx = results;
|
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
else
|
2008-12-01 07:23:52 +08:00
|
|
|
{
|
2003-06-25 09:10:15 +08:00
|
|
|
/* fast track when no results */
|
2008-12-01 07:23:52 +08:00
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2002-09-05 04:31:48 +08:00
|
|
|
SRF_RETURN_DONE(funcctx);
|
2008-12-01 07:23:52 +08:00
|
|
|
}
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
MemoryContextSwitchTo(oldcontext);
|
2002-09-05 04:31:48 +08:00
|
|
|
}
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
/* stuff done on every call of the function */
|
2002-09-05 04:31:48 +08:00
|
|
|
funcctx = SRF_PERCALL_SETUP();
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
/*
|
|
|
|
* initialize per-call variables
|
|
|
|
*/
|
|
|
|
call_cntr = funcctx->call_cntr;
|
|
|
|
max_calls = funcctx->max_calls;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
results = (char **) funcctx->user_fctx;
|
|
|
|
attinmeta = funcctx->attinmeta;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
if (call_cntr < max_calls) /* do when there is more left to send */
|
2002-09-05 04:31:48 +08:00
|
|
|
{
|
2002-09-02 14:13:31 +08:00
|
|
|
char **values;
|
|
|
|
HeapTuple tuple;
|
|
|
|
Datum result;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
values = (char **) palloc(2 * sizeof(char *));
|
2002-09-05 04:31:48 +08:00
|
|
|
values[0] = (char *) palloc(12); /* sign, 10 digits, '\0' */
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
sprintf(values[0], "%d", call_cntr + 1);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
values[1] = results[call_cntr];
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
/* build the tuple */
|
|
|
|
tuple = BuildTupleFromCStrings(attinmeta, values);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
/* make the tuple into a datum */
|
2004-04-02 05:28:47 +08:00
|
|
|
result = HeapTupleGetDatum(tuple);
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2002-09-05 04:31:48 +08:00
|
|
|
SRF_RETURN_NEXT(funcctx, result);
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
2002-09-05 04:31:48 +08:00
|
|
|
else
|
2002-04-24 10:28:28 +08:00
|
|
|
{
|
2003-06-25 09:10:15 +08:00
|
|
|
/* do when there is no more left */
|
|
|
|
SRF_RETURN_DONE(funcctx);
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dblink_build_sql_insert
|
2002-09-05 04:31:48 +08:00
|
|
|
*
|
2002-04-24 10:28:28 +08:00
|
|
|
* Used to generate an SQL insert statement
|
|
|
|
* based on an existing tuple in a local relation.
|
|
|
|
* This is useful for selectively replicating data
|
|
|
|
* to another server via dblink.
|
|
|
|
*
|
|
|
|
* API:
|
|
|
|
* <relname> - name of local table of interest
|
|
|
|
* <pkattnums> - an int2vector of attnums which will be used
|
|
|
|
* to identify the local tuple of interest
|
|
|
|
* <pknumatts> - number of attnums in pkattnums
|
|
|
|
* <src_pkattvals_arry> - text array of key values which will be used
|
|
|
|
* to identify the local tuple of interest
|
|
|
|
* <tgt_pkattvals_arry> - text array of key values which will be used
|
|
|
|
* to build the string for execution remotely. These are substituted
|
|
|
|
* for their counterparts in src_pkattvals_arry
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_build_sql_insert);
|
|
|
|
Datum
|
|
|
|
dblink_build_sql_insert(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-11-18 10:38:24 +08:00
|
|
|
text *relname_text = PG_GETARG_TEXT_P(0);
|
|
|
|
int2vector *pkattnums = (int2vector *) PG_GETARG_POINTER(1);
|
|
|
|
int32 pknumatts_tmp = PG_GETARG_INT32(2);
|
|
|
|
ArrayType *src_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
|
|
|
|
ArrayType *tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(4);
|
2002-04-24 10:28:28 +08:00
|
|
|
Oid relid;
|
2002-09-24 02:03:32 +08:00
|
|
|
int16 pknumatts = 0;
|
2002-09-02 14:13:31 +08:00
|
|
|
char **src_pkattvals;
|
|
|
|
char **tgt_pkattvals;
|
2002-04-24 10:28:28 +08:00
|
|
|
int src_nitems;
|
|
|
|
int tgt_nitems;
|
2002-09-02 14:13:31 +08:00
|
|
|
char *sql;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert relname to rel OID.
|
|
|
|
*/
|
2002-05-28 05:59:12 +08:00
|
|
|
relid = get_relid_from_relname(relname_text);
|
2002-04-24 10:28:28 +08:00
|
|
|
if (!OidIsValid(relid))
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_TABLE),
|
|
|
|
errmsg("relation \"%s\" does not exist",
|
2008-03-26 06:42:46 +08:00
|
|
|
text_to_cstring(relname_text))));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be at least one key attribute
|
|
|
|
*/
|
2005-11-18 10:38:24 +08:00
|
|
|
if (pknumatts_tmp <= 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("number of key attributes must be > 0")));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2005-11-18 10:38:24 +08:00
|
|
|
if (pknumatts_tmp <= SHRT_MAX)
|
|
|
|
pknumatts = pknumatts_tmp;
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("input for number of primary key " \
|
|
|
|
"attributes too large")));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* Source array is made up of key values that will be used to locate the
|
|
|
|
* tuple of interest from the local system.
|
2002-04-24 10:28:28 +08:00
|
|
|
*/
|
2005-11-18 10:38:24 +08:00
|
|
|
src_pkattvals = get_text_array_contents(src_pkattvals_arry, &src_nitems);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be one source array key value for each key attnum
|
|
|
|
*/
|
|
|
|
if (src_nitems != pknumatts)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
2005-10-15 10:49:52 +08:00
|
|
|
errmsg("source key array length must match number of key " \
|
|
|
|
"attributes")));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* Target array is made up of key values that will be used to build the
|
|
|
|
* SQL string for use on the remote system.
|
2002-04-24 10:28:28 +08:00
|
|
|
*/
|
2005-11-18 10:38:24 +08:00
|
|
|
tgt_pkattvals = get_text_array_contents(tgt_pkattvals_arry, &tgt_nitems);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be one target array key value for each key attnum
|
|
|
|
*/
|
|
|
|
if (tgt_nitems != pknumatts)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
2005-10-15 10:49:52 +08:00
|
|
|
errmsg("target key array length must match number of key " \
|
|
|
|
"attributes")));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Prep work is finally done. Go get the SQL string.
|
|
|
|
*/
|
|
|
|
sql = get_sql_insert(relid, pkattnums, pknumatts, src_pkattvals, tgt_pkattvals);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And send it
|
|
|
|
*/
|
2008-03-26 06:42:46 +08:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(sql));
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dblink_build_sql_delete
|
2002-09-05 04:31:48 +08:00
|
|
|
*
|
2002-04-24 10:28:28 +08:00
|
|
|
* Used to generate an SQL delete statement.
|
|
|
|
* This is useful for selectively replicating a
|
|
|
|
* delete to another server via dblink.
|
|
|
|
*
|
|
|
|
* API:
|
|
|
|
* <relname> - name of remote table of interest
|
|
|
|
* <pkattnums> - an int2vector of attnums which will be used
|
|
|
|
* to identify the remote tuple of interest
|
|
|
|
* <pknumatts> - number of attnums in pkattnums
|
|
|
|
* <tgt_pkattvals_arry> - text array of key values which will be used
|
|
|
|
* to build the string for execution remotely.
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_build_sql_delete);
|
|
|
|
Datum
|
|
|
|
dblink_build_sql_delete(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-11-18 10:38:24 +08:00
|
|
|
text *relname_text = PG_GETARG_TEXT_P(0);
|
|
|
|
int2vector *pkattnums = (int2vector *) PG_GETARG_POINTER(1);
|
|
|
|
int32 pknumatts_tmp = PG_GETARG_INT32(2);
|
|
|
|
ArrayType *tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
|
2002-04-24 10:28:28 +08:00
|
|
|
Oid relid;
|
2002-09-24 02:03:32 +08:00
|
|
|
int16 pknumatts = 0;
|
2002-09-05 04:31:48 +08:00
|
|
|
char **tgt_pkattvals;
|
2002-04-24 10:28:28 +08:00
|
|
|
int tgt_nitems;
|
2002-09-05 04:31:48 +08:00
|
|
|
char *sql;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert relname to rel OID.
|
|
|
|
*/
|
2002-05-28 05:59:12 +08:00
|
|
|
relid = get_relid_from_relname(relname_text);
|
2002-04-24 10:28:28 +08:00
|
|
|
if (!OidIsValid(relid))
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_TABLE),
|
|
|
|
errmsg("relation \"%s\" does not exist",
|
2008-03-26 06:42:46 +08:00
|
|
|
text_to_cstring(relname_text))));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be at least one key attribute
|
|
|
|
*/
|
2005-11-18 10:38:24 +08:00
|
|
|
if (pknumatts_tmp <= 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("number of key attributes must be > 0")));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2005-11-18 10:38:24 +08:00
|
|
|
if (pknumatts_tmp <= SHRT_MAX)
|
|
|
|
pknumatts = pknumatts_tmp;
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("input for number of primary key " \
|
|
|
|
"attributes too large")));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* Target array is made up of key values that will be used to build the
|
|
|
|
* SQL string for use on the remote system.
|
2002-04-24 10:28:28 +08:00
|
|
|
*/
|
2005-11-18 10:38:24 +08:00
|
|
|
tgt_pkattvals = get_text_array_contents(tgt_pkattvals_arry, &tgt_nitems);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be one target array key value for each key attnum
|
|
|
|
*/
|
|
|
|
if (tgt_nitems != pknumatts)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
2005-10-15 10:49:52 +08:00
|
|
|
errmsg("target key array length must match number of key " \
|
|
|
|
"attributes")));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Prep work is finally done. Go get the SQL string.
|
|
|
|
*/
|
|
|
|
sql = get_sql_delete(relid, pkattnums, pknumatts, tgt_pkattvals);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And send it
|
|
|
|
*/
|
2008-03-26 06:42:46 +08:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(sql));
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dblink_build_sql_update
|
2002-09-05 04:31:48 +08:00
|
|
|
*
|
2002-04-24 10:28:28 +08:00
|
|
|
* Used to generate an SQL update statement
|
|
|
|
* based on an existing tuple in a local relation.
|
|
|
|
* This is useful for selectively replicating data
|
|
|
|
* to another server via dblink.
|
|
|
|
*
|
|
|
|
* API:
|
|
|
|
* <relname> - name of local table of interest
|
|
|
|
* <pkattnums> - an int2vector of attnums which will be used
|
|
|
|
* to identify the local tuple of interest
|
|
|
|
* <pknumatts> - number of attnums in pkattnums
|
|
|
|
* <src_pkattvals_arry> - text array of key values which will be used
|
|
|
|
* to identify the local tuple of interest
|
|
|
|
* <tgt_pkattvals_arry> - text array of key values which will be used
|
|
|
|
* to build the string for execution remotely. These are substituted
|
|
|
|
* for their counterparts in src_pkattvals_arry
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(dblink_build_sql_update);
|
|
|
|
Datum
|
|
|
|
dblink_build_sql_update(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2005-11-18 10:38:24 +08:00
|
|
|
text *relname_text = PG_GETARG_TEXT_P(0);
|
|
|
|
int2vector *pkattnums = (int2vector *) PG_GETARG_POINTER(1);
|
|
|
|
int32 pknumatts_tmp = PG_GETARG_INT32(2);
|
|
|
|
ArrayType *src_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(3);
|
|
|
|
ArrayType *tgt_pkattvals_arry = PG_GETARG_ARRAYTYPE_P(4);
|
2002-04-24 10:28:28 +08:00
|
|
|
Oid relid;
|
2002-09-24 02:03:32 +08:00
|
|
|
int16 pknumatts = 0;
|
2002-09-05 04:31:48 +08:00
|
|
|
char **src_pkattvals;
|
|
|
|
char **tgt_pkattvals;
|
2002-04-24 10:28:28 +08:00
|
|
|
int src_nitems;
|
|
|
|
int tgt_nitems;
|
2002-09-05 04:31:48 +08:00
|
|
|
char *sql;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert relname to rel OID.
|
|
|
|
*/
|
2002-05-28 05:59:12 +08:00
|
|
|
relid = get_relid_from_relname(relname_text);
|
2002-04-24 10:28:28 +08:00
|
|
|
if (!OidIsValid(relid))
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_TABLE),
|
|
|
|
errmsg("relation \"%s\" does not exist",
|
2008-03-26 06:42:46 +08:00
|
|
|
text_to_cstring(relname_text))));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be one source array key values for each key attnum
|
|
|
|
*/
|
2005-11-18 10:38:24 +08:00
|
|
|
if (pknumatts_tmp <= 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("number of key attributes must be > 0")));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2005-11-18 10:38:24 +08:00
|
|
|
if (pknumatts_tmp <= SHRT_MAX)
|
|
|
|
pknumatts = pknumatts_tmp;
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("input for number of primary key " \
|
|
|
|
"attributes too large")));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* Source array is made up of key values that will be used to locate the
|
|
|
|
* tuple of interest from the local system.
|
2002-04-24 10:28:28 +08:00
|
|
|
*/
|
2005-11-18 10:38:24 +08:00
|
|
|
src_pkattvals = get_text_array_contents(src_pkattvals_arry, &src_nitems);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be one source array key value for each key attnum
|
|
|
|
*/
|
|
|
|
if (src_nitems != pknumatts)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
2005-10-15 10:49:52 +08:00
|
|
|
errmsg("source key array length must match number of key " \
|
|
|
|
"attributes")));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* Target array is made up of key values that will be used to build the
|
|
|
|
* SQL string for use on the remote system.
|
2002-04-24 10:28:28 +08:00
|
|
|
*/
|
2005-11-18 10:38:24 +08:00
|
|
|
tgt_pkattvals = get_text_array_contents(tgt_pkattvals_arry, &tgt_nitems);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* There should be one target array key value for each key attnum
|
|
|
|
*/
|
|
|
|
if (tgt_nitems != pknumatts)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_ARRAY_SUBSCRIPT_ERROR),
|
2005-10-15 10:49:52 +08:00
|
|
|
errmsg("target key array length must match number of key " \
|
|
|
|
"attributes")));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Prep work is finally done. Go get the SQL string.
|
|
|
|
*/
|
|
|
|
sql = get_sql_update(relid, pkattnums, pknumatts, src_pkattvals, tgt_pkattvals);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* And send it
|
|
|
|
*/
|
2008-03-26 06:42:46 +08:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(sql));
|
2001-06-15 00:49:03 +08:00
|
|
|
}
|
|
|
|
|
2002-04-24 10:28:28 +08:00
|
|
|
/*************************************************************
|
2001-06-15 00:49:03 +08:00
|
|
|
* internal functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2002-04-24 10:28:28 +08:00
|
|
|
/*
|
|
|
|
* get_pkey_attnames
|
2002-09-05 04:31:48 +08:00
|
|
|
*
|
2002-04-24 10:28:28 +08:00
|
|
|
* Get the primary key attnames for the given relation.
|
|
|
|
* Return NULL, and set numatts = 0, if no primary key exists.
|
|
|
|
*/
|
2002-05-28 05:59:12 +08:00
|
|
|
static char **
|
2002-04-24 10:28:28 +08:00
|
|
|
get_pkey_attnames(Oid relid, int16 *numatts)
|
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
Relation indexRelation;
|
2008-01-14 10:49:47 +08:00
|
|
|
ScanKeyData skey;
|
|
|
|
SysScanDesc scan;
|
2002-09-05 04:31:48 +08:00
|
|
|
HeapTuple indexTuple;
|
|
|
|
int i;
|
|
|
|
char **result = NULL;
|
|
|
|
Relation rel;
|
|
|
|
TupleDesc tupdesc;
|
2007-08-27 09:24:50 +08:00
|
|
|
AclResult aclresult;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2008-01-14 10:49:47 +08:00
|
|
|
/* initialize numatts to 0 in case no primary key exists */
|
|
|
|
*numatts = 0;
|
|
|
|
|
2007-08-27 09:24:50 +08:00
|
|
|
/* open relation using relid, check permissions, get tupdesc */
|
2002-04-24 10:28:28 +08:00
|
|
|
rel = relation_open(relid, AccessShareLock);
|
2007-08-27 09:24:50 +08:00
|
|
|
|
|
|
|
aclresult = pg_class_aclcheck(RelationGetRelid(rel), GetUserId(),
|
|
|
|
ACL_SELECT);
|
|
|
|
if (aclresult != ACLCHECK_OK)
|
|
|
|
aclcheck_error(aclresult, ACL_KIND_CLASS,
|
|
|
|
RelationGetRelationName(rel));
|
|
|
|
|
2002-04-24 10:28:28 +08:00
|
|
|
tupdesc = rel->rd_att;
|
|
|
|
|
2008-01-14 10:49:47 +08:00
|
|
|
/* Prepare to scan pg_index for entries having indrelid = this rel. */
|
2005-04-15 04:03:27 +08:00
|
|
|
indexRelation = heap_open(IndexRelationId, AccessShareLock);
|
2008-01-14 10:49:47 +08:00
|
|
|
ScanKeyInit(&skey,
|
2003-11-13 05:15:59 +08:00
|
|
|
Anum_pg_index_indrelid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
|
|
|
ObjectIdGetDatum(relid));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2008-01-14 10:49:47 +08:00
|
|
|
scan = systable_beginscan(indexRelation, IndexIndrelidIndexId, true,
|
|
|
|
SnapshotNow, 1, &skey);
|
|
|
|
|
|
|
|
while (HeapTupleIsValid(indexTuple = systable_getnext(scan)))
|
2002-04-24 10:28:28 +08:00
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
Form_pg_index index = (Form_pg_index) GETSTRUCT(indexTuple);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-09-02 14:13:31 +08:00
|
|
|
/* we're only interested if it is the primary key */
|
2008-01-14 10:49:47 +08:00
|
|
|
if (index->indisprimary)
|
2002-04-24 10:28:28 +08:00
|
|
|
{
|
2005-03-29 08:17:27 +08:00
|
|
|
*numatts = index->indnatts;
|
2002-04-24 10:28:28 +08:00
|
|
|
if (*numatts > 0)
|
|
|
|
{
|
|
|
|
result = (char **) palloc(*numatts * sizeof(char *));
|
2002-09-02 14:13:31 +08:00
|
|
|
|
2002-04-24 10:28:28 +08:00
|
|
|
for (i = 0; i < *numatts; i++)
|
2005-03-29 08:17:27 +08:00
|
|
|
result[i] = SPI_fname(tupdesc, index->indkey.values[i]);
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-01-14 10:49:47 +08:00
|
|
|
|
|
|
|
systable_endscan(scan);
|
2002-04-24 10:28:28 +08:00
|
|
|
heap_close(indexRelation, AccessShareLock);
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-11-18 10:38:24 +08:00
|
|
|
/*
|
|
|
|
* Deconstruct a text[] into C-strings (note any NULL elements will be
|
|
|
|
* returned as NULL pointers)
|
|
|
|
*/
|
|
|
|
static char **
|
|
|
|
get_text_array_contents(ArrayType *array, int *numitems)
|
|
|
|
{
|
|
|
|
int ndim = ARR_NDIM(array);
|
|
|
|
int *dims = ARR_DIMS(array);
|
|
|
|
int nitems;
|
|
|
|
int16 typlen;
|
|
|
|
bool typbyval;
|
|
|
|
char typalign;
|
|
|
|
char **values;
|
|
|
|
char *ptr;
|
|
|
|
bits8 *bitmap;
|
|
|
|
int bitmask;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
Assert(ARR_ELEMTYPE(array) == TEXTOID);
|
|
|
|
|
|
|
|
*numitems = nitems = ArrayGetNItems(ndim, dims);
|
|
|
|
|
|
|
|
get_typlenbyvalalign(ARR_ELEMTYPE(array),
|
|
|
|
&typlen, &typbyval, &typalign);
|
|
|
|
|
|
|
|
values = (char **) palloc(nitems * sizeof(char *));
|
|
|
|
|
|
|
|
ptr = ARR_DATA_PTR(array);
|
|
|
|
bitmap = ARR_NULLBITMAP(array);
|
|
|
|
bitmask = 1;
|
|
|
|
|
|
|
|
for (i = 0; i < nitems; i++)
|
|
|
|
{
|
|
|
|
if (bitmap && (*bitmap & bitmask) == 0)
|
|
|
|
{
|
|
|
|
values[i] = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-26 06:42:46 +08:00
|
|
|
values[i] = TextDatumGetCString(PointerGetDatum(ptr));
|
2007-04-06 12:21:44 +08:00
|
|
|
ptr = att_addlength_pointer(ptr, typlen, ptr);
|
|
|
|
ptr = (char *) att_align_nominal(ptr, typalign);
|
2005-11-18 10:38:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* advance bitmap pointer if any */
|
|
|
|
if (bitmap)
|
|
|
|
{
|
|
|
|
bitmask <<= 1;
|
|
|
|
if (bitmask == 0x100)
|
|
|
|
{
|
|
|
|
bitmap++;
|
|
|
|
bitmask = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
2002-05-28 05:59:12 +08:00
|
|
|
static char *
|
2005-03-29 08:17:27 +08:00
|
|
|
get_sql_insert(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pkattvals, char **tgt_pkattvals)
|
2002-04-24 10:28:28 +08:00
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
Relation rel;
|
|
|
|
char *relname;
|
|
|
|
HeapTuple tuple;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
int natts;
|
2006-03-01 14:51:01 +08:00
|
|
|
StringInfoData buf;
|
2002-09-05 04:31:48 +08:00
|
|
|
char *val;
|
|
|
|
int16 key;
|
|
|
|
int i;
|
|
|
|
bool needComma;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
initStringInfo(&buf);
|
|
|
|
|
2002-11-24 02:59:25 +08:00
|
|
|
/* get relation name including any needed schema prefix and quoting */
|
|
|
|
relname = generate_relation_name(relid);
|
|
|
|
|
2002-04-24 10:28:28 +08:00
|
|
|
/*
|
|
|
|
* Open relation using relid
|
|
|
|
*/
|
|
|
|
rel = relation_open(relid, AccessShareLock);
|
|
|
|
tupdesc = rel->rd_att;
|
|
|
|
natts = tupdesc->natts;
|
|
|
|
|
|
|
|
tuple = get_tuple_of_interest(relid, pkattnums, pknumatts, src_pkattvals);
|
2002-09-02 14:13:31 +08:00
|
|
|
if (!tuple)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_CARDINALITY_VIOLATION),
|
|
|
|
errmsg("source row not found")));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, "INSERT INTO %s(", relname);
|
2002-08-03 02:15:10 +08:00
|
|
|
|
|
|
|
needComma = false;
|
2002-04-24 10:28:28 +08:00
|
|
|
for (i = 0; i < natts; i++)
|
|
|
|
{
|
2002-08-03 02:15:10 +08:00
|
|
|
if (tupdesc->attrs[i]->attisdropped)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (needComma)
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, ",");
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfoString(&buf,
|
2005-10-15 10:49:52 +08:00
|
|
|
quote_ident_cstr(NameStr(tupdesc->attrs[i]->attname)));
|
2002-08-03 02:15:10 +08:00
|
|
|
needComma = true;
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, ") VALUES(");
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* remember attvals are 1 based
|
|
|
|
*/
|
2002-08-03 02:15:10 +08:00
|
|
|
needComma = false;
|
2002-04-24 10:28:28 +08:00
|
|
|
for (i = 0; i < natts; i++)
|
|
|
|
{
|
2002-08-03 02:15:10 +08:00
|
|
|
if (tupdesc->attrs[i]->attisdropped)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (needComma)
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, ",");
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
if (tgt_pkattvals != NULL)
|
|
|
|
key = get_attnum_pk_pos(pkattnums, pknumatts, i + 1);
|
|
|
|
else
|
|
|
|
key = -1;
|
|
|
|
|
|
|
|
if (key > -1)
|
2005-11-18 10:38:24 +08:00
|
|
|
val = tgt_pkattvals[key] ? pstrdup(tgt_pkattvals[key]) : NULL;
|
2002-04-24 10:28:28 +08:00
|
|
|
else
|
|
|
|
val = SPI_getvalue(tuple, tupdesc, i + 1);
|
|
|
|
|
|
|
|
if (val != NULL)
|
|
|
|
{
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfoString(&buf, quote_literal_cstr(val));
|
2002-04-24 10:28:28 +08:00
|
|
|
pfree(val);
|
|
|
|
}
|
|
|
|
else
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, "NULL");
|
2002-08-03 02:15:10 +08:00
|
|
|
needComma = true;
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, ")");
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
relation_close(rel, AccessShareLock);
|
2006-03-01 14:51:01 +08:00
|
|
|
return (buf.data);
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
|
|
|
|
2002-05-28 05:59:12 +08:00
|
|
|
static char *
|
2005-03-29 08:17:27 +08:00
|
|
|
get_sql_delete(Oid relid, int2vector *pkattnums, int16 pknumatts, char **tgt_pkattvals)
|
2002-04-24 10:28:28 +08:00
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
Relation rel;
|
|
|
|
char *relname;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
int natts;
|
2006-10-04 08:30:14 +08:00
|
|
|
StringInfoData buf;
|
2002-09-05 04:31:48 +08:00
|
|
|
int i;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
initStringInfo(&buf);
|
|
|
|
|
2002-11-24 02:59:25 +08:00
|
|
|
/* get relation name including any needed schema prefix and quoting */
|
|
|
|
relname = generate_relation_name(relid);
|
|
|
|
|
2002-04-24 10:28:28 +08:00
|
|
|
/*
|
|
|
|
* Open relation using relid
|
|
|
|
*/
|
|
|
|
rel = relation_open(relid, AccessShareLock);
|
|
|
|
tupdesc = rel->rd_att;
|
|
|
|
natts = tupdesc->natts;
|
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, "DELETE FROM %s WHERE ", relname);
|
2002-04-24 10:28:28 +08:00
|
|
|
for (i = 0; i < pknumatts; i++)
|
|
|
|
{
|
2005-03-29 08:17:27 +08:00
|
|
|
int16 pkattnum = pkattnums->values[i];
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
if (i > 0)
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, " AND ");
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfoString(&buf,
|
2005-10-15 10:49:52 +08:00
|
|
|
quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum - 1]->attname)));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2005-11-18 10:38:24 +08:00
|
|
|
if (tgt_pkattvals == NULL)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "target key array must not be NULL");
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2005-11-18 10:38:24 +08:00
|
|
|
if (tgt_pkattvals[i] != NULL)
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, " = %s",
|
2005-11-18 10:38:24 +08:00
|
|
|
quote_literal_cstr(tgt_pkattvals[i]));
|
2002-04-24 10:28:28 +08:00
|
|
|
else
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, " IS NULL");
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
relation_close(rel, AccessShareLock);
|
2006-03-01 14:51:01 +08:00
|
|
|
return (buf.data);
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
|
|
|
|
2002-05-28 05:59:12 +08:00
|
|
|
static char *
|
2005-03-29 08:17:27 +08:00
|
|
|
get_sql_update(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pkattvals, char **tgt_pkattvals)
|
2002-04-24 10:28:28 +08:00
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
Relation rel;
|
|
|
|
char *relname;
|
|
|
|
HeapTuple tuple;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
int natts;
|
2006-03-01 14:51:01 +08:00
|
|
|
StringInfoData buf;
|
2002-09-05 04:31:48 +08:00
|
|
|
char *val;
|
|
|
|
int16 key;
|
|
|
|
int i;
|
|
|
|
bool needComma;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
initStringInfo(&buf);
|
|
|
|
|
2002-11-24 02:59:25 +08:00
|
|
|
/* get relation name including any needed schema prefix and quoting */
|
|
|
|
relname = generate_relation_name(relid);
|
|
|
|
|
2002-04-24 10:28:28 +08:00
|
|
|
/*
|
|
|
|
* Open relation using relid
|
|
|
|
*/
|
|
|
|
rel = relation_open(relid, AccessShareLock);
|
|
|
|
tupdesc = rel->rd_att;
|
|
|
|
natts = tupdesc->natts;
|
|
|
|
|
|
|
|
tuple = get_tuple_of_interest(relid, pkattnums, pknumatts, src_pkattvals);
|
2002-09-02 14:13:31 +08:00
|
|
|
if (!tuple)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_CARDINALITY_VIOLATION),
|
|
|
|
errmsg("source row not found")));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, "UPDATE %s SET ", relname);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2002-08-03 02:15:10 +08:00
|
|
|
needComma = false;
|
2002-04-24 10:28:28 +08:00
|
|
|
for (i = 0; i < natts; i++)
|
|
|
|
{
|
2002-08-03 02:15:10 +08:00
|
|
|
if (tupdesc->attrs[i]->attisdropped)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (needComma)
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, ", ");
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, "%s = ",
|
2005-10-15 10:49:52 +08:00
|
|
|
quote_ident_cstr(NameStr(tupdesc->attrs[i]->attname)));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
if (tgt_pkattvals != NULL)
|
|
|
|
key = get_attnum_pk_pos(pkattnums, pknumatts, i + 1);
|
|
|
|
else
|
|
|
|
key = -1;
|
|
|
|
|
|
|
|
if (key > -1)
|
2005-11-23 02:17:34 +08:00
|
|
|
val = tgt_pkattvals[key] ? pstrdup(tgt_pkattvals[key]) : NULL;
|
2002-04-24 10:28:28 +08:00
|
|
|
else
|
|
|
|
val = SPI_getvalue(tuple, tupdesc, i + 1);
|
|
|
|
|
|
|
|
if (val != NULL)
|
|
|
|
{
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfoString(&buf, quote_literal_cstr(val));
|
2002-04-24 10:28:28 +08:00
|
|
|
pfree(val);
|
|
|
|
}
|
|
|
|
else
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfoString(&buf, "NULL");
|
2002-08-03 02:15:10 +08:00
|
|
|
needComma = true;
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, " WHERE ");
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
for (i = 0; i < pknumatts; i++)
|
|
|
|
{
|
2005-03-29 08:17:27 +08:00
|
|
|
int16 pkattnum = pkattnums->values[i];
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
if (i > 0)
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, " AND ");
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, "%s",
|
2005-10-15 10:49:52 +08:00
|
|
|
quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum - 1]->attname)));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
if (tgt_pkattvals != NULL)
|
2005-11-23 02:17:34 +08:00
|
|
|
val = tgt_pkattvals[i] ? pstrdup(tgt_pkattvals[i]) : NULL;
|
2002-04-24 10:28:28 +08:00
|
|
|
else
|
|
|
|
val = SPI_getvalue(tuple, tupdesc, pkattnum);
|
|
|
|
|
|
|
|
if (val != NULL)
|
|
|
|
{
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, " = %s", quote_literal_cstr(val));
|
2002-04-24 10:28:28 +08:00
|
|
|
pfree(val);
|
|
|
|
}
|
|
|
|
else
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, " IS NULL");
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
relation_close(rel, AccessShareLock);
|
2006-03-01 14:51:01 +08:00
|
|
|
return (buf.data);
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a properly quoted literal value.
|
|
|
|
* Uses quote_literal in quote.c
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
quote_literal_cstr(char *rawstr)
|
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
text *rawstr_text;
|
|
|
|
text *result_text;
|
|
|
|
char *result;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
rawstr_text = cstring_to_text(rawstr);
|
|
|
|
result_text = DatumGetTextP(DirectFunctionCall1(quote_literal,
|
|
|
|
PointerGetDatum(rawstr_text)));
|
|
|
|
result = text_to_cstring(result_text);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a properly quoted identifier.
|
|
|
|
* Uses quote_ident in quote.c
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
quote_ident_cstr(char *rawstr)
|
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
text *rawstr_text;
|
|
|
|
text *result_text;
|
|
|
|
char *result;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
rawstr_text = cstring_to_text(rawstr);
|
|
|
|
result_text = DatumGetTextP(DirectFunctionCall1(quote_ident,
|
|
|
|
PointerGetDatum(rawstr_text)));
|
|
|
|
result = text_to_cstring(result_text);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-05-28 05:59:12 +08:00
|
|
|
static int16
|
2005-03-29 08:17:27 +08:00
|
|
|
get_attnum_pk_pos(int2vector *pkattnums, int16 pknumatts, int16 key)
|
2002-04-24 10:28:28 +08:00
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
int i;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
2002-09-05 04:31:48 +08:00
|
|
|
* Not likely a long list anyway, so just scan for the value
|
2002-04-24 10:28:28 +08:00
|
|
|
*/
|
|
|
|
for (i = 0; i < pknumatts; i++)
|
2005-03-29 08:17:27 +08:00
|
|
|
if (key == pkattnums->values[i])
|
2002-04-24 10:28:28 +08:00
|
|
|
return i;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-05-28 05:59:12 +08:00
|
|
|
static HeapTuple
|
2005-03-29 08:17:27 +08:00
|
|
|
get_tuple_of_interest(Oid relid, int2vector *pkattnums, int16 pknumatts, char **src_pkattvals)
|
2002-04-24 10:28:28 +08:00
|
|
|
{
|
2002-09-05 04:31:48 +08:00
|
|
|
Relation rel;
|
|
|
|
char *relname;
|
|
|
|
TupleDesc tupdesc;
|
2006-03-01 14:51:01 +08:00
|
|
|
StringInfoData buf;
|
2002-09-05 04:31:48 +08:00
|
|
|
int ret;
|
|
|
|
HeapTuple tuple;
|
|
|
|
int i;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
initStringInfo(&buf);
|
|
|
|
|
2002-11-24 02:59:25 +08:00
|
|
|
/* get relation name including any needed schema prefix and quoting */
|
|
|
|
relname = generate_relation_name(relid);
|
|
|
|
|
2002-04-24 10:28:28 +08:00
|
|
|
/*
|
|
|
|
* Open relation using relid
|
|
|
|
*/
|
|
|
|
rel = relation_open(relid, AccessShareLock);
|
2002-09-02 14:13:31 +08:00
|
|
|
tupdesc = CreateTupleDescCopy(rel->rd_att);
|
|
|
|
relation_close(rel, AccessShareLock);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Connect to SPI manager
|
|
|
|
*/
|
|
|
|
if ((ret = SPI_connect()) < 0)
|
2003-07-25 01:52:50 +08:00
|
|
|
/* internal error */
|
|
|
|
elog(ERROR, "SPI connect failure - returned %d", ret);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
2005-10-15 10:49:52 +08:00
|
|
|
* Build sql statement to look up tuple of interest Use src_pkattvals as
|
|
|
|
* the criteria.
|
2002-04-24 10:28:28 +08:00
|
|
|
*/
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, "SELECT * FROM %s WHERE ", relname);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
for (i = 0; i < pknumatts; i++)
|
|
|
|
{
|
2005-03-29 08:17:27 +08:00
|
|
|
int16 pkattnum = pkattnums->values[i];
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
if (i > 0)
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, " AND ");
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfoString(&buf,
|
2005-10-15 10:49:52 +08:00
|
|
|
quote_ident_cstr(NameStr(tupdesc->attrs[pkattnum - 1]->attname)));
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2005-11-18 10:38:24 +08:00
|
|
|
if (src_pkattvals[i] != NULL)
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, " = %s",
|
2005-11-18 10:38:24 +08:00
|
|
|
quote_literal_cstr(src_pkattvals[i]));
|
2002-04-24 10:28:28 +08:00
|
|
|
else
|
2006-03-01 14:51:01 +08:00
|
|
|
appendStringInfo(&buf, " IS NULL");
|
2002-04-24 10:28:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve the desired tuple
|
|
|
|
*/
|
2006-03-01 14:51:01 +08:00
|
|
|
ret = SPI_exec(buf.data, 0);
|
|
|
|
pfree(buf.data);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Only allow one qualifying tuple
|
|
|
|
*/
|
|
|
|
if ((ret == SPI_OK_SELECT) && (SPI_processed > 1))
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_CARDINALITY_VIOLATION),
|
|
|
|
errmsg("source criteria matched more than one record")));
|
|
|
|
|
2002-04-24 10:28:28 +08:00
|
|
|
else if (ret == SPI_OK_SELECT && SPI_processed == 1)
|
|
|
|
{
|
|
|
|
SPITupleTable *tuptable = SPI_tuptable;
|
2002-09-05 04:31:48 +08:00
|
|
|
|
2002-04-24 10:28:28 +08:00
|
|
|
tuple = SPI_copytuple(tuptable->vals[0]);
|
2003-11-27 04:43:25 +08:00
|
|
|
SPI_finish();
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
return tuple;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* no qualifying tuples
|
|
|
|
*/
|
2003-11-27 04:43:25 +08:00
|
|
|
SPI_finish();
|
|
|
|
|
2002-04-24 10:28:28 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* never reached, but keep compiler quiet
|
|
|
|
*/
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-05-28 05:59:12 +08:00
|
|
|
static Oid
|
|
|
|
get_relid_from_relname(text *relname_text)
|
2002-04-24 10:28:28 +08:00
|
|
|
{
|
2002-05-28 05:59:12 +08:00
|
|
|
RangeVar *relvar;
|
|
|
|
Relation rel;
|
|
|
|
Oid relid;
|
2002-04-24 10:28:28 +08:00
|
|
|
|
2005-05-27 08:57:49 +08:00
|
|
|
relvar = makeRangeVarFromNameList(textToQualifiedNameList(relname_text));
|
2002-05-28 05:59:12 +08:00
|
|
|
rel = heap_openrv(relvar, AccessShareLock);
|
|
|
|
relid = RelationGetRelid(rel);
|
|
|
|
relation_close(rel, AccessShareLock);
|
2002-04-24 10:28:28 +08:00
|
|
|
|
|
|
|
return relid;
|
|
|
|
}
|
|
|
|
|
2002-11-24 02:59:25 +08:00
|
|
|
/*
|
|
|
|
* generate_relation_name - copied from ruleutils.c
|
|
|
|
* Compute the name to display for a relation specified by OID
|
|
|
|
*
|
|
|
|
* The result includes all necessary quoting and schema-prefixing.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
generate_relation_name(Oid relid)
|
|
|
|
{
|
|
|
|
HeapTuple tp;
|
|
|
|
Form_pg_class reltup;
|
|
|
|
char *nspname;
|
|
|
|
char *result;
|
|
|
|
|
|
|
|
tp = SearchSysCache(RELOID,
|
|
|
|
ObjectIdGetDatum(relid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(tp))
|
2003-07-25 01:52:50 +08:00
|
|
|
elog(ERROR, "cache lookup failed for relation %u", relid);
|
|
|
|
|
2002-11-24 02:59:25 +08:00
|
|
|
reltup = (Form_pg_class) GETSTRUCT(tp);
|
|
|
|
|
|
|
|
/* Qualify the name if not visible in search path */
|
|
|
|
if (RelationIsVisible(relid))
|
|
|
|
nspname = NULL;
|
|
|
|
else
|
|
|
|
nspname = get_namespace_name(reltup->relnamespace);
|
|
|
|
|
|
|
|
result = quote_qualified_identifier(nspname, NameStr(reltup->relname));
|
|
|
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2003-06-25 09:10:15 +08:00
|
|
|
|
|
|
|
|
|
|
|
static remoteConn *
|
|
|
|
getConnectionByName(const char *name)
|
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
remoteConnHashEnt *hentry;
|
|
|
|
char key[NAMEDATALEN];
|
2003-06-25 09:10:15 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!remoteConnHash)
|
|
|
|
remoteConnHash = createConnHash();
|
2003-06-25 09:10:15 +08:00
|
|
|
|
|
|
|
MemSet(key, 0, NAMEDATALEN);
|
|
|
|
snprintf(key, NAMEDATALEN - 1, "%s", name);
|
2003-08-04 08:43:34 +08:00
|
|
|
hentry = (remoteConnHashEnt *) hash_search(remoteConnHash,
|
|
|
|
key, HASH_FIND, NULL);
|
2003-06-25 09:10:15 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (hentry)
|
2005-10-08 20:12:29 +08:00
|
|
|
return (hentry->rconn);
|
2003-06-25 09:10:15 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
return (NULL);
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static HTAB *
|
|
|
|
createConnHash(void)
|
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
HASHCTL ctl;
|
2003-06-25 09:10:15 +08:00
|
|
|
|
|
|
|
ctl.keysize = NAMEDATALEN;
|
|
|
|
ctl.entrysize = sizeof(remoteConnHashEnt);
|
|
|
|
|
2004-10-25 08:46:43 +08:00
|
|
|
return hash_create("Remote Con hash", NUMCONN, &ctl, HASH_ELEM);
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
|
|
|
|
2003-07-25 01:52:50 +08:00
|
|
|
static void
|
2005-10-15 10:49:52 +08:00
|
|
|
createNewConnection(const char *name, remoteConn * rconn)
|
2003-06-25 09:10:15 +08:00
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
remoteConnHashEnt *hentry;
|
|
|
|
bool found;
|
|
|
|
char key[NAMEDATALEN];
|
2003-06-25 09:10:15 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!remoteConnHash)
|
2003-07-25 01:52:50 +08:00
|
|
|
remoteConnHash = createConnHash();
|
2003-06-25 09:10:15 +08:00
|
|
|
|
|
|
|
MemSet(key, 0, NAMEDATALEN);
|
|
|
|
snprintf(key, NAMEDATALEN - 1, "%s", name);
|
|
|
|
hentry = (remoteConnHashEnt *) hash_search(remoteConnHash, key,
|
|
|
|
HASH_ENTER, &found);
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (found)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_DUPLICATE_OBJECT),
|
|
|
|
errmsg("duplicate connection name")));
|
2003-06-25 09:10:15 +08:00
|
|
|
|
2005-10-08 20:12:29 +08:00
|
|
|
hentry->rconn = rconn;
|
2007-02-07 08:52:35 +08:00
|
|
|
strlcpy(hentry->name, name, sizeof(hentry->name));
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
deleteConnection(const char *name)
|
|
|
|
{
|
2003-08-04 08:43:34 +08:00
|
|
|
remoteConnHashEnt *hentry;
|
|
|
|
bool found;
|
|
|
|
char key[NAMEDATALEN];
|
2003-06-25 09:10:15 +08:00
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!remoteConnHash)
|
|
|
|
remoteConnHash = createConnHash();
|
2003-06-25 09:10:15 +08:00
|
|
|
|
|
|
|
MemSet(key, 0, NAMEDATALEN);
|
|
|
|
snprintf(key, NAMEDATALEN - 1, "%s", name);
|
|
|
|
|
|
|
|
hentry = (remoteConnHashEnt *) hash_search(remoteConnHash,
|
|
|
|
key, HASH_REMOVE, &found);
|
|
|
|
|
2003-08-04 08:43:34 +08:00
|
|
|
if (!hentry)
|
2003-07-25 01:52:50 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("undefined connection name")));
|
|
|
|
|
2003-06-25 09:10:15 +08:00
|
|
|
}
|
2008-01-04 05:27:59 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
dblink_security_check(PGconn *conn, remoteConn *rconn)
|
|
|
|
{
|
|
|
|
if (!superuser())
|
|
|
|
{
|
|
|
|
if (!PQconnectionUsedPassword(conn))
|
|
|
|
{
|
|
|
|
PQfinish(conn);
|
|
|
|
if (rconn)
|
|
|
|
pfree(rconn);
|
|
|
|
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
|
|
|
|
errmsg("password is required"),
|
|
|
|
errdetail("Non-superuser cannot connect if the server does not request a password."),
|
|
|
|
errhint("Target server's authentication method must be changed.")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-03 11:56:57 +08:00
|
|
|
|
2008-09-22 21:55:14 +08:00
|
|
|
/*
|
|
|
|
* For non-superusers, insist that the connstr specify a password. This
|
|
|
|
* prevents a password from being picked up from .pgpass, a service file,
|
|
|
|
* the environment, etc. We don't want the postgres user's passwords
|
|
|
|
* to be accessible to non-superusers.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
dblink_connstr_check(const char *connstr)
|
|
|
|
{
|
|
|
|
if (!superuser())
|
|
|
|
{
|
|
|
|
PQconninfoOption *options;
|
|
|
|
PQconninfoOption *option;
|
|
|
|
bool connstr_gives_password = false;
|
|
|
|
|
|
|
|
options = PQconninfoParse(connstr, NULL);
|
|
|
|
if (options)
|
|
|
|
{
|
|
|
|
for (option = options; option->keyword != NULL; option++)
|
|
|
|
{
|
|
|
|
if (strcmp(option->keyword, "password") == 0)
|
|
|
|
{
|
|
|
|
if (option->val != NULL && option->val[0] != '\0')
|
|
|
|
{
|
|
|
|
connstr_gives_password = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PQconninfoFree(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!connstr_gives_password)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
|
|
|
|
errmsg("password is required"),
|
|
|
|
errdetail("Non-superusers must provide a password in the connection string.")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-03 11:56:57 +08:00
|
|
|
static void
|
|
|
|
dblink_res_error(const char *conname, PGresult *res, const char *dblink_context_msg, bool fail)
|
|
|
|
{
|
|
|
|
int level;
|
|
|
|
char *pg_diag_sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
|
|
|
|
char *pg_diag_message_primary = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
|
|
|
|
char *pg_diag_message_detail = PQresultErrorField(res, PG_DIAG_MESSAGE_DETAIL);
|
|
|
|
char *pg_diag_message_hint = PQresultErrorField(res, PG_DIAG_MESSAGE_HINT);
|
|
|
|
char *pg_diag_context = PQresultErrorField(res, PG_DIAG_CONTEXT);
|
|
|
|
int sqlstate;
|
|
|
|
char *message_primary;
|
|
|
|
char *message_detail;
|
|
|
|
char *message_hint;
|
|
|
|
char *message_context;
|
|
|
|
const char *dblink_context_conname = "unnamed";
|
|
|
|
|
|
|
|
if (fail)
|
|
|
|
level = ERROR;
|
|
|
|
else
|
|
|
|
level = NOTICE;
|
|
|
|
|
|
|
|
if (pg_diag_sqlstate)
|
|
|
|
sqlstate = MAKE_SQLSTATE(pg_diag_sqlstate[0],
|
|
|
|
pg_diag_sqlstate[1],
|
|
|
|
pg_diag_sqlstate[2],
|
|
|
|
pg_diag_sqlstate[3],
|
|
|
|
pg_diag_sqlstate[4]);
|
|
|
|
else
|
|
|
|
sqlstate = ERRCODE_CONNECTION_FAILURE;
|
|
|
|
|
|
|
|
xpstrdup(message_primary, pg_diag_message_primary);
|
|
|
|
xpstrdup(message_detail, pg_diag_message_detail);
|
|
|
|
xpstrdup(message_hint, pg_diag_message_hint);
|
|
|
|
xpstrdup(message_context, pg_diag_context);
|
|
|
|
|
|
|
|
if (res)
|
|
|
|
PQclear(res);
|
|
|
|
|
|
|
|
if (conname)
|
|
|
|
dblink_context_conname = conname;
|
|
|
|
|
|
|
|
ereport(level,
|
|
|
|
(errcode(sqlstate),
|
|
|
|
message_primary ? errmsg("%s", message_primary) : errmsg("unknown error"),
|
|
|
|
message_detail ? errdetail("%s", message_detail) : 0,
|
|
|
|
message_hint ? errhint("%s", message_hint) : 0,
|
|
|
|
message_context ? errcontext("%s", message_context) : 0,
|
|
|
|
errcontext("Error occurred on dblink connection named \"%s\": %s.",
|
|
|
|
dblink_context_conname, dblink_context_msg)));
|
|
|
|
}
|