On Thu, Jul 10, 2014 at 06:38:26PM -0400, Bruce Momjian wrote:
> On Thu, Jul 10, 2014 at 06:17:14PM -0400, Bruce Momjian wrote:
> > Well, we are going to need to call internal C functions, often bypassing
> > their typical call sites and the assumption about locking, etc.  Perhaps
> > this could be done from a plpgsql function.  We could add and drop a
> > dummy column to force TOAST table creation --- we would then only need a
> > way to detect if a function _needs_ a TOAST table, which was skipped in
> > binary upgrade mode previously.
> > 
> > That might be a minimalistic approach.
> 
> I have thought some more on this.  I thought I would need to open
> pg_class in C and do complex backend stuff, but I now realize I can do
> it from libpq, and just call ALTER TABLE and I think that always
> auto-checks if a TOAST table is needed.  All I have to do is query
> pg_class from libpq, then construct ALTER TABLE commands for each item,
> and it will optionally create the TOAST table if needed.  I just have to
> use a no-op ALTER TABLE command, like SET STATISTICS.
> 
> I am in Asia the next two weeks but will work on it after I return.

Attached is the backend part of the patch.  I will work on the
pg_upgrade/libpq/ALTER TABLE part later.

-- 
  Bruce Momjian  <br...@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + Everyone has their own god. +
diff --git a/src/backend/catalog/toasting.c b/src/backend/catalog/toasting.c
new file mode 100644
index bdfeb90..6e8ef36
*** a/src/backend/catalog/toasting.c
--- b/src/backend/catalog/toasting.c
*************** create_toast_table(Relation rel, Oid toa
*** 165,180 ****
  	if (rel->rd_rel->reltoastrelid != InvalidOid)
  		return false;
  
! 	/*
! 	 * Check to see whether the table actually needs a TOAST table.
! 	 *
! 	 * If an update-in-place toast relfilenode is specified, force toast file
! 	 * creation even if it seems not to need one.
! 	 */
! 	if (!needs_toast_table(rel) &&
! 		(!IsBinaryUpgrade ||
! 		 !OidIsValid(binary_upgrade_next_toast_pg_class_oid)))
! 		return false;
  
  	/*
  	 * If requested check lockmode is sufficient. This is a cross check in
--- 165,211 ----
  	if (rel->rd_rel->reltoastrelid != InvalidOid)
  		return false;
  
! 	if (!IsBinaryUpgrade)
! 	{
! 		if (!needs_toast_table(rel))
! 			return false;
! 	}
! 	else
! 	{
! 		/*
! 		 * Check to see whether the table needs a TOAST table.
! 		 *
! 		 * If an update-in-place TOAST relfilenode is specified, force TOAST file
! 		 * creation even if it seems not to need one.  This handles the case
! 		 * where the old cluster needed a TOAST table but the new cluster
! 		 * would not normally create one.
! 		 */
! 		 
! 		/*
! 		 * If a TOAST oid is not specified, skip TOAST creation as we will do
! 		 * it later so we don't create a TOAST table whose OID later conflicts
! 		 * with a user-supplied OID.  This handles cases where the old cluster
! 		 * didn't need a TOAST table, but the new cluster does.
! 		 */
! 		if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid))
! 			return false;
! 
! 		/*
! 		 * If a special TOAST value has been passed in, it means we are in
! 		 * cleanup mode --- we are creating needed TOAST tables after all user
! 		 * tables with specified OIDs have been created.  We let the system
! 		 * assign a TOAST oid for us.  The tables are empty so the missing
! 		 * TOAST tables were not a problem.
! 		 */
! 		if (binary_upgrade_next_toast_pg_class_oid == OPTIONALLY_CREATE_TOAST_OID)
! 		{
! 			/* clear it so it is no longer used */
! 			binary_upgrade_next_toast_pg_class_oid = InvalidOid;
! 
! 			if (!needs_toast_table(rel))
! 				return false;
! 		}
! 	}
  
  	/*
  	 * If requested check lockmode is sufficient. This is a cross check in
diff --git a/src/include/catalog/binary_upgrade.h b/src/include/catalog/binary_upgrade.h
new file mode 100644
index f39017c..7aff509
*** a/src/include/catalog/binary_upgrade.h
--- b/src/include/catalog/binary_upgrade.h
***************
*** 14,19 ****
--- 14,22 ----
  #ifndef BINARY_UPGRADE_H
  #define BINARY_UPGRADE_H
  
+ /* pick a OID that will never be normally used for TOAST table */
+ #define OPTIONALLY_CREATE_TOAST_OID	1
+ 
  extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_oid;
  extern PGDLLIMPORT Oid binary_upgrade_next_array_pg_type_oid;
  extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_type_oid;
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to