Attached is a patch that avoids a needless copy of the result tuple in
nodeMaterial, in the case that we don't have a previously-materialized
tuple to return. We can just return the TTS produced by executing our
child node, rather than returning a copy of it.

I didn't bother pulling the MinimalTuple out of "outerslot" and stuffing
it back into the nodeMaterial's result slot, as AFAICS that is not
necessary. Although I suppose you could make a cleanliness argument that
that would be worth doing instead.

(This is 8.4 material...)

-Neil

Index: source/src/backend/executor/nodeMaterial.c
===================================================================
RCS file: /home/neilc/postgres/cvs_root/pgsql/src/backend/executor/nodeMaterial.c,v
retrieving revision 1.59
diff -p -c -r1.59 nodeMaterial.c
*** source/src/backend/executor/nodeMaterial.c	21 May 2007 17:57:33 -0000	1.59
--- source/src/backend/executor/nodeMaterial.c	16 Oct 2007 04:00:46 -0000
*************** ExecMaterial(MaterialState *node)
*** 98,103 ****
--- 98,105 ----
  			eof_tuplestore = true;
  	}
  
+ 	ExecClearTuple(slot);
+ 
  	/*
  	 * If necessary, try to fetch another row from the subplan.
  	 *
*************** ExecMaterial(MaterialState *node)
*** 124,147 ****
  		}
  
  		/*
! 		 * Append returned tuple to tuplestore.  NOTE: because the tuplestore
! 		 * is certainly in EOF state, its read position will move forward over
! 		 * the added tuple.  This is what we want.
  		 */
  		if (tuplestorestate)
  			tuplestore_puttupleslot(tuplestorestate, outerslot);
  
! 		/*
! 		 * And return a copy of the tuple.	(XXX couldn't we just return the
! 		 * outerslot?)
! 		 */
! 		return ExecCopySlot(slot, outerslot);
  	}
  
! 	/*
! 	 * Nothing left ...
! 	 */
! 	return ExecClearTuple(slot);
  }
  
  /* ----------------------------------------------------------------
--- 126,143 ----
  		}
  
  		/*
! 		 * Append a copy of the returned tuple to tuplestore.  NOTE: because
! 		 * the tuplestore is certainly in EOF state, its read position will
! 		 * move forward over the added tuple.  This is what we want.
  		 */
  		if (tuplestorestate)
  			tuplestore_puttupleslot(tuplestorestate, outerslot);
  
! 		return outerslot;
  	}
  
! 	/* Nothing left, return empty slot */
! 	return slot;
  }
  
  /* ----------------------------------------------------------------
---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

                http://www.postgresql.org/about/donate

Reply via email to