📄 pg_dump.c
字号:
clearTableInfo(TableInfo *tblinfo, int numTables){ int i, j; for (i = 0; i < numTables; ++i) { if (tblinfo[i].oid) free(tblinfo[i].oid); if (tblinfo[i].relacl) free(tblinfo[i].relacl); if (tblinfo[i].usename) free(tblinfo[i].usename); if (tblinfo[i].relname) free(tblinfo[i].relname); if (tblinfo[i].sequence) continue; /* Process Attributes */ for (j = 0; j < tblinfo[i].numatts; j++) { if (tblinfo[i].attnames[j]) free(tblinfo[i].attnames[j]); if (tblinfo[i].typnames[j]) free(tblinfo[i].typnames[j]); } if (tblinfo[i].atttypmod) free((int *) tblinfo[i].atttypmod); if (tblinfo[i].inhAttrs) free((int *) tblinfo[i].inhAttrs); if (tblinfo[i].attnames) free(tblinfo[i].attnames); if (tblinfo[i].typnames) free(tblinfo[i].typnames); if (tblinfo[i].notnull) free(tblinfo[i].notnull); } free(tblinfo);}voidclearInhInfo(InhInfo *inh, int numInherits){ int i; if (!inh) return; for (i = 0; i < numInherits; ++i) { if (inh[i].inhrel) free(inh[i].inhrel); if (inh[i].inhparent) free(inh[i].inhparent); } free(inh);}voidclearOprInfo(OprInfo *opr, int numOprs){ int i; if (!opr) return; for (i = 0; i < numOprs; ++i) { if (opr[i].oid) free(opr[i].oid); if (opr[i].oprname) free(opr[i].oprname); if (opr[i].oprkind) free(opr[i].oprkind); if (opr[i].oprcode) free(opr[i].oprcode); if (opr[i].oprleft) free(opr[i].oprleft); if (opr[i].oprright) free(opr[i].oprright); if (opr[i].oprcom) free(opr[i].oprcom); if (opr[i].oprnegate) free(opr[i].oprnegate); if (opr[i].oprrest) free(opr[i].oprrest); if (opr[i].oprjoin) free(opr[i].oprjoin); if (opr[i].oprcanhash) free(opr[i].oprcanhash); if (opr[i].oprlsortop) free(opr[i].oprlsortop); if (opr[i].oprrsortop) free(opr[i].oprrsortop); if (opr[i].usename) free(opr[i].usename); } free(opr);}voidclearIndInfo(IndInfo *ind, int numIndices){ int i, a; if (!ind) return; for (i = 0; i < numIndices; ++i) { if (ind[i].indexrelname) free(ind[i].indexrelname); if (ind[i].indrelname) free(ind[i].indrelname); if (ind[i].indamname) free(ind[i].indamname); if (ind[i].indproc) free(ind[i].indproc); if (ind[i].indisunique) free(ind[i].indisunique); for (a = 0; a < INDEX_MAX_KEYS; ++a) { if (ind[i].indkey[a]) free(ind[i].indkey[a]); if (ind[i].indclass[a]) free(ind[i].indclass[a]); } } free(ind);}voidclearAggInfo(AggInfo *agginfo, int numArgs){ int i; if (!agginfo) return; for (i = 0; i < numArgs; ++i) { if (agginfo[i].oid) free(agginfo[i].oid); if (agginfo[i].aggname) free(agginfo[i].aggname); if (agginfo[i].aggtransfn1) free(agginfo[i].aggtransfn1); if (agginfo[i].aggtransfn2) free(agginfo[i].aggtransfn2); if (agginfo[i].aggfinalfn) free(agginfo[i].aggfinalfn); if (agginfo[i].aggtranstype1) free(agginfo[i].aggtranstype1); if (agginfo[i].aggbasetype) free(agginfo[i].aggbasetype); if (agginfo[i].aggtranstype2) free(agginfo[i].aggtranstype2); if (agginfo[i].agginitval1) free(agginfo[i].agginitval1); if (agginfo[i].agginitval2) free(agginfo[i].agginitval2); if (agginfo[i].usename) free(agginfo[i].usename); } free(agginfo);}/* * 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; char query[MAX_QUERY_SIZE]; AggInfo *agginfo; int i_oid; int i_aggname; int i_aggtransfn1; int i_aggtransfn2; int i_aggfinalfn; int i_aggtranstype1; int i_aggbasetype; int i_aggtranstype2; int i_agginitval1; int i_agginitval2; int i_usename; /* find all user-defined aggregates */ sprintf(query, "SELECT pg_aggregate.oid, aggname, aggtransfn1, aggtransfn2, " "aggfinalfn, aggtranstype1, aggbasetype, aggtranstype2, " "agginitval1, agginitval2, usename from pg_aggregate, pg_user " "where aggowner = usesysid"); res = PQexec(g_conn, query); if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "getAggregates(): SELECT failed. Explanation from backend: '%s'.\n", PQerrorMessage(g_conn)); exit_nicely(g_conn); } ntups = PQntuples(res); *numAggs = ntups; agginfo = (AggInfo *) malloc(ntups * sizeof(AggInfo)); i_oid = PQfnumber(res, "oid"); i_aggname = PQfnumber(res, "aggname"); i_aggtransfn1 = PQfnumber(res, "aggtransfn1"); i_aggtransfn2 = PQfnumber(res, "aggtransfn2"); i_aggfinalfn = PQfnumber(res, "aggfinalfn"); i_aggtranstype1 = PQfnumber(res, "aggtranstype1"); i_aggbasetype = PQfnumber(res, "aggbasetype"); i_aggtranstype2 = PQfnumber(res, "aggtranstype2"); i_agginitval1 = PQfnumber(res, "agginitval1"); i_agginitval2 = PQfnumber(res, "agginitval2"); i_usename = PQfnumber(res, "usename"); for (i = 0; i < ntups; i++) { agginfo[i].oid = strdup(PQgetvalue(res, i, i_oid)); agginfo[i].aggname = strdup(PQgetvalue(res, i, i_aggname)); agginfo[i].aggtransfn1 = strdup(PQgetvalue(res, i, i_aggtransfn1)); agginfo[i].aggtransfn2 = strdup(PQgetvalue(res, i, i_aggtransfn2)); agginfo[i].aggfinalfn = strdup(PQgetvalue(res, i, i_aggfinalfn)); agginfo[i].aggtranstype1 = strdup(PQgetvalue(res, i, i_aggtranstype1)); agginfo[i].aggbasetype = strdup(PQgetvalue(res, i, i_aggbasetype)); agginfo[i].aggtranstype2 = strdup(PQgetvalue(res, i, i_aggtranstype2)); agginfo[i].agginitval1 = strdup(PQgetvalue(res, i, i_agginitval1)); agginfo[i].agginitval2 = strdup(PQgetvalue(res, i, i_agginitval2)); agginfo[i].usename = strdup(PQgetvalue(res, i, i_usename)); } PQclear(res); 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; char query[MAX_QUERY_SIZE]; FuncInfo *finfo; int i_oid; int i_proname; int i_prolang; int i_pronargs; int i_proargtypes; int i_prorettype; int i_proretset; int i_prosrc; int i_probin; int i_usename; /* find all user-defined funcs */ sprintf(query, "SELECT pg_proc.oid, proname, prolang, pronargs, prorettype, " "proretset, proargtypes, prosrc, probin, usename " "from pg_proc, pg_user " "where pg_proc.oid > '%u'::oid and proowner = usesysid", g_last_builtin_oid); res = PQexec(g_conn, query); if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "getFuncs(): SELECT failed. Explanation from backend: '%s'.\n", PQerrorMessage(g_conn)); exit_nicely(g_conn); } ntups = PQntuples(res); *numFuncs = ntups; finfo = (FuncInfo *) malloc(ntups * sizeof(FuncInfo)); i_oid = PQfnumber(res, "oid"); i_proname = PQfnumber(res, "proname"); i_prolang = PQfnumber(res, "prolang"); i_pronargs = PQfnumber(res, "pronargs"); i_proargtypes = PQfnumber(res, "proargtypes"); i_prorettype = PQfnumber(res, "prorettype"); i_proretset = PQfnumber(res, "proretset"); i_prosrc = PQfnumber(res, "prosrc"); i_probin = PQfnumber(res, "probin"); i_usename = PQfnumber(res, "usename"); for (i = 0; i < ntups; i++) { finfo[i].oid = strdup(PQgetvalue(res, i, i_oid)); finfo[i].proname = strdup(PQgetvalue(res, i, i_proname)); finfo[i].prosrc = checkForQuote(PQgetvalue(res, i, i_prosrc)); finfo[i].probin = strdup(PQgetvalue(res, i, i_probin)); finfo[i].prorettype = strdup(PQgetvalue(res, i, i_prorettype)); finfo[i].retset = (strcmp(PQgetvalue(res, i, i_proretset), "t") == 0); finfo[i].nargs = atoi(PQgetvalue(res, i, i_pronargs)); finfo[i].lang = atoi(PQgetvalue(res, i, i_prolang)); finfo[i].usename = strdup(PQgetvalue(res, i, i_usename)); parseArgTypes(finfo[i].argtypes, PQgetvalue(res, i, i_proargtypes)); finfo[i].dumped = 0; } PQclear(res); return finfo;}/* * getTables * read all the user-defined tables (no indices, no catalogs) * in the system catalogs return them in the TableInfo* structure * * numTables is set to the number of tables read in * * */TableInfo *getTables(int *numTables, FuncInfo *finfo, int numFuncs){ PGresult *res; int ntups; int i; char query[MAX_QUERY_SIZE]; TableInfo *tblinfo; int i_oid; int i_relname; int i_relkind; int i_relacl; int i_usename; int i_relchecks; int i_reltriggers; /* * find all the user-defined tables (no indices and no catalogs), * ordering by oid is important so that we always process the parent * tables before the child tables when traversing the tblinfo* * * we ignore tables that are not type 'r' (ordinary relation) * or 'S' (sequence) --- in particular, Large Object relations * (type 'l') are ignored. */ sprintf(query, "SELECT pg_class.oid, relname, relkind, relacl, usename, " "relchecks, reltriggers " "from pg_class, pg_user " "where relowner = usesysid and " "(relkind = 'r' or relkind = 'S') and relname !~ '^pg_' " "order by oid"); res = PQexec(g_conn, query); if (!res || PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "getTables(): SELECT failed. Explanation from backend: '%s'.\n", PQerrorMessage(g_conn)); exit_nicely(g_conn); } ntups = PQntuples(res); *numTables = ntups; tblinfo = (TableInfo *) malloc(ntups * sizeof(TableInfo)); i_oid = PQfnumber(res, "oid"); i_relname = PQfnumber(res, "relname"); i_relkind = PQfnumber(res, "relkind"); i_relacl = PQfnumber(res, "relacl"); i_usename = PQfnumber(res, "usename"); i_relchecks = PQfnumber(res, "relchecks"); i_reltriggers = PQfnumber(res, "reltriggers"); for (i = 0; i < ntups; i++) { tblinfo[i].oid = strdup(PQgetvalue(res, i, i_oid)); tblinfo[i].relname = strdup(PQgetvalue(res, i, i_relname)); tblinfo[i].relacl = strdup(PQgetvalue(res, i, i_relacl)); tblinfo[i].sequence = (strcmp(PQgetvalue(res, i, i_relkind), "S") == 0); tblinfo[i].usename = strdup(PQgetvalue(res, i, i_usename)); tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks)); tblinfo[i].ntrig = atoi(PQgetvalue(res, i, i_reltriggers)); /* * Exclude inherited CHECKs from CHECK constraints total. If a * constraint matches by name and condition with a constraint * belonging to a parent class, we assume it was inherited. */ if (tblinfo[i].ncheck > 0) { PGresult *res2; int ntups2; if (g_verbose) fprintf(stderr, "%s excluding inherited CHECK constraints " "for relation: '%s' %s\n", g_comment_start, tblinfo[i].relname, g_comment_end); sprintf(query, "SELECT rcname from pg_relcheck, pg_inherits as i " "where rcrelid = '%s'::oid " " and rcrelid = i.inhrel" " and exists " " (select * from pg_relcheck as c " " where c.rcname = pg_relcheck.rcname " " and c.rcsrc = pg_relcheck.rcsrc " " and c.rcrelid = i.inhparent) ", tblinfo[i].oid); res2 = PQexec(g_conn, query); if (!res2 || PQresultStatus(res2) != PGRES_TUPLES_OK) { fprintf(stderr, "getTables(): SELECT (for inherited CHECK) failed. Explanation from backend: '%s'.\n", PQerrorMessage(g_conn)); exit_nicely(g_conn); } ntups2 = PQntuples(res2); tblinfo[i].ncheck -= ntups2; if (tblinfo[i].ncheck < 0) { fprintf(stderr, "getTables(): found more inherited CHECKs than total for " "relation %s\n", tblinfo[i].relname); exit_nicely(g_conn); } PQclear(res2); } /* Get non-inherited CHECK constraints, if any */ if (tblinfo[i].ncheck > 0) { PGresult *res2; int i_rcname, i_rcsrc; int ntups2; int i2; if (g_verbose) fprintf(stderr, "%s finding CHECK constraints for relation: '%s' %s\n", g_comment_start, tblinfo[i].relname, g_comment_end); sprintf(query, "SELECT rcname, rcsrc from pg_relcheck " "where rcrelid = '%s'::oid " " and not exists " " (select * from pg_relcheck as c, pg_inherits as i " " where i.inhrel = pg_relcheck.rcrelid " " and c.rcname = pg_relcheck.rcname " " and c.rcsrc = pg_relcheck.rcsrc " " and c.rcrelid = i.inhparent) ", tblinfo[i].oid); res2 = PQexec(g_conn, query); if (!res2 || PQresultStatus(res2) != PGRES_TUPLES_OK) { fprintf(stderr, "getTables(): SELECT (for CHECK) failed. Explanation from backend: '%s'.\n", PQerrorMessage(g_conn)); exit_nicely(g_conn); } ntups2 = PQntuples(res2); if (ntups2 != tblinfo[i].ncheck) { fprintf(stderr, "getTables(): relation '%s': %d CHECKs were expected, but got %d\n", tblinfo[i].relname, tblinfo[i].ncheck, ntups2); exit_nicely(g_conn); } i_rcname = PQfnumber(res2, "rcname"); i_rcsrc = PQfnumber(res2, "rcsrc"); tblinfo[i].check_expr = (char **) malloc(ntups2 * sizeof(char *)); for (i2 = 0; i2 < ntups2; i2++) { char *name = PQgetvalue(res2, i2, i_rcname); char *expr = PQgetvalue(res2, i2, i_rcsrc); query[0] = '\0'; if (name[0] != '$') sprintf(query, "CONSTRAINT %s ", fmtId(name, force_quotes)); sprintf(query + strlen(query), "CHECK (%s)", expr); tblinfo[i].check_expr[i2] = strdup(query); } PQclear(res2); } else tblinfo[i].check_expr = NULL; /* Get Triggers */ if (tblinfo[i].ntrig > 0) { PGresult *res2; int i_tgname, i_tgfoid, i_tgtype,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -