Skip WAL in ALTER TABLE

Started by Itagaki Takahiroabout 16 years ago5 messages
#1Itagaki Takahiro
itagaki.takahiro@oss.ntt.co.jp

We can skip writing WAL during COPY and CLUSTER if archive_mode is off,
but we don't use the skipping during tables rewrites in ALTER TABLE.
Also we don't use BulkInsertState there.

Is it possible to use WAL-skipping and BulkInsertState in ATRewriteTable() ?
If ok, I'll submit a patch for the next commitfest.

Regards,
---
ITAGAKI Takahiro
NTT Open Source Software Center

#2Simon Riggs
simon@2ndQuadrant.com
In reply to: Itagaki Takahiro (#1)
Re: Skip WAL in ALTER TABLE

On Tue, 2009-10-13 at 11:39 +0900, Itagaki Takahiro wrote:

We can skip writing WAL during COPY and CLUSTER if archive_mode is off,
but we don't use the skipping during tables rewrites in ALTER TABLE.
Also we don't use BulkInsertState there.

Is it possible to use WAL-skipping and BulkInsertState in ATRewriteTable() ?
If ok, I'll submit a patch for the next commitfest.

Yes

--
Simon Riggs www.2ndQuadrant.com

#3Itagaki Takahiro
itagaki.takahiro@oss.ntt.co.jp
In reply to: Simon Riggs (#2)
1 attachment(s)
Re: Skip WAL in ALTER TABLE

Simon Riggs <simon@2ndQuadrant.com> wrote:

Is it possible to use WAL-skipping and BulkInsertState in ATRewriteTable() ?
If ok, I'll submit a patch for the next commitfest.

Yes

Patch attached.
This patch skip WAL writes during table rewrites from ALTER TABLE.

Regards,
---
ITAGAKI Takahiro
NTT Open Source Software Center

Attachments:

nowal-on-alter-table_20091015.patchapplication/octet-stream; name=nowal-on-alter-table_20091015.patchDownload
diff -cprN head/src/backend/commands/tablecmds.c work/src/backend/commands/tablecmds.c
*** head/src/backend/commands/tablecmds.c	2009-10-15 10:26:48.573966000 +0900
--- work/src/backend/commands/tablecmds.c	2009-10-15 13:06:11.271226497 +0900
*************** ATRewriteTable(AlteredTableInfo *tab, Oi
*** 3037,3042 ****
--- 3037,3043 ----
  	int			i;
  	ListCell   *l;
  	EState	   *estate;
+ 	int			hi_options = HEAP_INSERT_SKIP_FSM;
  
  	/*
  	 * Open the relation(s).  We have surely already locked the existing
*************** ATRewriteTable(AlteredTableInfo *tab, Oi
*** 3129,3134 ****
--- 3130,3137 ----
  		MemoryContext oldCxt;
  		List	   *dropped_attrs = NIL;
  		ListCell   *lc;
+ 		CommandId	mycid = GetCurrentCommandId(true);
+ 		BulkInsertState bistate;
  
  		econtext = GetPerTupleExprContext(estate);
  
*************** ATRewriteTable(AlteredTableInfo *tab, Oi
*** 3158,3163 ****
--- 3161,3171 ----
  				dropped_attrs = lappend_int(dropped_attrs, i);
  		}
  
+ 		if (!XLogArchivingActive() || newrel->rd_istemp)
+ 			hi_options |= HEAP_INSERT_SKIP_WAL;
+ 
+ 		bistate = GetBulkInsertState();
+ 
  		/*
  		 * Scan through the rows, generating a new row if needed and then
  		 * checking all the constraints.
*************** ATRewriteTable(AlteredTableInfo *tab, Oi
*** 3252,3258 ****
  
  			/* Write the tuple out to the new relation */
  			if (newrel)
! 				simple_heap_insert(newrel, tuple);
  
  			ResetExprContext(econtext);
  
--- 3260,3266 ----
  
  			/* Write the tuple out to the new relation */
  			if (newrel)
! 				heap_insert(newrel, tuple, mycid, hi_options, bistate);
  
  			ResetExprContext(econtext);
  
*************** ATRewriteTable(AlteredTableInfo *tab, Oi
*** 3262,3267 ****
--- 3270,3277 ----
  		MemoryContextSwitchTo(oldCxt);
  		heap_endscan(scan);
  
+ 		FreeBulkInsertState(bistate);
+ 
  		ExecDropSingleTupleTableSlot(oldslot);
  		ExecDropSingleTupleTableSlot(newslot);
  	}
*************** ATRewriteTable(AlteredTableInfo *tab, Oi
*** 3270,3276 ****
--- 3280,3291 ----
  
  	heap_close(oldrel, NoLock);
  	if (newrel)
+ 	{
+ 		/* If we skipped writing WAL, then we need to sync the heap. */
+ 		if (hi_options & HEAP_INSERT_SKIP_WAL)
+ 			heap_sync(newrel);
  		heap_close(newrel, NoLock);
+ 	}
  }
  
  /*
#4Simon Riggs
simon@2ndQuadrant.com
In reply to: Itagaki Takahiro (#3)
Re: Skip WAL in ALTER TABLE

On Thu, 2009-10-15 at 13:18 +0900, Itagaki Takahiro wrote:

Simon Riggs <simon@2ndQuadrant.com> wrote:

Is it possible to use WAL-skipping and BulkInsertState in ATRewriteTable() ?
If ok, I'll submit a patch for the next commitfest.

Yes

Patch attached.
This patch skip WAL writes during table rewrites from ALTER TABLE.

Looks fine to me, apart from

if (!XLogArchivingActive() || newrel->rd_istemp)
hi_options |= HEAP_INSERT_SKIP_WAL;

I think the second condition is unnecessary, so just

if (!XLogArchivingActive())
hi_options |= HEAP_INSERT_SKIP_WAL;

which is what COPY does. Temp tables are excluded in heap_insert()

--
Simon Riggs www.2ndQuadrant.com

#5Heikki Linnakangas
heikki.linnakangas@enterprisedb.com
In reply to: Simon Riggs (#4)
Re: Skip WAL in ALTER TABLE

Simon Riggs wrote:

On Thu, 2009-10-15 at 13:18 +0900, Itagaki Takahiro wrote:

Simon Riggs <simon@2ndQuadrant.com> wrote:

Is it possible to use WAL-skipping and BulkInsertState in ATRewriteTable() ?
If ok, I'll submit a patch for the next commitfest.

Yes

Patch attached.
This patch skip WAL writes during table rewrites from ALTER TABLE.

Looks fine to me, apart from

if (!XLogArchivingActive() || newrel->rd_istemp)
hi_options |= HEAP_INSERT_SKIP_WAL;

I think the second condition is unnecessary, so just

if (!XLogArchivingActive())
hi_options |= HEAP_INSERT_SKIP_WAL;

which is what COPY does. Temp tables are excluded in heap_insert()

Yep.

Committed with the above and some other small changes. I moved the
initialization of BulkInsertState and hi_options outside the if-block.
Feels clearer this way, and they're only needed if newrel==true, not if
only needscan==true.

--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com