Repair problems with the result of lookup_rowtype_tupdesc() possibly being
authorTom Lane <[email protected]>
Tue, 17 Jan 2006 17:33:23 +0000 (17:33 +0000)
committerTom Lane <[email protected]>
Tue, 17 Jan 2006 17:33:23 +0000 (17:33 +0000)
discarded by cache flush while still in use.  This is a minimal patch that
just copies the tupdesc anywhere it could be needed across a flush.  Applied
to back branches only; Neil Conway is working on a better long-term solution
for HEAD.

src/backend/access/heap/tuptoaster.c
src/backend/parser/parse_coerce.c
src/backend/parser/parse_target.c
src/backend/utils/adt/rowtypes.c
src/backend/utils/adt/ruleutils.c
src/backend/utils/cache/typcache.c
src/backend/utils/fmgr/funcapi.c
src/pl/plperl/plperl.c
src/pl/plpgsql/src/pl_exec.c
src/pl/plpython/plpython.c
src/pl/tcl/pltcl.c

index 5370f7c93eca5c77d78f47b51906f4415d52aad5..9cf5b3124e02826cdf8beece78facf907b260e80 100644 (file)
@@ -847,6 +847,7 @@ toast_flatten_tuple_attribute(Datum value,
        if (tupleDesc == NULL)
                return value;                   /* not a composite type */
 
+       tupleDesc = CreateTupleDescCopy(tupleDesc);
        att = tupleDesc->attrs;
        numAttrs = tupleDesc->natts;
 
@@ -893,7 +894,10 @@ toast_flatten_tuple_attribute(Datum value,
         * If nothing to untoast, just return the original tuple.
         */
        if (!need_change)
+       {
+               FreeTupleDesc(tupleDesc);
                return value;
+       }
 
        /*
         * Calculate the new size of the tuple.  Header size should not change,
@@ -930,6 +934,7 @@ toast_flatten_tuple_attribute(Datum value,
        for (i = 0; i < numAttrs; i++)
                if (toast_free[i])
                        pfree(DatumGetPointer(toast_values[i]));
+       FreeTupleDesc(tupleDesc);
 
        return PointerGetDatum(new_data);
 }
index 8666015f34551ff934dbaf4de337bd5c280011f2..aa530764f3d12830d0e26f83e05555b5986b38f9 100644 (file)
@@ -705,7 +705,7 @@ coerce_record_to_complex(ParseState *pstate, Node *node,
                                                format_type_be(RECORDOID),
                                                format_type_be(targetTypeId))));
 
-       tupdesc = lookup_rowtype_tupdesc(targetTypeId, -1);
+       tupdesc = CreateTupleDescCopy(lookup_rowtype_tupdesc(targetTypeId, -1));
        newargs = NIL;
        ucolno = 1;
        arg = list_head(args);
@@ -763,6 +763,8 @@ coerce_record_to_complex(ParseState *pstate, Node *node,
                                                format_type_be(targetTypeId)),
                                 errdetail("Input has too many columns.")));
 
+       FreeTupleDesc(tupdesc);
+
        rowexpr = makeNode(RowExpr);
        rowexpr->args = newargs;
        rowexpr->row_typeid = targetTypeId;
index 1431126e7b886a8175f1ef4706f6bee625c0f4c9..354ad63ec4bd8937ad6378c05827439b14af847b 100644 (file)
@@ -816,7 +816,8 @@ ExpandIndirectionStar(ParseState *pstate, A_Indirection *ind)
                ((Var *) expr)->vartype == RECORDOID)
                tupleDesc = expandRecordVariable(pstate, (Var *) expr, 0);
        else if (get_expr_result_type(expr, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
-               tupleDesc = lookup_rowtype_tupdesc(exprType(expr), exprTypmod(expr));
+               tupleDesc = CreateTupleDescCopy(lookup_rowtype_tupdesc(exprType(expr),
+                                                                                                                          exprTypmod(expr)));
        Assert(tupleDesc);
 
        /* Generate a list of references to the individual fields */
@@ -993,7 +994,8 @@ expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
         * appropriate error message while failing.
         */
        if (get_expr_result_type(expr, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
-               tupleDesc = lookup_rowtype_tupdesc(exprType(expr), exprTypmod(expr));
+               tupleDesc = CreateTupleDescCopy(lookup_rowtype_tupdesc(exprType(expr),
+                                                                                                                          exprTypmod(expr)));
 
        return tupleDesc;
 }
index 57b834dda57e5ed37cee6f108707612e5bbb6438..321e8e1545e4a0427448270fa3fd8865b01a3bba 100644 (file)
@@ -83,6 +83,7 @@ record_in(PG_FUNCTION_ARGS)
                   errmsg("input of anonymous composite types is not implemented")));
        tupTypmod = -1;                         /* for all non-anonymous types */
        tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
+       tupdesc = CreateTupleDescCopy(tupdesc);
        ncolumns = tupdesc->natts;
 
        /*
@@ -261,6 +262,7 @@ record_in(PG_FUNCTION_ARGS)
        pfree(buf.data);
        pfree(values);
        pfree(nulls);
+       FreeTupleDesc(tupdesc);
 
        PG_RETURN_HEAPTUPLEHEADER(result);
 }
@@ -288,6 +290,7 @@ record_out(PG_FUNCTION_ARGS)
        tupType = HeapTupleHeaderGetTypeId(rec);
        tupTypmod = HeapTupleHeaderGetTypMod(rec);
        tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
+       tupdesc = CreateTupleDescCopy(tupdesc);
        ncolumns = tupdesc->natts;
 
        /* Build a temporary HeapTuple control structure */
@@ -409,6 +412,7 @@ record_out(PG_FUNCTION_ARGS)
 
        pfree(values);
        pfree(nulls);
+       FreeTupleDesc(tupdesc);
 
        PG_RETURN_CSTRING(buf.data);
 }
@@ -449,6 +453,7 @@ record_recv(PG_FUNCTION_ARGS)
                   errmsg("input of anonymous composite types is not implemented")));
        tupTypmod = -1;                         /* for all non-anonymous types */
        tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
+       tupdesc = CreateTupleDescCopy(tupdesc);
        ncolumns = tupdesc->natts;
 
        /*
@@ -597,6 +602,7 @@ record_recv(PG_FUNCTION_ARGS)
        heap_freetuple(tuple);
        pfree(values);
        pfree(nulls);
+       FreeTupleDesc(tupdesc);
 
        PG_RETURN_HEAPTUPLEHEADER(result);
 }
@@ -624,6 +630,7 @@ record_send(PG_FUNCTION_ARGS)
        tupType = HeapTupleHeaderGetTypeId(rec);
        tupTypmod = HeapTupleHeaderGetTypMod(rec);
        tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
+       tupdesc = CreateTupleDescCopy(tupdesc);
        ncolumns = tupdesc->natts;
 
        /* Build a temporary HeapTuple control structure */
@@ -724,6 +731,7 @@ record_send(PG_FUNCTION_ARGS)
 
        pfree(values);
        pfree(nulls);
+       FreeTupleDesc(tupdesc);
 
        PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
 }
index a36ab3ea6f0ef84c101358749bbe3234a72f8cd4..37fa6e7a3d56757ab1249270b1054369010d0381 100644 (file)
@@ -2699,7 +2699,7 @@ get_name_for_var_field(Var *var, int fieldno,
 
        /* Got the tupdesc, so we can extract the field name */
        Assert(fieldno >= 1 && fieldno <= tupleDesc->natts);
-       return NameStr(tupleDesc->attrs[fieldno - 1]->attname);
+       return pstrdup(NameStr(tupleDesc->attrs[fieldno - 1]->attname));
 }
 
 
@@ -3493,6 +3493,7 @@ get_rule_expr(Node *node, deparse_context *context,
                                if (rowexpr->row_typeid != RECORDOID)
                                {
                                        tupdesc = lookup_rowtype_tupdesc(rowexpr->row_typeid, -1);
+                                       tupdesc = CreateTupleDescCopy(tupdesc);
                                        Assert(list_length(rowexpr->args) <= tupdesc->natts);
                                }
 
index 5dfcf26500806f4a18cb0c5a9a85f997cc6d4c54..ee239104db61c387ca38d8f2e416b6a3aac533a7 100644 (file)
@@ -377,6 +377,8 @@ lookup_default_opclass(Oid type_id, Oid am_id)
  *
  * Note: returned TupleDesc points to cached copy; caller must copy it
  * if intending to scribble on it or keep a reference for a long time.
+ * ("A long time" basically means "across any possible cache flush",
+ * which typically could occur at any relation open or catalog lookup.)
  */
 TupleDesc
 lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
index f27a7769dde02849bf3c0570faa9c63aaa2b8676..14d6cd6c8c9bb1e3481600fcbfaf5f79dac913df 100644 (file)
@@ -246,7 +246,7 @@ get_expr_result_type(Node *expr,
                        *resultTupleDesc = NULL;
                result = get_type_func_class(typid);
                if (result == TYPEFUNC_COMPOSITE && resultTupleDesc)
-                       *resultTupleDesc = lookup_rowtype_tupdesc(typid, -1);
+                       *resultTupleDesc = CreateTupleDescCopy(lookup_rowtype_tupdesc(typid, -1));
        }
 
        return result;
@@ -363,7 +363,7 @@ internal_get_result_type(Oid funcid,
        {
                case TYPEFUNC_COMPOSITE:
                        if (resultTupleDesc)
-                               *resultTupleDesc = lookup_rowtype_tupdesc(rettype, -1);
+                               *resultTupleDesc = CreateTupleDescCopy(lookup_rowtype_tupdesc(rettype, -1));
                        /* Named composite types can't have any polymorphic columns */
                        break;
                case TYPEFUNC_SCALAR:
index 94295b9d0ddf44077877f2496ce6c1c9249454b2..52a007a43cee65e88f4cab684ae3e237207d582a 100644 (file)
@@ -796,12 +796,14 @@ plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo)
                        tupType = HeapTupleHeaderGetTypeId(td);
                        tupTypmod = HeapTupleHeaderGetTypMod(td);
                        tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
+                       tupdesc = CreateTupleDescCopy(tupdesc);
                        /* Build a temporary HeapTuple control structure */
                        tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
                        tmptup.t_data = td;
 
                        hashref = plperl_hash_from_tuple(&tmptup, tupdesc);
                        XPUSHs(sv_2mortal(hashref));
+                       FreeTupleDesc(tupdesc);
                }
                else
                {
index 5d395c76d570abe4c6ebc31e4113f848ea1e904d..f327c5e186f9f32b5dff1242a9f4ae29f3173a07 100644 (file)
@@ -253,12 +253,14 @@ plpgsql_exec_function(PLpgSQL_function *func, FunctionCallInfo fcinfo)
                                                tupType = HeapTupleHeaderGetTypeId(td);
                                                tupTypmod = HeapTupleHeaderGetTypMod(td);
                                                tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
+                                               tupdesc = CreateTupleDescCopy(tupdesc);
                                                /* Build a temporary HeapTuple control structure */
                                                tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
                                                ItemPointerSetInvalid(&(tmptup.t_self));
                                                tmptup.t_tableOid = InvalidOid;
                                                tmptup.t_data = td;
                                                exec_move_row(&estate, NULL, row, &tmptup, tupdesc);
+                                               FreeTupleDesc(tupdesc);
                                        }
                                        else
                                        {
@@ -3110,12 +3112,14 @@ exec_assign_value(PLpgSQL_execstate *estate,
                                        tupType = HeapTupleHeaderGetTypeId(td);
                                        tupTypmod = HeapTupleHeaderGetTypMod(td);
                                        tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
+                                       tupdesc = CreateTupleDescCopy(tupdesc);
                                        /* Build a temporary HeapTuple control structure */
                                        tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
                                        ItemPointerSetInvalid(&(tmptup.t_self));
                                        tmptup.t_tableOid = InvalidOid;
                                        tmptup.t_data = td;
                                        exec_move_row(estate, NULL, row, &tmptup, tupdesc);
+                                       FreeTupleDesc(tupdesc);
                                }
                                break;
                        }
@@ -3152,12 +3156,14 @@ exec_assign_value(PLpgSQL_execstate *estate,
                                        tupType = HeapTupleHeaderGetTypeId(td);
                                        tupTypmod = HeapTupleHeaderGetTypMod(td);
                                        tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
+                                       tupdesc = CreateTupleDescCopy(tupdesc);
                                        /* Build a temporary HeapTuple control structure */
                                        tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
                                        ItemPointerSetInvalid(&(tmptup.t_self));
                                        tmptup.t_tableOid = InvalidOid;
                                        tmptup.t_data = td;
                                        exec_move_row(estate, rec, NULL, &tmptup, tupdesc);
+                                       FreeTupleDesc(tupdesc);
                                }
                                break;
                        }
index bffaa1e710c85276d04ad977a3026bdc995f409d..a82e44512349bb687ec1eabf4b3dd46fde23c9ec 100644 (file)
@@ -862,6 +862,7 @@ PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc)
                                        tupType = HeapTupleHeaderGetTypeId(td);
                                        tupTypmod = HeapTupleHeaderGetTypMod(td);
                                        tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
+                                       tupdesc = CreateTupleDescCopy(tupdesc);
 
                                        /* Set up I/O funcs if not done yet */
                                        if (proc->args[i].is_rowtype != 1)
@@ -872,6 +873,7 @@ PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure * proc)
                                        tmptup.t_data = td;
 
                                        arg = PLyDict_FromTuple(&(proc->args[i]), &tmptup, tupdesc);
+                                       FreeTupleDesc(tupdesc);
                                }
                        }
                        else
index b73598657ed843325395ec682e570e86d5b3054a..592cb6d69ad53e9dae058c3b3cdf963721fd69e8 100644 (file)
@@ -531,6 +531,7 @@ pltcl_func_handler(PG_FUNCTION_ARGS)
                                        tupType = HeapTupleHeaderGetTypeId(td);
                                        tupTypmod = HeapTupleHeaderGetTypMod(td);
                                        tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
+                                       tupdesc = CreateTupleDescCopy(tupdesc);
                                        /* Build a temporary HeapTuple control structure */
                                        tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
                                        tmptup.t_data = td;
@@ -539,6 +540,7 @@ pltcl_func_handler(PG_FUNCTION_ARGS)
                                        pltcl_build_tuple_argument(&tmptup, tupdesc, &list_tmp);
                                        Tcl_DStringAppendElement(&tcl_cmd,
                                                                                         Tcl_DStringValue(&list_tmp));
+                                       FreeTupleDesc(tupdesc);
                                }
                        }
                        else