3
3
* procedural language
4
4
*
5
5
* IDENTIFICATION
6
- * $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.52.2.1 2002/03/25 07:41:21 tgl Exp $
6
+ * $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.52.2.2 2005/06/20 20:45:12 tgl Exp $
7
7
*
8
8
* This software is copyrighted by Jan Wieck - Hamburg.
9
9
*
@@ -2665,12 +2665,6 @@ exec_assign_value(PLpgSQL_execstate * estate,
2665
2665
*/
2666
2666
var = (PLpgSQL_var * ) target ;
2667
2667
2668
- if (var -> freeval )
2669
- {
2670
- pfree ((void * ) (var -> value ));
2671
- var -> freeval = false;
2672
- }
2673
-
2674
2668
newvalue = exec_cast_value (value , valtype , var -> datatype -> typoid ,
2675
2669
& (var -> datatype -> typinput ),
2676
2670
var -> datatype -> typelem ,
@@ -2690,23 +2684,28 @@ exec_assign_value(PLpgSQL_execstate * estate,
2690
2684
if (!var -> datatype -> typbyval && !* isNull )
2691
2685
{
2692
2686
if (newvalue == value )
2693
- {
2694
- int len ;
2687
+ newvalue = datumCopy (newvalue ,
2688
+ false,
2689
+ var -> datatype -> typlen );
2690
+ }
2695
2691
2696
- if (var -> datatype -> typlen < 0 )
2697
- len = VARSIZE (newvalue );
2698
- else
2699
- len = var -> datatype -> typlen ;
2700
- var -> value = (Datum ) palloc (len );
2701
- memcpy ((void * ) (var -> value ), (void * ) newvalue , len );
2702
- }
2703
- else
2704
- var -> value = newvalue ;
2705
- var -> freeval = true;
2692
+ /*
2693
+ * Now free the old value. (We can't do this any earlier
2694
+ * because of the possibility that we are assigning the
2695
+ * var's old value to it, eg "foo := foo". We could optimize
2696
+ * out the assignment altogether in such cases, but it's too
2697
+ * infrequent to be worth testing for.)
2698
+ */
2699
+ if (var -> freeval )
2700
+ {
2701
+ pfree (DatumGetPointer (var -> value ));
2702
+ var -> freeval = false;
2706
2703
}
2707
- else
2708
- var -> value = newvalue ;
2704
+
2705
+ var -> value = newvalue ;
2709
2706
var -> isnull = * isNull ;
2707
+ if (!var -> datatype -> typbyval && !* isNull )
2708
+ var -> freeval = true;
2710
2709
break ;
2711
2710
2712
2711
case PLPGSQL_DTYPE_RECFIELD :
@@ -3145,6 +3144,14 @@ exec_move_row(PLpgSQL_execstate * estate,
3145
3144
*/
3146
3145
if (rec != NULL )
3147
3146
{
3147
+ /*
3148
+ * copy input first, just in case it is pointing at variable's value
3149
+ */
3150
+ if (HeapTupleIsValid (tup ))
3151
+ tup = heap_copytuple (tup );
3152
+ if (tupdesc )
3153
+ tupdesc = CreateTupleDescCopy (tupdesc );
3154
+
3148
3155
if (rec -> freetup )
3149
3156
{
3150
3157
heap_freetuple (rec -> tup );
@@ -3158,8 +3165,8 @@ exec_move_row(PLpgSQL_execstate * estate,
3158
3165
3159
3166
if (HeapTupleIsValid (tup ))
3160
3167
{
3161
- rec -> tup = heap_copytuple ( tup ) ;
3162
- rec -> tupdesc = CreateTupleDescCopy ( tupdesc ) ;
3168
+ rec -> tup = tup ;
3169
+ rec -> tupdesc = tupdesc ;
3163
3170
rec -> freetup = true;
3164
3171
rec -> freetupdesc = true;
3165
3172
}
0 commit comments