PostgreSQL Source Code  git master
plpgsql.h File Reference
#include "access/xact.h"
#include "commands/event_trigger.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "utils/expandedrecord.h"
#include "utils/typcache.h"
Include dependency graph for plpgsql.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PLpgSQL_type
 
struct  PLpgSQL_expr
 
struct  PLpgSQL_datum
 
struct  PLpgSQL_variable
 
struct  PLpgSQL_var
 
struct  PLpgSQL_row
 
struct  PLpgSQL_rec
 
struct  PLpgSQL_recfield
 
struct  PLpgSQL_nsitem
 
struct  PLpgSQL_stmt
 
struct  PLpgSQL_condition
 
struct  PLpgSQL_exception_block
 
struct  PLpgSQL_exception
 
struct  PLpgSQL_stmt_block
 
struct  PLpgSQL_stmt_assign
 
struct  PLpgSQL_stmt_perform
 
struct  PLpgSQL_stmt_call
 
struct  PLpgSQL_stmt_commit
 
struct  PLpgSQL_stmt_rollback
 
struct  PLpgSQL_diag_item
 
struct  PLpgSQL_stmt_getdiag
 
struct  PLpgSQL_stmt_if
 
struct  PLpgSQL_if_elsif
 
struct  PLpgSQL_stmt_case
 
struct  PLpgSQL_case_when
 
struct  PLpgSQL_stmt_loop
 
struct  PLpgSQL_stmt_while
 
struct  PLpgSQL_stmt_fori
 
struct  PLpgSQL_stmt_forq
 
struct  PLpgSQL_stmt_fors
 
struct  PLpgSQL_stmt_forc
 
struct  PLpgSQL_stmt_dynfors
 
struct  PLpgSQL_stmt_foreach_a
 
struct  PLpgSQL_stmt_open
 
struct  PLpgSQL_stmt_fetch
 
struct  PLpgSQL_stmt_close
 
struct  PLpgSQL_stmt_exit
 
struct  PLpgSQL_stmt_return
 
struct  PLpgSQL_stmt_return_next
 
struct  PLpgSQL_stmt_return_query
 
struct  PLpgSQL_stmt_raise
 
struct  PLpgSQL_raise_option
 
struct  PLpgSQL_stmt_assert
 
struct  PLpgSQL_stmt_execsql
 
struct  PLpgSQL_stmt_dynexecute
 
struct  PLpgSQL_func_hashkey
 
struct  PLpgSQL_function
 
struct  PLpgSQL_execstate
 
struct  PLpgSQL_plugin
 
struct  PLword
 
struct  PLcword
 
struct  PLwdatum
 

Macros

#define TEXTDOMAIN   PG_TEXTDOMAIN("plpgsql")
 
#define _(x)   dgettext(TEXTDOMAIN, x)
 
#define PLPGSQL_XCHECK_NONE   0
 
#define PLPGSQL_XCHECK_SHADOWVAR   (1 << 1)
 
#define PLPGSQL_XCHECK_TOOMANYROWS   (1 << 2)
 
#define PLPGSQL_XCHECK_STRICTMULTIASSIGNMENT   (1 << 3)
 
#define PLPGSQL_XCHECK_ALL   ((int) ~0)
 

Typedefs

typedef enum PLpgSQL_nsitem_type PLpgSQL_nsitem_type
 
typedef enum PLpgSQL_label_type PLpgSQL_label_type
 
typedef enum PLpgSQL_datum_type PLpgSQL_datum_type
 
typedef enum PLpgSQL_promise_type PLpgSQL_promise_type
 
typedef enum PLpgSQL_type_type PLpgSQL_type_type
 
typedef enum PLpgSQL_stmt_type PLpgSQL_stmt_type
 
typedef enum PLpgSQL_getdiag_kind PLpgSQL_getdiag_kind
 
typedef enum PLpgSQL_raise_option_type PLpgSQL_raise_option_type
 
typedef enum PLpgSQL_resolve_option PLpgSQL_resolve_option
 
typedef struct PLpgSQL_type PLpgSQL_type
 
typedef struct PLpgSQL_expr PLpgSQL_expr
 
typedef struct PLpgSQL_datum PLpgSQL_datum
 
typedef struct PLpgSQL_variable PLpgSQL_variable
 
typedef struct PLpgSQL_var PLpgSQL_var
 
typedef struct PLpgSQL_row PLpgSQL_row
 
typedef struct PLpgSQL_rec PLpgSQL_rec
 
typedef struct PLpgSQL_recfield PLpgSQL_recfield
 
typedef struct PLpgSQL_nsitem PLpgSQL_nsitem
 
typedef struct PLpgSQL_stmt PLpgSQL_stmt
 
typedef struct PLpgSQL_condition PLpgSQL_condition
 
typedef struct PLpgSQL_exception_block PLpgSQL_exception_block
 
typedef struct PLpgSQL_exception PLpgSQL_exception
 
typedef struct PLpgSQL_stmt_block PLpgSQL_stmt_block
 
typedef struct PLpgSQL_stmt_assign PLpgSQL_stmt_assign
 
typedef struct PLpgSQL_stmt_perform PLpgSQL_stmt_perform
 
typedef struct PLpgSQL_stmt_call PLpgSQL_stmt_call
 
typedef struct PLpgSQL_stmt_commit PLpgSQL_stmt_commit
 
typedef struct PLpgSQL_stmt_rollback PLpgSQL_stmt_rollback
 
typedef struct PLpgSQL_diag_item PLpgSQL_diag_item
 
typedef struct PLpgSQL_stmt_getdiag PLpgSQL_stmt_getdiag
 
typedef struct PLpgSQL_stmt_if PLpgSQL_stmt_if
 
typedef struct PLpgSQL_if_elsif PLpgSQL_if_elsif
 
typedef struct PLpgSQL_stmt_case PLpgSQL_stmt_case
 
typedef struct PLpgSQL_case_when PLpgSQL_case_when
 
typedef struct PLpgSQL_stmt_loop PLpgSQL_stmt_loop
 
typedef struct PLpgSQL_stmt_while PLpgSQL_stmt_while
 
typedef struct PLpgSQL_stmt_fori PLpgSQL_stmt_fori
 
typedef struct PLpgSQL_stmt_forq PLpgSQL_stmt_forq
 
typedef struct PLpgSQL_stmt_fors PLpgSQL_stmt_fors
 
typedef struct PLpgSQL_stmt_forc PLpgSQL_stmt_forc
 
typedef struct PLpgSQL_stmt_dynfors PLpgSQL_stmt_dynfors
 
typedef struct PLpgSQL_stmt_foreach_a PLpgSQL_stmt_foreach_a
 
typedef struct PLpgSQL_stmt_open PLpgSQL_stmt_open
 
typedef struct PLpgSQL_stmt_fetch PLpgSQL_stmt_fetch
 
typedef struct PLpgSQL_stmt_close PLpgSQL_stmt_close
 
typedef struct PLpgSQL_stmt_exit PLpgSQL_stmt_exit
 
typedef struct PLpgSQL_stmt_return PLpgSQL_stmt_return
 
typedef struct PLpgSQL_stmt_return_next PLpgSQL_stmt_return_next
 
typedef struct PLpgSQL_stmt_return_query PLpgSQL_stmt_return_query
 
typedef struct PLpgSQL_stmt_raise PLpgSQL_stmt_raise
 
typedef struct PLpgSQL_raise_option PLpgSQL_raise_option
 
typedef struct PLpgSQL_stmt_assert PLpgSQL_stmt_assert
 
typedef struct PLpgSQL_stmt_execsql PLpgSQL_stmt_execsql
 
typedef struct PLpgSQL_stmt_dynexecute PLpgSQL_stmt_dynexecute
 
typedef struct PLpgSQL_func_hashkey PLpgSQL_func_hashkey
 
typedef enum PLpgSQL_trigtype PLpgSQL_trigtype
 
typedef struct PLpgSQL_function PLpgSQL_function
 
typedef struct PLpgSQL_execstate PLpgSQL_execstate
 
typedef struct PLpgSQL_plugin PLpgSQL_plugin
 
typedef struct PLword PLword
 
typedef struct PLcword PLcword
 
typedef struct PLwdatum PLwdatum
 

Enumerations

enum  PLpgSQL_nsitem_type { PLPGSQL_NSTYPE_LABEL, PLPGSQL_NSTYPE_VAR, PLPGSQL_NSTYPE_REC }
 
enum  PLpgSQL_label_type { PLPGSQL_LABEL_BLOCK, PLPGSQL_LABEL_LOOP, PLPGSQL_LABEL_OTHER }
 
enum  PLpgSQL_datum_type {
  PLPGSQL_DTYPE_VAR, PLPGSQL_DTYPE_ROW, PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_RECFIELD,
  PLPGSQL_DTYPE_PROMISE
}
 
enum  PLpgSQL_promise_type {
  PLPGSQL_PROMISE_NONE = 0, PLPGSQL_PROMISE_TG_NAME, PLPGSQL_PROMISE_TG_WHEN, PLPGSQL_PROMISE_TG_LEVEL,
  PLPGSQL_PROMISE_TG_OP, PLPGSQL_PROMISE_TG_RELID, PLPGSQL_PROMISE_TG_TABLE_NAME, PLPGSQL_PROMISE_TG_TABLE_SCHEMA,
  PLPGSQL_PROMISE_TG_NARGS, PLPGSQL_PROMISE_TG_ARGV, PLPGSQL_PROMISE_TG_EVENT, PLPGSQL_PROMISE_TG_TAG
}
 
enum  PLpgSQL_type_type { PLPGSQL_TTYPE_SCALAR, PLPGSQL_TTYPE_REC, PLPGSQL_TTYPE_PSEUDO }
 
enum  PLpgSQL_stmt_type {
  PLPGSQL_STMT_BLOCK, PLPGSQL_STMT_ASSIGN, PLPGSQL_STMT_IF, PLPGSQL_STMT_CASE,
  PLPGSQL_STMT_LOOP, PLPGSQL_STMT_WHILE, PLPGSQL_STMT_FORI, PLPGSQL_STMT_FORS,
  PLPGSQL_STMT_FORC, PLPGSQL_STMT_FOREACH_A, PLPGSQL_STMT_EXIT, PLPGSQL_STMT_RETURN,
  PLPGSQL_STMT_RETURN_NEXT, PLPGSQL_STMT_RETURN_QUERY, PLPGSQL_STMT_RAISE, PLPGSQL_STMT_ASSERT,
  PLPGSQL_STMT_EXECSQL, PLPGSQL_STMT_DYNEXECUTE, PLPGSQL_STMT_DYNFORS, PLPGSQL_STMT_GETDIAG,
  PLPGSQL_STMT_OPEN, PLPGSQL_STMT_FETCH, PLPGSQL_STMT_CLOSE, PLPGSQL_STMT_PERFORM,
  PLPGSQL_STMT_CALL, PLPGSQL_STMT_COMMIT, PLPGSQL_STMT_ROLLBACK
}
 
enum  { PLPGSQL_RC_OK, PLPGSQL_RC_EXIT, PLPGSQL_RC_RETURN, PLPGSQL_RC_CONTINUE }
 
enum  PLpgSQL_getdiag_kind {
  PLPGSQL_GETDIAG_ROW_COUNT, PLPGSQL_GETDIAG_CONTEXT, PLPGSQL_GETDIAG_ERROR_CONTEXT, PLPGSQL_GETDIAG_ERROR_DETAIL,
  PLPGSQL_GETDIAG_ERROR_HINT, PLPGSQL_GETDIAG_RETURNED_SQLSTATE, PLPGSQL_GETDIAG_COLUMN_NAME, PLPGSQL_GETDIAG_CONSTRAINT_NAME,
  PLPGSQL_GETDIAG_DATATYPE_NAME, PLPGSQL_GETDIAG_MESSAGE_TEXT, PLPGSQL_GETDIAG_TABLE_NAME, PLPGSQL_GETDIAG_SCHEMA_NAME
}
 
enum  PLpgSQL_raise_option_type {
  PLPGSQL_RAISEOPTION_ERRCODE, PLPGSQL_RAISEOPTION_MESSAGE, PLPGSQL_RAISEOPTION_DETAIL, PLPGSQL_RAISEOPTION_HINT,
  PLPGSQL_RAISEOPTION_COLUMN, PLPGSQL_RAISEOPTION_CONSTRAINT, PLPGSQL_RAISEOPTION_DATATYPE, PLPGSQL_RAISEOPTION_TABLE,
  PLPGSQL_RAISEOPTION_SCHEMA
}
 
enum  PLpgSQL_resolve_option { PLPGSQL_RESOLVE_ERROR, PLPGSQL_RESOLVE_VARIABLE, PLPGSQL_RESOLVE_COLUMN }
 
enum  PLpgSQL_trigtype { PLPGSQL_DML_TRIGGER, PLPGSQL_EVENT_TRIGGER, PLPGSQL_NOT_TRIGGER }
 
enum  IdentifierLookup { IDENTIFIER_LOOKUP_NORMAL, IDENTIFIER_LOOKUP_DECLARE, IDENTIFIER_LOOKUP_EXPR }
 

Functions

PLpgSQL_functionplpgsql_compile (FunctionCallInfo fcinfo, bool forValidator)
 
PLpgSQL_functionplpgsql_compile_inline (char *proc_source)
 
void plpgsql_parser_setup (struct ParseState *pstate, PLpgSQL_expr *expr)
 
bool plpgsql_parse_word (char *word1, const char *yytxt, bool lookup, PLwdatum *wdatum, PLword *word)
 
bool plpgsql_parse_dblword (char *word1, char *word2, PLwdatum *wdatum, PLcword *cword)
 
bool plpgsql_parse_tripword (char *word1, char *word2, char *word3, PLwdatum *wdatum, PLcword *cword)
 
PLpgSQL_typeplpgsql_parse_wordtype (char *ident)
 
PLpgSQL_typeplpgsql_parse_cwordtype (List *idents)
 
PLpgSQL_typeplpgsql_parse_wordrowtype (char *ident)
 
PLpgSQL_typeplpgsql_parse_cwordrowtype (List *idents)
 
PLpgSQL_typeplpgsql_build_datatype (Oid typeOid, int32 typmod, Oid collation, TypeName *origtypname)
 
PLpgSQL_variableplpgsql_build_variable (const char *refname, int lineno, PLpgSQL_type *dtype, bool add2namespace)
 
PLpgSQL_recplpgsql_build_record (const char *refname, int lineno, PLpgSQL_type *dtype, Oid rectypeid, bool add2namespace)
 
PLpgSQL_recfieldplpgsql_build_recfield (PLpgSQL_rec *rec, const char *fldname)
 
int plpgsql_recognize_err_condition (const char *condname, bool allow_sqlstate)
 
PLpgSQL_conditionplpgsql_parse_err_condition (char *condname)
 
void plpgsql_adddatum (PLpgSQL_datum *newdatum)
 
int plpgsql_add_initdatums (int **varnos)
 
void plpgsql_HashTableInit (void)
 
void _PG_init (void)
 
Datum plpgsql_exec_function (PLpgSQL_function *func, FunctionCallInfo fcinfo, EState *simple_eval_estate, ResourceOwner simple_eval_resowner, ResourceOwner procedure_resowner, bool atomic)
 
HeapTuple plpgsql_exec_trigger (PLpgSQL_function *func, TriggerData *trigdata)
 
void plpgsql_exec_event_trigger (PLpgSQL_function *func, EventTriggerData *trigdata)
 
void plpgsql_xact_cb (XactEvent event, void *arg)
 
void plpgsql_subxact_cb (SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)
 
Oid plpgsql_exec_get_datum_type (PLpgSQL_execstate *estate, PLpgSQL_datum *datum)
 
void plpgsql_exec_get_datum_type_info (PLpgSQL_execstate *estate, PLpgSQL_datum *datum, Oid *typeId, int32 *typMod, Oid *collation)
 
void plpgsql_ns_init (void)
 
void plpgsql_ns_push (const char *label, PLpgSQL_label_type label_type)
 
void plpgsql_ns_pop (void)
 
PLpgSQL_nsitemplpgsql_ns_top (void)
 
void plpgsql_ns_additem (PLpgSQL_nsitem_type itemtype, int itemno, const char *name)
 
PLpgSQL_nsitemplpgsql_ns_lookup (PLpgSQL_nsitem *ns_cur, bool localmode, const char *name1, const char *name2, const char *name3, int *names_used)
 
PLpgSQL_nsitemplpgsql_ns_lookup_label (PLpgSQL_nsitem *ns_cur, const char *name)
 
PLpgSQL_nsitemplpgsql_ns_find_nearest_loop (PLpgSQL_nsitem *ns_cur)
 
const char * plpgsql_stmt_typename (PLpgSQL_stmt *stmt)
 
const char * plpgsql_getdiag_kindname (PLpgSQL_getdiag_kind kind)
 
void plpgsql_free_function_memory (PLpgSQL_function *func)
 
void plpgsql_dumptree (PLpgSQL_function *func)
 
int plpgsql_base_yylex (void)
 
int plpgsql_yylex (void)
 
void plpgsql_push_back_token (int token)
 
bool plpgsql_token_is_unreserved_keyword (int token)
 
void plpgsql_append_source_text (StringInfo buf, int startlocation, int endlocation)
 
int plpgsql_peek (void)
 
void plpgsql_peek2 (int *tok1_p, int *tok2_p, int *tok1_loc, int *tok2_loc)
 
int plpgsql_scanner_errposition (int location)
 
void plpgsql_yyerror (const char *message) pg_attribute_noreturn()
 
int plpgsql_location_to_lineno (int location)
 
int plpgsql_latest_lineno (void)
 
void plpgsql_scanner_init (const char *str)
 
void plpgsql_scanner_finish (void)
 
int plpgsql_yyparse (void)
 

Variables

IdentifierLookup plpgsql_IdentifierLookup
 
int plpgsql_variable_conflict
 
bool plpgsql_print_strict_params
 
bool plpgsql_check_asserts
 
int plpgsql_extra_warnings
 
int plpgsql_extra_errors
 
bool plpgsql_check_syntax
 
bool plpgsql_DumpExecTree
 
PLpgSQL_stmt_blockplpgsql_parse_result
 
int plpgsql_nDatums
 
PLpgSQL_datum ** plpgsql_Datums
 
char * plpgsql_error_funcname
 
PLpgSQL_functionplpgsql_curr_compile
 
MemoryContext plpgsql_compile_tmp_cxt
 
PLpgSQL_plugin ** plpgsql_plugin_ptr
 

Macro Definition Documentation

◆ _

#define _ (   x)    dgettext(TEXTDOMAIN, x)

Definition at line 36 of file plpgsql.h.

◆ PLPGSQL_XCHECK_ALL

#define PLPGSQL_XCHECK_ALL   ((int) ~0)

Definition at line 1188 of file plpgsql.h.

Referenced by plpgsql_extra_checks_check_hook().

◆ PLPGSQL_XCHECK_NONE

#define PLPGSQL_XCHECK_NONE   0

Definition at line 1184 of file plpgsql.h.

Referenced by plpgsql_extra_checks_check_hook().

◆ PLPGSQL_XCHECK_SHADOWVAR

#define PLPGSQL_XCHECK_SHADOWVAR   (1 << 1)

Definition at line 1185 of file plpgsql.h.

Referenced by plpgsql_extra_checks_check_hook().

◆ PLPGSQL_XCHECK_STRICTMULTIASSIGNMENT

#define PLPGSQL_XCHECK_STRICTMULTIASSIGNMENT   (1 << 3)

Definition at line 1187 of file plpgsql.h.

Referenced by exec_move_row_from_fields(), and plpgsql_extra_checks_check_hook().

◆ PLPGSQL_XCHECK_TOOMANYROWS

#define PLPGSQL_XCHECK_TOOMANYROWS   (1 << 2)

Definition at line 1186 of file plpgsql.h.

Referenced by exec_stmt_execsql(), and plpgsql_extra_checks_check_hook().

◆ TEXTDOMAIN

#define TEXTDOMAIN   PG_TEXTDOMAIN("plpgsql")

Definition at line 33 of file plpgsql.h.

Typedef Documentation

◆ PLcword

typedef struct PLcword PLcword

◆ PLpgSQL_case_when

◆ PLpgSQL_condition

◆ PLpgSQL_datum

typedef struct PLpgSQL_datum PLpgSQL_datum

◆ PLpgSQL_datum_type

◆ PLpgSQL_diag_item

◆ PLpgSQL_exception

◆ PLpgSQL_exception_block

◆ PLpgSQL_execstate

◆ PLpgSQL_expr

typedef struct PLpgSQL_expr PLpgSQL_expr

◆ PLpgSQL_func_hashkey

◆ PLpgSQL_function

◆ PLpgSQL_getdiag_kind

◆ PLpgSQL_if_elsif

◆ PLpgSQL_label_type

◆ PLpgSQL_nsitem

◆ PLpgSQL_nsitem_type

◆ PLpgSQL_plugin

◆ PLpgSQL_promise_type

◆ PLpgSQL_raise_option

◆ PLpgSQL_raise_option_type

◆ PLpgSQL_rec

typedef struct PLpgSQL_rec PLpgSQL_rec

◆ PLpgSQL_recfield

◆ PLpgSQL_resolve_option

◆ PLpgSQL_row

typedef struct PLpgSQL_row PLpgSQL_row

◆ PLpgSQL_stmt

typedef struct PLpgSQL_stmt PLpgSQL_stmt

◆ PLpgSQL_stmt_assert

◆ PLpgSQL_stmt_assign

◆ PLpgSQL_stmt_block

◆ PLpgSQL_stmt_call

◆ PLpgSQL_stmt_case

◆ PLpgSQL_stmt_close

◆ PLpgSQL_stmt_commit

◆ PLpgSQL_stmt_dynexecute

◆ PLpgSQL_stmt_dynfors

◆ PLpgSQL_stmt_execsql

◆ PLpgSQL_stmt_exit

◆ PLpgSQL_stmt_fetch

◆ PLpgSQL_stmt_forc

◆ PLpgSQL_stmt_foreach_a

◆ PLpgSQL_stmt_fori

◆ PLpgSQL_stmt_forq

◆ PLpgSQL_stmt_fors

◆ PLpgSQL_stmt_getdiag

◆ PLpgSQL_stmt_if

◆ PLpgSQL_stmt_loop

◆ PLpgSQL_stmt_open

◆ PLpgSQL_stmt_perform

◆ PLpgSQL_stmt_raise

◆ PLpgSQL_stmt_return

◆ PLpgSQL_stmt_return_next

◆ PLpgSQL_stmt_return_query

◆ PLpgSQL_stmt_rollback

◆ PLpgSQL_stmt_type

◆ PLpgSQL_stmt_while

◆ PLpgSQL_trigtype

◆ PLpgSQL_type

typedef struct PLpgSQL_type PLpgSQL_type

◆ PLpgSQL_type_type

◆ PLpgSQL_var

typedef struct PLpgSQL_var PLpgSQL_var

◆ PLpgSQL_variable

◆ PLwdatum

typedef struct PLwdatum PLwdatum

◆ PLword

typedef struct PLword PLword

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
PLPGSQL_RC_OK 
PLPGSQL_RC_EXIT 
PLPGSQL_RC_RETURN 
PLPGSQL_RC_CONTINUE 

Definition at line 136 of file plpgsql.h.

◆ IdentifierLookup

Enumerator
IDENTIFIER_LOOKUP_NORMAL 
IDENTIFIER_LOOKUP_DECLARE 
IDENTIFIER_LOOKUP_EXPR 

Definition at line 1168 of file plpgsql.h.

1169 {
1170  IDENTIFIER_LOOKUP_NORMAL, /* normal processing of var names */
1171  IDENTIFIER_LOOKUP_DECLARE, /* In DECLARE --- don't look up names */
1172  IDENTIFIER_LOOKUP_EXPR /* In SQL expression --- special case */
IdentifierLookup
Definition: plpgsql.h:1168

◆ PLpgSQL_datum_type

Enumerator
PLPGSQL_DTYPE_VAR 
PLPGSQL_DTYPE_ROW 
PLPGSQL_DTYPE_REC 
PLPGSQL_DTYPE_RECFIELD 
PLPGSQL_DTYPE_PROMISE 

Definition at line 61 of file plpgsql.h.

◆ PLpgSQL_getdiag_kind

Enumerator
PLPGSQL_GETDIAG_ROW_COUNT 
PLPGSQL_GETDIAG_CONTEXT 
PLPGSQL_GETDIAG_ERROR_CONTEXT 
PLPGSQL_GETDIAG_ERROR_DETAIL 
PLPGSQL_GETDIAG_ERROR_HINT 
PLPGSQL_GETDIAG_RETURNED_SQLSTATE 
PLPGSQL_GETDIAG_COLUMN_NAME 
PLPGSQL_GETDIAG_CONSTRAINT_NAME 
PLPGSQL_GETDIAG_DATATYPE_NAME 
PLPGSQL_GETDIAG_MESSAGE_TEXT 
PLPGSQL_GETDIAG_TABLE_NAME 
PLPGSQL_GETDIAG_SCHEMA_NAME 

Definition at line 147 of file plpgsql.h.

◆ PLpgSQL_label_type

Enumerator
PLPGSQL_LABEL_BLOCK 
PLPGSQL_LABEL_LOOP 
PLPGSQL_LABEL_OTHER 

Definition at line 51 of file plpgsql.h.

52 {
53  PLPGSQL_LABEL_BLOCK, /* DECLARE/BEGIN block */
54  PLPGSQL_LABEL_LOOP, /* looping construct */
55  PLPGSQL_LABEL_OTHER /* anything else */
PLpgSQL_label_type
Definition: plpgsql.h:51

◆ PLpgSQL_nsitem_type

Enumerator
PLPGSQL_NSTYPE_LABEL 
PLPGSQL_NSTYPE_VAR 
PLPGSQL_NSTYPE_REC 

Definition at line 41 of file plpgsql.h.

42 {
43  PLPGSQL_NSTYPE_LABEL, /* block label */
44  PLPGSQL_NSTYPE_VAR, /* scalar variable */
45  PLPGSQL_NSTYPE_REC /* composite variable */
PLpgSQL_nsitem_type
Definition: plpgsql.h:41

◆ PLpgSQL_promise_type

Enumerator
PLPGSQL_PROMISE_NONE 
PLPGSQL_PROMISE_TG_NAME 
PLPGSQL_PROMISE_TG_WHEN 
PLPGSQL_PROMISE_TG_LEVEL 
PLPGSQL_PROMISE_TG_OP 
PLPGSQL_PROMISE_TG_RELID 
PLPGSQL_PROMISE_TG_TABLE_NAME 
PLPGSQL_PROMISE_TG_TABLE_SCHEMA 
PLPGSQL_PROMISE_TG_NARGS 
PLPGSQL_PROMISE_TG_ARGV 
PLPGSQL_PROMISE_TG_EVENT 
PLPGSQL_PROMISE_TG_TAG 

Definition at line 73 of file plpgsql.h.

◆ PLpgSQL_raise_option_type

Enumerator
PLPGSQL_RAISEOPTION_ERRCODE 
PLPGSQL_RAISEOPTION_MESSAGE 
PLPGSQL_RAISEOPTION_DETAIL 
PLPGSQL_RAISEOPTION_HINT 
PLPGSQL_RAISEOPTION_COLUMN 
PLPGSQL_RAISEOPTION_CONSTRAINT 
PLPGSQL_RAISEOPTION_DATATYPE 
PLPGSQL_RAISEOPTION_TABLE 
PLPGSQL_RAISEOPTION_SCHEMA 

Definition at line 166 of file plpgsql.h.

◆ PLpgSQL_resolve_option

Enumerator
PLPGSQL_RESOLVE_ERROR 
PLPGSQL_RESOLVE_VARIABLE 
PLPGSQL_RESOLVE_COLUMN 

Definition at line 182 of file plpgsql.h.

183 {
184  PLPGSQL_RESOLVE_ERROR, /* throw error if ambiguous */
185  PLPGSQL_RESOLVE_VARIABLE, /* prefer plpgsql var to table column */
186  PLPGSQL_RESOLVE_COLUMN /* prefer table column to plpgsql var */
PLpgSQL_resolve_option
Definition: plpgsql.h:182

◆ PLpgSQL_stmt_type

Enumerator
PLPGSQL_STMT_BLOCK 
PLPGSQL_STMT_ASSIGN 
PLPGSQL_STMT_IF 
PLPGSQL_STMT_CASE 
PLPGSQL_STMT_LOOP 
PLPGSQL_STMT_WHILE 
PLPGSQL_STMT_FORI 
PLPGSQL_STMT_FORS 
PLPGSQL_STMT_FORC 
PLPGSQL_STMT_FOREACH_A 
PLPGSQL_STMT_EXIT 
PLPGSQL_STMT_RETURN 
PLPGSQL_STMT_RETURN_NEXT 
PLPGSQL_STMT_RETURN_QUERY 
PLPGSQL_STMT_RAISE 
PLPGSQL_STMT_ASSERT 
PLPGSQL_STMT_EXECSQL 
PLPGSQL_STMT_DYNEXECUTE 
PLPGSQL_STMT_DYNFORS 
PLPGSQL_STMT_GETDIAG 
PLPGSQL_STMT_OPEN 
PLPGSQL_STMT_FETCH 
PLPGSQL_STMT_CLOSE 
PLPGSQL_STMT_PERFORM 
PLPGSQL_STMT_CALL 
PLPGSQL_STMT_COMMIT 
PLPGSQL_STMT_ROLLBACK 

Definition at line 102 of file plpgsql.h.

103 {
PLpgSQL_stmt_type
Definition: plpgsql.h:102

◆ PLpgSQL_trigtype

Enumerator
PLPGSQL_DML_TRIGGER 
PLPGSQL_EVENT_TRIGGER 
PLPGSQL_NOT_TRIGGER 

Definition at line 955 of file plpgsql.h.

◆ PLpgSQL_type_type

Enumerator
PLPGSQL_TTYPE_SCALAR 
PLPGSQL_TTYPE_REC 
PLPGSQL_TTYPE_PSEUDO 

Definition at line 92 of file plpgsql.h.

93 {
94  PLPGSQL_TTYPE_SCALAR, /* scalar types and domains */
95  PLPGSQL_TTYPE_REC, /* composite types, including RECORD */
96  PLPGSQL_TTYPE_PSEUDO /* pseudotypes */
PLpgSQL_type_type
Definition: plpgsql.h:92

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 56 of file auth_delay.c.

57 {
58  /* Define custom GUC variables */
59  DefineCustomIntVariable("auth_delay.milliseconds",
60  "Milliseconds to delay before reporting authentication failure",
61  NULL,
63  0,
64  0, INT_MAX / 1000,
65  PGC_SIGHUP,
67  NULL,
68  NULL,
69  NULL);
70  /* Install Hooks */
73 }
void DefineCustomIntVariable(const char *name, const char *short_desc, const char *long_desc, int *valueAddr, int bootValue, int minValue, int maxValue, GucContext context, int flags, GucIntCheckHook check_hook, GucIntAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:9200
ClientAuthentication_hook_type ClientAuthentication_hook
Definition: auth.c:234
static ClientAuthentication_hook_type original_client_auth_hook
Definition: auth_delay.c:29
Definition: guc.h:72
static void auth_delay_checks(Port *port, int status)
Definition: auth_delay.c:35
static int auth_delay_milliseconds
Definition: auth_delay.c:26
#define GUC_UNIT_MS
Definition: guc.h:225

◆ plpgsql_add_initdatums()

int plpgsql_add_initdatums ( int **  varnos)

Definition at line 2394 of file pl_comp.c.

References datums_last, PLpgSQL_datum::dno, i, palloc(), PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_VAR, and plpgsql_nDatums.

2395 {
2396  int i;
2397  int n = 0;
2398 
2399  /*
2400  * The set of dtypes recognized here must match what exec_stmt_block()
2401  * cares about (re)initializing at block entry.
2402  */
2403  for (i = datums_last; i < plpgsql_nDatums; i++)
2404  {
2405  switch (plpgsql_Datums[i]->dtype)
2406  {
2407  case PLPGSQL_DTYPE_VAR:
2408  case PLPGSQL_DTYPE_REC:
2409  n++;
2410  break;
2411 
2412  default:
2413  break;
2414  }
2415  }
2416 
2417  if (varnos != NULL)
2418  {
2419  if (n > 0)
2420  {
2421  *varnos = (int *) palloc(sizeof(int) * n);
2422 
2423  n = 0;
2424  for (i = datums_last; i < plpgsql_nDatums; i++)
2425  {
2426  switch (plpgsql_Datums[i]->dtype)
2427  {
2428  case PLPGSQL_DTYPE_VAR:
2429  case PLPGSQL_DTYPE_REC:
2430  (*varnos)[n++] = plpgsql_Datums[i]->dno;
2431 
2432  default:
2433  break;
2434  }
2435  }
2436  }
2437  else
2438  *varnos = NULL;
2439  }
2440 
2442  return n;
2443 }
int plpgsql_nDatums
Definition: pl_comp.c:45
PLpgSQL_datum ** plpgsql_Datums
Definition: pl_comp.c:46
void * palloc(Size size)
Definition: mcxt.c:1062
int i
static int datums_last
Definition: pl_comp.c:47

◆ plpgsql_adddatum()

void plpgsql_adddatum ( PLpgSQL_datum newdatum)

Definition at line 2333 of file pl_comp.c.

References datums_alloc, PLpgSQL_datum::dno, plpgsql_nDatums, and repalloc().

Referenced by do_compile(), plpgsql_build_recfield(), plpgsql_build_record(), and plpgsql_build_variable().

2334 {
2336  {
2337  datums_alloc *= 2;
2339  }
2340 
2341  newdatum->dno = plpgsql_nDatums;
2342  plpgsql_Datums[plpgsql_nDatums++] = newdatum;
2343 }
int plpgsql_nDatums
Definition: pl_comp.c:45
PLpgSQL_datum ** plpgsql_Datums
Definition: pl_comp.c:46
static int datums_alloc
Definition: pl_comp.c:44
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182

◆ plpgsql_append_source_text()

void plpgsql_append_source_text ( StringInfo  buf,
int  startlocation,
int  endlocation 
)

Definition at line 404 of file pl_scanner.c.

References appendBinaryStringInfo(), Assert, and scanorig.

406 {
407  Assert(startlocation <= endlocation);
408  appendBinaryStringInfo(buf, scanorig + startlocation,
409  endlocation - startlocation);
410 }
static const char * scanorig
Definition: pl_scanner.c:110
#define Assert(condition)
Definition: c.h:804
void appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
Definition: stringinfo.c:227

◆ plpgsql_base_yylex()

int plpgsql_base_yylex ( void  )

◆ plpgsql_build_datatype()

PLpgSQL_type* plpgsql_build_datatype ( Oid  typeOid,
int32  typmod,
Oid  collation,
TypeName origtypname 
)

Definition at line 2091 of file pl_comp.c.

References build_datatype(), elog, ERROR, HeapTupleIsValid, ObjectIdGetDatum, ReleaseSysCache(), SearchSysCache1(), and TYPEOID.

Referenced by do_compile(), exec_stmt_case(), plpgsql_compile_inline(), plpgsql_parse_cwordrowtype(), and plpgsql_parse_wordrowtype().

2093 {
2094  HeapTuple typeTup;
2095  PLpgSQL_type *typ;
2096 
2097  typeTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typeOid));
2098  if (!HeapTupleIsValid(typeTup))
2099  elog(ERROR, "cache lookup failed for type %u", typeOid);
2100 
2101  typ = build_datatype(typeTup, typmod, collation, origtypname);
2102 
2103  ReleaseSysCache(typeTup);
2104 
2105  return typ;
2106 }
static PLpgSQL_type * build_datatype(HeapTuple typeTup, int32 typmod, Oid collation, TypeName *origtypname)
Definition: pl_comp.c:2113
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define elog(elevel,...)
Definition: elog.h:232

◆ plpgsql_build_recfield()

PLpgSQL_recfield* plpgsql_build_recfield ( PLpgSQL_rec rec,
const char *  fldname 
)

Definition at line 2044 of file pl_comp.c.

References Assert, PLpgSQL_datum::dno, PLpgSQL_rec::dno, PLpgSQL_recfield::dtype, PLpgSQL_recfield::fieldname, PLpgSQL_rec::firstfield, i, INVALID_TUPLEDESC_IDENTIFIER, PLpgSQL_recfield::nextfield, palloc0(), plpgsql_adddatum(), PLPGSQL_DTYPE_RECFIELD, pstrdup(), PLpgSQL_recfield::recparentno, and PLpgSQL_recfield::rectupledescid.

Referenced by plpgsql_parse_dblword(), and plpgsql_parse_tripword().

2045 {
2046  PLpgSQL_recfield *recfield;
2047  int i;
2048 
2049  /* search for an existing datum referencing this field */
2050  i = rec->firstfield;
2051  while (i >= 0)
2052  {
2054 
2056  fld->recparentno == rec->dno);
2057  if (strcmp(fld->fieldname, fldname) == 0)
2058  return fld;
2059  i = fld->nextfield;
2060  }
2061 
2062  /* nope, so make a new one */
2063  recfield = palloc0(sizeof(PLpgSQL_recfield));
2064  recfield->dtype = PLPGSQL_DTYPE_RECFIELD;
2065  recfield->fieldname = pstrdup(fldname);
2066  recfield->recparentno = rec->dno;
2068 
2069  plpgsql_adddatum((PLpgSQL_datum *) recfield);
2070 
2071  /* now we can link it into the parent's chain */
2072  recfield->nextfield = rec->firstfield;
2073  rec->firstfield = recfield->dno;
2074 
2075  return recfield;
2076 }
PLpgSQL_datum ** plpgsql_Datums
Definition: pl_comp.c:46
char * pstrdup(const char *in)
Definition: mcxt.c:1299
#define INVALID_TUPLEDESC_IDENTIFIER
Definition: typcache.h:155
void plpgsql_adddatum(PLpgSQL_datum *newdatum)
Definition: pl_comp.c:2333
uint64 rectupledescid
Definition: plpgsql.h:428
void * palloc0(Size size)
Definition: mcxt.c:1093
PLpgSQL_datum_type dtype
Definition: plpgsql.h:421
#define Assert(condition)
Definition: c.h:804
int i
char * fieldname
Definition: plpgsql.h:425
int firstfield
Definition: plpgsql.h:408

◆ plpgsql_build_record()

PLpgSQL_rec* plpgsql_build_record ( const char *  refname,
int  lineno,
PLpgSQL_type dtype,
Oid  rectypeid,
bool  add2namespace 
)

Definition at line 1950 of file pl_comp.c.

References PLpgSQL_rec::datatype, PLpgSQL_datum::dno, PLpgSQL_rec::dtype, PLpgSQL_rec::erh, PLpgSQL_rec::firstfield, PLpgSQL_rec::lineno, palloc0(), plpgsql_adddatum(), PLPGSQL_DTYPE_REC, plpgsql_ns_additem(), PLPGSQL_NSTYPE_REC, pstrdup(), PLpgSQL_rec::rectypeid, and PLpgSQL_rec::refname.

Referenced by do_compile(), and plpgsql_build_variable().

1953 {
1954  PLpgSQL_rec *rec;
1955 
1956  rec = palloc0(sizeof(PLpgSQL_rec));
1957  rec->dtype = PLPGSQL_DTYPE_REC;
1958  rec->refname = pstrdup(refname);
1959  rec->lineno = lineno;
1960  /* other fields are left as 0, might be changed by caller */
1961  rec->datatype = dtype;
1962  rec->rectypeid = rectypeid;
1963  rec->firstfield = -1;
1964  rec->erh = NULL;
1966  if (add2namespace)
1968 
1969  return rec;
1970 }
PLpgSQL_datum_type dtype
Definition: plpgsql.h:390
PLpgSQL_type * datatype
Definition: plpgsql.h:405
char * pstrdup(const char *in)
Definition: mcxt.c:1299
void plpgsql_ns_additem(PLpgSQL_nsitem_type itemtype, int itemno, const char *name)
Definition: pl_funcs.c:92
ExpandedRecordHeader * erh
Definition: plpgsql.h:413
char * refname
Definition: plpgsql.h:392
void plpgsql_adddatum(PLpgSQL_datum *newdatum)
Definition: pl_comp.c:2333
void * palloc0(Size size)
Definition: mcxt.c:1093
Oid rectypeid
Definition: plpgsql.h:406
int lineno
Definition: plpgsql.h:393
int firstfield
Definition: plpgsql.h:408

◆ plpgsql_build_variable()

PLpgSQL_variable* plpgsql_build_variable ( const char *  refname,
int  lineno,
PLpgSQL_type dtype,
bool  add2namespace 
)

Definition at line 1887 of file pl_comp.c.

References PLpgSQL_var::datatype, PLpgSQL_datum::dno, PLpgSQL_var::dtype, elog, ereport, errcode(), errmsg(), ERROR, format_type_be(), PLpgSQL_var::freeval, PLpgSQL_var::isnull, PLpgSQL_var::lineno, palloc0(), plpgsql_adddatum(), plpgsql_build_record(), PLPGSQL_DTYPE_VAR, plpgsql_ns_additem(), PLPGSQL_NSTYPE_VAR, PLPGSQL_TTYPE_PSEUDO, PLPGSQL_TTYPE_REC, PLPGSQL_TTYPE_SCALAR, pstrdup(), PLpgSQL_var::refname, PLpgSQL_type::ttype, PLpgSQL_type::typoid, and PLpgSQL_var::value.

Referenced by do_compile(), and plpgsql_compile_inline().

1889 {
1890  PLpgSQL_variable *result;
1891 
1892  switch (dtype->ttype)
1893  {
1894  case PLPGSQL_TTYPE_SCALAR:
1895  {
1896  /* Ordinary scalar datatype */
1897  PLpgSQL_var *var;
1898 
1899  var = palloc0(sizeof(PLpgSQL_var));
1900  var->dtype = PLPGSQL_DTYPE_VAR;
1901  var->refname = pstrdup(refname);
1902  var->lineno = lineno;
1903  var->datatype = dtype;
1904  /* other fields are left as 0, might be changed by caller */
1905 
1906  /* preset to NULL */
1907  var->value = 0;
1908  var->isnull = true;
1909  var->freeval = false;
1910 
1912  if (add2namespace)
1914  var->dno,
1915  refname);
1916  result = (PLpgSQL_variable *) var;
1917  break;
1918  }
1919  case PLPGSQL_TTYPE_REC:
1920  {
1921  /* Composite type -- build a record variable */
1922  PLpgSQL_rec *rec;
1923 
1924  rec = plpgsql_build_record(refname, lineno,
1925  dtype, dtype->typoid,
1926  add2namespace);
1927  result = (PLpgSQL_variable *) rec;
1928  break;
1929  }
1930  case PLPGSQL_TTYPE_PSEUDO:
1931  ereport(ERROR,
1932  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1933  errmsg("variable \"%s\" has pseudo-type %s",
1934  refname, format_type_be(dtype->typoid))));
1935  result = NULL; /* keep compiler quiet */
1936  break;
1937  default:
1938  elog(ERROR, "unrecognized ttype: %d", dtype->ttype);
1939  result = NULL; /* keep compiler quiet */
1940  break;
1941  }
1942 
1943  return result;
1944 }
char * refname
Definition: plpgsql.h:312
PLpgSQL_rec * plpgsql_build_record(const char *refname, int lineno, PLpgSQL_type *dtype, Oid rectypeid, bool add2namespace)
Definition: pl_comp.c:1950
PLpgSQL_datum_type dtype
Definition: plpgsql.h:310
char * pstrdup(const char *in)
Definition: mcxt.c:1299
PLpgSQL_type * datatype
Definition: plpgsql.h:319
void plpgsql_ns_additem(PLpgSQL_nsitem_type itemtype, int itemno, const char *name)
Definition: pl_funcs.c:92
PLpgSQL_type_type ttype
Definition: plpgsql.h:201
int errcode(int sqlerrcode)
Definition: elog.c:698
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
void plpgsql_adddatum(PLpgSQL_datum *newdatum)
Definition: pl_comp.c:2333
#define ERROR
Definition: elog.h:46
bool freeval
Definition: plpgsql.h:334
void * palloc0(Size size)
Definition: mcxt.c:1093
Datum value
Definition: plpgsql.h:332
#define ereport(elevel,...)
Definition: elog.h:157
int lineno
Definition: plpgsql.h:313
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
bool isnull
Definition: plpgsql.h:333
Oid typoid
Definition: plpgsql.h:200

◆ plpgsql_compile()

PLpgSQL_function* plpgsql_compile ( FunctionCallInfo  fcinfo,
bool  forValidator 
)

Definition at line 136 of file pl_comp.c.

References compute_function_hashkey(), delete_function(), do_compile(), elog, ERROR, FunctionCallInfoBaseData::flinfo, FmgrInfo::fn_extra, FmgrInfo::fn_oid, PLpgSQL_function::fn_tid, PLpgSQL_function::fn_xmin, plpgsql_hashent::function, GETSTRUCT, HeapTupleHeaderGetRawXmin, HeapTupleIsValid, ItemPointerEquals(), ObjectIdGetDatum, plpgsql_HashTableLookup(), PROCOID, ReleaseSysCache(), SearchSysCache1(), HeapTupleData::t_data, HeapTupleData::t_self, and PLpgSQL_function::use_count.

Referenced by plpgsql_call_handler(), and plpgsql_validator().

137 {
138  Oid funcOid = fcinfo->flinfo->fn_oid;
139  HeapTuple procTup;
140  Form_pg_proc procStruct;
141  PLpgSQL_function *function;
142  PLpgSQL_func_hashkey hashkey;
143  bool function_valid = false;
144  bool hashkey_valid = false;
145 
146  /*
147  * Lookup the pg_proc tuple by Oid; we'll need it in any case
148  */
149  procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcOid));
150  if (!HeapTupleIsValid(procTup))
151  elog(ERROR, "cache lookup failed for function %u", funcOid);
152  procStruct = (Form_pg_proc) GETSTRUCT(procTup);
153 
154  /*
155  * See if there's already a cache entry for the current FmgrInfo. If not,
156  * try to find one in the hash table.
157  */
158  function = (PLpgSQL_function *) fcinfo->flinfo->fn_extra;
159 
160 recheck:
161  if (!function)
162  {
163  /* Compute hashkey using function signature and actual arg types */
164  compute_function_hashkey(fcinfo, procStruct, &hashkey, forValidator);
165  hashkey_valid = true;
166 
167  /* And do the lookup */
168  function = plpgsql_HashTableLookup(&hashkey);
169  }
170 
171  if (function)
172  {
173  /* We have a compiled function, but is it still valid? */
174  if (function->fn_xmin == HeapTupleHeaderGetRawXmin(procTup->t_data) &&
175  ItemPointerEquals(&function->fn_tid, &procTup->t_self))
176  function_valid = true;
177  else
178  {
179  /*
180  * Nope, so remove it from hashtable and try to drop associated
181  * storage (if not done already).
182  */
183  delete_function(function);
184 
185  /*
186  * If the function isn't in active use then we can overwrite the
187  * func struct with new data, allowing any other existing fn_extra
188  * pointers to make use of the new definition on their next use.
189  * If it is in use then just leave it alone and make a new one.
190  * (The active invocations will run to completion using the
191  * previous definition, and then the cache entry will just be
192  * leaked; doesn't seem worth adding code to clean it up, given
193  * what a corner case this is.)
194  *
195  * If we found the function struct via fn_extra then it's possible
196  * a replacement has already been made, so go back and recheck the
197  * hashtable.
198  */
199  if (function->use_count != 0)
200  {
201  function = NULL;
202  if (!hashkey_valid)
203  goto recheck;
204  }
205  }
206  }
207 
208  /*
209  * If the function wasn't found or was out-of-date, we have to compile it
210  */
211  if (!function_valid)
212  {
213  /*
214  * Calculate hashkey if we didn't already; we'll need it to store the
215  * completed function.
216  */
217  if (!hashkey_valid)
218  compute_function_hashkey(fcinfo, procStruct, &hashkey,
219  forValidator);
220 
221  /*
222  * Do the hard part.
223  */
224  function = do_compile(fcinfo, procTup, function,
225  &hashkey, forValidator);
226  }
227 
228  ReleaseSysCache(procTup);
229 
230  /*
231  * Save pointer in FmgrInfo to avoid search on subsequent calls
232  */
233  fcinfo->flinfo->fn_extra = (void *) function;
234 
235  /*
236  * Finally return the compiled function
237  */
238  return function;
239 }
static PLpgSQL_function * do_compile(FunctionCallInfo fcinfo, HeapTuple procTup, PLpgSQL_function *function, PLpgSQL_func_hashkey *hashkey, bool forValidator)
Definition: pl_comp.c:264
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
static void compute_function_hashkey(FunctionCallInfo fcinfo, Form_pg_proc procStruct, PLpgSQL_func_hashkey *hashkey, bool forValidator)
Definition: pl_comp.c:2452
unsigned int Oid
Definition: postgres_ext.h:31
HeapTupleHeader t_data
Definition: htup.h:68
static void delete_function(PLpgSQL_function *func)
Definition: pl_comp.c:2577
static PLpgSQL_function * plpgsql_HashTableLookup(PLpgSQL_func_hashkey *func_key)
Definition: pl_comp.c:2605
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
ItemPointerData t_self
Definition: htup.h:65
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
FmgrInfo * flinfo
Definition: fmgr.h:87
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
Oid fn_oid
Definition: fmgr.h:59
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define HeapTupleHeaderGetRawXmin(tup)
Definition: htup_details.h:308
void * fn_extra
Definition: fmgr.h:64
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:29
on_exit_nicely_callback function
#define elog(elevel,...)
Definition: elog.h:232

◆ plpgsql_compile_inline()

PLpgSQL_function* plpgsql_compile_inline ( char *  proc_source)

Definition at line 847 of file pl_comp.c.

References add_dummy_return(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ErrorContextCallback::arg, ErrorContextCallback::callback, check_function_bodies, CurrentMemoryContext, PLpgSQL_variable::dno, elog, ERROR, error_context_stack, PLpgSQL_function::fn_rettype, plpgsql_hashent::function, InvalidOid, MemoryContextSwitchTo(), palloc0(), plpgsql_build_datatype(), plpgsql_build_variable(), plpgsql_check_syntax, plpgsql_compile_error_callback(), plpgsql_DumpExecTree, plpgsql_error_funcname, plpgsql_finish_datums(), PLPGSQL_LABEL_BLOCK, PLPGSQL_NOT_TRIGGER, plpgsql_ns_init(), plpgsql_ns_push(), plpgsql_parse_result, plpgsql_print_strict_params, plpgsql_scanner_finish(), plpgsql_scanner_init(), plpgsql_start_datums(), plpgsql_variable_conflict, plpgsql_yyparse(), ErrorContextCallback::previous, and pstrdup().

Referenced by plpgsql_inline_handler().

848 {
849  char *func_name = "inline_code_block";
850  PLpgSQL_function *function;
851  ErrorContextCallback plerrcontext;
852  PLpgSQL_variable *var;
853  int parse_rc;
854  MemoryContext func_cxt;
855 
856  /*
857  * Setup the scanner input and error info. We assume that this function
858  * cannot be invoked recursively, so there's no need to save and restore
859  * the static variables used here.
860  */
861  plpgsql_scanner_init(proc_source);
862 
863  plpgsql_error_funcname = func_name;
864 
865  /*
866  * Setup error traceback support for ereport()
867  */
869  plerrcontext.arg = proc_source;
870  plerrcontext.previous = error_context_stack;
871  error_context_stack = &plerrcontext;
872 
873  /* Do extra syntax checking if check_function_bodies is on */
875 
876  /* Function struct does not live past current statement */
877  function = (PLpgSQL_function *) palloc0(sizeof(PLpgSQL_function));
878 
879  plpgsql_curr_compile = function;
880 
881  /*
882  * All the rest of the compile-time storage (e.g. parse tree) is kept in
883  * its own memory context, so it can be reclaimed easily.
884  */
886  "PL/pgSQL inline code context",
889 
890  function->fn_signature = pstrdup(func_name);
891  function->fn_is_trigger = PLPGSQL_NOT_TRIGGER;
892  function->fn_input_collation = InvalidOid;
893  function->fn_cxt = func_cxt;
894  function->out_param_varno = -1; /* set up for no OUT param */
895  function->resolve_option = plpgsql_variable_conflict;
896  function->print_strict_params = plpgsql_print_strict_params;
897 
898  /*
899  * don't do extra validation for inline code as we don't want to add spam
900  * at runtime
901  */
902  function->extra_warnings = 0;
903  function->extra_errors = 0;
904 
905  function->nstatements = 0;
906  function->requires_procedure_resowner = false;
907 
908  plpgsql_ns_init();
910  plpgsql_DumpExecTree = false;
912 
913  /* Set up as though in a function returning VOID */
914  function->fn_rettype = VOIDOID;
915  function->fn_retset = false;
916  function->fn_retistuple = false;
917  function->fn_retisdomain = false;
918  function->fn_prokind = PROKIND_FUNCTION;
919  /* a bit of hardwired knowledge about type VOID here */
920  function->fn_retbyval = true;
921  function->fn_rettyplen = sizeof(int32);
922 
923  /*
924  * Remember if function is STABLE/IMMUTABLE. XXX would it be better to
925  * set this true inside a read-only transaction? Not clear.
926  */
927  function->fn_readonly = false;
928 
929  /*
930  * Create the magic FOUND variable.
931  */
932  var = plpgsql_build_variable("found", 0,
933  plpgsql_build_datatype(BOOLOID,
934  -1,
935  InvalidOid,
936  NULL),
937  true);
938  function->found_varno = var->dno;
939 
940  /*
941  * Now parse the function's text
942  */
943  parse_rc = plpgsql_yyparse();
944  if (parse_rc != 0)
945  elog(ERROR, "plpgsql parser returned %d", parse_rc);
946  function->action = plpgsql_parse_result;
947 
949 
950  /*
951  * If it returns VOID (always true at the moment), we allow control to
952  * fall off the end without an explicit RETURN statement.
953  */
954  if (function->fn_rettype == VOIDOID)
955  add_dummy_return(function);
956 
957  /*
958  * Complete the function's info
959  */
960  function->fn_nargs = 0;
961 
962  plpgsql_finish_datums(function);
963 
964  /*
965  * Pop the error context stack
966  */
967  error_context_stack = plerrcontext.previous;
968  plpgsql_error_funcname = NULL;
969 
970  plpgsql_check_syntax = false;
971 
974  return function;
975 }
int plpgsql_yyparse(void)
#define AllocSetContextCreate
Definition: memutils.h:173
char * pstrdup(const char *in)
Definition: mcxt.c:1299
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
bool check_function_bodies
Definition: guc.c:586
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
static void plpgsql_start_datums(void)
Definition: pl_comp.c:2316
signed int int32
Definition: c.h:429
ErrorContextCallback * error_context_stack
Definition: elog.c:93
void plpgsql_scanner_finish(void)
Definition: pl_scanner.c:613
bool plpgsql_check_syntax
Definition: pl_comp.c:51
#define ERROR
Definition: elog.h:46
char * plpgsql_error_funcname
Definition: pl_comp.c:49
bool plpgsql_DumpExecTree
Definition: pl_comp.c:50
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
static void add_dummy_return(PLpgSQL_function *function)
Definition: pl_comp.c:1036
MemoryContext CurrentMemoryContext
Definition: mcxt.c:42
void plpgsql_ns_init(void)
Definition: pl_funcs.c:43
void * palloc0(Size size)
Definition: mcxt.c:1093
PLpgSQL_type * plpgsql_build_datatype(Oid typeOid, int32 typmod, Oid collation, TypeName *origtypname)
Definition: pl_comp.c:2091
#define InvalidOid
Definition: postgres_ext.h:36
bool plpgsql_print_strict_params
Definition: pl_handler.c:46
static void plpgsql_compile_error_callback(void *arg)
Definition: pl_comp.c:984
int plpgsql_variable_conflict
Definition: pl_handler.c:44
void plpgsql_ns_push(const char *label, PLpgSQL_label_type label_type)
Definition: pl_funcs.c:54
PLpgSQL_stmt_block * plpgsql_parse_result
Definition: pl_comp.c:42
PLpgSQL_variable * plpgsql_build_variable(const char *refname, int lineno, PLpgSQL_type *dtype, bool add2namespace)
Definition: pl_comp.c:1887
on_exit_nicely_callback function
void plpgsql_scanner_init(const char *str)
Definition: pl_scanner.c:586
static void plpgsql_finish_datums(PLpgSQL_function *function)
Definition: pl_comp.c:2350
#define elog(elevel,...)
Definition: elog.h:232
MemoryContext plpgsql_compile_tmp_cxt
Definition: pl_comp.c:56
PLpgSQL_function * plpgsql_curr_compile
Definition: pl_comp.c:53

◆ plpgsql_dumptree()

void plpgsql_dumptree ( PLpgSQL_function func)

Definition at line 1598 of file pl_funcs.c.

References PLpgSQL_function::action, PLpgSQL_type::atttypmod, PLpgSQL_var::cursor_explicit_argrow, PLpgSQL_var::cursor_explicit_expr, PLpgSQL_var::datatype, PLpgSQL_function::datums, PLpgSQL_var::default_val, PLpgSQL_datum::dtype, PLpgSQL_recfield::dtype, dump_block(), dump_expr(), dump_indent, PLpgSQL_row::fieldnames, PLpgSQL_function::fn_signature, i, PLpgSQL_var::isconst, PLpgSQL_stmt_block::lineno, PLpgSQL_function::ndatums, PLpgSQL_row::nfields, PLpgSQL_var::notnull, PLPGSQL_DTYPE_PROMISE, PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_RECFIELD, PLPGSQL_DTYPE_ROW, PLPGSQL_DTYPE_VAR, PLPGSQL_PROMISE_NONE, printf, PLpgSQL_var::promise, PLpgSQL_var::refname, PLpgSQL_row::refname, generate_unaccent_rules::stdout, PLpgSQL_type::typname, PLpgSQL_type::typoid, and PLpgSQL_row::varnos.

Referenced by do_compile().

1599 {
1600  int i;
1601  PLpgSQL_datum *d;
1602 
1603  printf("\nExecution tree of successfully compiled PL/pgSQL function %s:\n",
1604  func->fn_signature);
1605 
1606  printf("\nFunction's data area:\n");
1607  for (i = 0; i < func->ndatums; i++)
1608  {
1609  d = func->datums[i];
1610 
1611  printf(" entry %d: ", i);
1612  switch (d->dtype)
1613  {
1614  case PLPGSQL_DTYPE_VAR:
1615  case PLPGSQL_DTYPE_PROMISE:
1616  {
1617  PLpgSQL_var *var = (PLpgSQL_var *) d;
1618 
1619  printf("VAR %-16s type %s (typoid %u) atttypmod %d\n",
1620  var->refname, var->datatype->typname,
1621  var->datatype->typoid,
1622  var->datatype->atttypmod);
1623  if (var->isconst)
1624  printf(" CONSTANT\n");
1625  if (var->notnull)
1626  printf(" NOT NULL\n");
1627  if (var->default_val != NULL)
1628  {
1629  printf(" DEFAULT ");
1630  dump_expr(var->default_val);
1631  printf("\n");
1632  }
1633  if (var->cursor_explicit_expr != NULL)
1634  {
1635  if (var->cursor_explicit_argrow >= 0)
1636  printf(" CURSOR argument row %d\n", var->cursor_explicit_argrow);
1637 
1638  printf(" CURSOR IS ");
1640  printf("\n");
1641  }
1642  if (var->promise != PLPGSQL_PROMISE_NONE)
1643  printf(" PROMISE %d\n",
1644  (int) var->promise);
1645  }
1646  break;
1647  case PLPGSQL_DTYPE_ROW:
1648  {
1649  PLpgSQL_row *row = (PLpgSQL_row *) d;
1650  int i;
1651 
1652  printf("ROW %-16s fields", row->refname);
1653  for (i = 0; i < row->nfields; i++)
1654  {
1655  printf(" %s=var %d", row->fieldnames[i],
1656  row->varnos[i]);
1657  }
1658  printf("\n");
1659  }
1660  break;
1661  case PLPGSQL_DTYPE_REC:
1662  printf("REC %-16s typoid %u\n",
1663  ((PLpgSQL_rec *) d)->refname,
1664  ((PLpgSQL_rec *) d)->rectypeid);
1665  if (((PLpgSQL_rec *) d)->isconst)
1666  printf(" CONSTANT\n");
1667  if (((PLpgSQL_rec *) d)->notnull)
1668  printf(" NOT NULL\n");
1669  if (((PLpgSQL_rec *) d)->default_val != NULL)
1670  {
1671  printf(" DEFAULT ");
1672  dump_expr(((PLpgSQL_rec *) d)->default_val);
1673  printf("\n");
1674  }
1675  break;
1677  printf("RECFIELD %-16s of REC %d\n",
1678  ((PLpgSQL_recfield *) d)->fieldname,
1679  ((PLpgSQL_recfield *) d)->recparentno);
1680  break;
1681  default:
1682  printf("??? unknown data type %d\n", d->dtype);
1683  }
1684  }
1685  printf("\nFunction's statements:\n");
1686 
1687  dump_indent = 0;
1688  printf("%3d:", func->action->lineno);
1689  dump_block(func->action);
1690  printf("\nEnd of execution tree of function %s\n\n", func->fn_signature);
1691  fflush(stdout);
1692 }
PLpgSQL_promise_type promise
Definition: plpgsql.h:341
PLpgSQL_datum ** datums
Definition: plpgsql.h:1002
char * refname
Definition: plpgsql.h:366
char * refname
Definition: plpgsql.h:312
PLpgSQL_stmt_block * action
Definition: plpgsql.h:1006
PLpgSQL_expr * cursor_explicit_expr
Definition: plpgsql.h:326
PLpgSQL_type * datatype
Definition: plpgsql.h:319
bool isconst
Definition: plpgsql.h:314
#define printf(...)
Definition: port.h:223
PLpgSQL_datum_type dtype
Definition: plpgsql.h:276
bool notnull
Definition: plpgsql.h:315
int cursor_explicit_argrow
Definition: plpgsql.h:327
char ** fieldnames
Definition: plpgsql.h:381
PLpgSQL_expr * default_val
Definition: plpgsql.h:316
int * varnos
Definition: plpgsql.h:382
PLpgSQL_datum_type dtype
Definition: plpgsql.h:421
int nfields
Definition: plpgsql.h:380
static int dump_indent
Definition: pl_funcs.c:784
char * fn_signature
Definition: plpgsql.h:967
char * typname
Definition: plpgsql.h:199
static void dump_block(PLpgSQL_stmt_block *block)
Definition: pl_funcs.c:933
int32 atttypmod
Definition: plpgsql.h:207
static void dump_expr(PLpgSQL_expr *expr)
Definition: pl_funcs.c:1592
int i
Oid typoid
Definition: plpgsql.h:200

◆ plpgsql_exec_event_trigger()

void plpgsql_exec_event_trigger ( PLpgSQL_function func,
EventTriggerData trigdata 
)

Definition at line 1146 of file pl_exec.c.

References PLpgSQL_function::action, ErrorContextCallback::arg, ErrorContextCallback::callback, copy_plpgsql_datums(), ereport, PLpgSQL_execstate::err_text, errcode(), errmsg(), ERROR, error_context_stack, PLpgSQL_execstate::evtrigdata, exec_eval_cleanup(), exec_toplevel_block(), PLpgSQL_plugin::func_beg, PLpgSQL_plugin::func_end, gettext_noop, plpgsql_destroy_econtext(), plpgsql_estate_setup(), plpgsql_exec_error_callback(), plpgsql_plugin_ptr, PLPGSQL_RC_RETURN, and ErrorContextCallback::previous.

Referenced by plpgsql_call_handler().

1147 {
1148  PLpgSQL_execstate estate;
1149  ErrorContextCallback plerrcontext;
1150  int rc;
1151 
1152  /*
1153  * Setup the execution state
1154  */
1155  plpgsql_estate_setup(&estate, func, NULL, NULL, NULL);
1156  estate.evtrigdata = trigdata;
1157 
1158  /*
1159  * Setup error traceback support for ereport()
1160  */
1161  plerrcontext.callback = plpgsql_exec_error_callback;
1162  plerrcontext.arg = &estate;
1163  plerrcontext.previous = error_context_stack;
1164  error_context_stack = &plerrcontext;
1165 
1166  /*
1167  * Make local execution copies of all the datums
1168  */
1169  estate.err_text = gettext_noop("during initialization of execution state");
1170  copy_plpgsql_datums(&estate, func);
1171 
1172  /*
1173  * Let the instrumentation plugin peek at this function
1174  */
1175  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
1176  ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1177 
1178  /*
1179  * Now call the toplevel block of statements
1180  */
1181  estate.err_text = NULL;
1182  rc = exec_toplevel_block(&estate, func->action);
1183  if (rc != PLPGSQL_RC_RETURN)
1184  {
1185  estate.err_text = NULL;
1186  ereport(ERROR,
1187  (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1188  errmsg("control reached end of trigger procedure without RETURN")));
1189  }
1190 
1191  estate.err_text = gettext_noop("during function exit");
1192 
1193  /*
1194  * Let the instrumentation plugin peek at this function
1195  */
1196  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
1197  ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1198 
1199  /* Clean up any leftover temporary memory */
1200  plpgsql_destroy_econtext(&estate);
1201  exec_eval_cleanup(&estate);
1202  /* stmt_mcontext will be destroyed when function's main context is */
1203 
1204  /*
1205  * Pop the error context stack
1206  */
1207  error_context_stack = plerrcontext.previous;
1208 }
PLpgSQL_stmt_block * action
Definition: plpgsql.h:1006
static void plpgsql_exec_error_callback(void *arg)
Definition: pl_exec.c:1214
#define gettext_noop(x)
Definition: c.h:1197
int errcode(int sqlerrcode)
Definition: elog.c:698
static void exec_eval_cleanup(PLpgSQL_execstate *estate)
Definition: pl_exec.c:4077
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
static void copy_plpgsql_datums(PLpgSQL_execstate *estate, PLpgSQL_function *func)
Definition: pl_exec.c:1272
ErrorContextCallback * error_context_stack
Definition: elog.c:93
static int exec_toplevel_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
Definition: pl_exec.c:1596
#define ERROR
Definition: elog.h:46
static void plpgsql_destroy_econtext(PLpgSQL_execstate *estate)
Definition: pl_exec.c:8309
PLpgSQL_plugin ** plpgsql_plugin_ptr
Definition: pl_handler.c:56
void(* func_beg)(PLpgSQL_execstate *estate, PLpgSQL_function *func)
Definition: plpgsql.h:1130
EventTriggerData * evtrigdata
Definition: plpgsql.h:1025
const char * err_text
Definition: plpgsql.h:1091
#define ereport(elevel,...)
Definition: elog.h:157
void(* func_end)(PLpgSQL_execstate *estate, PLpgSQL_function *func)
Definition: plpgsql.h:1131
static void plpgsql_estate_setup(PLpgSQL_execstate *estate, PLpgSQL_function *func, ReturnSetInfo *rsi, EState *simple_eval_estate, ResourceOwner simple_eval_resowner)
Definition: pl_exec.c:3929
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ plpgsql_exec_function()

Datum plpgsql_exec_function ( PLpgSQL_function func,
FunctionCallInfo  fcinfo,
EState simple_eval_estate,
ResourceOwner  simple_eval_resowner,
ResourceOwner  procedure_resowner,
bool  atomic 
)

Definition at line 470 of file pl_exec.c.

References PLpgSQL_function::action, ReturnSetInfo::allowedModes, ErrorContextCallback::arg, FunctionCallInfoBaseData::args, assign_simple_var(), PLpgSQL_execstate::atomic, ErrorContextCallback::callback, coerce_function_result_tuple(), copy_plpgsql_datums(), CreateTupleDescCopy(), PLpgSQL_var::datatype, PLpgSQL_execstate::datum_context, DatumGetPointer, PLpgSQL_function::datums, PLpgSQL_execstate::datums, domain_check(), PLpgSQL_datum::dtype, elog, ereport, PLpgSQL_execstate::err_text, errcode(), errmsg(), ERROR, error_context_stack, exec_cast_value(), exec_eval_cleanup(), exec_move_row(), exec_move_row_from_datum(), exec_set_found(), exec_toplevel_block(), expand_array(), PLpgSQL_function::fn_argvarnos, PLpgSQL_function::fn_nargs, PLpgSQL_function::fn_retbyval, PLpgSQL_function::fn_retisdomain, PLpgSQL_function::fn_rettype, PLpgSQL_function::fn_rettyplen, PLpgSQL_plugin::func_beg, PLpgSQL_plugin::func_end, get_call_result_type(), gettext_noop, i, IsA, FunctionCallInfoBaseData::isnull, PLpgSQL_var::isnull, NullableDatum::isnull, MemoryContextSwitchTo(), plpgsql_destroy_econtext(), PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_VAR, plpgsql_estate_setup(), plpgsql_exec_error_callback(), plpgsql_plugin_ptr, PLPGSQL_RC_RETURN, ErrorContextCallback::previous, PLpgSQL_execstate::procedure_resowner, FunctionCallInfoBaseData::resultinfo, PLpgSQL_execstate::retisnull, PLpgSQL_execstate::retisset, PLpgSQL_execstate::retistuple, PLpgSQL_execstate::rettype, ReturnSetInfo::returnMode, PLpgSQL_execstate::retval, PLpgSQL_execstate::rsi, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SPI_datumTransfer(), TransferExpandedObject(), PLpgSQL_execstate::tuple_store, PLpgSQL_execstate::tuple_store_cxt, PLpgSQL_execstate::tuple_store_desc, TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, TYPEFUNC_RECORD, PLpgSQL_type::typisarray, PLpgSQL_var::value, NullableDatum::value, VARATT_IS_EXTERNAL_EXPANDED_RO, and VARATT_IS_EXTERNAL_EXPANDED_RW.

Referenced by plpgsql_call_handler(), and plpgsql_inline_handler().

475 {
476  PLpgSQL_execstate estate;
477  ErrorContextCallback plerrcontext;
478  int i;
479  int rc;
480 
481  /*
482  * Setup the execution state
483  */
484  plpgsql_estate_setup(&estate, func, (ReturnSetInfo *) fcinfo->resultinfo,
485  simple_eval_estate, simple_eval_resowner);
486  estate.procedure_resowner = procedure_resowner;
487  estate.atomic = atomic;
488 
489  /*
490  * Setup error traceback support for ereport()
491  */
492  plerrcontext.callback = plpgsql_exec_error_callback;
493  plerrcontext.arg = &estate;
494  plerrcontext.previous = error_context_stack;
495  error_context_stack = &plerrcontext;
496 
497  /*
498  * Make local execution copies of all the datums
499  */
500  estate.err_text = gettext_noop("during initialization of execution state");
501  copy_plpgsql_datums(&estate, func);
502 
503  /*
504  * Store the actual call argument values into the appropriate variables
505  */
506  estate.err_text = gettext_noop("while storing call arguments into local variables");
507  for (i = 0; i < func->fn_nargs; i++)
508  {
509  int n = func->fn_argvarnos[i];
510 
511  switch (estate.datums[n]->dtype)
512  {
513  case PLPGSQL_DTYPE_VAR:
514  {
515  PLpgSQL_var *var = (PLpgSQL_var *) estate.datums[n];
516 
517  assign_simple_var(&estate, var,
518  fcinfo->args[i].value,
519  fcinfo->args[i].isnull,
520  false);
521 
522  /*
523  * Force any array-valued parameter to be stored in
524  * expanded form in our local variable, in hopes of
525  * improving efficiency of uses of the variable. (This is
526  * a hack, really: why only arrays? Need more thought
527  * about which cases are likely to win. See also
528  * typisarray-specific heuristic in exec_assign_value.)
529  *
530  * Special cases: If passed a R/W expanded pointer, assume
531  * we can commandeer the object rather than having to copy
532  * it. If passed a R/O expanded pointer, just keep it as
533  * the value of the variable for the moment. (We'll force
534  * it to R/W if the variable gets modified, but that may
535  * very well never happen.)
536  */
537  if (!var->isnull && var->datatype->typisarray)
538  {
540  {
541  /* take ownership of R/W object */
542  assign_simple_var(&estate, var,
544  estate.datum_context),
545  false,
546  true);
547  }
549  {
550  /* R/O pointer, keep it as-is until assigned to */
551  }
552  else
553  {
554  /* flat array, so force to expanded form */
555  assign_simple_var(&estate, var,
556  expand_array(var->value,
557  estate.datum_context,
558  NULL),
559  false,
560  true);
561  }
562  }
563  }
564  break;
565 
566  case PLPGSQL_DTYPE_REC:
567  {
568  PLpgSQL_rec *rec = (PLpgSQL_rec *) estate.datums[n];
569 
570  if (!fcinfo->args[i].isnull)
571  {
572  /* Assign row value from composite datum */
573  exec_move_row_from_datum(&estate,
574  (PLpgSQL_variable *) rec,
575  fcinfo->args[i].value);
576  }
577  else
578  {
579  /* If arg is null, set variable to null */
580  exec_move_row(&estate, (PLpgSQL_variable *) rec,
581  NULL, NULL);
582  }
583  /* clean up after exec_move_row() */
584  exec_eval_cleanup(&estate);
585  }
586  break;
587 
588  default:
589  /* Anything else should not be an argument variable */
590  elog(ERROR, "unrecognized dtype: %d", func->datums[i]->dtype);
591  }
592  }
593 
594  estate.err_text = gettext_noop("during function entry");
595 
596  /*
597  * Set the magic variable FOUND to false
598  */
599  exec_set_found(&estate, false);
600 
601  /*
602  * Let the instrumentation plugin peek at this function
603  */
604  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
605  ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
606 
607  /*
608  * Now call the toplevel block of statements
609  */
610  estate.err_text = NULL;
611  rc = exec_toplevel_block(&estate, func->action);
612  if (rc != PLPGSQL_RC_RETURN)
613  {
614  estate.err_text = NULL;
615  ereport(ERROR,
616  (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
617  errmsg("control reached end of function without RETURN")));
618  }
619 
620  /*
621  * We got a return value - process it
622  */
623  estate.err_text = gettext_noop("while casting return value to function's return type");
624 
625  fcinfo->isnull = estate.retisnull;
626 
627  if (estate.retisset)
628  {
629  ReturnSetInfo *rsi = estate.rsi;
630 
631  /* Check caller can handle a set result */
632  if (!rsi || !IsA(rsi, ReturnSetInfo) ||
633  (rsi->allowedModes & SFRM_Materialize) == 0)
634  ereport(ERROR,
635  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
636  errmsg("set-valued function called in context that cannot accept a set")));
638 
639  /* If we produced any tuples, send back the result */
640  if (estate.tuple_store)
641  {
642  MemoryContext oldcxt;
643 
644  rsi->setResult = estate.tuple_store;
645  oldcxt = MemoryContextSwitchTo(estate.tuple_store_cxt);
647  MemoryContextSwitchTo(oldcxt);
648  }
649  estate.retval = (Datum) 0;
650  fcinfo->isnull = true;
651  }
652  else if (!estate.retisnull)
653  {
654  /*
655  * Cast result value to function's declared result type, and copy it
656  * out to the upper executor memory context. We must treat tuple
657  * results specially in order to deal with cases like rowtypes
658  * involving dropped columns.
659  */
660  if (estate.retistuple)
661  {
662  /* Don't need coercion if rowtype is known to match */
663  if (func->fn_rettype == estate.rettype &&
664  func->fn_rettype != RECORDOID)
665  {
666  /*
667  * Copy the tuple result into upper executor memory context.
668  * However, if we have a R/W expanded datum, we can just
669  * transfer its ownership out to the upper context.
670  */
671  estate.retval = SPI_datumTransfer(estate.retval,
672  false,
673  -1);
674  }
675  else
676  {
677  /*
678  * Need to look up the expected result type. XXX would be
679  * better to cache the tupdesc instead of repeating
680  * get_call_result_type(), but the only easy place to save it
681  * is in the PLpgSQL_function struct, and that's too
682  * long-lived: composite types could change during the
683  * existence of a PLpgSQL_function.
684  */
685  Oid resultTypeId;
686  TupleDesc tupdesc;
687 
688  switch (get_call_result_type(fcinfo, &resultTypeId, &tupdesc))
689  {
690  case TYPEFUNC_COMPOSITE:
691  /* got the expected result rowtype, now coerce it */
692  coerce_function_result_tuple(&estate, tupdesc);
693  break;
695  /* got the expected result rowtype, now coerce it */
696  coerce_function_result_tuple(&estate, tupdesc);
697  /* and check domain constraints */
698  /* XXX allowing caching here would be good, too */
699  domain_check(estate.retval, false, resultTypeId,
700  NULL, NULL);
701  break;
702  case TYPEFUNC_RECORD:
703 
704  /*
705  * Failed to determine actual type of RECORD. We
706  * could raise an error here, but what this means in
707  * practice is that the caller is expecting any old
708  * generic rowtype, so we don't really need to be
709  * restrictive. Pass back the generated result as-is.
710  */
711  estate.retval = SPI_datumTransfer(estate.retval,
712  false,
713  -1);
714  break;
715  default:
716  /* shouldn't get here if retistuple is true ... */
717  elog(ERROR, "return type must be a row type");
718  break;
719  }
720  }
721  }
722  else
723  {
724  /* Scalar case: use exec_cast_value */
725  estate.retval = exec_cast_value(&estate,
726  estate.retval,
727  &fcinfo->isnull,
728  estate.rettype,
729  -1,
730  func->fn_rettype,
731  -1);
732 
733  /*
734  * If the function's return type isn't by value, copy the value
735  * into upper executor memory context. However, if we have a R/W
736  * expanded datum, we can just transfer its ownership out to the
737  * upper executor context.
738  */
739  if (!fcinfo->isnull && !func->fn_retbyval)
740  estate.retval = SPI_datumTransfer(estate.retval,
741  false,
742  func->fn_rettyplen);
743  }
744  }
745  else
746  {
747  /*
748  * We're returning a NULL, which normally requires no conversion work
749  * regardless of datatypes. But, if we are casting it to a domain
750  * return type, we'd better check that the domain's constraints pass.
751  */
752  if (func->fn_retisdomain)
753  estate.retval = exec_cast_value(&estate,
754  estate.retval,
755  &fcinfo->isnull,
756  estate.rettype,
757  -1,
758  func->fn_rettype,
759  -1);
760  }
761 
762  estate.err_text = gettext_noop("during function exit");
763 
764  /*
765  * Let the instrumentation plugin peek at this function
766  */
767  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
768  ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
769 
770  /* Clean up any leftover temporary memory */
771  plpgsql_destroy_econtext(&estate);
772  exec_eval_cleanup(&estate);
773  /* stmt_mcontext will be destroyed when function's main context is */
774 
775  /*
776  * Pop the error context stack
777  */
778  error_context_stack = plerrcontext.previous;
779 
780  /*
781  * Return the function's result
782  */
783  return estate.retval;
784 }
bool fn_retbyval
Definition: plpgsql.h:978
PLpgSQL_datum ** datums
Definition: plpgsql.h:1002
int fn_argvarnos[FUNC_MAX_ARGS]
Definition: plpgsql.h:986
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:111
#define IsA(nodeptr, _type_)
Definition: nodes.h:587
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:207
PLpgSQL_stmt_block * action
Definition: plpgsql.h:1006
PLpgSQL_type * datatype
Definition: plpgsql.h:319
Datum expand_array(Datum arraydatum, MemoryContext parentcontext, ArrayMetaState *metacache)
static void plpgsql_exec_error_callback(void *arg)
Definition: pl_exec.c:1214
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define gettext_noop(x)
Definition: c.h:1197
int errcode(int sqlerrcode)
Definition: elog.c:698
static void assign_simple_var(PLpgSQL_execstate *estate, PLpgSQL_var *var, Datum newvalue, bool isnull, bool freeable)
Definition: pl_exec.c:8400
static void exec_eval_cleanup(PLpgSQL_execstate *estate)
Definition: pl_exec.c:4077
unsigned int Oid
Definition: postgres_ext.h:31
static void exec_move_row_from_datum(PLpgSQL_execstate *estate, PLpgSQL_variable *target, Datum value)
Definition: pl_exec.c:7353
PLpgSQL_datum_type dtype
Definition: plpgsql.h:276
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
static void copy_plpgsql_datums(PLpgSQL_execstate *estate, PLpgSQL_function *func)
Definition: pl_exec.c:1272
PLpgSQL_datum ** datums
Definition: plpgsql.h:1057
ErrorContextCallback * error_context_stack
Definition: elog.c:93
Datum TransferExpandedObject(Datum d, MemoryContext new_parent)
static int exec_toplevel_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
Definition: pl_exec.c:1596
TupleDesc tuple_store_desc
Definition: plpgsql.h:1043
static Datum exec_cast_value(PLpgSQL_execstate *estate, Datum value, bool *isnull, Oid valtype, int32 valtypmod, Oid reqtype, int32 reqtypmod)
Definition: pl_exec.c:7650
#define ERROR
Definition: elog.h:46
MemoryContext tuple_store_cxt
Definition: plpgsql.h:1044
fmNodePtr resultinfo
Definition: fmgr.h:89
Datum SPI_datumTransfer(Datum value, bool typByVal, int typLen)
Definition: spi.c:1266
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
static void plpgsql_destroy_econtext(PLpgSQL_execstate *estate)
Definition: pl_exec.c:8309
ResourceOwner procedure_resowner
Definition: plpgsql.h:1074
ReturnSetInfo * rsi
Definition: plpgsql.h:1046
Datum value
Definition: postgres.h:422
PLpgSQL_plugin ** plpgsql_plugin_ptr
Definition: pl_handler.c:56
Tuplestorestate * tuple_store
Definition: plpgsql.h:1042
bool fn_retisdomain
Definition: plpgsql.h:980
void(* func_beg)(PLpgSQL_execstate *estate, PLpgSQL_function *func)
Definition: plpgsql.h:1130
bool typisarray
Definition: plpgsql.h:206
uintptr_t Datum
Definition: postgres.h:411
const char * err_text
Definition: plpgsql.h:1091
void domain_check(Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt)
Definition: domains.c:327
Datum value
Definition: plpgsql.h:332
#define ereport(elevel,...)
Definition: elog.h:157
void(* func_end)(PLpgSQL_execstate *estate, PLpgSQL_function *func)
Definition: plpgsql.h:1131
int allowedModes
Definition: execnodes.h:306
SetFunctionReturnMode returnMode
Definition: execnodes.h:308
#define VARATT_IS_EXTERNAL_EXPANDED_RO(PTR)
Definition: postgres.h:331
static void plpgsql_estate_setup(PLpgSQL_execstate *estate, PLpgSQL_function *func, ReturnSetInfo *rsi, EState *simple_eval_estate, ResourceOwner simple_eval_resowner)
Definition: pl_exec.c:3929
static void exec_set_found(PLpgSQL_execstate *estate, bool state)
Definition: pl_exec.c:8224
Tuplestorestate * setResult
Definition: execnodes.h:311
#define DatumGetPointer(X)
Definition: postgres.h:593
MemoryContext datum_context
Definition: plpgsql.h:1059
TupleDesc setDesc
Definition: execnodes.h:312
int errmsg(const char *fmt,...)
Definition: elog.c:909
static void exec_move_row(PLpgSQL_execstate *estate, PLpgSQL_variable *target, HeapTuple tup, TupleDesc tupdesc)
Definition: pl_exec.c:6679
#define elog(elevel,...)
Definition: elog.h:232
int i
#define VARATT_IS_EXTERNAL_EXPANDED_RW(PTR)
Definition: postgres.h:333
static void coerce_function_result_tuple(PLpgSQL_execstate *estate, TupleDesc tupdesc)
Definition: pl_exec.c:795
bool isnull
Definition: plpgsql.h:333

◆ plpgsql_exec_get_datum_type()

Oid plpgsql_exec_get_datum_type ( PLpgSQL_execstate estate,
PLpgSQL_datum datum 
)

Definition at line 5391 of file pl_exec.c.

References PLpgSQL_var::datatype, PLpgSQL_execstate::datums, PLpgSQL_datum::dtype, elog, ExpandedRecordHeader::er_tupdesc_id, ExpandedRecordHeader::er_typeid, ereport, PLpgSQL_rec::erh, errcode(), errmsg(), ERROR, expanded_record_lookup_field(), PLpgSQL_recfield::fieldname, PLpgSQL_recfield::finfo, ExpandedRecordFieldInfo::ftypeid, instantiate_empty_record_variable(), InvalidOid, PLPGSQL_DTYPE_PROMISE, PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_RECFIELD, PLPGSQL_DTYPE_VAR, PLpgSQL_recfield::recparentno, PLpgSQL_recfield::rectupledescid, PLpgSQL_rec::rectypeid, PLpgSQL_rec::refname, PLpgSQL_type::typoid, and unlikely.

Referenced by exec_stmt_foreach_a().

5393 {
5394  Oid typeid;
5395 
5396  switch (datum->dtype)
5397  {
5398  case PLPGSQL_DTYPE_VAR:
5399  case PLPGSQL_DTYPE_PROMISE:
5400  {
5401  PLpgSQL_var *var = (PLpgSQL_var *) datum;
5402 
5403  typeid = var->datatype->typoid;
5404  break;
5405  }
5406 
5407  case PLPGSQL_DTYPE_REC:
5408  {
5409  PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
5410 
5411  if (rec->erh == NULL || rec->rectypeid != RECORDOID)
5412  {
5413  /* Report variable's declared type */
5414  typeid = rec->rectypeid;
5415  }
5416  else
5417  {
5418  /* Report record's actual type if declared RECORD */
5419  typeid = rec->erh->er_typeid;
5420  }
5421  break;
5422  }
5423 
5425  {
5426  PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
5427  PLpgSQL_rec *rec;
5428 
5429  rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
5430 
5431  /*
5432  * If record variable is NULL, instantiate it if it has a
5433  * named composite type, else complain. (This won't change
5434  * the logical state of the record: it's still NULL.)
5435  */
5436  if (rec->erh == NULL)
5437  instantiate_empty_record_variable(estate, rec);
5438 
5439  /*
5440  * Look up the field's properties if we have not already, or
5441  * if the tuple descriptor ID changed since last time.
5442  */
5443  if (unlikely(recfield->rectupledescid != rec->erh->er_tupdesc_id))
5444  {
5446  recfield->fieldname,
5447  &recfield->finfo))
5448  ereport(ERROR,
5449  (errcode(ERRCODE_UNDEFINED_COLUMN),
5450  errmsg("record \"%s\" has no field \"%s\"",
5451  rec->refname, recfield->fieldname)));
5452  recfield->rectupledescid = rec->erh->er_tupdesc_id;
5453  }
5454 
5455  typeid = recfield->finfo.ftypeid;
5456  break;
5457  }
5458 
5459  default:
5460  elog(ERROR, "unrecognized dtype: %d", datum->dtype);
5461  typeid = InvalidOid; /* keep compiler quiet */
5462  break;
5463  }
5464 
5465  return typeid;
5466 }
bool expanded_record_lookup_field(ExpandedRecordHeader *erh, const char *fieldname, ExpandedRecordFieldInfo *finfo)
PLpgSQL_type * datatype
Definition: plpgsql.h:319
static void instantiate_empty_record_variable(PLpgSQL_execstate *estate, PLpgSQL_rec *rec)
Definition: pl_exec.c:7586
ExpandedRecordHeader * erh
Definition: plpgsql.h:413
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
PLpgSQL_datum_type dtype
Definition: plpgsql.h:276
char * refname
Definition: plpgsql.h:392
PLpgSQL_datum ** datums
Definition: plpgsql.h:1057
#define ERROR
Definition: elog.h:46
uint64 rectupledescid
Definition: plpgsql.h:428
ExpandedRecordFieldInfo finfo
Definition: plpgsql.h:429
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
#define unlikely(x)
Definition: c.h:273
Oid rectypeid
Definition: plpgsql.h:406
char * fieldname
Definition: plpgsql.h:425
Oid typoid
Definition: plpgsql.h:200

◆ plpgsql_exec_get_datum_type_info()

void plpgsql_exec_get_datum_type_info ( PLpgSQL_execstate estate,
PLpgSQL_datum datum,
Oid typeId,
int32 typMod,
Oid collation 
)

Definition at line 5476 of file pl_exec.c.

References PLpgSQL_type::atttypmod, PLpgSQL_type::collation, PLpgSQL_var::datatype, PLpgSQL_execstate::datums, PLpgSQL_datum::dtype, elog, ExpandedRecordHeader::er_tupdesc_id, ExpandedRecordHeader::er_typeid, ereport, PLpgSQL_rec::erh, errcode(), errmsg(), ERROR, expanded_record_lookup_field(), ExpandedRecordFieldInfo::fcollation, PLpgSQL_recfield::fieldname, PLpgSQL_recfield::finfo, ExpandedRecordFieldInfo::ftypeid, ExpandedRecordFieldInfo::ftypmod, instantiate_empty_record_variable(), InvalidOid, PLPGSQL_DTYPE_PROMISE, PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_RECFIELD, PLPGSQL_DTYPE_VAR, PLpgSQL_recfield::recparentno, PLpgSQL_recfield::rectupledescid, PLpgSQL_rec::rectypeid, PLpgSQL_rec::refname, PLpgSQL_type::typoid, and unlikely.

Referenced by make_datum_param().

5479 {
5480  switch (datum->dtype)
5481  {
5482  case PLPGSQL_DTYPE_VAR:
5483  case PLPGSQL_DTYPE_PROMISE:
5484  {
5485  PLpgSQL_var *var = (PLpgSQL_var *) datum;
5486 
5487  *typeId = var->datatype->typoid;
5488  *typMod = var->datatype->atttypmod;
5489  *collation = var->datatype->collation;
5490  break;
5491  }
5492 
5493  case PLPGSQL_DTYPE_REC:
5494  {
5495  PLpgSQL_rec *rec = (PLpgSQL_rec *) datum;
5496 
5497  if (rec->erh == NULL || rec->rectypeid != RECORDOID)
5498  {
5499  /* Report variable's declared type */
5500  *typeId = rec->rectypeid;
5501  *typMod = -1;
5502  }
5503  else
5504  {
5505  /* Report record's actual type if declared RECORD */
5506  *typeId = rec->erh->er_typeid;
5507  /* do NOT return the mutable typmod of a RECORD variable */
5508  *typMod = -1;
5509  }
5510  /* composite types are never collatable */
5511  *collation = InvalidOid;
5512  break;
5513  }
5514 
5516  {
5517  PLpgSQL_recfield *recfield = (PLpgSQL_recfield *) datum;
5518  PLpgSQL_rec *rec;
5519 
5520  rec = (PLpgSQL_rec *) (estate->datums[recfield->recparentno]);
5521 
5522  /*
5523  * If record variable is NULL, instantiate it if it has a
5524  * named composite type, else complain. (This won't change
5525  * the logical state of the record: it's still NULL.)
5526  */
5527  if (rec->erh == NULL)
5528  instantiate_empty_record_variable(estate, rec);
5529 
5530  /*
5531  * Look up the field's properties if we have not already, or
5532  * if the tuple descriptor ID changed since last time.
5533  */
5534  if (unlikely(recfield->rectupledescid != rec->erh->er_tupdesc_id))
5535  {
5537  recfield->fieldname,
5538  &recfield->finfo))
5539  ereport(ERROR,
5540  (errcode(ERRCODE_UNDEFINED_COLUMN),
5541  errmsg("record \"%s\" has no field \"%s\"",
5542  rec->refname, recfield->fieldname)));
5543  recfield->rectupledescid = rec->erh->er_tupdesc_id;
5544  }
5545 
5546  *typeId = recfield->finfo.ftypeid;
5547  *typMod = recfield->finfo.ftypmod;
5548  *collation = recfield->finfo.fcollation;
5549  break;
5550  }
5551 
5552  default:
5553  elog(ERROR, "unrecognized dtype: %d", datum->dtype);
5554  *typeId = InvalidOid; /* keep compiler quiet */
5555  *typMod = -1;
5556  *collation = InvalidOid;
5557  break;
5558  }
5559 }
bool expanded_record_lookup_field(ExpandedRecordHeader *erh, const char *fieldname, ExpandedRecordFieldInfo *finfo)
PLpgSQL_type * datatype
Definition: plpgsql.h:319
static void instantiate_empty_record_variable(PLpgSQL_execstate *estate, PLpgSQL_rec *rec)
Definition: pl_exec.c:7586
ExpandedRecordHeader * erh
Definition: plpgsql.h:413
int errcode(int sqlerrcode)
Definition: elog.c:698
PLpgSQL_datum_type dtype
Definition: plpgsql.h:276
char * refname
Definition: plpgsql.h:392
PLpgSQL_datum ** datums
Definition: plpgsql.h:1057
#define ERROR
Definition: elog.h:46
uint64 rectupledescid
Definition: plpgsql.h:428
Oid collation
Definition: plpgsql.h:205
ExpandedRecordFieldInfo finfo
Definition: plpgsql.h:429
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909
int32 atttypmod
Definition: plpgsql.h:207
#define elog(elevel,...)
Definition: elog.h:232
#define unlikely(x)
Definition: c.h:273
Oid rectypeid
Definition: plpgsql.h:406
char * fieldname
Definition: plpgsql.h:425
Oid typoid
Definition: plpgsql.h:200

◆ plpgsql_exec_trigger()

HeapTuple plpgsql_exec_trigger ( PLpgSQL_function func,
TriggerData trigdata 
)

Definition at line 906 of file pl_exec.c.

References PLpgSQL_function::action, ErrorContextCallback::arg, Assert, ErrorContextCallback::callback, TupleDescData::constr, convert_tuples_by_position(), copy_plpgsql_datums(), PLpgSQL_execstate::datum_context, DatumGetEOHP(), DatumGetPointer, PLpgSQL_execstate::datums, deconstruct_composite_datum(), elog, ER_MAGIC, ExpandedRecordHeader::er_magic, ereport, PLpgSQL_rec::erh, PLpgSQL_execstate::err_text, errcode(), errmsg(), ERROR, error_context_stack, exec_eval_cleanup(), exec_set_found(), exec_toplevel_block(), execute_attr_map_tuple(), expanded_record_get_tupdesc(), expanded_record_get_tuple(), expanded_record_set_field_internal(), expanded_record_set_tuple(), PLpgSQL_plugin::func_beg, PLpgSQL_plugin::func_end, gettext_noop, TupleConstr::has_generated_stored, i, make_expanded_record_from_exprecord(), make_expanded_record_from_tupdesc(), TupleDescData::natts, PLpgSQL_function::new_varno, PLpgSQL_function::old_varno, plpgsql_destroy_econtext(), plpgsql_estate_setup(), plpgsql_exec_error_callback(), plpgsql_plugin_ptr, PLPGSQL_RC_RETURN, ErrorContextCallback::previous, RelationGetDescr, ReleaseTupleDesc, PLpgSQL_execstate::retisnull, PLpgSQL_execstate::retisset, PLpgSQL_execstate::rettype, PLpgSQL_execstate::retval, SPI_copytuple(), SPI_register_trigger_data(), TriggerData::tg_event, TriggerData::tg_newtuple, TriggerData::tg_relation, TriggerData::tg_trigtuple, PLpgSQL_execstate::trigdata, TRIGGER_FIRED_BEFORE, TRIGGER_FIRED_BY_DELETE, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_UPDATE, TRIGGER_FIRED_FOR_ROW, TupleDescAttr, type_is_rowtype(), and VARATT_IS_EXTERNAL_EXPANDED.

Referenced by plpgsql_call_handler().

908 {
909  PLpgSQL_execstate estate;
910  ErrorContextCallback plerrcontext;
911  int rc;
912  TupleDesc tupdesc;
913  PLpgSQL_rec *rec_new,
914  *rec_old;
915  HeapTuple rettup;
916 
917  /*
918  * Setup the execution state
919  */
920  plpgsql_estate_setup(&estate, func, NULL, NULL, NULL);
921  estate.trigdata = trigdata;
922 
923  /*
924  * Setup error traceback support for ereport()
925  */
926  plerrcontext.callback = plpgsql_exec_error_callback;
927  plerrcontext.arg = &estate;
928  plerrcontext.previous = error_context_stack;
929  error_context_stack = &plerrcontext;
930 
931  /*
932  * Make local execution copies of all the datums
933  */
934  estate.err_text = gettext_noop("during initialization of execution state");
935  copy_plpgsql_datums(&estate, func);
936 
937  /*
938  * Put the OLD and NEW tuples into record variables
939  *
940  * We set up expanded records for both variables even though only one may
941  * have a value. This allows record references to succeed in functions
942  * that are used for multiple trigger types. For example, we might have a
943  * test like "if (TG_OP = 'INSERT' and NEW.foo = 'xyz')", which should
944  * work regardless of the current trigger type. If a value is actually
945  * fetched from an unsupplied tuple, it will read as NULL.
946  */
947  tupdesc = RelationGetDescr(trigdata->tg_relation);
948 
949  rec_new = (PLpgSQL_rec *) (estate.datums[func->new_varno]);
950  rec_old = (PLpgSQL_rec *) (estate.datums[func->old_varno]);
951 
952  rec_new->erh = make_expanded_record_from_tupdesc(tupdesc,
953  estate.datum_context);
954  rec_old->erh = make_expanded_record_from_exprecord(rec_new->erh,
955  estate.datum_context);
956 
957  if (!TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
958  {
959  /*
960  * Per-statement triggers don't use OLD/NEW variables
961  */
962  }
963  else if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
964  {
965  expanded_record_set_tuple(rec_new->erh, trigdata->tg_trigtuple,
966  false, false);
967  }
968  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
969  {
970  expanded_record_set_tuple(rec_new->erh, trigdata->tg_newtuple,
971  false, false);
972  expanded_record_set_tuple(rec_old->erh, trigdata->tg_trigtuple,
973  false, false);
974 
975  /*
976  * In BEFORE trigger, stored generated columns are not computed yet,
977  * so make them null in the NEW row. (Only needed in UPDATE branch;
978  * in the INSERT case, they are already null, but in UPDATE, the field
979  * still contains the old value.) Alternatively, we could construct a
980  * whole new row structure without the generated columns, but this way
981  * seems more efficient and potentially less confusing.
982  */
983  if (tupdesc->constr && tupdesc->constr->has_generated_stored &&
984  TRIGGER_FIRED_BEFORE(trigdata->tg_event))
985  {
986  for (int i = 0; i < tupdesc->natts; i++)
987  if (TupleDescAttr(tupdesc, i)->attgenerated == ATTRIBUTE_GENERATED_STORED)
989  i + 1,
990  (Datum) 0,
991  true, /* isnull */
992  false, false);
993  }
994  }
995  else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
996  {
997  expanded_record_set_tuple(rec_old->erh, trigdata->tg_trigtuple,
998  false, false);
999  }
1000  else
1001  elog(ERROR, "unrecognized trigger action: not INSERT, DELETE, or UPDATE");
1002 
1003  /* Make transition tables visible to this SPI connection */
1004  rc = SPI_register_trigger_data(trigdata);
1005  Assert(rc >= 0);
1006 
1007  estate.err_text = gettext_noop("during function entry");
1008 
1009  /*
1010  * Set the magic variable FOUND to false
1011  */
1012  exec_set_found(&estate, false);
1013 
1014  /*
1015  * Let the instrumentation plugin peek at this function
1016  */
1017  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_beg)
1018  ((*plpgsql_plugin_ptr)->func_beg) (&estate, func);
1019 
1020  /*
1021  * Now call the toplevel block of statements
1022  */
1023  estate.err_text = NULL;
1024  rc = exec_toplevel_block(&estate, func->action);
1025  if (rc != PLPGSQL_RC_RETURN)
1026  {
1027  estate.err_text = NULL;
1028  ereport(ERROR,
1029  (errcode(ERRCODE_S_R_E_FUNCTION_EXECUTED_NO_RETURN_STATEMENT),
1030  errmsg("control reached end of trigger procedure without RETURN")));
1031  }
1032 
1033  estate.err_text = gettext_noop("during function exit");
1034 
1035  if (estate.retisset)
1036  ereport(ERROR,
1037  (errcode(ERRCODE_DATATYPE_MISMATCH),
1038  errmsg("trigger procedure cannot return a set")));
1039 
1040  /*
1041  * Check that the returned tuple structure has the same attributes, the
1042  * relation that fired the trigger has. A per-statement trigger always
1043  * needs to return NULL, so we ignore any return value the function itself
1044  * produces (XXX: is this a good idea?)
1045  *
1046  * XXX This way it is possible, that the trigger returns a tuple where
1047  * attributes don't have the correct atttypmod's length. It's up to the
1048  * trigger's programmer to ensure that this doesn't happen. Jan
1049  */
1050  if (estate.retisnull || !TRIGGER_FIRED_FOR_ROW(trigdata->tg_event))
1051  rettup = NULL;
1052  else
1053  {
1054  TupleDesc retdesc;
1055  TupleConversionMap *tupmap;
1056 
1057  /* We assume exec_stmt_return verified that result is composite */
1058  Assert(type_is_rowtype(estate.rettype));
1059 
1060  /* We can special-case expanded records for speed */
1062  {
1064 
1065  Assert(erh->er_magic == ER_MAGIC);
1066 
1067  /* Extract HeapTuple and TupleDesc */
1068  rettup = expanded_record_get_tuple(erh);
1069  Assert(rettup);
1070  retdesc = expanded_record_get_tupdesc(erh);
1071 
1072  if (retdesc != RelationGetDescr(trigdata->tg_relation))
1073  {
1074  /* check rowtype compatibility */
1075  tupmap = convert_tuples_by_position(retdesc,
1076  RelationGetDescr(trigdata->tg_relation),
1077  gettext_noop("returned row structure does not match the structure of the triggering table"));
1078  /* it might need conversion */
1079  if (tupmap)
1080  rettup = execute_attr_map_tuple(rettup, tupmap);
1081  /* no need to free map, we're about to return anyway */
1082  }
1083 
1084  /*
1085  * Copy tuple to upper executor memory. But if user just did
1086  * "return new" or "return old" without changing anything, there's
1087  * no need to copy; we can return the original tuple (which will
1088  * save a few cycles in trigger.c as well as here).
1089  */
1090  if (rettup != trigdata->tg_newtuple &&
1091  rettup != trigdata->tg_trigtuple)
1092  rettup = SPI_copytuple(rettup);
1093  }
1094  else
1095  {
1096  /* Convert composite datum to a HeapTuple and TupleDesc */
1097  HeapTupleData tmptup;
1098 
1099  retdesc = deconstruct_composite_datum(estate.retval, &tmptup);
1100  rettup = &tmptup;
1101 
1102  /* check rowtype compatibility */
1103  tupmap = convert_tuples_by_position(retdesc,
1104  RelationGetDescr(trigdata->tg_relation),
1105  gettext_noop("returned row structure does not match the structure of the triggering table"));
1106  /* it might need conversion */
1107  if (tupmap)
1108  rettup = execute_attr_map_tuple(rettup, tupmap);
1109 
1110  ReleaseTupleDesc(retdesc);
1111  /* no need to free map, we're about to return anyway */
1112 
1113  /* Copy tuple to upper executor memory */
1114  rettup = SPI_copytuple(rettup);
1115  }
1116  }
1117 
1118  /*
1119  * Let the instrumentation plugin peek at this function
1120  */
1121  if (*plpgsql_plugin_ptr && (*plpgsql_plugin_ptr)->func_end)
1122  ((*plpgsql_plugin_ptr)->func_end) (&estate, func);
1123 
1124  /* Clean up any leftover temporary memory */
1125  plpgsql_destroy_econtext(&estate);
1126  exec_eval_cleanup(&estate);
1127  /* stmt_mcontext will be destroyed when function's main context is */
1128 
1129  /*
1130  * Pop the error context stack
1131  */
1132  error_context_stack = plerrcontext.previous;
1133 
1134  /*
1135  * Return the trigger's result
1136  */
1137  return rettup;
1138 }
void expanded_record_set_field_internal(ExpandedRecordHeader *erh, int fnumber, Datum newValue, bool isnull, bool expand_external, bool check_constraints)
HeapTuple expanded_record_get_tuple(ExpandedRecordHeader *erh)
#define VARATT_IS_EXTERNAL_EXPANDED(PTR)
Definition: postgres.h:335
void expanded_record_set_tuple(ExpandedRecordHeader *erh, HeapTuple tuple, bool copy, bool expand_external)
#define RelationGetDescr(relation)
Definition: rel.h:503
TupleConversionMap * convert_tuples_by_position(TupleDesc indesc, TupleDesc outdesc, const char *msg)
Definition: tupconvert.c:59
PLpgSQL_stmt_block * action
Definition: plpgsql.h:1006
TriggerData * trigdata
Definition: plpgsql.h:1024
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static void plpgsql_exec_error_callback(void *arg)
Definition: pl_exec.c:1214
#define gettext_noop(x)
Definition: c.h:1197
ExpandedRecordHeader * erh
Definition: plpgsql.h:413
static TupleDesc deconstruct_composite_datum(Datum value, HeapTupleData *tmptup)
Definition: pl_exec.c:7322
int errcode(int sqlerrcode)
Definition: elog.c:698
HeapTuple SPI_copytuple(HeapTuple tuple)
Definition: spi.c:952
static void exec_eval_cleanup(PLpgSQL_execstate *estate)
Definition: pl_exec.c:4077
void(* callback)(void *arg)
Definition: elog.h:247
struct ErrorContextCallback * previous
Definition: elog.h:246
static void copy_plpgsql_datums(PLpgSQL_execstate *estate, PLpgSQL_function *func)
Definition: pl_exec.c:1272
HeapTuple tg_trigtuple
Definition: trigger.h:35
PLpgSQL_datum ** datums
Definition: plpgsql.h:1057
ErrorContextCallback * error_context_stack
Definition: elog.c:93
static int exec_toplevel_block(PLpgSQL_execstate *estate, PLpgSQL_stmt_block *block)
Definition: pl_exec.c:1596
bool has_generated_stored
Definition: tupdesc.h:45
#define ERROR
Definition: elog.h:46
TupleConstr * constr
Definition: tupdesc.h:85
static void plpgsql_destroy_econtext(PLpgSQL_execstate *estate)
Definition: pl_exec.c:8309
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2602
HeapTuple execute_attr_map_tuple(HeapTuple tuple, TupleConversionMap *map)
Definition: tupconvert.c:139
int SPI_register_trigger_data(TriggerData *tdata)
Definition: spi.c:3240
PLpgSQL_plugin ** plpgsql_plugin_ptr
Definition: pl_handler.c:56
static TupleDesc expanded_record_get_tupdesc(ExpandedRecordHeader *erh)
ExpandedObjectHeader * DatumGetEOHP(Datum d)
Definition: expandeddatum.c:29
#define TRIGGER_FIRED_BY_DELETE(event)
Definition: trigger.h:112
void(* func_beg)(PLpgSQL_execstate *estate, PLpgSQL_function *func)
Definition: plpgsql.h:1130
uintptr_t Datum
Definition: postgres.h:411
#define ER_MAGIC
ExpandedRecordHeader * make_expanded_record_from_exprecord(ExpandedRecordHeader *olderh, MemoryContext parentcontext)
ExpandedRecordHeader * make_expanded_record_from_tupdesc(TupleDesc tupdesc, MemoryContext parentcontext)
const char * err_text
Definition: plpgsql.h:1091
HeapTuple tg_newtuple
Definition: trigger.h:36
#define ereport(elevel,...)
Definition: elog.h:157
void(* func_end)(PLpgSQL_execstate *estate, PLpgSQL_function *func)
Definition: plpgsql.h:1131
#define Assert(condition)
Definition: c.h:804
TriggerEvent tg_event
Definition: trigger.h:33
static void plpgsql_estate_setup(PLpgSQL_execstate *estate, PLpgSQL_function *func, ReturnSetInfo *rsi, EState *simple_eval_estate, ResourceOwner simple_eval_resowner)
Definition: pl_exec.c:3929
static void exec_set_found(PLpgSQL_execstate *estate, bool state)
Definition: pl_exec.c:8224
#define DatumGetPointer(X)
Definition: postgres.h:593
MemoryContext datum_context
Definition: plpgsql.h:1059
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:127
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:109
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define elog(elevel,...)
Definition: elog.h:232
int i
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:121
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:115
Relation tg_relation
Definition: trigger.h:34

◆ plpgsql_free_function_memory()

void plpgsql_free_function_memory ( PLpgSQL_function func)

Definition at line 725 of file pl_funcs.c.

References PLpgSQL_function::action, Assert, PLpgSQL_var::cursor_explicit_expr, PLpgSQL_function::datums, PLpgSQL_var::default_val, PLpgSQL_rec::default_val, PLpgSQL_datum::dtype, elog, ERROR, PLpgSQL_function::fn_cxt, free_block(), free_expr(), i, MemoryContextDelete(), PLpgSQL_function::ndatums, PLPGSQL_DTYPE_PROMISE, PLPGSQL_DTYPE_REC, PLPGSQL_DTYPE_RECFIELD, PLPGSQL_DTYPE_ROW, PLPGSQL_DTYPE_VAR, and PLpgSQL_function::use_count.

Referenced by delete_function(), and plpgsql_inline_handler().

726 {
727  int i;
728 
729  /* Better not call this on an in-use function */
730  Assert(func->use_count == 0);
731 
732  /* Release plans associated with variable declarations */
733  for (i = 0; i < func->ndatums; i++)
734  {
735  PLpgSQL_datum *d = func->datums[i];
736 
737  switch (d->dtype)
738  {
739  case PLPGSQL_DTYPE_VAR:
741  {
742  PLpgSQL_var *var = (PLpgSQL_var *) d;
743 
744  free_expr(var->default_val);
746  }
747  break;
748  case PLPGSQL_DTYPE_ROW:
749  break;
750  case PLPGSQL_DTYPE_REC:
751  {
752  PLpgSQL_rec *rec = (PLpgSQL_rec *) d;
753 
754  free_expr(rec->default_val);
755  }
756  break;
758  break;
759  default:
760  elog(ERROR, "unrecognized data type: %d", d->dtype);
761  }
762  }
763  func->ndatums = 0;
764 
765  /* Release plans in statement tree */
766  if (func->action)
767  free_block(func->action);
768  func->action = NULL;
769 
770  /*
771  * And finally, release all memory except the PLpgSQL_function struct
772  * itself (which has to be kept around because there may be multiple
773  * fn_extra pointers to it).
774  */
775  if (func->fn_cxt)
777  func->fn_cxt = NULL;
778 }
PLpgSQL_datum ** datums
Definition: plpgsql.h:1002
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:218
PLpgSQL_stmt_block * action
Definition: plpgsql.h:1006
PLpgSQL_expr * cursor_explicit_expr
Definition: plpgsql.h:326
PLpgSQL_datum_type dtype
Definition: plpgsql.h:276
#define ERROR
Definition: elog.h:46
PLpgSQL_expr * default_val
Definition: plpgsql.h:316
unsigned long use_count
Definition: plpgsql.h:1014
static void free_expr(PLpgSQL_expr *expr)
Definition: pl_funcs.c:715
MemoryContext fn_cxt
Definition: plpgsql.h:974
#define Assert(condition)
Definition: c.h:804
#define elog(elevel,...)
Definition: elog.h:232
int i
PLpgSQL_expr * default_val
Definition: plpgsql.h:396
static void free_block(PLpgSQL_stmt_block *block)
Definition: pl_funcs.c:478

◆ plpgsql_getdiag_kindname()

const char* plpgsql_getdiag_kindname ( PLpgSQL_getdiag_kind  kind)

Definition at line 300 of file pl_funcs.c.

References free_assert(), free_assign(), free_block(), free_call(), free_case(), free_close(), free_commit(), free_dynexecute(), free_dynfors(), free_execsql(), free_exit(), free_expr(), free_fetch(), free_forc(), free_foreach_a(), free_fori(), free_fors(), free_getdiag(), free_if(), free_loop(), free_open(), free_perform(), free_raise(), free_return(), free_return_next(), free_return_query(), free_rollback(), free_stmt(), free_while(), PLPGSQL_GETDIAG_COLUMN_NAME, PLPGSQL_GETDIAG_CONSTRAINT_NAME, PLPGSQL_GETDIAG_CONTEXT, PLPGSQL_GETDIAG_DATATYPE_NAME, PLPGSQL_GETDIAG_ERROR_CONTEXT, PLPGSQL_GETDIAG_ERROR_DETAIL, PLPGSQL_GETDIAG_ERROR_HINT, PLPGSQL_GETDIAG_MESSAGE_TEXT, PLPGSQL_GETDIAG_RETURNED_SQLSTATE, PLPGSQL_GETDIAG_ROW_COUNT, PLPGSQL_GETDIAG_SCHEMA_NAME, and PLPGSQL_GETDIAG_TABLE_NAME.

Referenced by dump_getdiag().

301 {
302  switch (kind)
303  {
305  return "ROW_COUNT";
307  return "PG_CONTEXT";
309  return "PG_EXCEPTION_CONTEXT";
311  return "PG_EXCEPTION_DETAIL";
313  return "PG_EXCEPTION_HINT";
315  return "RETURNED_SQLSTATE";
317  return "COLUMN_NAME";
319  return "CONSTRAINT_NAME";
321  return "PG_DATATYPE_NAME";
323  return "MESSAGE_TEXT";
325  return "TABLE_NAME";
327  return "SCHEMA_NAME";
328  }
329 
330  return "unknown";
331 }

◆ plpgsql_HashTableInit()

void plpgsql_HashTableInit ( void  )

Definition at line 2589 of file pl_comp.c.

References Assert, HASHCTL::entrysize, FUNCS_PER_USER, HASH_BLOBS, hash_create(), HASH_ELEM, and HASHCTL::keysize.

Referenced by _PG_init().

2590 {
2591  HASHCTL ctl;
2592 
2593  /* don't allow double-initialization */
2594  Assert(plpgsql_HashTable == NULL);
2595 
2596  ctl.keysize = sizeof(PLpgSQL_func_hashkey);
2597  ctl.entrysize = sizeof(plpgsql_HashEnt);
2598  plpgsql_HashTable = hash_create("PLpgSQL function hash",
2600  &ctl,
2601  HASH_ELEM | HASH_BLOBS);
2602 }
#define FUNCS_PER_USER
Definition: pl_comp.c:70
#define HASH_ELEM
Definition: hsearch.h:95
Size entrysize
Definition: hsearch.h:76
static HTAB * plpgsql_HashTable
Definition: pl_comp.c:62
struct plpgsql_hashent plpgsql_HashEnt
struct PLpgSQL_func_hashkey PLpgSQL_func_hashkey
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:349
#define HASH_BLOBS
Definition: hsearch.h:97
Size keysize
Definition: hsearch.h:75
#define Assert(condition)
Definition: c.h:804

◆ plpgsql_latest_lineno()

int plpgsql_latest_lineno ( void  )

Definition at line 572 of file pl_scanner.c.

References cur_line_num.

Referenced by plpgsql_compile_error_callback().

573 {
574  return cur_line_num;
575 }
static int cur_line_num
Definition: pl_scanner.c:128

◆ plpgsql_location_to_lineno()

int plpgsql_location_to_lineno ( int  location)

Definition at line 538 of file pl_scanner.c.

References cur_line_end, cur_line_num, cur_line_start, location_lineno_init(), and scanorig.

539 {
540  const char *loc;
541 
542  if (location < 0 || scanorig == NULL)
543  return 0; /* garbage in, garbage out */
544  loc = scanorig + location;
545 
546  /* be correct, but not fast, if input location goes backwards */
547  if (loc < cur_line_start)
549 
550  while (cur_line_end != NULL && loc > cur_line_end)
551  {
553  cur_line_num++;
554  cur_line_end = strchr(cur_line_start, '\n');
555  }
556 
557  return cur_line_num;
558 }
static int cur_line_num
Definition: pl_scanner.c:128
static void location_lineno_init(void)
Definition: pl_scanner.c:562
static const char * cur_line_start
Definition: pl_scanner.c:126
static const char * scanorig
Definition: pl_scanner.c:110
static const char * cur_line_end
Definition: pl_scanner.c:127

◆ plpgsql_ns_additem()

void plpgsql_ns_additem ( PLpgSQL_nsitem_type  itemtype,
int  itemno,
const char *  name 
)

Definition at line 92 of file pl_funcs.c.

References Assert, PLpgSQL_nsitem::itemno, PLpgSQL_nsitem::itemtype, PLpgSQL_nsitem::name, ns_top, offsetof, palloc(), PLPGSQL_NSTYPE_LABEL, and PLpgSQL_nsitem::prev.

Referenced by add_parameter_name(), plpgsql_build_record(), plpgsql_build_variable(), and plpgsql_ns_push().

93 {
94  PLpgSQL_nsitem *nse;
95 
96  Assert(name != NULL);
97  /* first item added must be a label */
98  Assert(ns_top != NULL || itemtype == PLPGSQL_NSTYPE_LABEL);
99 
100  nse = palloc(offsetof(PLpgSQL_nsitem, name) + strlen(name) + 1);
101  nse->itemtype = itemtype;
102  nse->itemno = itemno;
103  nse->prev = ns_top;
104  strcpy(nse->name, name);
105  ns_top = nse;
106 }
static PLpgSQL_nsitem * ns_top
Definition: pl_funcs.c:35
struct PLpgSQL_nsitem * prev
Definition: plpgsql.h:445
#define Assert(condition)
Definition: c.h:804
const char * name
Definition: encode.c:561
void * palloc(Size size)
Definition: mcxt.c:1062
PLpgSQL_nsitem_type itemtype
Definition: plpgsql.h:438
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: plpgsql.h:446
#define offsetof(type, field)
Definition: c.h:727

◆ plpgsql_ns_find_nearest_loop()

PLpgSQL_nsitem* plpgsql_ns_find_nearest_loop ( PLpgSQL_nsitem ns_cur)

Definition at line 214 of file pl_funcs.c.

References PLpgSQL_nsitem::itemno, PLpgSQL_nsitem::itemtype, PLPGSQL_LABEL_LOOP, PLPGSQL_NSTYPE_LABEL, and PLpgSQL_nsitem::prev.

215 {
216  while (ns_cur != NULL)
217  {
218  if (ns_cur->itemtype == PLPGSQL_NSTYPE_LABEL &&
219  ns_cur->itemno == PLPGSQL_LABEL_LOOP)
220  return ns_cur;
221  ns_cur = ns_cur->prev;
222  }
223 
224  return NULL; /* no loop found */
225 }
struct PLpgSQL_nsitem * prev
Definition: plpgsql.h:445
PLpgSQL_nsitem_type itemtype
Definition: plpgsql.h:438

◆ plpgsql_ns_init()

void plpgsql_ns_init ( void  )

Definition at line 43 of file pl_funcs.c.

Referenced by do_compile(), and plpgsql_compile_inline().

44 {
45  ns_top = NULL;
46 }
static PLpgSQL_nsitem * ns_top
Definition: pl_funcs.c:35

◆ plpgsql_ns_lookup()

PLpgSQL_nsitem* plpgsql_ns_lookup ( PLpgSQL_nsitem ns_cur,
bool  localmode,
const char *  name1,
const char *  name2,
const char *  name3,
int *  names_used 
)

Definition at line 130 of file pl_funcs.c.

References PLpgSQL_nsitem::itemtype, PLpgSQL_nsitem::name, PLPGSQL_NSTYPE_LABEL, PLPGSQL_NSTYPE_VAR, and PLpgSQL_nsitem::prev.

Referenced by add_parameter_name(), plpgsql_param_ref(), plpgsql_parse_cwordtype(), plpgsql_parse_dblword(), plpgsql_parse_tripword(), plpgsql_parse_word(), plpgsql_parse_wordtype(), and resolve_column_ref().

133 {
134  /* Outer loop iterates once per block level in the namespace chain */
135  while (ns_cur != NULL)
136  {
137  PLpgSQL_nsitem *nsitem;
138 
139  /* Check this level for unqualified match to variable name */
140  for (nsitem = ns_cur;
141  nsitem->itemtype != PLPGSQL_NSTYPE_LABEL;
142  nsitem = nsitem->prev)
143  {
144  if (strcmp(nsitem->name, name1) == 0)
145  {
146  if (name2 == NULL ||
147  nsitem->itemtype != PLPGSQL_NSTYPE_VAR)
148  {
149  if (names_used)
150  *names_used = 1;
151  return nsitem;
152  }
153  }
154  }
155 
156  /* Check this level for qualified match to variable name */
157  if (name2 != NULL &&
158  strcmp(nsitem->name, name1) == 0)
159  {
160  for (nsitem = ns_cur;
161  nsitem->itemtype != PLPGSQL_NSTYPE_LABEL;
162  nsitem = nsitem->prev)
163  {
164  if (strcmp(nsitem->name, name2) == 0)
165  {
166  if (name3 == NULL ||
167  nsitem->itemtype != PLPGSQL_NSTYPE_VAR)
168  {
169  if (names_used)
170  *names_used = 2;
171  return nsitem;
172  }
173  }
174  }
175  }
176 
177  if (localmode)
178  break; /* do not look into upper levels */
179 
180  ns_cur = nsitem->prev;
181  }
182 
183  /* This is just to suppress possibly-uninitialized-variable warnings */
184  if (names_used)
185  *names_used = 0;
186  return NULL; /* No match found */
187 }
struct PLpgSQL_nsitem * prev
Definition: plpgsql.h:445
PLpgSQL_nsitem_type itemtype
Definition: plpgsql.h:438
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: plpgsql.h:446

◆ plpgsql_ns_lookup_label()

PLpgSQL_nsitem* plpgsql_ns_lookup_label ( PLpgSQL_nsitem ns_cur,
const char *  name 
)

Definition at line 195 of file pl_funcs.c.

References PLpgSQL_nsitem::itemtype, PLpgSQL_nsitem::name, PLPGSQL_NSTYPE_LABEL, and PLpgSQL_nsitem::prev.

196 {
197  while (ns_cur != NULL)
198  {
199  if (ns_cur->itemtype == PLPGSQL_NSTYPE_LABEL &&
200  strcmp(ns_cur->name, name) == 0)
201  return ns_cur;
202  ns_cur = ns_cur->prev;
203  }
204 
205  return NULL; /* label not found */
206 }
struct PLpgSQL_nsitem * prev
Definition: plpgsql.h:445
const char * name
Definition: encode.c:561
PLpgSQL_nsitem_type itemtype
Definition: plpgsql.h:438
char name[FLEXIBLE_ARRAY_MEMBER]
Definition: plpgsql.h:446

◆ plpgsql_ns_pop()

void plpgsql_ns_pop ( void  )

Definition at line 67 of file pl_funcs.c.

References Assert, PLpgSQL_nsitem::itemtype, PLPGSQL_NSTYPE_LABEL, and PLpgSQL_nsitem::prev.

68 {
69  Assert(ns_top != NULL);
71  ns_top = ns_top->prev;
72  ns_top = ns_top->prev;
73 }
static PLpgSQL_nsitem * ns_top
Definition: pl_funcs.c:35
struct PLpgSQL_nsitem * prev
Definition: plpgsql.h:445
#define Assert(condition)
Definition: c.h:804
PLpgSQL_nsitem_type itemtype
Definition: plpgsql.h:438

◆ plpgsql_ns_push()

void plpgsql_ns_push ( const char *  label,
PLpgSQL_label_type  label_type 
)

Definition at line 54 of file pl_funcs.c.

References plpgsql_ns_additem(), and PLPGSQL_NSTYPE_LABEL.

Referenced by do_compile(), and plpgsql_compile_inline().

55 {
56  if (label == NULL)
57  label = "";
58  plpgsql_ns_additem(PLPGSQL_NSTYPE_LABEL, (int) label_type, label);
59 }
void plpgsql_ns_additem(PLpgSQL_nsitem_type itemtype, int itemno, const char *name)
Definition: pl_funcs.c:92
static char * label

◆ plpgsql_ns_top()

PLpgSQL_nsitem* plpgsql_ns_top ( void  )

Definition at line 81 of file pl_funcs.c.

References ns_top.

Referenced by add_parameter_name(), plpgsql_parse_cwordtype(), plpgsql_parse_dblword(), plpgsql_parse_tripword(), plpgsql_parse_word(), and plpgsql_parse_wordtype().

82 {
83  return ns_top;
84 }
static PLpgSQL_nsitem * ns_top
Definition: pl_funcs.c:35

◆ plpgsql_parse_cwordrowtype()

PLpgSQL_type* plpgsql_parse_cwordrowtype ( List idents)

Definition at line 1839 of file pl_comp.c.

References ereport, errcode(), errmsg(), ERROR, get_rel_type_id(), InvalidOid, linitial, list_length(), lsecond, makeRangeVar(), makeTypeNameFromNameList(), MemoryContextSwitchTo(), NoLock, OidIsValid, plpgsql_build_datatype(), RangeVarGetRelid, and strVal.

1840 {
1841  Oid classOid;
1842  Oid typOid;
1843  RangeVar *relvar;
1844  MemoryContext oldCxt;
1845 
1846  /*
1847  * As above, this is a relation lookup but could be a type lookup if we
1848  * weren't being backwards-compatible about error wording.
1849  */
1850  if (list_length(idents) != 2)
1851  return NULL;
1852 
1853  /* Avoid memory leaks in long-term function context */
1855 
1856  /* Look up relation name. Can't lock it - we might not have privileges. */
1857  relvar = makeRangeVar(strVal(linitial(idents)),
1858  strVal(lsecond(idents)),
1859  -1);
1860  classOid = RangeVarGetRelid(relvar, NoLock, false);
1861 
1862  /* Some relkinds lack type OIDs */
1863  typOid = get_rel_type_id(classOid);
1864  if (!OidIsValid(typOid))
1865  ereport(ERROR,
1866  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1867  errmsg("relation \"%s\" does not have a composite type",
1868  strVal(lsecond(idents)))));
1869 
1870  MemoryContextSwitchTo(oldCxt);
1871 
1872  /* Build and return the row type struct */
1873  return plpgsql_build_datatype(typOid, -1, InvalidOid,
1874  makeTypeNameFromNameList(idents));
1875 }
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:79
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define strVal(v)
Definition: value.h:65
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define lsecond(l)
Definition: pg_list.h:179
#define linitial(l)
Definition: pg_list.h:174
#define ERROR
Definition: elog.h:46
#define NoLock
Definition: lockdefs.h:34
PLpgSQL_type * plpgsql_build_datatype(Oid typeOid, int32 typmod, Oid collation, TypeName *origtypname)
Definition: pl_comp.c:2091
Oid get_rel_type_id(Oid relid)
Definition: lsyscache.c:1950
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:157
static int list_length(const List *l)
Definition: pg_list.h:149
int errmsg(const char *fmt,...)
Definition: elog.c:909
TypeName * makeTypeNameFromNameList(List *names)
Definition: makefuncs.c:456
MemoryContext plpgsql_compile_tmp_cxt
Definition: pl_comp.c:56
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:422

◆ plpgsql_parse_cwordtype()

PLpgSQL_type* plpgsql_parse_cwordtype ( List idents)

Definition at line 1682 of file pl_comp.c.

References build_datatype(), elog, ERROR, GETSTRUCT, HeapTupleIsValid, PLpgSQL_nsitem::itemno, PLpgSQL_nsitem::itemtype, linitial, list_length(), lsecond, lthird, makeRangeVar(), MemoryContextSwitchTo(), NoLock, ObjectIdGetDatum, OidIsValid, plpgsql_ns_lookup(), plpgsql_ns_top(), PLPGSQL_NSTYPE_VAR, RangeVarGetRelid, ReleaseSysCache(), RelnameGetRelid(), RELOID, SearchSysCache1(), SearchSysCacheAttName(), strVal, and TYPEOID.

1683 {
1684  PLpgSQL_type *dtype = NULL;
1685  PLpgSQL_nsitem *nse;
1686  const char *fldname;
1687  Oid classOid;
1688  HeapTuple classtup = NULL;
1689  HeapTuple attrtup = NULL;
1690  HeapTuple typetup = NULL;
1691  Form_pg_class classStruct;
1692  Form_pg_attribute attrStruct;
1693  MemoryContext oldCxt;
1694 
1695  /* Avoid memory leaks in the long-term function context */
1697 
1698  if (list_length(idents) == 2)
1699  {
1700  /*
1701  * Do a lookup in the current namespace stack. We don't need to check
1702  * number of names matched, because we will only consider scalar
1703  * variables.
1704  */
1705  nse = plpgsql_ns_lookup(plpgsql_ns_top(), false,
1706  strVal(linitial(idents)),
1707  strVal(lsecond(idents)),
1708  NULL,
1709  NULL);
1710 
1711  if (nse != NULL && nse->itemtype == PLPGSQL_NSTYPE_VAR)
1712  {
1713  dtype = ((PLpgSQL_var *) (plpgsql_Datums[nse->itemno]))->datatype;
1714  goto done;
1715  }
1716 
1717  /*
1718  * First word could also be a table name
1719  */
1720  classOid = RelnameGetRelid(strVal(linitial(idents)));
1721  if (!OidIsValid(classOid))
1722  goto done;
1723  fldname = strVal(lsecond(idents));
1724  }
1725  else if (list_length(idents) == 3)
1726  {
1727  RangeVar *relvar;
1728 
1729  relvar = makeRangeVar(strVal(linitial(idents)),
1730  strVal(lsecond(idents)),
1731  -1);
1732  /* Can't lock relation - we might not have privileges. */
1733  classOid = RangeVarGetRelid(relvar, NoLock, true);
1734  if (!OidIsValid(classOid))
1735  goto done;
1736  fldname = strVal(lthird(idents));
1737  }
1738  else
1739  goto done;
1740 
1741  classtup = SearchSysCache1(RELOID, ObjectIdGetDatum(classOid));
1742  if (!HeapTupleIsValid(classtup))
1743  goto done;
1744  classStruct = (Form_pg_class) GETSTRUCT(classtup);
1745 
1746  /*
1747  * It must be a relation, sequence, view, materialized view, composite
1748  * type, or foreign table
1749  */
1750  if (classStruct->relkind != RELKIND_RELATION &&
1751  classStruct->relkind != RELKIND_SEQUENCE &&
1752  classStruct->relkind != RELKIND_VIEW &&
1753  classStruct->relkind != RELKIND_MATVIEW &&
1754  classStruct->relkind != RELKIND_COMPOSITE_TYPE &&
1755  classStruct->relkind != RELKIND_FOREIGN_TABLE &&
1756  classStruct->relkind != RELKIND_PARTITIONED_TABLE)
1757  goto done;
1758 
1759  /*
1760  * Fetch the named table field and its type
1761  */
1762  attrtup = SearchSysCacheAttName(classOid, fldname);
1763  if (!HeapTupleIsValid(attrtup))
1764  goto done;
1765  attrStruct = (Form_pg_attribute) GETSTRUCT(attrtup);
1766 
1767  typetup = SearchSysCache1(TYPEOID,
1768  ObjectIdGetDatum(attrStruct->atttypid));
1769  if (!HeapTupleIsValid(typetup))
1770  elog(ERROR, "cache lookup failed for type %u", attrStruct->atttypid);
1771 
1772  /*
1773  * Found that - build a compiler type struct in the caller's cxt and
1774  * return it. Note that we treat the type as being found-by-OID; no
1775  * attempt to re-look-up the type name will happen during invalidations.
1776  */
1777  MemoryContextSwitchTo(oldCxt);
1778  dtype = build_datatype(typetup,
1779  attrStruct->atttypmod,
1780  attrStruct->attcollation,
1781  NULL);
1783 
1784 done:
1785  if (HeapTupleIsValid(classtup))
1786  ReleaseSysCache(classtup);
1787  if (HeapTupleIsValid(attrtup))
1788  ReleaseSysCache(attrtup);
1789  if (HeapTupleIsValid(typetup))
1790  ReleaseSysCache(typetup);
1791 
1792  MemoryContextSwitchTo(oldCxt);
1793  return dtype;
1794 }
PLpgSQL_datum ** plpgsql_Datums
Definition: pl_comp.c:46
PLpgSQL_nsitem * plpgsql_ns_top(void)
Definition: pl_funcs.c:81
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
Oid RelnameGetRelid(const char *relname)
Definition: namespace.c:681
static PLpgSQL_type * build_datatype(HeapTuple typeTup, int32 typmod, Oid collation, TypeName *origtypname)
Definition: pl_comp.c:2113
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:79
PLpgSQL_nsitem * plpgsql_ns_lookup(PLpgSQL_nsitem *ns_cur, bool localmode, const char *name1, const char *name2, const char *name3, int *names_used)
Definition: pl_funcs.c:130
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define strVal(v)
Definition: value.h:65
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define lsecond(l)
Definition: pg_list.h:179
#define linitial(l)
Definition: pg_list.h:174
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define ERROR
Definition: elog.h:46
#define NoLock
Definition: lockdefs.h:34
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:207
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1127
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static int list_length(const List *l)
Definition: pg_list.h:149
HeapTuple SearchSysCacheAttName(Oid relid, const char *attname)
Definition: syscache.c:1268
FormData_pg_class * Form_pg_class
Definition: pg_class.h:153
PLpgSQL_nsitem_type itemtype
Definition: plpgsql.h:438
#define elog(elevel,...)
Definition: elog.h:232
MemoryContext plpgsql_compile_tmp_cxt
Definition: pl_comp.c:56
#define lthird(l)
Definition: pg_list.h:184
RangeVar * makeRangeVar(char *schemaname, char *relname, int location)
Definition: makefuncs.c:422

◆ plpgsql_parse_dblword()

bool plpgsql_parse_dblword ( char *  word1,
char *  word2,
PLwdatum wdatum,
PLcword cword 
)

Definition at line 1451 of file pl_comp.c.

References PLwdatum::datum, PLwdatum::ident, IDENTIFIER_LOOKUP_DECLARE, PLcword::idents, PLwdatum::idents, PLpgSQL_nsitem::itemno, PLpgSQL_nsitem::itemtype, list_make2, makeString(), plpgsql_build_recfield(), plpgsql_IdentifierLookup, plpgsql_ns_lookup(), plpgsql_ns_top(), PLPGSQL_NSTYPE_REC, PLPGSQL_NSTYPE_VAR, and PLwdatum::quoted.

Referenced by plpgsql_yylex().

1453 {
1454  PLpgSQL_nsitem *ns;
1455  List *idents;
1456  int nnames;
1457 
1458  idents = list_make2(makeString(word1),
1459  makeString(word2));
1460 
1461  /*
1462  * We should do nothing in DECLARE sections. In SQL expressions, we
1463  * really only need to make sure that RECFIELD datums are created when
1464  * needed. In all the cases handled by this function, returning a T_DATUM
1465  * with a two-word idents string is the right thing.
1466  */
1468  {
1469  /*
1470  * Do a lookup in the current namespace stack
1471  */
1472  ns = plpgsql_ns_lookup(plpgsql_ns_top(), false,
1473  word1, word2, NULL,
1474  &nnames);
1475  if (ns != NULL)
1476  {
1477  switch (ns->itemtype)
1478  {
1479  case PLPGSQL_NSTYPE_VAR:
1480  /* Block-qualified reference to scalar variable. */
1481  wdatum->datum = plpgsql_Datums[ns->itemno];
1482  wdatum->ident = NULL;
1483  wdatum->quoted = false; /* not used */
1484  wdatum->idents = idents;
1485  return true;
1486 
1487  case PLPGSQL_NSTYPE_REC:
1488  if (nnames == 1)
1489  {
1490  /*
1491  * First word is a record name, so second word could
1492  * be a field in this record. We build a RECFIELD
1493  * datum whether it is or not --- any error will be
1494  * detected later.
1495  */
1496  PLpgSQL_rec *rec;
1497  PLpgSQL_recfield *new;
1498 
1499  rec = (PLpgSQL_rec *) (plpgsql_Datums[ns->itemno]);
1500  new = plpgsql_build_recfield(rec, word2);
1501 
1502  wdatum->datum = (PLpgSQL_datum *) new;
1503  }
1504  else
1505  {
1506  /* Block-qualified reference to record variable. */
1507  wdatum->datum = plpgsql_Datums[ns->itemno];
1508  }
1509  wdatum->ident = NULL;
1510  wdatum->quoted = false; /* not used */
1511  wdatum->idents = idents;
1512  return true;
1513 
1514  default:
1515  break;
1516  }
1517  }
1518  }
1519 
1520  /* Nothing found */
1521  cword->idents = idents;
1522  return false;
1523 }
#define list_make2(x1, x2)
Definition: pg_list.h:208
PLpgSQL_datum ** plpgsql_Datums
Definition: pl_comp.c:46
PLpgSQL_nsitem * plpgsql_ns_top(void)
Definition: pl_funcs.c:81
PLpgSQL_nsitem * plpgsql_ns_lookup(PLpgSQL_nsitem *ns_cur, bool localmode, const char *name1, const char *name2, const char *name3, int *names_used)
Definition: pl_funcs.c:130
PLpgSQL_recfield * plpgsql_build_recfield(PLpgSQL_rec *rec, const char *fldname)
Definition: pl_comp.c:2044
String * makeString(char *str)
Definition: value.c:51
List * idents
Definition: plpgsql.h:1153
List * idents
Definition: plpgsql.h:1161
IdentifierLookup plpgsql_IdentifierLookup
Definition: pl_scanner.c:26
PLpgSQL_datum * datum
Definition: plpgsql.h:1158
bool quoted
Definition: plpgsql.h:1160
char * ident
Definition: plpgsql.h:1159
PLpgSQL_nsitem_type itemtype
Definition: plpgsql.h:438
Definition: pg_list.h:50

◆ plpgsql_parse_err_condition()

PLpgSQL_condition* plpgsql_parse_err_condition ( char *  condname)

Definition at line 2265 of file pl_comp.c.

References ereport, errcode(), errmsg(), ERROR, i, ExceptionLabelMap::label, label, palloc(), and ExceptionLabelMap::sqlerrstate.

2266 {
2267  int i;
2268  PLpgSQL_condition *new;
2269  PLpgSQL_condition *prev;
2270 
2271  /*
2272  * XXX Eventually we will want to look for user-defined exception names
2273  * here.
2274  */
2275 
2276  /*
2277  * OTHERS is represented as code 0 (which would map to '00000', but we
2278  * have no need to represent that as an exception condition).
2279  */
2280  if (strcmp(condname, "others") == 0)
2281  {
2282  new = palloc(sizeof(PLpgSQL_condition));
2283  new->sqlerrstate = 0;
2284  new->condname = condname;
2285  new->next = NULL;
2286  return new;
2287  }
2288 
2289  prev = NULL;
2290  for (i = 0; exception_label_map[i].label != NULL; i++)
2291  {
2292  if (strcmp(condname, exception_label_map[i].label) == 0)
2293  {
2294  new = palloc(sizeof(PLpgSQL_condition));
2295  new->sqlerrstate = exception_label_map[i].sqlerrstate;
2296  new->condname = condname;
2297  new->next = prev;
2298  prev = new;
2299  }
2300  }
2301 
2302  if (!prev)
2303  ereport(ERROR,
2304  (errcode(ERRCODE_UNDEFINED_OBJECT),
2305  errmsg("unrecognized exception condition \"%s\"",
2306  condname)));
2307 
2308  return prev;
2309 }
const char * label
Definition: pl_comp.c:78
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
static char * label
#define ereport(elevel,...)
Definition: elog.h:157
static const ExceptionLabelMap exception_label_map[]
Definition: pl_comp.c:82
void * palloc(Size size)
Definition: mcxt.c:1062
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i

◆ plpgsql_parse_tripword()

bool plpgsql_parse_tripword ( char *  word1,
char *  word2,
char *  word3,
PLwdatum wdatum,
PLcword cword 
)

Definition at line 1532 of file pl_comp.c.

References PLwdatum::datum, PLwdatum::ident, IDENTIFIER_LOOKUP_DECLARE, PLcword::idents, PLwdatum::idents, PLpgSQL_nsitem::itemno, PLpgSQL_nsitem::itemtype, list_make2, list_make3, makeString(), plpgsql_build_recfield(), plpgsql_IdentifierLookup, plpgsql_ns_lookup(), plpgsql_ns_top(), PLPGSQL_NSTYPE_REC, and PLwdatum::quoted.

Referenced by plpgsql_yylex().

1534 {
1535  PLpgSQL_nsitem *ns;
1536  List *idents;
1537  int nnames;
1538 
1539  /*
1540  * We should do nothing in DECLARE sections. In SQL expressions, we need
1541  * to make sure that RECFIELD datums are created when needed, and we need
1542  * to be careful about how many names are reported as belonging to the
1543  * T_DATUM: the third word could be a sub-field reference, which we don't
1544  * care about here.
1545  */
1547  {
1548  /*
1549  * Do a lookup in the current namespace stack. Must find a record
1550  * reference, else ignore.
1551  */
1552  ns = plpgsql_ns_lookup(plpgsql_ns_top(), false,
1553  word1, word2, word3,
1554  &nnames);
1555  if (ns != NULL)
1556  {
1557  switch (ns->itemtype)
1558  {
1559  case PLPGSQL_NSTYPE_REC:
1560  {
1561  PLpgSQL_rec *rec;
1562  PLpgSQL_recfield *new;
1563 
1564  rec = (PLpgSQL_rec *) (plpgsql_Datums[ns->itemno]);
1565  if (nnames == 1)
1566  {
1567  /*
1568  * First word is a record name, so second word
1569  * could be a field in this record (and the third,
1570  * a sub-field). We build a RECFIELD datum
1571  * whether it is or not --- any error will be
1572  * detected later.
1573  */
1574  new = plpgsql_build_recfield(rec, word2);
1575  idents = list_make2(makeString(word1),
1576  makeString(word2));
1577  }
1578  else
1579  {
1580  /* Block-qualified reference to record variable. */
1581  new = plpgsql_build_recfield(rec, word3);
1582  idents = list_make3(makeString(word1),
1583  makeString(word2),
1584  makeString(word3));
1585  }
1586  wdatum->datum = (PLpgSQL_datum *) new;
1587  wdatum->ident = NULL;
1588  wdatum->quoted = false; /* not used */
1589  wdatum->idents = idents;
1590  return true;
1591  }
1592 
1593  default:
1594  break;
1595  }
1596  }
1597  }
1598 
1599  /* Nothing found */
1600  idents = list_make3(makeString(word1),
1601  makeString(word2),
1602  makeString(word3));
1603  cword->idents = idents;
1604  return false;
1605 }
#define list_make2(x1, x2)
Definition: pg_list.h:208
#define list_make3(x1, x2, x3)
Definition: pg_list.h:210
PLpgSQL_datum ** plpgsql_Datums
Definition: pl_comp.c:46
PLpgSQL_nsitem * plpgsql_ns_top(void)
Definition: pl_funcs.c:81
PLpgSQL_nsitem * plpgsql_ns_lookup(PLpgSQL_nsitem *ns_cur, bool localmode, const char *name1, const char *name2, const char *name3, int *names_used)
Definition: pl_funcs.c:130
PLpgSQL_recfield * plpgsql_build_recfield(PLpgSQL_rec *rec, const char *fldname)
Definition: pl_comp.c:2044
String * makeString(char *str)
Definition: value.c:51
List * idents
Definition: plpgsql.h:1153
List * idents
Definition: plpgsql.h:1161
IdentifierLookup plpgsql_IdentifierLookup
Definition: pl_scanner.c:26
PLpgSQL_datum * datum
Definition: plpgsql.h:1158
bool quoted
Definition: plpgsql.h:1160
char * ident
Definition: plpgsql.h:1159
PLpgSQL_nsitem_type itemtype
Definition: plpgsql.h:438
Definition: pg_list.h:50

◆ plpgsql_parse_word()

bool plpgsql_parse_word ( char *  word1,
const char *  yytxt,
bool  lookup,
PLwdatum wdatum,
PLword word 
)

Definition at line 1396 of file pl_comp.c.

References PLwdatum::datum, elog, ERROR, PLword::ident, PLwdatum::ident, IDENTIFIER_LOOKUP_NORMAL, PLwdatum::idents, PLpgSQL_nsitem::itemno, PLpgSQL_nsitem::itemtype, NIL, plpgsql_IdentifierLookup, plpgsql_ns_lookup(), plpgsql_ns_top(), PLPGSQL_NSTYPE_REC, PLPGSQL_NSTYPE_VAR, PLword::quoted, and PLwdatum::quoted.

Referenced by plpgsql_yylex().

1398 {
1399  PLpgSQL_nsitem *ns;
1400 
1401  /*
1402  * We should not lookup variables in DECLARE sections. In SQL
1403  * expressions, there's no need to do so either --- lookup will happen
1404  * when the expression is compiled.
1405  */
1407  {
1408  /*
1409  * Do a lookup in the current namespace stack
1410  */
1411  ns = plpgsql_ns_lookup(plpgsql_ns_top(), false,
1412  word1, NULL, NULL,
1413  NULL);
1414 
1415  if (ns != NULL)
1416  {
1417  switch (ns->itemtype)
1418  {
1419  case PLPGSQL_NSTYPE_VAR:
1420  case PLPGSQL_NSTYPE_REC:
1421  wdatum->datum = plpgsql_Datums[ns->itemno];
1422  wdatum->ident = word1;
1423  wdatum->quoted = (yytxt[0] == '"');
1424  wdatum->idents = NIL;
1425  return true;
1426 
1427  default:
1428  /* plpgsql_ns_lookup should never return anything else */
1429  elog(ERROR, "unrecognized plpgsql itemtype: %d",
1430  ns->itemtype);
1431  }
1432  }
1433  }
1434 
1435  /*
1436  * Nothing found - up to now it's a word without any special meaning for
1437  * us.
1438  */
1439  word->ident = word1;
1440  word->quoted = (yytxt[0] == '"');
1441  return false;
1442 }
#define NIL
Definition: pg_list.h:65
PLpgSQL_datum ** plpgsql_Datums
Definition: pl_comp.c:46
PLpgSQL_nsitem * plpgsql_ns_top(void)
Definition: pl_funcs.c:81
PLpgSQL_nsitem * plpgsql_ns_lookup(PLpgSQL_nsitem *ns_cur, bool localmode, const char *name1, const char *name2, const char *name3, int *names_used)
Definition: pl_funcs.c:130
char * ident
Definition: plpgsql.h:1147
#define ERROR
Definition: elog.h:46
Definition: zic.c:301
List * idents
Definition: plpgsql.h:1161
IdentifierLookup plpgsql_IdentifierLookup
Definition: pl_scanner.c:26
bool quoted
Definition: plpgsql.h:1148
PLpgSQL_datum * datum
Definition: plpgsql.h:1158
bool quoted
Definition: plpgsql.h:1160
char * ident
Definition: plpgsql.h:1159
PLpgSQL_nsitem_type itemtype
Definition: plpgsql.h:438
#define elog(elevel,...)
Definition: elog.h:232

◆ plpgsql_parse_wordrowtype()

PLpgSQL_type* plpgsql_parse_wordrowtype ( char *  ident)

Definition at line 1802 of file pl_comp.c.

References ereport, errcode(), ERRCODE_UNDEFINED_TABLE, errmsg(), ERROR, get_rel_type_id(), InvalidOid, makeTypeName(), OidIsValid, plpgsql_build_datatype(), and RelnameGetRelid().

1803 {
1804  Oid classOid;
1805  Oid typOid;
1806 
1807  /*
1808  * Look up the relation. Note that because relation rowtypes have the
1809  * same names as their relations, this could be handled as a type lookup
1810  * equally well; we use the relation lookup code path only because the
1811  * errors thrown here have traditionally referred to relations not types.
1812  * But we'll make a TypeName in case we have to do re-look-up of the type.
1813  */
1814  classOid = RelnameGetRelid(ident);
1815  if (!OidIsValid(classOid))
1816  ereport(ERROR,
1818  errmsg("relation \"%s\" does not exist", ident)));
1819 
1820  /* Some relkinds lack type OIDs */
1821  typOid = get_rel_type_id(classOid);
1822  if (!OidIsValid(typOid))
1823  ereport(ERROR,
1824  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
1825  errmsg("relation \"%s\" does not have a composite type",
1826  ident)));
1827 
1828  /* Build and return the row type struct */
1829  return plpgsql_build_datatype(typOid, -1, InvalidOid,
1830  makeTypeName(ident));
1831 }
#define ERRCODE_UNDEFINED_TABLE
Definition: pgbench.c:78
Oid RelnameGetRelid(const char *relname)
Definition: namespace.c:681
int errcode(int sqlerrcode)
Definition: elog.c:698
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:710
#define ERROR
Definition: elog.h:46
PLpgSQL_type * plpgsql_build_datatype(Oid typeOid, int32 typmod, Oid collation, TypeName *origtypname)
Definition: pl_comp.c:2091
Oid get_rel_type_id(Oid relid)
Definition: lsyscache.c:1950
#define InvalidOid
Definition: postgres_ext.h:36
TypeName * makeTypeName(char *typnam)
Definition: makefuncs.c:444
#define ereport(elevel,...)
Definition: elog.h:157
int errmsg(const char *fmt,...)
Definition: elog.c:909

◆ plpgsql_parse_wordtype()

PLpgSQL_type* plpgsql_parse_wordtype ( char *  ident)

Definition at line 1616 of file pl_comp.c.

References build_datatype(), PLpgSQL_function::fn_input_collation, GETSTRUCT, InvalidOid, PLpgSQL_nsitem::itemno, PLpgSQL_nsitem::itemtype, LookupTypeName(), makeTypeName(), plpgsql_ns_lookup(), plpgsql_ns_top(), PLPGSQL_NSTYPE_VAR, and ReleaseSysCache().

1617 {
1618  PLpgSQL_type *dtype;
1619  PLpgSQL_nsitem *nse;
1620  TypeName *typeName;
1621  HeapTuple typeTup;
1622 
1623  /*
1624  * Do a lookup in the current namespace stack
1625  */
1626  nse = plpgsql_ns_lookup(plpgsql_ns_top(), false,
1627  ident, NULL, NULL,
1628  NULL);
1629 
1630  if (nse != NULL)
1631  {
1632  switch (nse->itemtype)
1633  {
1634  case PLPGSQL_NSTYPE_VAR:
1635  return ((PLpgSQL_var *) (plpgsql_Datums[nse->itemno]))->datatype;
1636 
1637  /* XXX perhaps allow REC/ROW here? */
1638 
1639  default:
1640  return NULL;
1641  }
1642  }
1643 
1644  /*
1645  * Word wasn't found in the namespace stack. Try to find a data type with
1646  * that name, but ignore shell types and complex types.
1647  */
1648  typeName = makeTypeName(ident);
1649  typeTup = LookupTypeName(NULL, typeName, NULL, false);
1650  if (typeTup)
1651  {
1652  Form_pg_type typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1653 
1654  if (!typeStruct->typisdefined ||
1655  typeStruct->typrelid != InvalidOid)
1656  {
1657  ReleaseSysCache(typeTup);
1658  return NULL;
1659  }
1660 
1661  dtype = build_datatype(typeTup, -1,
1663  typeName);
1664 
1665  ReleaseSysCache(typeTup);
1666  return dtype;
1667  }
1668 
1669  /*
1670  * Nothing found - up to now it's a word without any special meaning for
1671  * us.
1672  */
1673  return NULL;
1674 }
Type LookupTypeName(ParseState *pstate, const TypeName *typeName, int32 *typmod_p, bool missing_ok)
Definition: parse_type.c:38
PLpgSQL_datum ** plpgsql_Datums
Definition: pl_comp.c:46
PLpgSQL_nsitem * plpgsql_ns_top(void)
Definition: pl_funcs.c:81
#define GETSTRUCT(TUP)
Definition: htup_details.h:654
static PLpgSQL_type * build_datatype(HeapTuple typeTup, int32 typmod, Oid collation, TypeName *origtypname)
Definition: pl_comp.c:2113
PLpgSQL_nsitem * plpgsql_ns_lookup(PLpgSQL_nsitem *ns_cur, bool localmode, const char *name1, const char *name2, const char *name3, int *names_used)
Definition: pl_funcs.c:130
Oid fn_input_collation
Definition: plpgsql.h:972
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1175
#define InvalidOid
Definition: postgres_ext.h:36
TypeName * makeTypeName(char *typnam)
Definition: makefuncs.c:444
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
PLpgSQL_nsitem_type itemtype
Definition: plpgsql.h:438
PLpgSQL_function * plpgsql_curr_compile
Definition: pl_comp.c:53

◆ plpgsql_parser_setup()

void plpgsql_parser_setup ( struct ParseState pstate,
PLpgSQL_expr expr 
)

Definition at line 1081 of file pl_comp.c.

References ParseState::p_paramref_hook, ParseState::p_post_columnref_hook, ParseState::p_pre_columnref_hook, ParseState::p_ref_hook_state, plpgsql_param_ref(), plpgsql_post_column_ref(), and plpgsql_pre_column_ref().

Referenced by exec_prepare_plan(), and plpgsql_estate_setup().

1082 {
1086  /* no need to use p_coerce_param_hook */
1087  pstate->p_ref_hook_state = (void *) expr;
1088 }
static Node * plpgsql_pre_column_ref(ParseState *pstate, ColumnRef *cref)
Definition: pl_comp.c:1094
PostParseColumnRefHook p_post_columnref_hook
Definition: parse_node.h:221
ParseParamRefHook p_paramref_hook
Definition: parse_node.h:222
static Node * plpgsql_param_ref(ParseState *pstate, ParamRef *pref)
Definition: pl_comp.c:1152
void * p_ref_hook_state
Definition: parse_node.h:224
PreParseColumnRefHook p_pre_columnref_hook
Definition: parse_node.h:220
static Node * plpgsql_post_column_ref(ParseState *pstate, ColumnRef *cref, Node *var)
Definition: pl_comp.c:1108

◆ plpgsql_peek()

int plpgsql_peek ( void  )

Definition at line 420 of file pl_scanner.c.

References internal_yylex(), and push_back_token().

421 {
422  int tok1;
423  TokenAuxData aux1;
424 
425  tok1 = internal_yylex(&aux1);
426  push_back_token(tok1, &aux1);
427  return tok1;
428 }
static int internal_yylex(TokenAuxData *auxdata)
Definition: pl_scanner.c:308
static void push_back_token(int token, TokenAuxData *auxdata)
Definition: pl_scanner.c:354

◆ plpgsql_peek2()

void plpgsql_peek2 ( int *  tok1_p,
int *  tok2_p,
int *  tok1_loc,
int *  tok2_loc 
)

Definition at line 439 of file pl_scanner.c.

References internal_yylex(), TokenAuxData::lloc, and push_back_token().

440 {
441  int tok1,
442  tok2;
443  TokenAuxData aux1,
444  aux2;
445 
446  tok1 = internal_yylex(&aux1);
447  tok2 = internal_yylex(&aux2);
448 
449  *tok1_p = tok1;
450  if (tok1_loc)
451  *tok1_loc = aux1.lloc;
452  *tok2_p = tok2;
453  if (tok2_loc)
454  *tok2_loc = aux2.lloc;
455 
456  push_back_token(tok2, &aux2);
457  push_back_token(tok1, &aux1);
458 }
YYLTYPE lloc
Definition: pl_scanner.c:94
static int internal_yylex(TokenAuxData *auxdata)
Definition: pl_scanner.c:308
static void push_back_token(int token, TokenAuxData *auxdata)
Definition: pl_scanner.c:354

◆ plpgsql_push_back_token()

void plpgsql_push_back_token ( int  token)

Definition at line 370 of file pl_scanner.c.

References TokenAuxData::leng, TokenAuxData::lloc, TokenAuxData::lval, plpgsql_yyleng, and push_back_token().

371 {
372  TokenAuxData auxdata;
373 
374  auxdata.lval = plpgsql_yylval;
375  auxdata.lloc = plpgsql_yylloc;
376  auxdata.leng = plpgsql_yyleng;
377  push_back_token(token, &auxdata);
378 }
YYLTYPE lloc
Definition: pl_scanner.c:94
YYSTYPE lval
Definition: pl_scanner.c:93
static int plpgsql_yyleng
Definition: pl_scanner.c:113
static void push_back_token(int token, TokenAuxData *auxdata)
Definition: pl_scanner.c:354

◆ plpgsql_recognize_err_condition()

int plpgsql_recognize_err_condition ( const char *  condname,
bool  allow_sqlstate 
)

Definition at line 2229 of file pl_comp.c.

References ereport, errcode(), errmsg(), ERROR, i, ExceptionLabelMap::label, label, MAKE_SQLSTATE, and ExceptionLabelMap::sqlerrstate.

Referenced by exec_stmt_raise().

2230 {
2231  int i;
2232 
2233  if (allow_sqlstate)
2234  {
2235  if (strlen(condname) == 5 &&
2236  strspn(condname, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") == 5)
2237  return MAKE_SQLSTATE(condname[0],
2238  condname[1],
2239  condname[2],
2240  condname[3],
2241  condname[4]);
2242  }
2243 
2244  for (i = 0; exception_label_map[i].label != NULL; i++)
2245  {
2246  if (strcmp(condname, exception_label_map[i].label) == 0)
2248  }
2249 
2250  ereport(ERROR,
2251  (errcode(ERRCODE_UNDEFINED_OBJECT),
2252  errmsg("unrecognized exception condition \"%s\"",
2253  condname)));
2254  return 0; /* keep compiler quiet */
2255 }
const char * label
Definition: pl_comp.c:78
#define MAKE_SQLSTATE(ch1, ch2, ch3, ch4, ch5)
Definition: elog.h:64
int errcode(int sqlerrcode)
Definition: elog.c:698
#define ERROR
Definition: elog.h:46
static char * label
#define ereport(elevel,...)
Definition: elog.h:157
static const ExceptionLabelMap exception_label_map[]
Definition: pl_comp.c:82
int errmsg(const char *fmt,...)
Definition: elog.c:909
int i

◆ plpgsql_scanner_errposition()

int plpgsql_scanner_errposition ( int  location)

Definition at line 472 of file pl_scanner.c.

References internalerrposition(), internalerrquery(), pg_mbstrlen_with_len(), and scanorig.

Referenced by plpgsql_yyerror().

473 {
474  int pos;
475 
476  if (location < 0 || scanorig == NULL)
477  return 0; /* no-op if location is unknown */
478 
479  /* Convert byte offset to character number */
480  pos = pg_mbstrlen_with_len(scanorig, location) + 1;
481  /* And pass it to the ereport mechanism */
482  (void) internalerrposition(pos);
483  /* Also pass the function body string */
484  return internalerrquery(scanorig);
485 }
int pg_mbstrlen_with_len(const char *mbstr, int limit)
Definition: mbutils.c:1000
static const char * scanorig
Definition: pl_scanner.c:110
int internalerrquery(const char *query)
Definition: elog.c:1321
int internalerrposition(int cursorpos)
Definition: elog.c:1301

◆ plpgsql_scanner_finish()

void plpgsql_scanner_finish ( void  )

Definition at line 613 of file pl_scanner.c.

References scanner_finish(), scanorig, and yyscanner.

Referenced by do_compile(), and plpgsql_compile_inline().

614 {
615  /* release storage */
617  /* avoid leaving any dangling pointers */
618  yyscanner = NULL;
619  scanorig = NULL;
620 }
static const char * scanorig
Definition: pl_scanner.c:110
static core_yyscan_t yyscanner
Definition: pl_scanner.c:106
void scanner_finish(core_yyscan_t yyscanner)

◆ plpgsql_scanner_init()

void plpgsql_scanner_init ( const char *  str)

Definition at line 586 of file pl_scanner.c.

References IDENTIFIER_LOOKUP_NORMAL, location_lineno_init(), num_pushbacks, plpgsql_IdentifierLookup, plpgsql_yytoken, ReservedPLKeywordTokens, scanner_init(), scanorig, generate_unaccent_rules::str, and yyscanner.

Referenced by do_compile(), and plpgsql_compile_inline().

587 {
588  /* Start up the core scanner */
590  &ReservedPLKeywords, ReservedPLKeywordTokens);
591 
592  /*
593  * scanorig points to the original string, which unlike the scanner's
594  * scanbuf won't be modified on-the-fly by flex. Notice that although
595  * yytext points into scanbuf, we rely on being able to apply locations
596  * (offsets from string start) to scanorig as well.
597  */
598  scanorig = str;
599 
600  /* Other setup */
602  plpgsql_yytoken = 0;
603 
604  num_pushbacks = 0;
605 
607 }
static void location_lineno_init(void)
Definition: pl_scanner.c:562
static int plpgsql_yytoken
Definition: pl_scanner.c:116
core_yyscan_t scanner_init(const char *str, core_yy_extra_type *yyext, const ScanKeywordList *keywordlist, const uint16 *keyword_tokens)
static const char * scanorig
Definition: pl_scanner.c:110
IdentifierLookup plpgsql_IdentifierLookup
Definition: pl_scanner.c:26
static int num_pushbacks
Definition: pl_scanner.c:121
static core_yy_extra_type core_yy
Definition: pl_scanner.c:107
static const uint16 ReservedPLKeywordTokens[]
Definition: pl_scanner.c:66
static core_yyscan_t yyscanner
Definition: pl_scanner.c:106

◆ plpgsql_stmt_typename()

const char* plpgsql_stmt_typename ( PLpgSQL_stmt stmt)

Definition at line 232 of file pl_funcs.c.

References _, PLpgSQL_stmt::cmd_type, PLPGSQL_STMT_ASSERT, PLPGSQL_STMT_ASSIGN, PLPGSQL_STMT_BLOCK, PLPGSQL_STMT_CALL, PLPGSQL_STMT_CASE, PLPGSQL_STMT_CLOSE, PLPGSQL_STMT_COMMIT, PLPGSQL_STMT_DYNEXECUTE, PLPGSQL_STMT_DYNFORS, PLPGSQL_STMT_EXECSQL, PLPGSQL_STMT_EXIT, PLPGSQL_STMT_FETCH, PLPGSQL_STMT_FORC, PLPGSQL_STMT_FOREACH_A, PLPGSQL_STMT_FORI, PLPGSQL_STMT_FORS, PLPGSQL_STMT_GETDIAG, PLPGSQL_STMT_IF, PLPGSQL_STMT_LOOP, PLPGSQL_STMT_OPEN, PLPGSQL_STMT_PERFORM, PLPGSQL_STMT_RAISE, PLPGSQL_STMT_RETURN, PLPGSQL_STMT_RETURN_NEXT, PLPGSQL_STMT_RETURN_QUERY, PLPGSQL_STMT_ROLLBACK, and PLPGSQL_STMT_WHILE.

Referenced by plpgsql_exec_error_callback().

233 {
234  switch (stmt->cmd_type)
235  {
236  case PLPGSQL_STMT_BLOCK:
237  return _("statement block");
238  case PLPGSQL_STMT_ASSIGN:
239  return _("assignment");
240  case PLPGSQL_STMT_IF:
241  return "IF";
242  case PLPGSQL_STMT_CASE:
243  return "CASE";
244  case PLPGSQL_STMT_LOOP:
245  return "LOOP";
246  case PLPGSQL_STMT_WHILE:
247  return "WHILE";
248  case PLPGSQL_STMT_FORI:
249  return _("FOR with integer loop variable");
250  case PLPGSQL_STMT_FORS:
251  return _("FOR over SELECT rows");
252  case PLPGSQL_STMT_FORC:
253  return _("FOR over cursor");
255  return _("FOREACH over array");
256  case PLPGSQL_STMT_EXIT:
257  return ((PLpgSQL_stmt_exit *) stmt)->is_exit ? "EXIT" : "CONTINUE";
258  case PLPGSQL_STMT_RETURN:
259  return "RETURN";
261  return "RETURN NEXT";
263  return "RETURN QUERY";
264  case PLPGSQL_STMT_RAISE:
265  return "RAISE";
266  case PLPGSQL_STMT_ASSERT:
267  return "ASSERT";
269  return _("SQL statement");
271  return "EXECUTE";
273  return _("FOR over EXECUTE statement");
275  return ((PLpgSQL_stmt_getdiag *) stmt)->is_stacked ?
276  "GET STACKED DIAGNOSTICS" : "GET DIAGNOSTICS";
277  case PLPGSQL_STMT_OPEN:
278  return "OPEN";
279  case PLPGSQL_STMT_FETCH:
280  return ((PLpgSQL_stmt_fetch *) stmt)->is_move ? "MOVE" : "FETCH";
281  case PLPGSQL_STMT_CLOSE:
282  return "CLOSE";
284  return "PERFORM";
285  case PLPGSQL_STMT_CALL:
286  return ((PLpgSQL_stmt_call *) stmt)->is_call ? "CALL" : "DO";
287  case PLPGSQL_STMT_COMMIT:
288  return "COMMIT";
290  return "ROLLBACK";
291  }
292 
293  return "unknown";
294 }
PLpgSQL_stmt_type cmd_type
Definition: plpgsql.h:454
#define _(x)
Definition: elog.c:89

◆ plpgsql_subxact_cb()

void plpgsql_subxact_cb ( SubXactEvent  event,
SubTransactionId  mySubid,
SubTransactionId  parentSubid,
void *  arg 
)

Definition at line 8373 of file pl_exec.c.

References FreeExprContext(), SimpleEcontextStackEntry::next, pfree(), SimpleEcontextStackEntry::stack_econtext, SUBXACT_EVENT_ABORT_SUB, SUBXACT_EVENT_COMMIT_SUB, and SimpleEcontextStackEntry::xact_subxid.

Referenced by _PG_init(), and plpgsql_inline_handler().

8375 {
8376  if (event == SUBXACT_EVENT_COMMIT_SUB || event == SUBXACT_EVENT_ABORT_SUB)
8377  {
8378  while (simple_econtext_stack != NULL &&
8379  simple_econtext_stack->xact_subxid == mySubid)
8380  {
8382 
8384  (event == SUBXACT_EVENT_COMMIT_SUB));
8385  next = simple_econtext_stack->next;
8388  }
8389  }
8390 }
static int32 next
Definition: blutils.c:219
struct SimpleEcontextStackEntry * next
Definition: pl_exec.c:92
void pfree(void *pointer)
Definition: mcxt.c:1169
ExprContext * stack_econtext
Definition: pl_exec.c:90
static SimpleEcontextStackEntry * simple_econtext_stack
Definition: pl_exec.c:96
SubTransactionId xact_subxid
Definition: pl_exec.c:91
void FreeExprContext(ExprContext *econtext, bool isCommit)
Definition: execUtils.c:411

◆ plpgsql_token_is_unreserved_keyword()

bool plpgsql_token_is_unreserved_keyword ( int  token)

Definition at line 387 of file pl_scanner.c.

References i, lengthof, and UnreservedPLKeywordTokens.

388 {
389  int i;
390 
391  for (i = 0; i < lengthof(UnreservedPLKeywordTokens); i++)
392  {
393  if (UnreservedPLKeywordTokens[i] == token)
394  return true;
395  }
396  return false;
397 }
#define lengthof(array)
Definition: c.h:734
static const uint16 UnreservedPLKeywordTokens[]
Definition: pl_scanner.c:70
int i

◆ plpgsql_xact_cb()

void plpgsql_xact_cb ( XactEvent  event,
void *  arg 
)

Definition at line 8331 of file pl_exec.c.

References FreeExecutorState(), ResourceOwnerReleaseAllPlanCacheRefs(), XACT_EVENT_ABORT, XACT_EVENT_COMMIT, XACT_EVENT_PARALLEL_ABORT, XACT_EVENT_PARALLEL_COMMIT, and XACT_EVENT_PREPARE.

Referenced by _PG_init().

8332 {
8333  /*
8334  * If we are doing a clean transaction shutdown, free the EState and tell
8335  * the resowner to release whatever plancache references it has, so that
8336  * all remaining resources will be released correctly. (We don't need to
8337  * actually delete the resowner here; deletion of the
8338  * TopTransactionResourceOwner will take care of that.)
8339  *
8340  * In an abort, we expect the regular abort recovery procedures to release
8341  * everything of interest, so just clear our pointers.
8342  */
8343  if (event == XACT_EVENT_COMMIT ||
8344  event == XACT_EVENT_PARALLEL_COMMIT ||
8345  event == XACT_EVENT_PREPARE)
8346  {
8347  simple_econtext_stack = NULL;
8348 
8355  }
8356  else if (event == XACT_EVENT_ABORT ||
8357  event == XACT_EVENT_PARALLEL_ABORT)
8358  {
8359  simple_econtext_stack = NULL;
8362  }
8363 }
static ResourceOwner shared_simple_eval_resowner
Definition: pl_exec.c:106
void ResourceOwnerReleaseAllPlanCacheRefs(ResourceOwner owner)
Definition: resowner.c:718
void FreeExecutorState(EState *estate)
Definition: execUtils.c:186
static SimpleEcontextStackEntry * simple_econtext_stack
Definition: pl_exec.c:96
static EState * shared_simple_eval_estate
Definition: pl_exec.c:95

◆ plpgsql_yyerror()

void plpgsql_yyerror ( const char *  message)

Definition at line 499 of file pl_scanner.c.

References _, ereport, errcode(), errmsg(), ERROR, plpgsql_scanner_errposition(), plpgsql_yyleng, and core_yy_extra_type::scanbuf.

500 {
501  char *yytext = core_yy.scanbuf + plpgsql_yylloc;
502 
503  if (*yytext == '\0')
504  {
505  ereport(ERROR,
506  (errcode(ERRCODE_SYNTAX_ERROR),
507  /* translator: %s is typically the translation of "syntax error" */
508  errmsg("%s at end of input", _(message)),
509  plpgsql_scanner_errposition(plpgsql_yylloc)));
510  }
511  else
512  {
513  /*
514  * If we have done any lookahead then flex will have restored the
515  * character after the end-of-token. Zap it again so that we report
516  * only the single token here. This modifies scanbuf but we no longer
517  * care about that.
518  */
519  yytext[plpgsql_yyleng] = '\0';
520 
521  ereport(ERROR,
522  (errcode(ERRCODE_SYNTAX_ERROR),
523  /* translator: first %s is typically the translation of "syntax error" */
524  errmsg("%s at or near \"%s\"", _(message), yytext),
525  plpgsql_scanner_errposition(plpgsql_yylloc)));
526  }
527 }
int errcode(int sqlerrcode)
Definition: elog.c:698
char * scanbuf
Definition: scanner.h:72
#define ERROR
Definition: elog.h:46
#define ereport(elevel,...)
Definition: elog.h:157
static core_yy_extra_type core_yy
Definition: pl_scanner.c:107
static int plpgsql_yyleng
Definition: pl_scanner.c:113
int plpgsql_scanner_errposition(int location)
Definition: pl_scanner.c:472
int errmsg(const char *fmt,...)
Definition: elog.c:909
#define _(x)
Definition: elog.c:89

◆ plpgsql_yylex()

int plpgsql_yylex ( void  )

Definition at line 146 of file pl_scanner.c.

References AT_STMT_START, GetScanKeyword(), internal_yylex(), TokenAuxData::leng, TokenAuxData::lloc, TokenAuxData::lval, plpgsql_parse_dblword(), plpgsql_parse_tripword(), plpgsql_parse_word(), plpgsql_yyleng, plpgsql_yytoken, push_back_token(), core_yy_extra_type::scanbuf, ScanKeywordLookup(), and UnreservedPLKeywordTokens.

147 {
148  int tok1;
149  TokenAuxData aux1;
150  int kwnum;
151 
152  tok1 = internal_yylex(&aux1);
153  if (tok1 == IDENT || tok1 == PARAM)
154  {
155  int tok2;
156  TokenAuxData aux2;
157 
158  tok2 = internal_yylex(&aux2);
159  if (tok2 == '.')
160  {
161  int tok3;
162  TokenAuxData aux3;
163 
164  tok3 = internal_yylex(&aux3);
165  if (tok3 == IDENT)
166  {
167  int tok4;
168  TokenAuxData aux4;
169 
170  tok4 = internal_yylex(&aux4);
171  if (tok4 == '.')
172  {
173  int tok5;
174  TokenAuxData aux5;
175 
176  tok5 = internal_yylex(&aux5);
177  if (tok5 == IDENT)
178  {
179  if (plpgsql_parse_tripword(aux1.lval.str,