13 #include "catalog/pg_class_d.h"
45 const char *old_pgdata,
const char *new_pgdata)
51 bool all_matched =
true;
62 old_relnum = new_relnum = 0;
63 while (old_relnum < old_db->rel_arr.nrels ||
64 new_relnum < new_db->rel_arr.nrels)
68 RelInfo *new_rel = (new_relnum < new_db->rel_arr.nrels) ?
91 if (strcmp(new_rel->nspname,
"pg_toast") != 0)
101 if (old_rel->
reloid < new_rel->reloid)
109 else if (old_rel->
reloid > new_rel->reloid)
112 if (strcmp(new_rel->nspname,
"pg_toast") != 0)
126 if (strcmp(old_rel->
nspname, new_rel->nspname) != 0 ||
127 strcmp(old_rel->
relname, new_rel->relname) != 0)
129 pg_log(
PG_WARNING,
"Relation names for OID %u in database \"%s\" do not match: "
130 "old name \"%s.%s\", new name \"%s.%s\"",
133 new_rel->nspname, new_rel->relname);
142 old_rel, new_rel,
maps + num_maps);
149 pg_fatal(
"Failed to match up old and new tables in database \"%s\"",
218 snprintf(reldesc,
sizeof(reldesc),
"\"%s.%s\"",
229 sizeof(reldesc) - strlen(reldesc),
230 _(
" which is an index on \"%s.%s\""),
239 sizeof(reldesc) - strlen(reldesc),
240 _(
" which is an index on OID %u"), rel->
indtable);
251 sizeof(reldesc) - strlen(reldesc),
252 _(
" which is the TOAST table for \"%s.%s\""),
259 sizeof(reldesc) - strlen(reldesc),
260 _(
" which is the TOAST table for OID %u"), rel->
toastheap);
264 pg_log(
PG_WARNING,
"No match found in old cluster for new relation with OID %u in database \"%s\": %s",
267 pg_log(
PG_WARNING,
"No match found in new cluster for old relation with OID %u in database \"%s\": %s",
284 if (
cluster->dbarr.dbs != NULL)
290 for (dbnum = 0; dbnum <
cluster->dbarr.ndbs; dbnum++)
328 int i_datlocprovider;
335 "SELECT encoding, datlocprovider, "
336 " datcollate, datctype, datlocale "
337 "FROM pg_catalog.pg_database "
338 "WHERE datname='template0'");
341 "SELECT encoding, datlocprovider, "
342 " datcollate, datctype, daticulocale AS datlocale "
343 "FROM pg_catalog.pg_database "
344 "WHERE datname='template0'");
347 "SELECT encoding, 'c' AS datlocprovider, "
348 " datcollate, datctype, NULL AS datlocale "
349 "FROM pg_catalog.pg_database "
350 "WHERE datname='template0'");
358 i_datencoding =
PQfnumber(dbres,
"encoding");
359 i_datlocprovider =
PQfnumber(dbres,
"datlocprovider");
360 i_datcollate =
PQfnumber(dbres,
"datcollate");
361 i_datctype =
PQfnumber(dbres,
"datctype");
362 i_datlocale =
PQfnumber(dbres,
"datlocale");
400 "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, ");
402 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
403 "datlocprovider, datlocale, ");
405 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
406 "datlocprovider, daticulocale AS datlocale, ");
408 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
409 "'c' AS datlocprovider, NULL AS datlocale, ");
410 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
411 "pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
412 "FROM pg_catalog.pg_database d "
413 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
414 " ON d.dattablespace = t.oid "
415 "WHERE d.datallowconn = true "
427 for (tupnum = 0; tupnum < ntups; tupnum++)
431 snprintf(dbinfos[tupnum].db_tablespace,
sizeof(dbinfos[tupnum].db_tablespace),
"%s",
462 char *nspname = NULL;
474 char *last_namespace = NULL,
475 *last_tablespace = NULL;
489 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
490 "WITH regular_heap (reloid, indtable, toastheap) AS ( "
491 " SELECT c.oid, 0::oid, 0::oid "
492 " FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
493 " ON c.relnamespace = n.oid "
494 " WHERE relkind IN (" CppAsString2(RELKIND_RELATION)
", "
497 " ((n.nspname !~ '^pg_temp_' AND "
498 " n.nspname !~ '^pg_toast_temp_' AND "
499 " n.nspname NOT IN ('pg_catalog', 'information_schema', "
500 " 'binary_upgrade', 'pg_toast') AND "
501 " c.oid >= %u::pg_catalog.oid) OR "
502 " (n.nspname = 'pg_catalog' AND "
503 " relname IN ('pg_largeobject') ))), ",
511 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
512 " toast_heap (reloid, indtable, toastheap) AS ( "
513 " SELECT c.reltoastrelid, 0::oid, c.oid "
514 " FROM regular_heap JOIN pg_catalog.pg_class c "
515 " ON regular_heap.reloid = c.oid "
516 " WHERE c.reltoastrelid != 0), ");
524 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
525 " all_index (reloid, indtable, toastheap) AS ( "
526 " SELECT indexrelid, indrelid, 0::oid "
527 " FROM pg_catalog.pg_index "
528 " WHERE indisvalid AND indisready "
530 " (SELECT reloid FROM regular_heap "
532 " SELECT reloid FROM toast_heap)) ");
538 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
539 "SELECT all_rels.*, n.nspname, c.relname, "
540 " c.relfilenode, c.reltablespace, "
541 " pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
542 "FROM (SELECT * FROM regular_heap "
544 " SELECT * FROM toast_heap "
546 " SELECT * FROM all_index) all_rels "
547 " JOIN pg_catalog.pg_class c "
548 " ON all_rels.reloid = c.oid "
549 " JOIN pg_catalog.pg_namespace n "
550 " ON c.relnamespace = n.oid "
551 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
552 " ON c.reltablespace = t.oid "
570 for (relnum = 0; relnum < ntups; relnum++)
572 RelInfo *curr = &relinfos[num_rels++];
587 if (last_namespace && strcmp(nspname, last_namespace) == 0)
588 curr->
nspname = last_namespace;
611 if (last_tablespace && strcmp(
tablespace, last_tablespace) == 0)
679 "%s as caught_up, conflict_reason IS NOT NULL as invalid "
680 "FROM pg_catalog.pg_replication_slots "
681 "WHERE slot_type = 'logical' AND "
682 "database = current_database() AND "
683 "temporary IS FALSE;",
684 live_check ?
"FALSE" :
685 "(CASE WHEN conflict_reason IS NOT NULL THEN FALSE "
686 "ELSE (SELECT pg_catalog.binary_upgrade_logical_slot_has_caught_up(slot_name)) "
709 for (
int slotnum = 0; slotnum < num_slots; slotnum++)
771 "FROM pg_catalog.pg_subscription WHERE subdbid = %u",
804 for (dbnum = 0; dbnum < db_arr->
ndbs; dbnum++)
820 for (relnum = 0; relnum < rel_arr->
nrels; relnum++)
838 for (dbnum = 0; dbnum < db_arr->
ndbs; dbnum++)
854 for (relnum = 0; relnum < rel_arr->
nrels; relnum++)
866 if (slot_arr->
nslots == 0)
871 for (
int slotnum = 0; slotnum < slot_arr->
nslots; slotnum++)
878 slot_info->
two_phase ?
"true" :
"false");
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)
void * pg_malloc0(size_t size)
char * pg_strdup(const char *in)
void * pg_malloc(size_t size)
static void print_slot_infos(LogicalSlotInfoArr *slot_arr)
static void get_template0_info(ClusterInfo *cluster)
int count_old_cluster_subscriptions(void)
static void get_old_cluster_logical_slot_infos(DbInfo *dbinfo, bool live_check)
static void get_db_subscription_count(DbInfo *dbinfo)
void get_db_rel_and_slot_infos(ClusterInfo *cluster, bool live_check)
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)
int count_old_cluster_logical_slots(void)
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
LogicalSlotInfoArr slot_arr
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[]