* by PostgreSQL
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.457 2007/01/22 01:35:21 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.458 2007/01/23 17:54:50 tgl Exp $
*
*-------------------------------------------------------------------------
*/
static void dumpCast(Archive *fout, CastInfo *cast);
static void dumpOpr(Archive *fout, OprInfo *oprinfo);
static void dumpOpclass(Archive *fout, OpclassInfo *opcinfo);
+static void dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo);
static void dumpConversion(Archive *fout, ConvInfo *convinfo);
static void dumpRule(Archive *fout, RuleInfo *rinfo);
static void dumpAgg(Archive *fout, AggInfo *agginfo);
return opcinfo;
}
+/*
+ * getOpfamilies:
+ * read all opfamilies in the system catalogs and return them in the
+ * OpfamilyInfo* structure
+ *
+ * numOpfamilies is set to the number of opfamilies read in
+ */
+OpfamilyInfo *
+getOpfamilies(int *numOpfamilies)
+{
+ PGresult *res;
+ int ntups;
+ int i;
+ PQExpBuffer query;
+ OpfamilyInfo *opfinfo;
+ int i_tableoid;
+ int i_oid;
+ int i_opfname;
+ int i_opfnamespace;
+ int i_rolname;
+
+ /* Before 8.3, there is no separate concept of opfamilies */
+ if (g_fout->remoteVersion < 80300)
+ {
+ *numOpfamilies = 0;
+ return NULL;
+ }
+
+ query = createPQExpBuffer();
+
+ /*
+ * find all opfamilies, including builtin opfamilies; we filter out
+ * system-defined opfamilies at dump-out time.
+ */
+
+ /* Make sure we are in proper schema */
+ selectSourceSchema("pg_catalog");
+
+ appendPQExpBuffer(query, "SELECT tableoid, oid, opfname, "
+ "opfnamespace, "
+ "(%s opfowner) as rolname "
+ "FROM pg_opfamily",
+ username_subquery);
+
+ res = PQexec(g_conn, query->data);
+ check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+
+ ntups = PQntuples(res);
+ *numOpfamilies = ntups;
+
+ opfinfo = (OpfamilyInfo *) malloc(ntups * sizeof(OpfamilyInfo));
+
+ i_tableoid = PQfnumber(res, "tableoid");
+ i_oid = PQfnumber(res, "oid");
+ i_opfname = PQfnumber(res, "opfname");
+ i_opfnamespace = PQfnumber(res, "opfnamespace");
+ i_rolname = PQfnumber(res, "rolname");
+
+ for (i = 0; i < ntups; i++)
+ {
+ opfinfo[i].dobj.objType = DO_OPFAMILY;
+ opfinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid));
+ opfinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid));
+ AssignDumpId(&opfinfo[i].dobj);
+ opfinfo[i].dobj.name = strdup(PQgetvalue(res, i, i_opfname));
+ opfinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_opfnamespace)),
+ opfinfo[i].dobj.catId.oid);
+ opfinfo[i].rolname = strdup(PQgetvalue(res, i, i_rolname));
+
+ /* Decide whether we want to dump it */
+ selectDumpableObject(&(opfinfo[i].dobj));
+
+ if (g_fout->remoteVersion >= 70300)
+ {
+ if (strlen(opfinfo[i].rolname) == 0)
+ write_msg(NULL, "WARNING: owner of operator family \"%s\" appears to be invalid\n",
+ opfinfo[i].dobj.name);
+ }
+ }
+
+ PQclear(res);
+
+ destroyPQExpBuffer(query);
+
+ return opfinfo;
+}
+
/*
* getAggregates:
* read all the user-defined aggregates in the system catalogs and
else if (g_fout->remoteVersion >= 70100)
{
appendPQExpBuffer(query,
- "SELECT tgname, tgfoid::regproc as tgfname, "
+ "SELECT tgname, tgfoid::pg_catalog.regproc as tgfname, "
"tgtype, tgnargs, tgargs, tgenabled, "
"tgisconstraint, tgconstrname, tgdeferrable, "
"tgconstrrelid, tginitdeferred, tableoid, oid, "
else
{
appendPQExpBuffer(query,
- "SELECT tgname, tgfoid::regproc as tgfname, "
+ "SELECT tgname, tgfoid::pg_catalog.regproc as tgfname, "
"tgtype, tgnargs, tgargs, tgenabled, "
"tgisconstraint, tgconstrname, tgdeferrable, "
"tgconstrrelid, tginitdeferred, "
case DO_OPCLASS:
dumpOpclass(fout, (OpclassInfo *) dobj);
break;
+ case DO_OPFAMILY:
+ dumpOpfamily(fout, (OpfamilyInfo *) dobj);
+ break;
case DO_CONVERSION:
dumpConversion(fout, (ConvInfo *) dobj);
break;
int i_opcintype;
int i_opckeytype;
int i_opcdefault;
+ int i_opcfamily;
+ int i_opcfamilynsp;
int i_amname;
int i_amopstrategy;
int i_amopreqcheck;
char *opcintype;
char *opckeytype;
char *opcdefault;
+ char *opcfamily;
+ char *opcfamilynsp;
char *amname;
char *amopstrategy;
char *amopreqcheck;
appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, "
"opckeytype::pg_catalog.regtype, "
"opcdefault, "
+ "opfname AS opcfamily, "
+ "nspname AS opcfamilynsp, "
"(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcmethod) AS amname "
- "FROM pg_catalog.pg_opclass "
- "WHERE oid = '%u'::pg_catalog.oid",
+ "FROM pg_catalog.pg_opclass c "
+ "LEFT JOIN pg_catalog.pg_opfamily f ON f.oid = opcfamily "
+ "LEFT JOIN pg_catalog.pg_namespace n ON n.oid = opfnamespace "
+ "WHERE c.oid = '%u'::pg_catalog.oid",
opcinfo->dobj.catId.oid);
}
else
appendPQExpBuffer(query, "SELECT opcintype::pg_catalog.regtype, "
"opckeytype::pg_catalog.regtype, "
"opcdefault, "
+ "NULL AS opcfamily, "
+ "NULL AS opcfamilynsp, "
"(SELECT amname FROM pg_catalog.pg_am WHERE oid = opcamid) AS amname "
"FROM pg_catalog.pg_opclass "
"WHERE oid = '%u'::pg_catalog.oid",
i_opcintype = PQfnumber(res, "opcintype");
i_opckeytype = PQfnumber(res, "opckeytype");
i_opcdefault = PQfnumber(res, "opcdefault");
+ i_opcfamily = PQfnumber(res, "opcfamily");
+ i_opcfamilynsp = PQfnumber(res, "opcfamilynsp");
i_amname = PQfnumber(res, "amname");
opcintype = PQgetvalue(res, 0, i_opcintype);
opckeytype = PQgetvalue(res, 0, i_opckeytype);
opcdefault = PQgetvalue(res, 0, i_opcdefault);
+ opcfamily = PQgetvalue(res, 0, i_opcfamily);
+ opcfamilynsp = PQgetvalue(res, 0, i_opcfamilynsp);
/* amname will still be needed after we PQclear res */
amname = strdup(PQgetvalue(res, 0, i_amname));
fmtId(opcinfo->dobj.name));
if (strcmp(opcdefault, "t") == 0)
appendPQExpBuffer(q, "DEFAULT ");
- appendPQExpBuffer(q, "FOR TYPE %s USING %s AS\n ",
+ appendPQExpBuffer(q, "FOR TYPE %s USING %s",
opcintype,
fmtId(amname));
+ if (strlen(opcfamily) > 0 &&
+ (strcmp(opcfamily, opcinfo->dobj.name) != 0 ||
+ strcmp(opcfamilynsp, opcinfo->dobj.namespace->dobj.name) != 0))
+ {
+ appendPQExpBuffer(q, " FAMILY ");
+ if (strcmp(opcfamilynsp, opcinfo->dobj.namespace->dobj.name) != 0)
+ appendPQExpBuffer(q, "%s.", fmtId(opcfamilynsp));
+ appendPQExpBuffer(q, "%s", fmtId(opcfamily));
+ }
+ appendPQExpBuffer(q, " AS\n ");
needComma = false;
appendPQExpBuffer(query, "SELECT amopstrategy, amopreqcheck, "
"amopopr::pg_catalog.regoperator "
"FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
- "WHERE refclassid = 'pg_catalog.pg_opclass'::regclass "
+ "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
"AND refobjid = '%u'::pg_catalog.oid "
- "AND classid = 'pg_catalog.pg_amop'::regclass "
+ "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
"AND objid = ao.oid "
"ORDER BY amopstrategy",
opcinfo->dobj.catId.oid);
appendPQExpBuffer(query, "SELECT amprocnum, "
"amproc::pg_catalog.regprocedure "
"FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
- "WHERE refclassid = 'pg_catalog.pg_opclass'::regclass "
+ "WHERE refclassid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
"AND refobjid = '%u'::pg_catalog.oid "
- "AND classid = 'pg_catalog.pg_amproc'::regclass "
+ "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
"AND objid = ap.oid "
"ORDER BY amprocnum",
opcinfo->dobj.catId.oid);
destroyPQExpBuffer(delq);
}
+/*
+ * dumpOpfamily
+ * write out a single operator family definition
+ */
+static void
+dumpOpfamily(Archive *fout, OpfamilyInfo *opfinfo)
+{
+ PQExpBuffer query;
+ PQExpBuffer q;
+ PQExpBuffer delq;
+ PGresult *res;
+ PGresult *res_ops;
+ PGresult *res_procs;
+ int ntups;
+ int i_amname;
+ int i_amopstrategy;
+ int i_amopreqcheck;
+ int i_amopopr;
+ int i_amprocnum;
+ int i_amproc;
+ int i_amproclefttype;
+ int i_amprocrighttype;
+ char *amname;
+ char *amopstrategy;
+ char *amopreqcheck;
+ char *amopopr;
+ char *amprocnum;
+ char *amproc;
+ char *amproclefttype;
+ char *amprocrighttype;
+ bool needComma;
+ int i;
+
+ /* Skip if not to be dumped */
+ if (!opfinfo->dobj.dump || dataOnly)
+ return;
+
+ /*
+ * We want to dump the opfamily only if (1) it contains "loose" operators
+ * or functions, or (2) it contains an opclass with a different name or
+ * owner. Otherwise it's sufficient to let it be created during creation
+ * of the contained opclass, and not dumping it improves portability of
+ * the dump. Since we have to fetch the loose operators/funcs anyway,
+ * do that first.
+ */
+
+ query = createPQExpBuffer();
+ q = createPQExpBuffer();
+ delq = createPQExpBuffer();
+
+ /* Make sure we are in proper schema so regoperator works correctly */
+ selectSourceSchema(opfinfo->dobj.namespace->dobj.name);
+
+ /*
+ * Fetch only those opfamily members that are tied directly to the opfamily
+ * by pg_depend entries.
+ */
+ appendPQExpBuffer(query, "SELECT amopstrategy, amopreqcheck, "
+ "amopopr::pg_catalog.regoperator "
+ "FROM pg_catalog.pg_amop ao, pg_catalog.pg_depend "
+ "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
+ "AND refobjid = '%u'::pg_catalog.oid "
+ "AND classid = 'pg_catalog.pg_amop'::pg_catalog.regclass "
+ "AND objid = ao.oid "
+ "ORDER BY amopstrategy",
+ opfinfo->dobj.catId.oid);
+
+ res_ops = PQexec(g_conn, query->data);
+ check_sql_result(res_ops, g_conn, query->data, PGRES_TUPLES_OK);
+
+ resetPQExpBuffer(query);
+
+ appendPQExpBuffer(query, "SELECT amprocnum, "
+ "amproc::pg_catalog.regprocedure, "
+ "amproclefttype::pg_catalog.regtype, "
+ "amprocrighttype::pg_catalog.regtype "
+ "FROM pg_catalog.pg_amproc ap, pg_catalog.pg_depend "
+ "WHERE refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
+ "AND refobjid = '%u'::pg_catalog.oid "
+ "AND classid = 'pg_catalog.pg_amproc'::pg_catalog.regclass "
+ "AND objid = ap.oid "
+ "ORDER BY amprocnum",
+ opfinfo->dobj.catId.oid);
+
+ res_procs = PQexec(g_conn, query->data);
+ check_sql_result(res_procs, g_conn, query->data, PGRES_TUPLES_OK);
+
+ if (PQntuples(res_ops) == 0 && PQntuples(res_procs) == 0)
+ {
+ /* No loose members, so check contained opclasses */
+ resetPQExpBuffer(query);
+
+ appendPQExpBuffer(query, "SELECT 1 "
+ "FROM pg_catalog.pg_opclass c, pg_catalog.pg_opfamily f, pg_catalog.pg_depend "
+ "WHERE f.oid = '%u'::pg_catalog.oid "
+ "AND refclassid = 'pg_catalog.pg_opfamily'::pg_catalog.regclass "
+ "AND refobjid = f.oid "
+ "AND classid = 'pg_catalog.pg_opclass'::pg_catalog.regclass "
+ "AND objid = c.oid "
+ "AND (opcname != opfname OR opcnamespace != opfnamespace OR opcowner != opfowner) "
+ "LIMIT 1",
+ opfinfo->dobj.catId.oid);
+
+ res = PQexec(g_conn, query->data);
+ check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+
+ if (PQntuples(res) == 0)
+ {
+ /* no need to dump it, so bail out */
+ PQclear(res);
+ PQclear(res_ops);
+ PQclear(res_procs);
+ destroyPQExpBuffer(query);
+ destroyPQExpBuffer(q);
+ destroyPQExpBuffer(delq);
+ return;
+ }
+
+ PQclear(res);
+ }
+
+ /* Get additional fields from the pg_opfamily row */
+ resetPQExpBuffer(query);
+
+ appendPQExpBuffer(query, "SELECT "
+ "(SELECT amname FROM pg_catalog.pg_am WHERE oid = opfmethod) AS amname "
+ "FROM pg_catalog.pg_opfamily "
+ "WHERE oid = '%u'::pg_catalog.oid",
+ opfinfo->dobj.catId.oid);
+
+ res = PQexec(g_conn, query->data);
+ check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK);
+
+ /* Expecting a single result only */
+ ntups = PQntuples(res);
+ if (ntups != 1)
+ {
+ write_msg(NULL, "Got %d rows instead of one from: %s",
+ ntups, query->data);
+ exit_nicely();
+ }
+
+ i_amname = PQfnumber(res, "amname");
+
+ /* amname will still be needed after we PQclear res */
+ amname = strdup(PQgetvalue(res, 0, i_amname));
+
+ /*
+ * DROP must be fully qualified in case same name appears in pg_catalog
+ */
+ appendPQExpBuffer(delq, "DROP OPERATOR FAMILY %s",
+ fmtId(opfinfo->dobj.namespace->dobj.name));
+ appendPQExpBuffer(delq, ".%s",
+ fmtId(opfinfo->dobj.name));
+ appendPQExpBuffer(delq, " USING %s;\n",
+ fmtId(amname));
+
+ /* Build the fixed portion of the CREATE command */
+ appendPQExpBuffer(q, "CREATE OPERATOR FAMILY %s",
+ fmtId(opfinfo->dobj.name));
+ appendPQExpBuffer(q, " USING %s;\n",
+ fmtId(amname));
+
+ PQclear(res);
+
+ /* Do we need an ALTER to add loose members? */
+ if (PQntuples(res_ops) > 0 || PQntuples(res_procs) > 0)
+ {
+ appendPQExpBuffer(q, "ALTER OPERATOR FAMILY %s",
+ fmtId(opfinfo->dobj.name));
+ appendPQExpBuffer(q, " USING %s ADD\n ",
+ fmtId(amname));
+
+ needComma = false;
+
+ /*
+ * Now fetch and print the OPERATOR entries (pg_amop rows).
+ */
+ ntups = PQntuples(res_ops);
+
+ i_amopstrategy = PQfnumber(res_ops, "amopstrategy");
+ i_amopreqcheck = PQfnumber(res_ops, "amopreqcheck");
+ i_amopopr = PQfnumber(res_ops, "amopopr");
+
+ for (i = 0; i < ntups; i++)
+ {
+ amopstrategy = PQgetvalue(res_ops, i, i_amopstrategy);
+ amopreqcheck = PQgetvalue(res_ops, i, i_amopreqcheck);
+ amopopr = PQgetvalue(res_ops, i, i_amopopr);
+
+ if (needComma)
+ appendPQExpBuffer(q, " ,\n ");
+
+ appendPQExpBuffer(q, "OPERATOR %s %s",
+ amopstrategy, amopopr);
+ if (strcmp(amopreqcheck, "t") == 0)
+ appendPQExpBuffer(q, " RECHECK");
+
+ needComma = true;
+ }
+
+ /*
+ * Now fetch and print the FUNCTION entries (pg_amproc rows).
+ */
+ ntups = PQntuples(res_procs);
+
+ i_amprocnum = PQfnumber(res_procs, "amprocnum");
+ i_amproc = PQfnumber(res_procs, "amproc");
+ i_amproclefttype = PQfnumber(res_procs, "amproclefttype");
+ i_amprocrighttype = PQfnumber(res_procs, "amprocrighttype");
+
+ for (i = 0; i < ntups; i++)
+ {
+ amprocnum = PQgetvalue(res_procs, i, i_amprocnum);
+ amproc = PQgetvalue(res_procs, i, i_amproc);
+ amproclefttype = PQgetvalue(res_procs, i, i_amproclefttype);
+ amprocrighttype = PQgetvalue(res_procs, i, i_amprocrighttype);
+
+ if (needComma)
+ appendPQExpBuffer(q, " ,\n ");
+
+ appendPQExpBuffer(q, "FUNCTION %s (%s, %s) %s",
+ amprocnum, amproclefttype, amprocrighttype,
+ amproc);
+
+ needComma = true;
+ }
+
+ appendPQExpBuffer(q, ";\n");
+ }
+
+ ArchiveEntry(fout, opfinfo->dobj.catId, opfinfo->dobj.dumpId,
+ opfinfo->dobj.name,
+ opfinfo->dobj.namespace->dobj.name,
+ NULL,
+ opfinfo->rolname,
+ false, "OPERATOR FAMILY", q->data, delq->data, NULL,
+ opfinfo->dobj.dependencies, opfinfo->dobj.nDeps,
+ NULL, NULL);
+
+ /* Dump Operator Family Comments */
+ resetPQExpBuffer(q);
+ appendPQExpBuffer(q, "OPERATOR FAMILY %s",
+ fmtId(opfinfo->dobj.name));
+ appendPQExpBuffer(q, " USING %s",
+ fmtId(amname));
+ dumpComment(fout, q->data,
+ NULL, opfinfo->rolname,
+ opfinfo->dobj.catId, 0, opfinfo->dobj.dumpId);
+
+ free(amname);
+ PQclear(res_ops);
+ PQclear(res_procs);
+ destroyPQExpBuffer(query);
+ destroyPQExpBuffer(q);
+ destroyPQExpBuffer(delq);
+}
+
/*
* dumpConversion
* write out a single conversion definition