diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2008-09-01 22:30:40 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2008-09-01 22:30:40 +0000 |
commit | 6c5cf22edd519cefefa335f46199005b45f404d7 (patch) | |
tree | ab51449487173416c2d4ffe078d0064bf19ed937 /src | |
parent | db8e4c7028909c2420568883a931438e5dcddab5 (diff) | |
download | postgresql-6c5cf22edd519cefefa335f46199005b45f404d7.tar.gz postgresql-6c5cf22edd519cefefa335f46199005b45f404d7.zip |
Fix plpgsql's exec_move_row() to supply valid type OIDs to exec_assign_value()
whenever possible, as per bug report from Oleg Serov. While at it, reorder
the operations in the RECORD case to avoid possible palloc failure while the
variable update is only partly complete.
Back-patch as far as 8.1. Although the code of the particular function is
similar in 8.0, 8.0's support for composite fields in rows is sufficiently
broken elsewhere that it doesn't seem worth fixing this.
Diffstat (limited to 'src')
-rw-r--r-- | src/pl/plpgsql/src/pl_exec.c | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c index 976246f6748..6c9062f9888 100644 --- a/src/pl/plpgsql/src/pl_exec.c +++ b/src/pl/plpgsql/src/pl_exec.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.202 2008/01/01 19:46:00 momjian Exp $ + * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.202.2.1 2008/09/01 22:30:40 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -4296,13 +4296,27 @@ exec_move_row(PLpgSQL_execstate *estate, if (rec != NULL) { /* - * copy input first, just in case it is pointing at variable's value + * Copy input first, just in case it is pointing at variable's value */ if (HeapTupleIsValid(tup)) tup = heap_copytuple(tup); + else if (tupdesc) + { + /* If we have a tupdesc but no data, form an all-nulls tuple */ + char *nulls; + + nulls = (char *) palloc(tupdesc->natts * sizeof(char)); + memset(nulls, 'n', tupdesc->natts * sizeof(char)); + + tup = heap_formtuple(tupdesc, NULL, nulls); + + pfree(nulls); + } + if (tupdesc) tupdesc = CreateTupleDescCopy(tupdesc); + /* Free the old value ... */ if (rec->freetup) { heap_freetuple(rec->tup); @@ -4314,24 +4328,12 @@ exec_move_row(PLpgSQL_execstate *estate, rec->freetupdesc = false; } + /* ... and install the new */ if (HeapTupleIsValid(tup)) { rec->tup = tup; rec->freetup = true; } - else if (tupdesc) - { - /* If we have a tupdesc but no data, form an all-nulls tuple */ - char *nulls; - - nulls = (char *) palloc(tupdesc->natts * sizeof(char)); - memset(nulls, 'n', tupdesc->natts * sizeof(char)); - - rec->tup = heap_formtuple(tupdesc, NULL, nulls); - rec->freetup = true; - - pfree(nulls); - } else rec->tup = NULL; @@ -4363,6 +4365,7 @@ exec_move_row(PLpgSQL_execstate *estate, */ if (row != NULL) { + int td_natts = tupdesc ? tupdesc->natts : 0; int t_natts; int fnum; int anum; @@ -4385,12 +4388,18 @@ exec_move_row(PLpgSQL_execstate *estate, var = (PLpgSQL_var *) (estate->datums[row->varnos[fnum]]); - while (anum < t_natts && tupdesc->attrs[anum]->attisdropped) + while (anum < td_natts && tupdesc->attrs[anum]->attisdropped) anum++; /* skip dropped column in tuple */ - if (anum < t_natts) + if (anum < td_natts) { - value = SPI_getbinval(tup, tupdesc, anum + 1, &isnull); + if (anum < t_natts) + value = SPI_getbinval(tup, tupdesc, anum + 1, &isnull); + else + { + value = (Datum) 0; + isnull = true; + } valtype = SPI_gettypeid(tupdesc, anum + 1); anum++; } |