1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
/*-------------------------------------------------------------------------
*
* pglogical_rpc.c
* Remote calls
*
* Copyright (c) 2015, PostgreSQL Global Development Group
*
* IDENTIFICATION
* pglogical_rpc.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "lib/stringinfo.h"
#include "nodes/makefuncs.h"
#include "catalog/pg_type.h"
#include "storage/lock.h"
#include "utils/rel.h"
#include "pglogical_relcache.h"
#include "pglogical_repset.h"
#include "pglogical_rpc.h"
#include "pglogical.h"
#define atooid(x) ((Oid) strtoul((x), NULL, 10))
/*
* Fetch list of tables that are grouped in specified replication sets.
*/
List *
pg_logical_get_remote_repset_tables(PGconn *conn, List *replication_sets)
{
PGresult *res;
int i;
List *tables = NIL;
ListCell *lc;
bool first = true;
StringInfoData query;
StringInfoData repsetarr;
initStringInfo(&repsetarr);
foreach (lc, replication_sets)
{
char *repset_name = lfirst(lc);
if (first)
first = false;
else
appendStringInfoChar(&repsetarr, ',');
appendStringInfo(&repsetarr, "%s",
PQescapeLiteral(conn, repset_name, strlen(repset_name)));
}
initStringInfo(&query);
if (pglogical_remote_function_exists(conn, "pglogical", "show_repset_table_info", 2, NULL))
{
/* PGLogical 2.0+ */
appendStringInfo(&query,
"SELECT i.relid, i.nspname, i.relname, i.att_list,"
" i.has_row_filter"
" FROM (SELECT DISTINCT relid FROM pglogical.tables WHERE set_name = ANY(ARRAY[%s])) t,"
" LATERAL pglogical.show_repset_table_info(t.relid, ARRAY[%s]) i",
repsetarr.data, repsetarr.data);
}
else
{
/* PGLogical 1.x */
appendStringInfo(&query,
"SELECT r.oid AS relid, t.nspname, t.relname, ARRAY(SELECT attname FROM pg_attribute WHERE attrelid = r.oid AND NOT attisdropped AND attnum > 0) AS att_list,"
" false AS has_row_filter"
" FROM pglogical.tables t, pg_catalog.pg_class r, pg_catalog.pg_namespace n"
" WHERE t.set_name = ANY(ARRAY[%s]) AND r.relname = t.relname AND n.oid = r.relnamespace AND n.nspname = t.nspname",
repsetarr.data);
}
res = PQexec(conn, query.data);
/* TODO: better error message? */
if (PQresultStatus(res) != PGRES_TUPLES_OK)
elog(ERROR, "could not get table list: %s", PQresultErrorMessage(res));
for (i = 0; i < PQntuples(res); i++)
{
PGLogicalRemoteRel *remoterel = palloc0(sizeof(PGLogicalRemoteRel));
remoterel->relid = atooid(PQgetvalue(res, i, 0));
remoterel->nspname = pstrdup(PQgetvalue(res, i, 1));
remoterel->relname = pstrdup(PQgetvalue(res, i, 2));
if (!parsePGArray(PQgetvalue(res, i, 3), &remoterel->attnames,
&remoterel->natts))
elog(ERROR, "could not parse column list for table");
remoterel->hasRowFilter = (strcmp(PQgetvalue(res, i, 4), "t") == 0);
tables = lappend(tables, remoterel);
}
PQclear(res);
return tables;
}
/*
* Like above but for one table.
*/
PGLogicalRemoteRel *
pg_logical_get_remote_repset_table(PGconn *conn, RangeVar *rv,
List *replication_sets)
{
PGLogicalRemoteRel *remoterel = palloc0(sizeof(PGLogicalRemoteRel));
PGresult *res;
ListCell *lc;
bool first = true;
StringInfoData query;
StringInfoData repsetarr;
StringInfoData relname;
initStringInfo(&relname);
appendStringInfo(&relname, "%s.%s",
PQescapeIdentifier(conn, rv->schemaname, strlen(rv->schemaname)),
PQescapeIdentifier(conn, rv->relname, strlen(rv->relname)));
initStringInfo(&repsetarr);
foreach (lc, replication_sets)
{
char *repset_name = lfirst(lc);
if (first)
first = false;
else
appendStringInfoChar(&repsetarr, ',');
appendStringInfo(&repsetarr, "%s",
PQescapeLiteral(conn, repset_name, strlen(repset_name)));
}
initStringInfo(&query);
if (pglogical_remote_function_exists(conn, "pglogical", "show_repset_table_info", 2, NULL))
{
/* PGLogical 2.0+ */
appendStringInfo(&query,
"SELECT i.relid, i.nspname, i.relname, i.att_list,"
" i.has_row_filter"
" FROM pglogical.show_repset_table_info(%s::regclass, ARRAY[%s]) i",
PQescapeLiteral(conn, relname.data, relname.len),
repsetarr.data);
}
else
{
/* PGLogical 1.x */
appendStringInfo(&query,
"SELECT r.oid AS relid, t.nspname, t.relname, ARRAY(SELECT attname FROM pg_attribute WHERE attrelid = r.oid AND NOT attisdropped AND attnum > 0) AS att_list,"
" false AS has_row_filter"
" FROM pglogical.tables t, pg_catalog.pg_class r, pg_catalog.pg_namespace n"
" WHERE r.oid = %s::regclass AND t.set_name = ANY(ARRAY[%s]) AND r.relname = t.relname AND n.oid = r.relnamespace AND n.nspname = t.nspname",
PQescapeLiteral(conn, relname.data, relname.len),
repsetarr.data);
}
res = PQexec(conn, query.data);
/* TODO: better error message? */
if (PQresultStatus(res) != PGRES_TUPLES_OK || PQntuples(res) != 1)
elog(ERROR, "could not get table list: %s", PQresultErrorMessage(res));
remoterel->relid = atooid(PQgetvalue(res, 0, 0));
remoterel->nspname = pstrdup(PQgetvalue(res, 0, 1));
remoterel->relname = pstrdup(PQgetvalue(res, 0, 2));
if (!parsePGArray(PQgetvalue(res, 0, 3), &remoterel->attnames,
&remoterel->natts))
elog(ERROR, "could not parse column list for table");
remoterel->hasRowFilter = (strcmp(PQgetvalue(res, 0, 4), "t") == 0);
PQclear(res);
return remoterel;
}
/*
* Is the remote slot active?.
*/
bool
pglogical_remote_slot_active(PGconn *conn, const char *slot_name)
{
PGresult *res;
const char *values[1];
Oid types[1] = { TEXTOID };
bool ret;
values[0] = slot_name;
res = PQexecParams(conn,
"SELECT plugin, active "
"FROM pg_catalog.pg_replication_slots "
"WHERE slot_name = $1",
1, types, values, NULL, NULL, 0);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
ereport(ERROR,
(errmsg("getting remote slot info failed"),
errdetail("SELECT FROM pg_catalog.pg_replication_slots failed with: %s",
PQerrorMessage(conn))));
}
/* Slot not found return false */
if (PQntuples(res) == 0)
{
PQclear(res);
return false;
}
/* Slot found, validate that it's pglogical slot */
if (PQgetisnull(res, 0, 0))
elog(ERROR, "Unexpectedly null field %s", PQfname(res, 0));
if (strcmp("pglogical_output", PQgetvalue(res, 0, 0)) != 0 &&
strcmp("pglogical", PQgetvalue(res, 0, 0)) != 0)
ereport(ERROR,
(errmsg("slot %s is not pglogical slot", slot_name)));
ret = (strcmp(PQgetvalue(res, 0, 1), "t") == 0);
PQclear(res);
return ret;
}
/*
* Drops replication slot on remote node that has been used by the local node.
*/
void
pglogical_drop_remote_slot(PGconn *conn, const char *slot_name)
{
PGresult *res;
const char *values[1];
Oid types[1] = { TEXTOID };
values[0] = slot_name;
/* Check if the slot exists */
res = PQexecParams(conn,
"SELECT plugin "
"FROM pg_catalog.pg_replication_slots "
"WHERE slot_name = $1",
1, types, values, NULL, NULL, 0);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
ereport(ERROR,
(errmsg("getting remote slot info failed"),
errdetail("SELECT FROM pg_catalog.pg_replication_slots failed with: %s",
PQerrorMessage(conn))));
}
/* Slot not found return false */
if (PQntuples(res) == 0)
{
PQclear(res);
return;
}
/* Slot found, validate that it's BDR slot */
if (PQgetisnull(res, 0, 0))
elog(ERROR, "Unexpectedly null field %s", PQfname(res, 0));
if (strcmp("pglogical_output", PQgetvalue(res, 0, 0)) != 0 &&
strcmp("pglogical", PQgetvalue(res, 0, 0)) != 0)
ereport(ERROR,
(errmsg("slot %s is not pglogical slot", slot_name)));
PQclear(res);
res = PQexecParams(conn, "SELECT pg_drop_replication_slot($1)",
1, types, values, NULL, NULL, 0);
/* And finally, drop the slot. */
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
ereport(ERROR,
(errmsg("remote slot drop failed"),
errdetail("SELECT pg_drop_replication_slot() failed with: %s",
PQerrorMessage(conn))));
}
PQclear(res);
}
void
pglogical_remote_node_info(PGconn *conn, Oid *nodeid, char **node_name, char **sysid, char **dbname, char **replication_sets)
{
PGresult *res;
res = PQexec(conn, "SELECT node_id, node_name, sysid, dbname, replication_sets FROM pglogical.pglogical_node_info()");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
elog(ERROR, "could not fetch remote node info: %s\n", PQerrorMessage(conn));
/* No nodes found? */
if (PQntuples(res) == 0)
elog(ERROR, "the remote database is not configured as a pglogical node.\n");
if (PQntuples(res) > 1)
elog(ERROR, "the remote database has multiple nodes configured. That is not supported with current version of pglogical.\n");
*nodeid = atooid(PQgetvalue(res, 0, 0));
*node_name = pstrdup(PQgetvalue(res, 0, 1));
if (sysid)
*sysid = pstrdup(PQgetvalue(res, 0, 2));
if (dbname)
*dbname = pstrdup(PQgetvalue(res, 0, 3));
if (replication_sets)
*replication_sets = pstrdup(PQgetvalue(res, 0, 4));
PQclear(res);
}
bool
pglogical_remote_function_exists(PGconn *conn, const char *nspname,
const char *proname, int nargs, char *argname)
{
PGresult *res;
const char *values[2];
Oid types[2] = { TEXTOID, TEXTOID };
bool ret;
StringInfoData query;
values[0] = proname;
values[1] = nspname;
initStringInfo(&query);
appendStringInfo(&query,
"SELECT oid "
" FROM pg_catalog.pg_proc "
" WHERE proname = $1 "
" AND pronamespace = "
" (SELECT oid "
" FROM pg_catalog.pg_namespace "
" WHERE nspname = $2)");
if (nargs >= 0)
appendStringInfo(&query,
" AND pronargs = '%d'", nargs);
if (argname != NULL)
appendStringInfo(&query,
" AND %s = ANY (proargnames)",
PQescapeLiteral(conn, argname, strlen(argname)));
res = PQexecParams(conn, query.data, 2, types, values, NULL, NULL, 0);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
elog(ERROR, "could not fetch remote function info: %s\n",
PQerrorMessage(conn));
ret = PQntuples(res) > 0;
PQclear(res);
return ret;
}
|