15
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.539.2.4 2010/03/03 20:10:42 heikki Exp $
15
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.539.2.5 2010/07/14 21:21:23 tgl Exp $
17
17
*-------------------------------------------------------------------------
158
158
static void dumpTSConfig(Archive *fout, TSConfigInfo *cfginfo);
159
159
static void dumpForeignDataWrapper(Archive *fout, FdwInfo *fdwinfo);
160
160
static void dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo);
161
static void dumpUserMappings(Archive *fout, const char *target,
161
static void dumpUserMappings(Archive *fout,
162
162
const char *servername, const char *namespace,
163
163
const char *owner, CatalogId catalogId, DumpId dumpId);
5450
5451
/* Make sure we are in proper schema */
5451
5452
selectSourceSchema("pg_catalog");
5453
appendPQExpBuffer(query, "SELECT oid, fdwname, "
5454
appendPQExpBuffer(query, "SELECT tableoid, oid, fdwname, "
5454
5455
"(%s fdwowner) AS rolname, fdwvalidator::pg_catalog.regproc, fdwacl,"
5455
5456
"array_to_string(ARRAY("
5456
5457
" SELECT option_name || ' ' || quote_literal(option_value) "
5467
5468
fdwinfo = (FdwInfo *) malloc(ntups * sizeof(FdwInfo));
5470
i_tableoid = PQfnumber(res, "tableoid");
5469
5471
i_oid = PQfnumber(res, "oid");
5470
5472
i_fdwname = PQfnumber(res, "fdwname");
5471
5473
i_rolname = PQfnumber(res, "rolname");
5476
5478
for (i = 0; i < ntups; i++)
5478
5480
fdwinfo[i].dobj.objType = DO_FDW;
5481
fdwinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5479
5482
fdwinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5480
5483
AssignDumpId(&fdwinfo[i].dobj);
5481
5484
fdwinfo[i].dobj.name = strdup(PQgetvalue(res, i, i_fdwname));
5531
5535
/* Make sure we are in proper schema */
5532
5536
selectSourceSchema("pg_catalog");
5534
appendPQExpBuffer(query, "SELECT oid, srvname, "
5538
appendPQExpBuffer(query, "SELECT tableoid, oid, srvname, "
5535
5539
"(%s srvowner) AS rolname, "
5536
5540
"srvfdw, srvtype, srvversion, srvacl,"
5537
5541
"array_to_string(ARRAY("
5549
5553
srvinfo = (ForeignServerInfo *) malloc(ntups * sizeof(ForeignServerInfo));
5555
i_tableoid = PQfnumber(res, "tableoid");
5551
5556
i_oid = PQfnumber(res, "oid");
5552
5557
i_srvname = PQfnumber(res, "srvname");
5553
5558
i_rolname = PQfnumber(res, "rolname");
5560
5565
for (i = 0; i < ntups; i++)
5562
5567
srvinfo[i].dobj.objType = DO_FOREIGN_SERVER;
5568
srvinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
5563
5569
srvinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
5564
5570
AssignDumpId(&srvinfo[i].dobj);
5565
5571
srvinfo[i].dobj.name = strdup(PQgetvalue(res, i, i_srvname));
9441
9447
query = createPQExpBuffer();
9443
9449
/* look up the foreign-data wrapper */
9450
selectSourceSchema("pg_catalog");
9444
9451
appendPQExpBuffer(query, "SELECT fdwname "
9445
9452
"FROM pg_foreign_data_wrapper w "
9446
9453
"WHERE w.oid = '%u'",
9501
9508
free(namecopy);
9503
9510
/* Dump user mappings */
9504
resetPQExpBuffer(q);
9505
appendPQExpBuffer(q, "SERVER %s", fmtId(srvinfo->dobj.name));
9506
dumpUserMappings(fout, q->data,
9511
dumpUserMappings(fout,
9507
9512
srvinfo->dobj.name, NULL,
9508
9513
srvinfo->rolname,
9509
9514
srvinfo->dobj.catId, srvinfo->dobj.dumpId);
9520
9525
* for the server.
9523
dumpUserMappings(Archive *fout, const char *target,
9528
dumpUserMappings(Archive *fout,
9524
9529
const char *servername, const char *namespace,
9525
9530
const char *owner,
9526
9531
CatalogId catalogId, DumpId dumpId)
9540
9545
delq = createPQExpBuffer();
9541
9546
query = createPQExpBuffer();
9549
* We read from the publicly accessible view pg_user_mappings, so as not
9550
* to fail if run by a non-superuser. Note that the view will show
9551
* umoptions as null if the user hasn't got privileges for the associated
9552
* server; this means that pg_dump will dump such a mapping, but with no
9553
* OPTIONS clause. A possible alternative is to skip such mappings
9554
* altogether, but it's not clear that that's an improvement.
9556
selectSourceSchema("pg_catalog");
9543
9558
appendPQExpBuffer(query,
9544
"SELECT (%s umuser) AS umuser, "
9545
9560
"array_to_string(ARRAY(SELECT option_name || ' ' || quote_literal(option_value) FROM pg_options_to_table(umoptions)), ', ') AS umoptions\n"
9546
"FROM pg_user_mapping "
9547
"WHERE umserver=%u",
9561
"FROM pg_user_mappings "
9549
9563
catalogId.oid);
9551
9565
res = PQexec(g_conn, query->data);
9552
9566
check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
9554
9568
ntups = PQntuples(res);
9555
i_umuser = PQfnumber(res, "umuser");
9569
i_usename = PQfnumber(res, "usename");
9556
9570
i_umoptions = PQfnumber(res, "umoptions");
9558
9572
for (i = 0; i < ntups; i++)
9561
9575
char *umoptions;
9563
umuser = PQgetvalue(res, i, i_umuser);
9577
usename = PQgetvalue(res, i, i_usename);
9564
9578
umoptions = PQgetvalue(res, i, i_umoptions);
9566
9580
resetPQExpBuffer(q);
9567
appendPQExpBuffer(q, "CREATE USER MAPPING FOR %s", fmtId(umuser));
9581
appendPQExpBuffer(q, "CREATE USER MAPPING FOR %s", fmtId(usename));
9568
9582
appendPQExpBuffer(q, " SERVER %s", fmtId(servername));
9570
9584
if (umoptions && strlen(umoptions) > 0)
9573
9587
appendPQExpBuffer(q, ";\n");
9575
9589
resetPQExpBuffer(delq);
9576
appendPQExpBuffer(delq, "DROP USER MAPPING FOR %s SERVER %s;\n", fmtId(umuser), fmtId(servername));
9590
appendPQExpBuffer(delq, "DROP USER MAPPING FOR %s", fmtId(usename));
9591
appendPQExpBuffer(delq, " SERVER %s;\n", fmtId(servername));
9578
9593
resetPQExpBuffer(tag);
9579
appendPQExpBuffer(tag, "USER MAPPING %s %s", fmtId(umuser), target);
9594
appendPQExpBuffer(tag, "USER MAPPING %s SERVER %s",
9595
usename, servername);
9581
9597
ArchiveEntry(fout, nilCatalogId, createDumpId(),