Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7f3014d

Browse files
committed
Change plpgsql's cast cache to consider source typmod as significant.
I had thought that there was no need to maintain separate cache entries for different source typmods, but further experimentation shows that there is an advantage to doing so in some cases. In particular, if a domain has a typmod (say, "CREATE DOMAIN d AS numeric(20,0)"), failing to notice the source typmod leads to applying a length-coercion step even when the source has the correct typmod.
1 parent 45f2c2f commit 7f3014d

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/pl/plpgsql/src/pl_exec.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ typedef struct
5757
/* NB: we assume this struct contains no padding bytes */
5858
Oid srctype; /* source type for cast */
5959
Oid dsttype; /* destination type for cast */
60+
int32 srctypmod; /* source typmod for cast */
6061
int32 dsttypmod; /* destination typmod for cast */
6162
} plpgsql_CastHashKey;
6263

@@ -231,7 +232,7 @@ static Datum exec_cast_value(PLpgSQL_execstate *estate,
231232
Oid valtype, int32 valtypmod,
232233
Oid reqtype, int32 reqtypmod);
233234
static ExprState *get_cast_expression(PLpgSQL_execstate *estate,
234-
Oid srctype, Oid dsttype, int32 dsttypmod);
235+
Oid srctype, int32 srctypmod, Oid dsttype, int32 dsttypmod);
235236
static void exec_init_tuple_store(PLpgSQL_execstate *estate);
236237
static void exec_set_found(PLpgSQL_execstate *estate, bool state);
237238
static void plpgsql_create_econtext(PLpgSQL_execstate *estate);
@@ -5733,7 +5734,9 @@ exec_cast_value(PLpgSQL_execstate *estate,
57335734
{
57345735
ExprState *cast_expr;
57355736

5736-
cast_expr = get_cast_expression(estate, valtype, reqtype, reqtypmod);
5737+
cast_expr = get_cast_expression(estate,
5738+
valtype, valtypmod,
5739+
reqtype, reqtypmod);
57375740
if (cast_expr)
57385741
{
57395742
ExprContext *econtext = estate->eval_econtext;
@@ -5769,7 +5772,7 @@ exec_cast_value(PLpgSQL_execstate *estate,
57695772
*/
57705773
static ExprState *
57715774
get_cast_expression(PLpgSQL_execstate *estate,
5772-
Oid srctype, Oid dsttype, int32 dsttypmod)
5775+
Oid srctype, int32 srctypmod, Oid dsttype, int32 dsttypmod)
57735776
{
57745777
HTAB *cast_hash = estate->func->cast_hash;
57755778
plpgsql_CastHashKey cast_key;
@@ -5799,6 +5802,7 @@ get_cast_expression(PLpgSQL_execstate *estate,
57995802
/* Look for existing entry */
58005803
cast_key.srctype = srctype;
58015804
cast_key.dsttype = dsttype;
5805+
cast_key.srctypmod = srctypmod;
58025806
cast_key.dsttypmod = dsttypmod;
58035807
cast_entry = (plpgsql_CastHashEntry *) hash_search(cast_hash,
58045808
(void *) &cast_key,
@@ -5815,7 +5819,7 @@ get_cast_expression(PLpgSQL_execstate *estate,
58155819
*/
58165820
placeholder = makeNode(CaseTestExpr);
58175821
placeholder->typeId = srctype;
5818-
placeholder->typeMod = -1;
5822+
placeholder->typeMod = srctypmod;
58195823
placeholder->collation = get_typcollation(srctype);
58205824
if (OidIsValid(estate->func->fn_input_collation) &&
58215825
OidIsValid(placeholder->collation))

0 commit comments

Comments
 (0)