⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pg_dumpall.c

📁 PostgreSQL 8.1.4的源码 适用于Linux下的开源数据库系统
💻 C
📖 第 1 页 / 共 3 页
字号:
						  "ORDER BY 1");	else		printfPQExpBuffer(buf,						  "SELECT usename as rolname, "						  "usesuper as rolsuper, "						  "true as rolinherit, "						  "usesuper as rolcreaterole, "						  "usecreatedb as rolcreatedb, "						  "usecatupd as rolcatupdate, "						  "true as rolcanlogin, "						  "-1 as rolconnlimit, "						  "passwd as rolpassword, "						  "valuntil as rolvaliduntil "						  "FROM pg_shadow "						  "UNION ALL "						  "SELECT groname as rolname, "						  "false as rolsuper, "						  "true as rolinherit, "						  "false as rolcreaterole, "						  "false as rolcreatedb, "						  "false as rolcatupdate, "						  "false as rolcanlogin, "						  "-1 as rolconnlimit, "						  "null::text as rolpassword, "						  "null::abstime as rolvaliduntil "						  "FROM pg_group "						  "WHERE NOT EXISTS (SELECT 1 FROM pg_shadow "						  " WHERE usename = groname) "						  "ORDER BY 1");	res = executeQuery(conn, buf->data);	i_rolname = PQfnumber(res, "rolname");	i_rolsuper = PQfnumber(res, "rolsuper");	i_rolinherit = PQfnumber(res, "rolinherit");	i_rolcreaterole = PQfnumber(res, "rolcreaterole");	i_rolcreatedb = PQfnumber(res, "rolcreatedb");	i_rolcatupdate = PQfnumber(res, "rolcatupdate");	i_rolcanlogin = PQfnumber(res, "rolcanlogin");	i_rolconnlimit = PQfnumber(res, "rolconnlimit");	i_rolpassword = PQfnumber(res, "rolpassword");	i_rolvaliduntil = PQfnumber(res, "rolvaliduntil");	if (PQntuples(res) > 0)		printf("--\n-- Roles\n--\n\n");	for (i = 0; i < PQntuples(res); i++)	{		const char *rolename;		rolename = PQgetvalue(res, i, i_rolname);		resetPQExpBuffer(buf);		if (output_clean)			appendPQExpBuffer(buf, "DROP ROLE %s;\n", fmtId(rolename));		/*		 * We dump CREATE ROLE followed by ALTER ROLE to ensure that the role		 * will acquire the right properties even if it already exists. (The		 * above DROP may therefore seem redundant, but it isn't really,		 * because this technique doesn't get rid of role memberships.)		 */		appendPQExpBuffer(buf, "CREATE ROLE %s;\n", fmtId(rolename));		appendPQExpBuffer(buf, "ALTER ROLE %s WITH", fmtId(rolename));		if (strcmp(PQgetvalue(res, i, i_rolsuper), "t") == 0)			appendPQExpBuffer(buf, " SUPERUSER");		else			appendPQExpBuffer(buf, " NOSUPERUSER");		if (strcmp(PQgetvalue(res, i, i_rolinherit), "t") == 0)			appendPQExpBuffer(buf, " INHERIT");		else			appendPQExpBuffer(buf, " NOINHERIT");		if (strcmp(PQgetvalue(res, i, i_rolcreaterole), "t") == 0)			appendPQExpBuffer(buf, " CREATEROLE");		else			appendPQExpBuffer(buf, " NOCREATEROLE");		if (strcmp(PQgetvalue(res, i, i_rolcreatedb), "t") == 0)			appendPQExpBuffer(buf, " CREATEDB");		else			appendPQExpBuffer(buf, " NOCREATEDB");		if (strcmp(PQgetvalue(res, i, i_rolcanlogin), "t") == 0)			appendPQExpBuffer(buf, " LOGIN");		else			appendPQExpBuffer(buf, " NOLOGIN");		if (strcmp(PQgetvalue(res, i, i_rolconnlimit), "-1") != 0)			appendPQExpBuffer(buf, " CONNECTION LIMIT %s",							  PQgetvalue(res, i, i_rolconnlimit));		if (!PQgetisnull(res, i, i_rolpassword))		{			appendPQExpBuffer(buf, " PASSWORD ");			appendStringLiteral(buf, PQgetvalue(res, i, i_rolpassword), true);		}		if (!PQgetisnull(res, i, i_rolvaliduntil))			appendPQExpBuffer(buf, " VALID UNTIL '%s'",							  PQgetvalue(res, i, i_rolvaliduntil));		appendPQExpBuffer(buf, ";\n");		printf("%s", buf->data);		if (server_version >= 70300)			dumpUserConfig(conn, rolename);	}	PQclear(res);	printf("\n\n");	destroyPQExpBuffer(buf);}/* * Dump role memberships.  This code is used for 8.1 and later servers. * * Note: we expect dumpRoles already created all the roles, but there is * no membership yet. */static voiddumpRoleMembership(PGconn *conn){	PGresult   *res;	int			i;	res = executeQuery(conn, "SELECT ur.rolname AS roleid, "					   "um.rolname AS member, "					   "ug.rolname AS grantor, "					   "a.admin_option "					   "FROM pg_auth_members a "					   "LEFT JOIN pg_authid ur on ur.oid = a.roleid "					   "LEFT JOIN pg_authid um on um.oid = a.member "					   "LEFT JOIN pg_authid ug on ug.oid = a.grantor "					   "ORDER BY 1,2,3");	if (PQntuples(res) > 0)		printf("--\n-- Role memberships\n--\n\n");	for (i = 0; i < PQntuples(res); i++)	{		char	   *roleid = PQgetvalue(res, i, 0);		char	   *member = PQgetvalue(res, i, 1);		char	   *grantor = PQgetvalue(res, i, 2);		char	   *option = PQgetvalue(res, i, 3);		printf("GRANT %s", fmtId(roleid));		printf(" TO %s", fmtId(member));		if (*option == 't')			printf(" WITH ADMIN OPTION");		printf(" GRANTED BY %s;\n", fmtId(grantor));	}	PQclear(res);	printf("\n\n");}/* * Dump group memberships from a pre-8.1 server.  It's annoying that we * can't share any useful amount of code with the post-8.1 case, but * the catalog representations are too different. * * Note: we expect dumpRoles already created all the roles, but there is * no membership yet. */static voiddumpGroups(PGconn *conn){	PQExpBuffer buf = createPQExpBuffer();	PGresult   *res;	int			i;	res = executeQuery(conn,					   "SELECT groname, grolist FROM pg_group ORDER BY 1");	if (PQntuples(res) > 0)		printf("--\n-- Role memberships\n--\n\n");	for (i = 0; i < PQntuples(res); i++)	{		char	   *groname = PQgetvalue(res, i, 0);		char	   *grolist = PQgetvalue(res, i, 1);		PGresult   *res2;		int			j;		/*		 * Array representation is {1,2,3} ... convert to (1,2,3)		 */		if (strlen(grolist) < 3)			continue;		grolist = strdup(grolist);		grolist[0] = '(';		grolist[strlen(grolist) - 1] = ')';		printfPQExpBuffer(buf,						  "SELECT usename FROM pg_shadow "						  "WHERE usesysid IN %s ORDER BY 1",						  grolist);		free(grolist);		res2 = executeQuery(conn, buf->data);		for (j = 0; j < PQntuples(res2); j++)		{			char	   *usename = PQgetvalue(res2, j, 0);			/*			 * Don't try to grant a role to itself; can happen if old			 * installation has identically named user and group.			 */			if (strcmp(groname, usename) == 0)				continue;			printf("GRANT %s", fmtId(groname));			printf(" TO %s;\n", fmtId(usename));		}		PQclear(res2);	}	PQclear(res);	destroyPQExpBuffer(buf);	printf("\n\n");}/* * Dump tablespaces. */static voiddumpTablespaces(PGconn *conn){	PGresult   *res;	int			i;	/*	 * Get all tablespaces except built-in ones (which we assume are named	 * pg_xxx)	 */	res = executeQuery(conn, "SELECT spcname, "					   "pg_catalog.pg_get_userbyid(spcowner) AS spcowner, "					   "spclocation, spcacl "					   "FROM pg_catalog.pg_tablespace "					   "WHERE spcname !~ '^pg_' "					   "ORDER BY 1");	if (PQntuples(res) > 0)		printf("--\n-- Tablespaces\n--\n\n");	for (i = 0; i < PQntuples(res); i++)	{		PQExpBuffer buf = createPQExpBuffer();		char	   *spcname = PQgetvalue(res, i, 0);		char	   *spcowner = PQgetvalue(res, i, 1);		char	   *spclocation = PQgetvalue(res, i, 2);		char	   *spcacl = PQgetvalue(res, i, 3);		char	   *fspcname;		/* needed for buildACLCommands() */		fspcname = strdup(fmtId(spcname));		if (output_clean)			appendPQExpBuffer(buf, "DROP TABLESPACE %s;\n", fspcname);		appendPQExpBuffer(buf, "CREATE TABLESPACE %s", fspcname);		appendPQExpBuffer(buf, " OWNER %s", fmtId(spcowner));		appendPQExpBuffer(buf, " LOCATION ");		appendStringLiteral(buf, spclocation, true);		appendPQExpBuffer(buf, ";\n");		if (!skip_acls &&			!buildACLCommands(fspcname, "TABLESPACE", spcacl, spcowner,							  server_version, buf))		{			fprintf(stderr, _("%s: could not parse ACL list (%s) for tablespace \"%s\"\n"),					progname, spcacl, fspcname);			PQfinish(conn);			exit(1);		}		printf("%s", buf->data);		free(fspcname);		destroyPQExpBuffer(buf);	}	PQclear(res);	printf("\n\n");}/* * Dump commands to create each database. * * To minimize the number of reconnections (and possibly ensuing * password prompts) required by the output script, we emit all CREATE * DATABASE commands during the initial phase of the script, and then * run pg_dump for each database to dump the contents of that * database.  We skip databases marked not datallowconn, since we'd be * unable to connect to them anyway (and besides, we don't want to * dump template0). */static voiddumpCreateDB(PGconn *conn){	PQExpBuffer buf = createPQExpBuffer();	PGresult   *res;	int			i;	printf("--\n-- Database creation\n--\n\n");	if (server_version >= 80100)		res = executeQuery(conn,						   "SELECT datname, "						   "coalesce(rolname, (select rolname from pg_authid where oid=(select datdba from pg_database where datname='template0'))), "						   "pg_encoding_to_char(d.encoding), "						   "datistemplate, datacl, datconnlimit, "						   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = d.dattablespace) AS dattablespace "			  "FROM pg_database d LEFT JOIN pg_authid u ON (datdba = u.oid) "						   "WHERE datallowconn ORDER BY 1");	else if (server_version >= 80000)		res = executeQuery(conn,						   "SELECT datname, "						   "coalesce(usename, (select usename from pg_shadow where usesysid=(select datdba from pg_database where datname='template0'))), "						   "pg_encoding_to_char(d.encoding), "						   "datistemplate, datacl, -1 as datconnlimit, "						   "(SELECT spcname FROM pg_tablespace t WHERE t.oid = d.dattablespace) AS dattablespace "		   "FROM pg_database d LEFT JOIN pg_shadow u ON (datdba = usesysid) "						   "WHERE datallowconn ORDER BY 1");	else if (server_version >= 70300)		res = executeQuery(conn,						   "SELECT datname, "						   "coalesce(usename, (select usename from pg_shadow where usesysid=(select datdba from pg_database where datname='template0'))), "						   "pg_encoding_to_char(d.encoding), "						   "datistemplate, datacl, -1 as datconnlimit, "						   "'pg_default' AS dattablespace "		   "FROM pg_database d LEFT JOIN pg_shadow u ON (datdba = usesysid) "						   "WHERE datallowconn ORDER BY 1");	else if (server_version >= 70100)		res = executeQuery(conn,						   "SELECT datname, "						   "coalesce("					"(select usename from pg_shadow where usesysid=datdba), "						   "(select usename from pg_shadow where usesysid=(select datdba from pg_database where datname='template0'))), "						   "pg_encoding_to_char(d.encoding), "						   "datistemplate, '' as datacl, -1 as datconnlimit, "						   "'pg_default' AS dattablespace "						   "FROM pg_database d "						   "WHERE datallowconn ORDER BY 1");	else	{		/*		 * Note: 7.0 fails to cope with sub-select in COALESCE, so just deal		 * with getting a NULL by not printing any OWNER clause.		 */		res = executeQuery(conn,						   "SELECT datname, "					"(select usename from pg_shadow where usesysid=datdba), "						   "pg_encoding_to_char(d.encoding), "						   "'f' as datistemplate, "						   "'' as datacl, -1 as datconnlimit, "						   "'pg_default' AS dattablespace "						   "FROM pg_database d "						   "ORDER BY 1");	}	for (i = 0; i < PQntuples(res); i++)	{		char	   *dbname = PQgetvalue(res, i, 0);		char	   *dbowner = PQgetvalue(res, i, 1);		char	   *dbencoding = PQgetvalue(res, i, 2);		char	   *dbistemplate = PQgetvalue(res, i, 3);		char	   *dbacl = PQgetvalue(res, i, 4);		char	   *dbconnlimit = PQgetvalue(res, i, 5);		char	   *dbtablespace = PQgetvalue(res, i, 6);		char	   *fdbname;		fdbname = strdup(fmtId(dbname));		resetPQExpBuffer(buf);		/*		 * Skip the CREATE DATABASE commands for "template1" and "postgres",		 * since they are presumably already there in the destination cluster.		 * We do want to emit their ACLs and config options if any, however.		 */		if (strcmp(dbname, "template1") != 0 &&			strcmp(dbname, "postgres") != 0)		{			if (output_clean)				appendPQExpBuffer(buf, "DROP DATABASE %s;\n", fdbname);			appendPQExpBuffer(buf, "CREATE DATABASE %s", fdbname);			appendPQExpBuffer(buf, " WITH TEMPLATE = template0");			if (strlen(dbowner) != 0)				appendPQExpBuffer(buf, " OWNER = %s", fmtId(dbowner));			appendPQExpBuffer(buf, " ENCODING = ");			appendStringLiteral(buf, dbencoding, true);			/* Output tablespace if it isn't default */			if (strcmp(dbtablespace, "pg_default") != 0)				appendPQExpBuffer(buf, " TABLESPACE = %s",								  fmtId(dbtablespace));			if (strcmp(dbconnlimit, "-1") != 0)				appendPQExpBuffer(buf, " CONNECTION LIMIT = %s",								  dbconnlimit);			appendPQExpBuffer(buf, ";\n");			if (strcmp(dbistemplate, "t") == 0)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -