13 #include "catalog/pg_class_d.h"
42 const char *old_pgdata,
const char *new_pgdata)
48 bool all_matched =
true;
59 old_relnum = new_relnum = 0;
60 while (old_relnum < old_db->rel_arr.nrels ||
61 new_relnum < new_db->rel_arr.nrels)
65 RelInfo *new_rel = (new_relnum < new_db->rel_arr.nrels) ?
88 if (strcmp(new_rel->nspname,
"pg_toast") != 0)
98 if (old_rel->
reloid < new_rel->reloid)
106 else if (old_rel->
reloid > new_rel->reloid)
109 if (strcmp(new_rel->nspname,
"pg_toast") != 0)
123 if (strcmp(old_rel->
nspname, new_rel->nspname) != 0 ||
124 strcmp(old_rel->
relname, new_rel->relname) != 0)
126 pg_log(
PG_WARNING,
"Relation names for OID %u in database \"%s\" do not match: "
127 "old name \"%s.%s\", new name \"%s.%s\"",
130 new_rel->nspname, new_rel->relname);
139 old_rel, new_rel,
maps + num_maps);
146 pg_fatal(
"Failed to match up old and new tables in database \"%s\"",
215 snprintf(reldesc,
sizeof(reldesc),
"\"%s.%s\"",
226 sizeof(reldesc) - strlen(reldesc),
227 _(
" which is an index on \"%s.%s\""),
236 sizeof(reldesc) - strlen(reldesc),
237 _(
" which is an index on OID %u"), rel->
indtable);
248 sizeof(reldesc) - strlen(reldesc),
249 _(
" which is the TOAST table for \"%s.%s\""),
256 sizeof(reldesc) - strlen(reldesc),
257 _(
" which is the TOAST table for OID %u"), rel->
toastheap);
261 pg_log(
PG_WARNING,
"No match found in old cluster for new relation with OID %u in database \"%s\": %s",
264 pg_log(
PG_WARNING,
"No match found in new cluster for old relation with OID %u in database \"%s\": %s",
279 if (
cluster->dbarr.dbs != NULL)
285 for (dbnum = 0; dbnum <
cluster->dbarr.ndbs; dbnum++)
309 int i_datlocprovider;
316 "SELECT encoding, datlocprovider, "
317 " datcollate, datctype, daticulocale "
318 "FROM pg_catalog.pg_database "
319 "WHERE datname='template0'");
322 "SELECT encoding, 'c' AS datlocprovider, "
323 " datcollate, datctype, NULL AS daticulocale "
324 "FROM pg_catalog.pg_database "
325 "WHERE datname='template0'");
333 i_datencoding =
PQfnumber(dbres,
"encoding");
334 i_datlocprovider =
PQfnumber(dbres,
"datlocprovider");
335 i_datcollate =
PQfnumber(dbres,
"datcollate");
336 i_datctype =
PQfnumber(dbres,
"datctype");
337 i_daticulocale =
PQfnumber(dbres,
"daticulocale");
344 locale->db_iculocale = NULL;
375 "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, ");
377 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
378 "'c' AS datlocprovider, NULL AS daticulocale, ");
380 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
381 "datlocprovider, daticulocale, ");
382 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
383 "pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
384 "FROM pg_catalog.pg_database d "
385 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
386 " ON d.dattablespace = t.oid "
387 "WHERE d.datallowconn = true "
399 for (tupnum = 0; tupnum < ntups; tupnum++)
403 snprintf(dbinfos[tupnum].db_tablespace,
sizeof(dbinfos[tupnum].db_tablespace),
"%s",
434 char *nspname = NULL;
446 char *last_namespace = NULL,
447 *last_tablespace = NULL;
461 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
462 "WITH regular_heap (reloid, indtable, toastheap) AS ( "
463 " SELECT c.oid, 0::oid, 0::oid "
464 " FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
465 " ON c.relnamespace = n.oid "
466 " WHERE relkind IN (" CppAsString2(RELKIND_RELATION)
", "
469 " ((n.nspname !~ '^pg_temp_' AND "
470 " n.nspname !~ '^pg_toast_temp_' AND "
471 " n.nspname NOT IN ('pg_catalog', 'information_schema', "
472 " 'binary_upgrade', 'pg_toast') AND "
473 " c.oid >= %u::pg_catalog.oid) OR "
474 " (n.nspname = 'pg_catalog' AND "
475 " relname IN ('pg_largeobject') ))), ",
483 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
484 " toast_heap (reloid, indtable, toastheap) AS ( "
485 " SELECT c.reltoastrelid, 0::oid, c.oid "
486 " FROM regular_heap JOIN pg_catalog.pg_class c "
487 " ON regular_heap.reloid = c.oid "
488 " WHERE c.reltoastrelid != 0), ");
496 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
497 " all_index (reloid, indtable, toastheap) AS ( "
498 " SELECT indexrelid, indrelid, 0::oid "
499 " FROM pg_catalog.pg_index "
500 " WHERE indisvalid AND indisready "
502 " (SELECT reloid FROM regular_heap "
504 " SELECT reloid FROM toast_heap)) ");
510 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
511 "SELECT all_rels.*, n.nspname, c.relname, "
512 " c.relfilenode, c.reltablespace, "
513 " pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
514 "FROM (SELECT * FROM regular_heap "
516 " SELECT * FROM toast_heap "
518 " SELECT * FROM all_index) all_rels "
519 " JOIN pg_catalog.pg_class c "
520 " ON all_rels.reloid = c.oid "
521 " JOIN pg_catalog.pg_namespace n "
522 " ON c.relnamespace = n.oid "
523 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
524 " ON c.reltablespace = t.oid "
542 for (relnum = 0; relnum < ntups; relnum++)
544 RelInfo *curr = &relinfos[num_rels++];
559 if (last_namespace && strcmp(nspname, last_namespace) == 0)
560 curr->
nspname = last_namespace;
583 if (last_tablespace && strcmp(
tablespace, last_tablespace) == 0)
609 for (dbnum = 0; dbnum < db_arr->
ndbs; dbnum++)
625 for (relnum = 0; relnum < rel_arr->
nrels; relnum++)
643 for (dbnum = 0; dbnum < db_arr->
ndbs; dbnum++)
656 for (relnum = 0; relnum < rel_arr->
nrels; relnum++)
void cluster(ParseState *pstate, ClusterStmt *stmt, bool isTopLevel)
static void PGresult * res
void PQfinish(PGconn *conn)
int PQntuples(const PGresult *res)
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
int PQfnumber(const PGresult *res, const char *field_name)
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
char * pg_strdup(const char *in)
void * pg_malloc(size_t size)
static void get_template0_info(ClusterInfo *cluster)
static void report_unmatched_relation(const RelInfo *rel, const DbInfo *db, bool is_new_db)
static void get_db_infos(ClusterInfo *cluster)
static void free_rel_infos(RelInfoArr *rel_arr)
static void print_rel_infos(RelInfoArr *rel_arr)
static void create_rel_filename_map(const char *old_data, const char *new_data, const DbInfo *old_db, const DbInfo *new_db, const RelInfo *old_rel, const RelInfo *new_rel, FileNameMap *map)
FileNameMap * gen_db_file_maps(DbInfo *old_db, DbInfo *new_db, int *nmaps, const char *old_pgdata, const char *new_pgdata)
static void free_db_and_rel_infos(DbInfoArr *db_arr)
static void print_db_infos(DbInfoArr *db_arr)
void get_db_and_rel_infos(ClusterInfo *cluster)
static void get_rel_infos(ClusterInfo *cluster, DbInfo *dbinfo)
void void pg_log(eLogType type, const char *fmt,...) pg_attribute_printf(2
PGconn * connectToServer(ClusterInfo *cluster, const char *db_name)
#define GET_MAJOR_VERSION(v)
PGresult * executeQueryOrDie(PGconn *conn, const char *fmt,...) pg_attribute_printf(2
const char * tablespace_suffix
char db_tablespace[MAXPGPATH]
const char * new_tablespace
const char * old_tablespace_suffix
const char * old_tablespace
RelFileNumber relfilenumber
const char * new_tablespace_suffix
RelFileNumber relfilenumber
#define FirstNormalObjectId
static const pg_conv_map maps[]