📄 pg_dump.c
字号:
/* * getOperators: * read all operators in the system catalogs and return them in the * OprInfo* structure * * numOprs is set to the number of operators read in */OprInfo *getOperators(int *numOprs){ PGresult *res; int ntups; int i; PQExpBuffer query = createPQExpBuffer(); OprInfo *oprinfo; int i_tableoid; int i_oid; int i_oprname; int i_oprnamespace; int i_rolname; int i_oprcode; /* * find all operators, including builtin operators; we filter out * system-defined operators at dump-out time. */ /* Make sure we are in proper schema */ selectSourceSchema("pg_catalog"); if (g_fout->remoteVersion >= 70300) { appendPQExpBuffer(query, "SELECT tableoid, oid, oprname, " "oprnamespace, " "(%s oprowner) as rolname, " "oprcode::oid as oprcode " "FROM pg_operator", username_subquery); } else if (g_fout->remoteVersion >= 70100) { appendPQExpBuffer(query, "SELECT tableoid, oid, oprname, " "0::oid as oprnamespace, " "(%s oprowner) as rolname, " "oprcode::oid as oprcode " "FROM pg_operator", username_subquery); } else { appendPQExpBuffer(query, "SELECT " "(SELECT oid FROM pg_class WHERE relname = 'pg_operator') AS tableoid, " "oid, oprname, " "0::oid as oprnamespace, " "(%s oprowner) as rolname, " "oprcode::oid as oprcode " "FROM pg_operator", username_subquery); } res = PQexec(g_conn, query->data); check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK); ntups = PQntuples(res); *numOprs = ntups; oprinfo = (OprInfo *) malloc(ntups * sizeof(OprInfo)); i_tableoid = PQfnumber(res, "tableoid"); i_oid = PQfnumber(res, "oid"); i_oprname = PQfnumber(res, "oprname"); i_oprnamespace = PQfnumber(res, "oprnamespace"); i_rolname = PQfnumber(res, "rolname"); i_oprcode = PQfnumber(res, "oprcode"); for (i = 0; i < ntups; i++) { oprinfo[i].dobj.objType = DO_OPERATOR; oprinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); oprinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); AssignDumpId(&oprinfo[i].dobj); oprinfo[i].dobj.name = strdup(PQgetvalue(res, i, i_oprname)); oprinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_oprnamespace)), oprinfo[i].dobj.catId.oid); oprinfo[i].rolname = strdup(PQgetvalue(res, i, i_rolname)); oprinfo[i].oprcode = atooid(PQgetvalue(res, i, i_oprcode)); if (strlen(oprinfo[i].rolname) == 0) write_msg(NULL, "WARNING: owner of operator \"%s\" appears to be invalid\n", oprinfo[i].dobj.name); } PQclear(res); destroyPQExpBuffer(query); return oprinfo;}/* * getConversions: * read all conversions in the system catalogs and return them in the * ConvInfo* structure * * numConversions is set to the number of conversions read in */ConvInfo *getConversions(int *numConversions){ PGresult *res; int ntups; int i; PQExpBuffer query = createPQExpBuffer(); ConvInfo *convinfo; int i_tableoid; int i_oid; int i_conname; int i_connamespace; int i_rolname; /* Conversions didn't exist pre-7.3 */ if (g_fout->remoteVersion < 70300) { *numConversions = 0; return NULL; } /* * find all conversions, including builtin conversions; we filter out * system-defined conversions at dump-out time. */ /* Make sure we are in proper schema */ selectSourceSchema("pg_catalog"); appendPQExpBuffer(query, "SELECT tableoid, oid, conname, " "connamespace, " "(%s conowner) as rolname " "FROM pg_conversion", username_subquery); res = PQexec(g_conn, query->data); check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK); ntups = PQntuples(res); *numConversions = ntups; convinfo = (ConvInfo *) malloc(ntups * sizeof(ConvInfo)); i_tableoid = PQfnumber(res, "tableoid"); i_oid = PQfnumber(res, "oid"); i_conname = PQfnumber(res, "conname"); i_connamespace = PQfnumber(res, "connamespace"); i_rolname = PQfnumber(res, "rolname"); for (i = 0; i < ntups; i++) { convinfo[i].dobj.objType = DO_CONVERSION; convinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); convinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); AssignDumpId(&convinfo[i].dobj); convinfo[i].dobj.name = strdup(PQgetvalue(res, i, i_conname)); convinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_connamespace)), convinfo[i].dobj.catId.oid); convinfo[i].rolname = strdup(PQgetvalue(res, i, i_rolname)); } PQclear(res); destroyPQExpBuffer(query); return convinfo;}/* * getOpclasses: * read all opclasses in the system catalogs and return them in the * OpclassInfo* structure * * numOpclasses is set to the number of opclasses read in */OpclassInfo *getOpclasses(int *numOpclasses){ PGresult *res; int ntups; int i; PQExpBuffer query = createPQExpBuffer(); OpclassInfo *opcinfo; int i_tableoid; int i_oid; int i_opcname; int i_opcnamespace; int i_rolname; /* * find all opclasses, including builtin opclasses; we filter out * system-defined opclasses at dump-out time. */ /* Make sure we are in proper schema */ selectSourceSchema("pg_catalog"); if (g_fout->remoteVersion >= 70300) { appendPQExpBuffer(query, "SELECT tableoid, oid, opcname, " "opcnamespace, " "(%s opcowner) as rolname " "FROM pg_opclass", username_subquery); } else if (g_fout->remoteVersion >= 70100) { appendPQExpBuffer(query, "SELECT tableoid, oid, opcname, " "0::oid as opcnamespace, " "''::name as rolname " "FROM pg_opclass"); } else { appendPQExpBuffer(query, "SELECT " "(SELECT oid FROM pg_class WHERE relname = 'pg_opclass') AS tableoid, " "oid, opcname, " "0::oid as opcnamespace, " "''::name as rolname " "FROM pg_opclass"); } res = PQexec(g_conn, query->data); check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK); ntups = PQntuples(res); *numOpclasses = ntups; opcinfo = (OpclassInfo *) malloc(ntups * sizeof(OpclassInfo)); i_tableoid = PQfnumber(res, "tableoid"); i_oid = PQfnumber(res, "oid"); i_opcname = PQfnumber(res, "opcname"); i_opcnamespace = PQfnumber(res, "opcnamespace"); i_rolname = PQfnumber(res, "rolname"); for (i = 0; i < ntups; i++) { opcinfo[i].dobj.objType = DO_OPCLASS; opcinfo[i].dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); opcinfo[i].dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); AssignDumpId(&opcinfo[i].dobj); opcinfo[i].dobj.name = strdup(PQgetvalue(res, i, i_opcname)); opcinfo[i].dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_opcnamespace)), opcinfo[i].dobj.catId.oid); opcinfo[i].rolname = strdup(PQgetvalue(res, i, i_rolname)); if (g_fout->remoteVersion >= 70300) { if (strlen(opcinfo[i].rolname) == 0) write_msg(NULL, "WARNING: owner of operator class \"%s\" appears to be invalid\n", opcinfo[i].dobj.name); } } PQclear(res); destroyPQExpBuffer(query); return opcinfo;}/* * getAggregates: * read all the user-defined aggregates in the system catalogs and * return them in the AggInfo* structure * * numAggs is set to the number of aggregates read in */AggInfo *getAggregates(int *numAggs){ PGresult *res; int ntups; int i; PQExpBuffer query = createPQExpBuffer(); AggInfo *agginfo; int i_tableoid; int i_oid; int i_aggname; int i_aggnamespace; int i_aggbasetype; int i_rolname; int i_aggacl; /* Make sure we are in proper schema */ selectSourceSchema("pg_catalog"); /* find all user-defined aggregates */ if (g_fout->remoteVersion >= 70300) { appendPQExpBuffer(query, "SELECT tableoid, oid, proname as aggname, " "pronamespace as aggnamespace, " "proargtypes[0] as aggbasetype, " "(%s proowner) as rolname, " "proacl as aggacl " "FROM pg_proc " "WHERE proisagg " "AND pronamespace != " "(select oid from pg_namespace where nspname = 'pg_catalog')", username_subquery); } else if (g_fout->remoteVersion >= 70100) { appendPQExpBuffer(query, "SELECT tableoid, oid, aggname, " "0::oid as aggnamespace, " "aggbasetype, " "(%s aggowner) as rolname, " "'{=X}' as aggacl " "FROM pg_aggregate " "where oid > '%u'::oid", username_subquery, g_last_builtin_oid); } else { appendPQExpBuffer(query, "SELECT " "(SELECT oid FROM pg_class WHERE relname = 'pg_aggregate') AS tableoid, " "oid, aggname, " "0::oid as aggnamespace, " "aggbasetype, " "(%s aggowner) as rolname, " "'{=X}' as aggacl " "FROM pg_aggregate " "where oid > '%u'::oid", username_subquery, g_last_builtin_oid); } res = PQexec(g_conn, query->data); check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK); ntups = PQntuples(res); *numAggs = ntups; agginfo = (AggInfo *) malloc(ntups * sizeof(AggInfo)); i_tableoid = PQfnumber(res, "tableoid"); i_oid = PQfnumber(res, "oid"); i_aggname = PQfnumber(res, "aggname"); i_aggnamespace = PQfnumber(res, "aggnamespace"); i_aggbasetype = PQfnumber(res, "aggbasetype"); i_rolname = PQfnumber(res, "rolname"); i_aggacl = PQfnumber(res, "aggacl"); for (i = 0; i < ntups; i++) { agginfo[i].aggfn.dobj.objType = DO_AGG; agginfo[i].aggfn.dobj.catId.tableoid = atooid(PQgetvalue(res, i, i_tableoid)); agginfo[i].aggfn.dobj.catId.oid = atooid(PQgetvalue(res, i, i_oid)); AssignDumpId(&agginfo[i].aggfn.dobj); agginfo[i].aggfn.dobj.name = strdup(PQgetvalue(res, i, i_aggname)); agginfo[i].aggfn.dobj.namespace = findNamespace(atooid(PQgetvalue(res, i, i_aggnamespace)), agginfo[i].aggfn.dobj.catId.oid); agginfo[i].aggfn.rolname = strdup(PQgetvalue(res, i, i_rolname)); if (strlen(agginfo[i].aggfn.rolname) == 0) write_msg(NULL, "WARNING: owner of aggregate function \"%s\" appears to be invalid\n", agginfo[i].aggfn.dobj.name); agginfo[i].aggfn.lang = InvalidOid; /* not currently interesting */ agginfo[i].aggfn.nargs = 1; agginfo[i].aggfn.argtypes = (Oid *) malloc(sizeof(Oid)); agginfo[i].aggfn.argtypes[0] = atooid(PQgetvalue(res, i, i_aggbasetype)); agginfo[i].aggfn.prorettype = InvalidOid; /* not saved */ agginfo[i].aggfn.proacl = strdup(PQgetvalue(res, i, i_aggacl)); agginfo[i].anybasetype = false; /* computed when it's dumped */ agginfo[i].fmtbasetype = NULL; /* computed when it's dumped */ } PQclear(res); destroyPQExpBuffer(query); return agginfo;}/* * getFuncs: * read all the user-defined functions in the system catalogs and * return them in the FuncInfo* structure * * numFuncs is set to the number of functions read in */FuncInfo *getFuncs(int *numFuncs){ PGresult *res; int ntups; int i; PQExpBuffer query = createPQExpBuffer(); FuncInfo *finfo; int i_tableoid; int i_oid; int i_proname; int i_pronamespace; int i_rolname; int i_prolang; int i_pronargs; int i_proargtypes; int i_prorettype; int i_proacl; /* Make sure we are in proper schema */ selectSourceSchema("pg_catalog"); /* find all user-defined funcs */ if (g_fout->remoteVersion >= 70300) { appendPQExpBuffer(query, "SELECT tableoid, oid, proname, prolang, " "pronargs, proargtypes, prorettype, proacl, " "pronamespace, " "(%s proowner) as rolname " "FROM pg_proc " "WHERE NOT proisagg " "AND pronamespace != " "(select oid from pg_namespace" " where nspname = 'pg_catalog')", username_subquery); } else if (g_fout->remoteVersion >= 70100) { appendPQExpBuffer(query, "SELECT tableoid, oid, proname, prolang, " "pronargs, proargtypes, prorettype, " "'{=X}' as proacl, " "0::oid as pronamespace, " "(%s proowner) as rolname " "FROM pg_proc " "where pg_proc.oid > '%u'::oid", username_subquery, g_last_builtin_oid); } else { appendPQExpBuffer(query, "SELECT " "(SELECT oid FROM pg_class " " WHERE relname = 'pg_proc') AS tableoid, " "oid, proname, prolang, " "pronargs, proargtypes, prorettype, " "'{=X}' as proacl, " "0::oid as pronamespace, " "(%s proowner) as rolname " "FROM pg_proc " "where pg_proc.oid > '%u'::oid", username_subquery, g_last_builtin_oid); } res = PQexec(g_conn, query->data); check_sql_result(res, g_conn, query->data, PGRES_TUPLES_OK); ntups = PQntuples(res); *numFuncs = ntups; finfo = (FuncInfo *) calloc(ntups, sizeof(FuncInfo)); i_tableoid = PQfnumber(res, "tableoid"); i_oid =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -