13 #include "catalog/pg_class_d.h"
44 const char *old_pgdata,
const char *new_pgdata)
50 bool all_matched =
true;
61 old_relnum = new_relnum = 0;
62 while (old_relnum < old_db->rel_arr.nrels ||
63 new_relnum < new_db->rel_arr.nrels)
67 RelInfo *new_rel = (new_relnum < new_db->rel_arr.nrels) ?
90 if (strcmp(new_rel->nspname,
"pg_toast") != 0)
100 if (old_rel->
reloid < new_rel->reloid)
108 else if (old_rel->
reloid > new_rel->reloid)
111 if (strcmp(new_rel->nspname,
"pg_toast") != 0)
125 if (strcmp(old_rel->
nspname, new_rel->nspname) != 0 ||
126 strcmp(old_rel->
relname, new_rel->relname) != 0)
128 pg_log(
PG_WARNING,
"Relation names for OID %u in database \"%s\" do not match: "
129 "old name \"%s.%s\", new name \"%s.%s\"",
132 new_rel->nspname, new_rel->relname);
141 old_rel, new_rel,
maps + num_maps);
148 pg_fatal(
"Failed to match up old and new tables in database \"%s\"",
217 snprintf(reldesc,
sizeof(reldesc),
"\"%s.%s\"",
228 sizeof(reldesc) - strlen(reldesc),
229 _(
" which is an index on \"%s.%s\""),
238 sizeof(reldesc) - strlen(reldesc),
239 _(
" which is an index on OID %u"), rel->
indtable);
250 sizeof(reldesc) - strlen(reldesc),
251 _(
" which is the TOAST table for \"%s.%s\""),
258 sizeof(reldesc) - strlen(reldesc),
259 _(
" which is the TOAST table for OID %u"), rel->
toastheap);
263 pg_log(
PG_WARNING,
"No match found in old cluster for new relation with OID %u in database \"%s\": %s",
266 pg_log(
PG_WARNING,
"No match found in new cluster for old relation with OID %u in database \"%s\": %s",
281 if (
cluster->dbarr.dbs != NULL)
287 for (dbnum = 0; dbnum <
cluster->dbarr.ndbs; dbnum++)
318 int i_datlocprovider;
325 "SELECT encoding, datlocprovider, "
326 " datcollate, datctype, datlocale "
327 "FROM pg_catalog.pg_database "
328 "WHERE datname='template0'");
331 "SELECT encoding, datlocprovider, "
332 " datcollate, datctype, daticulocale AS datlocale "
333 "FROM pg_catalog.pg_database "
334 "WHERE datname='template0'");
337 "SELECT encoding, 'c' AS datlocprovider, "
338 " datcollate, datctype, NULL AS datlocale "
339 "FROM pg_catalog.pg_database "
340 "WHERE datname='template0'");
348 i_datencoding =
PQfnumber(dbres,
"encoding");
349 i_datlocprovider =
PQfnumber(dbres,
"datlocprovider");
350 i_datcollate =
PQfnumber(dbres,
"datcollate");
351 i_datctype =
PQfnumber(dbres,
"datctype");
352 i_datlocale =
PQfnumber(dbres,
"datlocale");
390 "SELECT d.oid, d.datname, d.encoding, d.datcollate, d.datctype, ");
392 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
393 "datlocprovider, datlocale, ");
395 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
396 "datlocprovider, daticulocale AS datlocale, ");
398 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
399 "'c' AS datlocprovider, NULL AS datlocale, ");
400 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
401 "pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
402 "FROM pg_catalog.pg_database d "
403 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
404 " ON d.dattablespace = t.oid "
405 "WHERE d.datallowconn = true "
417 for (tupnum = 0; tupnum < ntups; tupnum++)
421 snprintf(dbinfos[tupnum].db_tablespace,
sizeof(dbinfos[tupnum].db_tablespace),
"%s",
452 char *nspname = NULL;
464 char *last_namespace = NULL,
465 *last_tablespace = NULL;
479 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
480 "WITH regular_heap (reloid, indtable, toastheap) AS ( "
481 " SELECT c.oid, 0::oid, 0::oid "
482 " FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n "
483 " ON c.relnamespace = n.oid "
484 " WHERE relkind IN (" CppAsString2(RELKIND_RELATION)
", "
487 " ((n.nspname !~ '^pg_temp_' AND "
488 " n.nspname !~ '^pg_toast_temp_' AND "
489 " n.nspname NOT IN ('pg_catalog', 'information_schema', "
490 " 'binary_upgrade', 'pg_toast') AND "
491 " c.oid >= %u::pg_catalog.oid) OR "
492 " (n.nspname = 'pg_catalog' AND "
493 " relname IN ('pg_largeobject') ))), ",
501 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
502 " toast_heap (reloid, indtable, toastheap) AS ( "
503 " SELECT c.reltoastrelid, 0::oid, c.oid "
504 " FROM regular_heap JOIN pg_catalog.pg_class c "
505 " ON regular_heap.reloid = c.oid "
506 " WHERE c.reltoastrelid != 0), ");
514 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
515 " all_index (reloid, indtable, toastheap) AS ( "
516 " SELECT indexrelid, indrelid, 0::oid "
517 " FROM pg_catalog.pg_index "
518 " WHERE indisvalid AND indisready "
520 " (SELECT reloid FROM regular_heap "
522 " SELECT reloid FROM toast_heap)) ");
528 snprintf(query + strlen(query),
sizeof(query) - strlen(query),
529 "SELECT all_rels.*, n.nspname, c.relname, "
530 " c.relfilenode, c.reltablespace, "
531 " pg_catalog.pg_tablespace_location(t.oid) AS spclocation "
532 "FROM (SELECT * FROM regular_heap "
534 " SELECT * FROM toast_heap "
536 " SELECT * FROM all_index) all_rels "
537 " JOIN pg_catalog.pg_class c "
538 " ON all_rels.reloid = c.oid "
539 " JOIN pg_catalog.pg_namespace n "
540 " ON c.relnamespace = n.oid "
541 " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
542 " ON c.reltablespace = t.oid "
560 for (relnum = 0; relnum < ntups; relnum++)
562 RelInfo *curr = &relinfos[num_rels++];
577 if (last_namespace && strcmp(nspname, last_namespace) == 0)
578 curr->
nspname = last_namespace;
601 if (last_tablespace && strcmp(
tablespace, last_tablespace) == 0)
617 dbinfo->rel_arr.rels = relinfos;
618 dbinfo->rel_arr.nrels = num_rels;
669 "%s as caught_up, invalidation_reason IS NOT NULL as invalid "
670 "FROM pg_catalog.pg_replication_slots "
671 "WHERE slot_type = 'logical' AND "
672 "database = current_database() AND "
673 "temporary IS FALSE;",
675 "(CASE WHEN invalidation_reason IS NOT NULL THEN FALSE "
676 "ELSE (SELECT pg_catalog.binary_upgrade_logical_slot_has_caught_up(slot_name)) "
699 for (
int slotnum = 0; slotnum < num_slots; slotnum++)
715 dbinfo->slot_arr.slots = slotinfos;
716 dbinfo->slot_arr.nslots = num_slots;
753 "FROM pg_catalog.pg_subscription");
765 for (dbnum = 0; dbnum < db_arr->
ndbs; dbnum++)
781 for (relnum = 0; relnum < rel_arr->
nrels; relnum++)
799 for (dbnum = 0; dbnum < db_arr->
ndbs; dbnum++)
815 for (relnum = 0; relnum < rel_arr->
nrels; relnum++)
827 if (slot_arr->
nslots == 0)
832 for (
int slotnum = 0; slotnum < slot_arr->
nslots; slotnum++)
839 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)
void get_subscription_count(ClusterInfo *cluster)
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 get_old_cluster_logical_slot_infos(DbInfo *dbinfo)
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 get_db_rel_and_slot_infos(ClusterInfo *cluster)
static struct LogicalRepInfo * 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
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[]