@@ -6958,6 +6958,28 @@ getPackages(Archive *fout, int *numPkgs)
69586958 int i_rolname;
69596959 int i_pkgacl;
69606960 int i_acldefault;
6961+
6962+
6963+ /*
6964+ * Check if pg_package table exists. This is needed for pg_upgrade from
6965+ * PostgreSQL to IvorySQL, where the source database is PostgreSQL which
6966+ * doesn't have pg_package table.
6967+ */
6968+ res = ExecuteSqlQuery(fout,
6969+ "SELECT 1 FROM pg_catalog.pg_class c "
6970+ "JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid "
6971+ "WHERE n.nspname = 'pg_catalog' AND c.relname = 'pg_package'",
6972+ PGRES_TUPLES_OK);
6973+ if (PQntuples(res) == 0)
6974+ {
6975+ /* pg_package table doesn't exist, return empty result */
6976+ PQclear(res);
6977+ destroyPQExpBuffer(query);
6978+ *numPkgs = 0;
6979+ return (PkgInfo *) pg_malloc0(sizeof(PkgInfo));
6980+ }
6981+ PQclear(res);
6982+
69616983 appendPQExpBuffer(query,
69626984 "SELECT p.tableoid, p.pkgname, p.oid, p.pkgnamespace,"
69636985 "p.pkgowner,"
@@ -7195,12 +7217,31 @@ getTables(Archive *fout, int *numTables)
71957217 "d.refobjsubid AS owning_col, "
71967218 "tsp.spcname AS reltablespace, ");
71977219
7198- if (fout->remoteVersion < 180000)
7199- appendPQExpBufferStr(query,
7200- "false AS relhasrowid, ");
7201- else
7202- appendPQExpBufferStr(query,
7203- "c.relhasrowid, ");
7220+ /*
7221+ * Check if relhasrowid column exists. This column is IvorySQL-specific
7222+ * for Oracle ROWID compatibility, and doesn't exist in native PostgreSQL.
7223+ * We need to check at runtime rather than relying on remoteVersion because
7224+ * when upgrading from PostgreSQL to IvorySQL, the source database is
7225+ * PostgreSQL which may have the same version number but lacks this column.
7226+ */
7227+ {
7228+ PGresult *res_rowid;
7229+ bool relhasrowid_exists = false;
7230+
7231+ res_rowid = ExecuteSqlQuery(fout,
7232+ "SELECT 1 FROM pg_catalog.pg_attribute "
7233+ "WHERE attrelid = 'pg_class'::regclass "
7234+ "AND attname = 'relhasrowid' "
7235+ "AND attnum > 0 LIMIT 1",
7236+ PGRES_TUPLES_OK);
7237+ relhasrowid_exists = (PQntuples(res_rowid) > 0);
7238+ PQclear(res_rowid);
7239+
7240+ if (relhasrowid_exists)
7241+ appendPQExpBufferStr(query, "c.relhasrowid, ");
7242+ else
7243+ appendPQExpBufferStr(query, "false AS relhasrowid, ");
7244+ }
72047245
72057246 if (fout->remoteVersion >= 120000)
72067247 appendPQExpBufferStr(query,
@@ -9309,13 +9350,31 @@ getTableAttrs(Archive *fout, TableInfo *tblinfo, int numTables)
93099350 appendPQExpBufferStr(q,
93109351 "'' AS attcompression,\n");
93119352
9312- if (fout->remoteVersion >= 170000)
9313- appendPQExpBuffer(q,
9314- "a.attisinvisible,\n");
9315- else
9316- appendPQExpBuffer(q,
9317- "'f' AS attisinvisible,\n");
9353+ /*
9354+ * Check if attisinvisible column exists. This column is IvorySQL-specific
9355+ * for Oracle compatibility, and doesn't exist in native PostgreSQL.
9356+ * We need to check at runtime rather than relying on remoteVersion because
9357+ * when upgrading from PostgreSQL to IvorySQL, the source database is
9358+ * PostgreSQL which may have the same version number but lacks this column.
9359+ */
9360+ {
9361+ PGresult *res_invisible;
9362+ bool attisinvisible_exists = false;
93189363
9364+ res_invisible = ExecuteSqlQuery(fout,
9365+ "SELECT 1 FROM pg_catalog.pg_attribute "
9366+ "WHERE attrelid = 'pg_attribute'::regclass "
9367+ "AND attname = 'attisinvisible' "
9368+ "AND attnum > 0 LIMIT 1",
9369+ PGRES_TUPLES_OK);
9370+ attisinvisible_exists = (PQntuples(res_invisible) > 0);
9371+ PQclear(res_invisible);
9372+
9373+ if (attisinvisible_exists)
9374+ appendPQExpBufferStr(q, "a.attisinvisible,\n");
9375+ else
9376+ appendPQExpBufferStr(q, "'f' AS attisinvisible,\n");
9377+ }
93199378
93209379 if (fout->remoteVersion >= 100000)
93219380 appendPQExpBufferStr(q,
0 commit comments