mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Replace uses of SPI_modifytuple that intend to allocate in current context.
Invent a new function heap_modify_tuple_by_cols() that is functionally equivalent to SPI_modifytuple except that it always allocates its result by simple palloc. I chose however to make the API details a bit more like heap_modify_tuple: pass a tupdesc rather than a Relation, and use bool convention for the isnull array. Use this function in place of SPI_modifytuple at all call sites where the intended behavior is to allocate in current context. (There actually are only two call sites left that depend on the old behavior, which makes me wonder if we should just drop this function rather than keep it.) This new function is easier to use than heap_modify_tuple() for purposes of replacing a single column (or, really, any fixed number of columns). There are a number of places where it would simplify the code to change over, but I resisted that temptation for the moment ... everywhere except in plpgsql's exec_assign_value(); changing that might offer some small performance benefit, so I did it. This is on the way to removing SPI_push/SPI_pop, but it seems like good code cleanup in its own right. Discussion: <9633.1478552022@sss.pgh.pa.us>
This commit is contained in:
@ -15,6 +15,7 @@ OH, me, I'm Terry Mackintosh <terry@terrym.com>
|
||||
*/
|
||||
#include "postgres.h"
|
||||
|
||||
#include "access/htup_details.h"
|
||||
#include "catalog/pg_type.h"
|
||||
#include "executor/spi.h"
|
||||
#include "commands/trigger.h"
|
||||
@ -34,6 +35,7 @@ moddatetime(PG_FUNCTION_ARGS)
|
||||
int attnum; /* positional number of field to change */
|
||||
Oid atttypid; /* type OID of field to change */
|
||||
Datum newdt; /* The current datetime. */
|
||||
bool newdtnull; /* null flag for it */
|
||||
char **args; /* arguments */
|
||||
char *relname; /* triggered relation name */
|
||||
Relation rel; /* triggered relation */
|
||||
@ -115,22 +117,13 @@ moddatetime(PG_FUNCTION_ARGS)
|
||||
args[0], relname)));
|
||||
newdt = (Datum) 0; /* keep compiler quiet */
|
||||
}
|
||||
newdtnull = false;
|
||||
|
||||
/* 1 is the number of items in the arrays attnum and newdt.
|
||||
attnum is the positional number of the field to be updated.
|
||||
newdt is the new datetime stamp.
|
||||
NOTE that attnum and newdt are not arrays, but then a 1 element array
|
||||
is not an array any more then they are. Thus, they can be considered a
|
||||
one element array.
|
||||
*/
|
||||
rettuple = SPI_modifytuple(rel, rettuple, 1, &attnum, &newdt, NULL);
|
||||
/* Replace the attnum'th column with newdt */
|
||||
rettuple = heap_modify_tuple_by_cols(rettuple, tupdesc,
|
||||
1, &attnum, &newdt, &newdtnull);
|
||||
|
||||
if (rettuple == NULL)
|
||||
/* internal error */
|
||||
elog(ERROR, "moddatetime (%s): %d returned by SPI_modifytuple",
|
||||
relname, SPI_result);
|
||||
|
||||
/* Clean up */
|
||||
/* Clean up */
|
||||
pfree(relname);
|
||||
|
||||
return PointerGetDatum(rettuple);
|
||||
|
Reference in New Issue
Block a user