1、循环调用getopt_long解析命令行参数,将参数保存到static DumpOptions dopt;中
2、判断参数是否相容,不相容则退出:
options -s/--schema-only and -a/--data-only cannot be used together
options -c/--clean and -a/--data-only cannot be used together
options --inserts/--column-inserts and -o/--oids cannot be used together
option --if-exists requires option -c/--clean
3、调用CreateArchive打开输出文件,输出流为fout。该函数使用4个文件封装了4种不同dump文件格式,增加新文件可以增加新的导出文件类型各自封装,独立易于维护。
CreateArchive->_allocAH:
switch (AH->format){
case archCustom:
InitArchiveFmt_Custom(AH);
break;
case archNull:
InitArchiveFmt_Null(AH);
break;
case archDirectory:
InitArchiveFmt_Directory(AH);
break;
case archTar:
InitArchiveFmt_Tar(AH);
break;
default:
exit_horribly(modulename, "unrecognized file format \"%d\"\n", fmt);
}
4、fout是一个重要的全局变量
5、调用ConnectDatabase连接数据库
6、调用setup_connection,在连接上执行一些SQL语句:
SELECT pg_catalog.set_config('search_path', '', false);
set client_encoding to '%s'//pg_dump -E指定
SET ROLE %s//
SET DATESTYLE = ISO;
SET INTERVALSTYLE = POSTGRES;
SET extra_float_digits TO 3;
SET synchronize_seqscans TO off;
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET row_security = off;
BEGIN;
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ, READ ONLY;
7、为兼容低版本,根据服务器版本号决定一些变量取值
8、调用tblinfo = getSchemaData(fout, &numTables);决定导出哪些数据库对象。本函数又调用如下函数,值得关注哦。为了存储每个对象的元数据,这些函数会malloc申请空间,直到pg_dump进程结束才释放。
extinfo = getExtensions(fout, &numExtensions);
extinfoindex = buildIndexArray(extinfo, numExtensions, sizeof(ExtensionInfo));
getExtensionMembership(fout, extinfo, numExtensions);
nspinfo = getNamespaces(fout, &numNamespaces);
nspinfoindex = buildIndexArray(nspinfo, numNamespaces, sizeof(NamespaceInfo));
tblinfo = getTables(fout, &numTables);
tblinfoindex = buildIndexArray(tblinfo, numTables, sizeof(TableInfo));
getOwnedSeqs(fout, tblinfo, numTables);
funinfo = getFuncs(fout, &numFuncs);
funinfoindex = buildIndexArray(funinfo, numFuncs, sizeof(FuncInfo));
typinfo = getTypes(fout, &numTypes);
typinfoindex = buildIndexArray(typinfo, numTypes, sizeof(TypeInfo));
getProcLangs(fout, &numProcLangs);
getAggregates(fout, &numAggregates);
oprinfo = getOperators(fout, &numOperators);
oprinfoindex = buildIndexArray(oprinfo, numOperators, sizeof(OprInfo));
getAccessMethods(fout, &numAccessMethods);
getOpclasses(fout, &numOpclasses);
getOpfamilies(fout, &numOpfamilies);
getTSParsers(fout, &numTSParsers);
getTSTemplates(fout, &numTSTemplates);
getTSDictionaries(fout, &numTSDicts);
getTSConfigurations(fout, &numTSConfigs);
getForeignDataWrappers(fout, &numForeignDataWrappers);
getForeignServers(fout, &numForeignServers);
getDefaultACLs(fout, &numDefaultACLs);
collinfo = getCollations(fout, &numCollations);
collinfoindex = buildIndexArray(collinfo, numCollations, sizeof(CollInfo));
getConversions(fout, &numConversions);
getCasts(fout, &numCasts);
getTransforms(fout, &numTransforms);
inhinfo = getInherits(fout, &numInherits);
getEventTriggers(fout, &numEventTriggers);
processExtensionTables(fout, extinfo, numExtensions);
flagInhTables(tblinfo, numTables, inhinfo, numInherits);
getTableAttrs(fout, tblinfo, numTables);
flagInhAttrs(fout->dopt, tblinfo, numTables);
getIndexes(fout, tblinfo, numTables);
getExtendedStatistics(fout);
getConstraints(fout, tblinfo, numTables);
getTriggers(fout, tblinfo, numTables);
getRules(fout, &numRules);
getPolicies(fout, tblinfo, numTables);
getPublications(fout);
getPublicationTables(fout, tblinfo, numTables);
getSubscriptions(fout);
对于每个getXXXs函数都将执行下面流程,以getTables为例:
1)根据服务器版本号查询系统表,读出对象的元数据信息
2)malloc内存空间并将查询结果存放到对象的数据结构中,TableInfo