A few additional clever trick notes in @crowmagnumb answer :
- This function should always be qualified on the circuit , even if
pg_temp is in the search_path (for example, by default), according to Tom Lane to prevent trojan horses:
CREATE FUNCTION pg_temp. f_inc(int) RETURNS int AS 'SELECT $1 + 1' LANGUAGE sql IMMUTABLE; SELECT pg_temp. f_inc(42); f_inc
The function created in the timeline is visible only within one session (like temp tables). It is invisible to all other sessions (even for the same role). You can access the function as another role in the same session after SET ROLE .
You can even create a functional index based on this temp function:
CREATE INDEX foo_idx ON tbl (pg_temp.f_inc(id));
Thus, a simple index is created using a temporary function in a table other than temp. Such an index will be displayed for all sessions, but still valid only for the creation session. The query planner will not use a functional index in which the expression will not be repeated in the query. Still a bit of a dirty trick. It will be automatically discarded when the session is closed - as a dependent object. Such a feeling should not be allowed at all ...
If you just need to execute the function again, and all you need is SQL, consider the prepared statement . It acts as a temporary SQL function that dies at the end of a session. However, it is not the same thing, and it can only be used with EXECUTE , not nested in another query. Example:
PREPARE upd_tbl AS UPDATE tbl t SET set_name = $2 WHERE tbl_id = $1;
Call:
EXECUTE upd_tbl(123, 'foo_name');
Details:
- Split the given string and prepare the register
Erwin Brandstetter Jun 28 '15 at 2:04 2015-06-28 02:04
source share