MySQL 8.3.0
Source Code Documentation
sql_udf.cc File Reference
#include "sql/sql_udf.h"
#include <stdio.h>
#include <string.h>
#include <iterator>
#include <memory>
#include <new>
#include <string>
#include <unordered_map>
#include <utility>
#include "m_string.h"
#include "map_helpers.h"
#include "my_alloc.h"
#include "my_base.h"
#include "my_config.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_macros.h"
#include "my_psi_config.h"
#include "my_sharedlib.h"
#include "my_sys.h"
#include "my_thread_local.h"
#include "mysql/components/service_implementation.h"
#include "mysql/components/services/bits/mysql_rwlock_bits.h"
#include "mysql/components/services/bits/psi_bits.h"
#include "mysql/components/services/bits/psi_memory_bits.h"
#include "mysql/components/services/bits/psi_rwlock_bits.h"
#include "mysql/components/services/log_builtins.h"
#include "mysql/components/services/log_shared.h"
#include "mysql/my_loglevel.h"
#include "mysql/psi/mysql_memory.h"
#include "mysql/psi/mysql_rwlock.h"
#include "mysql/strings/m_ctype.h"
#include "mysql_com.h"
#include "mysqld_error.h"
#include "nulls.h"
#include "sql/derror.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/iterators/row_iterator.h"
#include "sql/mdl.h"
#include "sql/mysqld.h"
#include "sql/sql_base.h"
#include "sql/sql_class.h"
#include "sql/sql_const.h"
#include "sql/sql_executor.h"
#include "sql/sql_parse.h"
#include "sql/sql_plugin.h"
#include "sql/sql_system_table_check.h"
#include "sql/sql_table.h"
#include "sql/table.h"
#include "sql/thd_raii.h"
#include "sql/thr_malloc.h"
#include "sql/transaction.h"
#include "string_with_len.h"
#include "strxnmov.h"
#include "thr_lock.h"
#include "udf_registration_imp.h"
#include <dlfcn.h>

Functions

static udf_funcadd_udf (LEX_STRING *name, Item_result ret, char *dl, Item_udftype typ)
 
static void udf_hash_delete (udf_func *udf)
 Delete the UDF function from the UDF hash. More...
 
static void * find_udf_dl (const char *dl)
 
static char * init_syms (udf_func *tmp, char *nm)
 
static void init_udf_psi_keys (void)
 
void udf_init_globals ()
 Initialize the UDF global structures. More...
 
void udf_read_functions_table ()
 
void udf_unload_udfs ()
 Deinitialize the UDF subsystem. More...
 
void udf_deinit_globals ()
 Deinitialize the UDF subsystem. More...
 
void free_udf (udf_func *udf)
 
udf_funcfind_udf (const char *name, size_t length, bool mark_used)
 
static bool udf_end_transaction (THD *thd, bool rollback, udf_func *udf, bool insert_udf)
 Commit or rollback a transaction. More...
 
bool mysql_create_function (THD *thd, udf_func *udf, bool if_not_exists)
 Create a user defined function. More...
 
bool mysql_drop_function (THD *thd, const LEX_STRING *udf_name)
 Drop a user defined function. More...
 
void udf_hash_rlock (void)
 
void udf_hash_unlock (void)
 
ulong udf_hash_size (void)
 
void udf_hash_for_each (udf_hash_for_each_func_t *func, void *arg)
 

Variables

static bool initialized = false
 A local flag indicating whether SQL based UDF operations are allowed. More...
 
static MEM_ROOT mem
 
static collation_unordered_map< std::string, udf_func * > * udf_hash
 
static mysql_rwlock_t THR_LOCK_udf
 
static constexpr const size_t UDF_ALLOC_BLOCK_SIZE {1024}
 
static const int MYSQL_UDF_TABLE_FIELD_COUNT = 4
 
static const TABLE_FIELD_TYPE mysql_udf_table_fields [MYSQL_UDF_TABLE_FIELD_COUNT]
 
static const TABLE_FIELD_DEF mysql_udf_table_def
 
static PSI_memory_key key_memory_udf_mem
 
static PSI_rwlock_key key_rwlock_THR_LOCK_udf
 
static PSI_rwlock_info all_udf_rwlocks []
 
static PSI_memory_info all_udf_memory []
 

Function Documentation

◆ add_udf()

static udf_func * add_udf ( LEX_STRING name,
Item_result  ret,
char *  dl,
Item_udftype  typ 
)
static

◆ find_udf()

udf_func * find_udf ( const char *  name,
size_t  length,
bool  mark_used 
)

◆ find_udf_dl()

static void * find_udf_dl ( const char *  dl)
static

◆ free_udf()

void free_udf ( udf_func udf)

◆ init_syms()

static char * init_syms ( udf_func tmp,
char *  nm 
)
static

◆ init_udf_psi_keys()

static void init_udf_psi_keys ( void  )
static

◆ mysql_create_function()

bool mysql_create_function ( THD thd,
udf_func udf,
bool  if_not_exists 
)

Create a user defined function.

Atomicity: The operation to create a user defined function is atomic/crash-safe. Changes to the Data-dictionary and writing event to binlog are part of the same transaction. All the changes are done as part of the same transaction or do not have any side effects on the operation failure. UDF hash is in sync with operation state. UDF hash do not contain any stale/incorrect data in case of failure. In case of crash, there won't be any discrepancy between the data-dictionary table and the binary log.

Parameters
thdTHD context.
udfPointer to UDF function.
if_not_existsTrue if 'IF NOT EXISTS' clause was specified.
Note
Like implementations of other DDL/DML in MySQL, this function relies on the caller to close the thread tables. This is done in the end of dispatch_command().

◆ mysql_drop_function()

bool mysql_drop_function ( THD thd,
const LEX_STRING udf_name 
)

Drop a user defined function.

Atomicity: The operation to drop a user defined function is atomic/crash-safe. Changes to the Data-dictionary and writing event to binlog are part of the same transaction. All the changes are done as part of the same transaction or do not have any side effects on the operation failure. UDF hash is in sync with operation state. UDF hash do not contain any stale/incorrect data in case of failure. In case of crash, there won't be any discrepancy between the data-dictionary table and the binary log.

Parameters
thdTHD context.
udf_nameName of the UDF function.

◆ udf_deinit_globals()

void udf_deinit_globals ( )

Deinitialize the UDF subsystem.

This function does the following:

  1. Free the UDF hash.
  2. Free the memroot allocated.
  3. Destroy the RW mutex object.

◆ udf_end_transaction()

static bool udf_end_transaction ( THD thd,
bool  rollback,
udf_func udf,
bool  insert_udf 
)
static

Commit or rollback a transaction.

Also close tables which it has opened and release metadata locks. Add/Remove from the in-memory hash depending on transaction commit or rollback and the bool flag passed to this function.

Parameters
thdTHD context.
rollbackRollback transaction if true.
udfPointer to UDF function.
insert_udfInsert UDF in hash if true.
Return values
False- Success.
True- Error.

◆ udf_hash_delete()

static void udf_hash_delete ( udf_func udf)
static

Delete the UDF function from the UDF hash.

Parameters
udfPointer to the UDF function.
Note
The function remove the udf function from the udf hash if it is not in use. If the function is in use, the function name is renamed so that it is not used. The function shall be removed when no threads use it.

◆ udf_hash_for_each()

void udf_hash_for_each ( udf_hash_for_each_func_t func,
void *  arg 
)

◆ udf_hash_rlock()

void udf_hash_rlock ( void  )

◆ udf_hash_size()

ulong udf_hash_size ( void  )

◆ udf_hash_unlock()

void udf_hash_unlock ( void  )

◆ udf_init_globals()

void udf_init_globals ( )

Initialize the UDF global structures.

This is done as a separate step so that the UDF registration service can work when initializing plugins, which happens before reading the UDF table.

◆ udf_read_functions_table()

void udf_read_functions_table ( )

◆ udf_unload_udfs()

void udf_unload_udfs ( )

Deinitialize the UDF subsystem.

This function closes the shared libraries.

Variable Documentation

◆ all_udf_memory

PSI_memory_info all_udf_memory[]
static
Initial value:
= {{&key_memory_udf_mem, "udf_mem",
"Shared structure of UDFs."}}
#define PSI_FLAG_ONLY_GLOBAL_STAT
Global stat only flag.
Definition: psi_bits.h:111
static PSI_memory_key key_memory_udf_mem
Definition: sql_udf.cc:168

◆ all_udf_rwlocks

PSI_rwlock_info all_udf_rwlocks[]
static
Initial value:
"THR_LOCK_udf", PSI_FLAG_SINGLETON,
#define PSI_DOCUMENT_ME
Definition: component_common.h:28
#define PSI_FLAG_SINGLETON
Singleton flag.
Definition: component_common.h:34
static PSI_rwlock_key key_rwlock_THR_LOCK_udf
Definition: sql_udf.cc:171

◆ initialized

bool initialized = false
static

A local flag indicating whether SQL based UDF operations are allowed.

Now the UDF structures are always allocated/deallocated due to the component service.

So this variable does not cover initialization/deinitialization of these. mem and THR_LOCK_udf are always initialized, even in –skip-grant-tables mode.

◆ key_memory_udf_mem

PSI_memory_key key_memory_udf_mem
static

◆ key_rwlock_THR_LOCK_udf

PSI_rwlock_key key_rwlock_THR_LOCK_udf
static

◆ mem

MEM_ROOT mem
static

◆ mysql_udf_table_def

const TABLE_FIELD_DEF mysql_udf_table_def
static
Initial value:
static const int MYSQL_UDF_TABLE_FIELD_COUNT
Definition: sql_udf.cc:121
static const TABLE_FIELD_TYPE mysql_udf_table_fields[MYSQL_UDF_TABLE_FIELD_COUNT]
Definition: sql_udf.cc:123

◆ MYSQL_UDF_TABLE_FIELD_COUNT

const int MYSQL_UDF_TABLE_FIELD_COUNT = 4
static

◆ mysql_udf_table_fields

const TABLE_FIELD_TYPE mysql_udf_table_fields[MYSQL_UDF_TABLE_FIELD_COUNT]
static
Initial value:
= {
{{STRING_WITH_LEN("name")},
{STRING_WITH_LEN("char(64)")},
{nullptr, 0}},
{{STRING_WITH_LEN("ret")}, {STRING_WITH_LEN("tinyint")}, {nullptr, 0}},
{{STRING_WITH_LEN("dl")}, {STRING_WITH_LEN("char(128)")}, {nullptr, 0}},
{{STRING_WITH_LEN("type")},
{STRING_WITH_LEN("enum('function','aggregate')")},
{STRING_WITH_LEN("utf8mb3")}}}
#define STRING_WITH_LEN(X)
Definition: string_with_len.h:28

◆ THR_LOCK_udf

mysql_rwlock_t THR_LOCK_udf
static

◆ UDF_ALLOC_BLOCK_SIZE

constexpr const size_t UDF_ALLOC_BLOCK_SIZE {1024}
staticconstexpr

◆ udf_hash

collation_unordered_map<std::string, udf_func *>* udf_hash
static