tt2v1-binary-upgrade.patch

text/plain

Filename: tt2v1-binary-upgrade.patch
Type: text/plain
Part: 1
Message: Re: pg_dump --binary-upgrade vs. ALTER TYPE ... DROP ATTRIBUTE

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: unified
File+
src/bin/pg_dump/pg_dump.c 0 0
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 1ccdb4d..41ec645 100644
*** a/src/bin/pg_dump/pg_dump.c
--- b/src/bin/pg_dump/pg_dump.c
***************
*** 7957,7962 **** static void
--- 7957,7963 ----
  dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
  {
  	PQExpBuffer q = createPQExpBuffer();
+ 	PQExpBuffer dropped = createPQExpBuffer();
  	PQExpBuffer delq = createPQExpBuffer();
  	PQExpBuffer labelq = createPQExpBuffer();
  	PQExpBuffer query = createPQExpBuffer();
***************
*** 7964,7971 **** dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
--- 7965,7976 ----
  	int			ntups;
  	int			i_attname;
  	int			i_atttypdefn;
+ 	int			i_attisdropped;
+ 	int			i_attlen;
+ 	int			i_attalign;
  	int			i_typrelid;
  	int			i;
+ 	int			actual_atts;
  
  	/* Set proper schema search path so type references list correctly */
  	selectSourceSchema(tyinfo->dobj.namespace->dobj.name);
***************
*** 7975,7985 **** dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
  
  	appendPQExpBuffer(query, "SELECT a.attname, "
  			"pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
! 					  "typrelid "
  					  "FROM pg_catalog.pg_type t, pg_catalog.pg_attribute a "
  					  "WHERE t.oid = '%u'::pg_catalog.oid "
  					  "AND a.attrelid = t.typrelid "
- 					  "AND NOT a.attisdropped "
  					  "ORDER BY a.attnum ",
  					  tyinfo->dobj.catId.oid);
  
--- 7980,7989 ----
  
  	appendPQExpBuffer(query, "SELECT a.attname, "
  			"pg_catalog.format_type(a.atttypid, a.atttypmod) AS atttypdefn, "
! 					  "a.attisdropped, a.attlen, a.attalign, typrelid "
  					  "FROM pg_catalog.pg_type t, pg_catalog.pg_attribute a "
  					  "WHERE t.oid = '%u'::pg_catalog.oid "
  					  "AND a.attrelid = t.typrelid "
  					  "ORDER BY a.attnum ",
  					  tyinfo->dobj.catId.oid);
  
***************
*** 7990,7995 **** dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
--- 7994,8002 ----
  
  	i_attname = PQfnumber(res, "attname");
  	i_atttypdefn = PQfnumber(res, "atttypdefn");
+ 	i_attisdropped = PQfnumber(res, "attisdropped");
+ 	i_attlen = PQfnumber(res, "attlen");
+ 	i_attalign = PQfnumber(res, "attalign");
  	i_typrelid = PQfnumber(res, "typrelid");
  
  	if (binary_upgrade)
***************
*** 8003,8021 **** dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
  	appendPQExpBuffer(q, "CREATE TYPE %s AS (",
  					  fmtId(tyinfo->dobj.name));
  
  	for (i = 0; i < ntups; i++)
  	{
  		char	   *attname;
  		char	   *atttypdefn;
  
  		attname = PQgetvalue(res, i, i_attname);
  		atttypdefn = PQgetvalue(res, i, i_atttypdefn);
  
! 		appendPQExpBuffer(q, "\n\t%s %s", fmtId(attname), atttypdefn);
! 		if (i < ntups - 1)
  			appendPQExpBuffer(q, ",");
  	}
  	appendPQExpBuffer(q, "\n);\n");
  
  	/*
  	 * DROP must be fully qualified in case same name appears in pg_catalog
--- 8010,8064 ----
  	appendPQExpBuffer(q, "CREATE TYPE %s AS (",
  					  fmtId(tyinfo->dobj.name));
  
+ 	actual_atts = 0;
  	for (i = 0; i < ntups; i++)
  	{
  		char	   *attname;
  		char	   *atttypdefn;
+ 		bool		attisdropped;
+ 		char	   *attlen;
+ 		char	   *attalign;
  
  		attname = PQgetvalue(res, i, i_attname);
  		atttypdefn = PQgetvalue(res, i, i_atttypdefn);
+ 		attisdropped = (PQgetvalue(res, i, i_attisdropped)[0] == 't');
+ 		attlen = PQgetvalue(res, i, i_attlen);
+ 		attalign = PQgetvalue(res, i, i_attalign);
  
! 		if (attisdropped && !binary_upgrade)
! 			continue;
! 
! 		/* Format properly if not first attr */
! 		if (actual_atts++ > 0)
  			appendPQExpBuffer(q, ",");
+ 		appendPQExpBuffer(q, "\n\t");
+ 
+ 		if (!attisdropped)
+ 			appendPQExpBuffer(q, "%s %s", fmtId(attname), atttypdefn);
+ 		else	/* binary_upgrade - see under dumpTableSchema() */
+ 		{
+ 			appendPQExpBuffer(q, "%s INTEGER /* dummy */", fmtId(attname));
+ 
+ 			/* stash separately for insertion after the CREATE TYPE */
+ 			appendPQExpBuffer(dropped,
+ 							  "\n-- For binary upgrade, recreate dropped column.\n");
+ 			appendPQExpBuffer(dropped, "UPDATE pg_catalog.pg_attribute\n"
+ 							  "SET attlen = %s, "
+ 							  "attalign = '%s', attbyval = false\n"
+ 							  "WHERE attname = ", attlen, attalign);
+ 			appendStringLiteralAH(dropped, attname, fout);
+ 			appendPQExpBuffer(dropped, "\n  AND attrelid = ");
+ 			appendStringLiteralAH(dropped, fmtId(tyinfo->dobj.name), fout);
+ 			appendPQExpBuffer(dropped, "::pg_catalog.regclass;\n");
+ 
+ 			appendPQExpBuffer(dropped, "ALTER TYPE %s ",
+ 							  fmtId(tyinfo->dobj.name));
+ 			appendPQExpBuffer(dropped, "DROP ATTRIBUTE %s;\n",
+ 							  fmtId(attname));
+ 		}
  	}
  	appendPQExpBuffer(q, "\n);\n");
+ 	appendPQExpBufferStr(q, dropped->data);
  
  	/*
  	 * DROP must be fully qualified in case same name appears in pg_catalog
***************
*** 8051,8056 **** dumpCompositeType(Archive *fout, TypeInfo *tyinfo)
--- 8094,8100 ----
  
  	PQclear(res);
  	destroyPQExpBuffer(q);
+ 	destroyPQExpBuffer(dropped);
  	destroyPQExpBuffer(delq);
  	destroyPQExpBuffer(labelq);
  	destroyPQExpBuffer(query);
***************
*** 11987,11993 **** dumpTableSchema(Archive *fout, TableInfo *tbinfo)
  								"UNLOGGED " : "",
  						  reltypename,
  						  fmtId(tbinfo->dobj.name));
! 		if (tbinfo->reloftype)
  			appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
  		actual_atts = 0;
  		for (j = 0; j < tbinfo->numatts; j++)
--- 12031,12037 ----
  								"UNLOGGED " : "",
  						  reltypename,
  						  fmtId(tbinfo->dobj.name));
! 		if (tbinfo->reloftype && !binary_upgrade)
  			appendPQExpBuffer(q, " OF %s", tbinfo->reloftype);
  		actual_atts = 0;
  		for (j = 0; j < tbinfo->numatts; j++)
***************
*** 12015,12021 **** dumpTableSchema(Archive *fout, TableInfo *tbinfo)
  				bool		has_notnull = (tbinfo->notnull[j]
  							  && (!tbinfo->inhNotNull[j] || binary_upgrade));
  
! 				if (tbinfo->reloftype && !has_default && !has_notnull)
  					continue;
  
  				/* Format properly if not first attr */
--- 12059,12066 ----
  				bool		has_notnull = (tbinfo->notnull[j]
  							  && (!tbinfo->inhNotNull[j] || binary_upgrade));
  
! 				if (tbinfo->reloftype && !binary_upgrade &&
! 					!has_default && !has_notnull)
  					continue;
  
  				/* Format properly if not first attr */
***************
*** 12043,12049 **** dumpTableSchema(Archive *fout, TableInfo *tbinfo)
  				}
  
  				/* Attribute type */
! 				if (tbinfo->reloftype)
  				{
  					appendPQExpBuffer(q, "WITH OPTIONS");
  				}
--- 12088,12094 ----
  				}
  
  				/* Attribute type */
! 				if (tbinfo->reloftype && !binary_upgrade)
  				{
  					appendPQExpBuffer(q, "WITH OPTIONS");
  				}
***************
*** 12109,12115 **** dumpTableSchema(Archive *fout, TableInfo *tbinfo)
  
  		if (actual_atts)
  			appendPQExpBuffer(q, "\n)");
! 		else if (!tbinfo->reloftype)
  		{
  			/*
  			 * We must have a parenthesized attribute list, even though empty,
--- 12154,12160 ----
  
  		if (actual_atts)
  			appendPQExpBuffer(q, "\n)");
! 		else if (!(tbinfo->reloftype && !binary_upgrade))
  		{
  			/*
  			 * We must have a parenthesized attribute list, even though empty,
***************
*** 12251,12256 **** dumpTableSchema(Archive *fout, TableInfo *tbinfo)
--- 12296,12308 ----
  				}
  			}
  
+ 			if (tbinfo->reloftype)
+ 			{
+ 				appendPQExpBuffer(q, "\n-- For binary upgrade, set up typed tables this way.\n");
+ 				appendPQExpBuffer(q, "ALTER TABLE ONLY %s OF %s;\n",
+ 								  fmtId(tbinfo->dobj.name), tbinfo->reloftype);
+ 			}
+ 
  			appendPQExpBuffer(q, "\n-- For binary upgrade, set heap's relfrozenxid\n");
  			appendPQExpBuffer(q, "UPDATE pg_catalog.pg_class\n"
  							  "SET relfrozenxid = '%u'\n"