Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to load external modules in PostgreSQL

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to load external modules in PostgreSQL. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

How PostgreSQL loads extension, such as PL/pgSQL and postgres_fdw, and view their symbol:

Quanzl-mac:postgresql.builtin_pool quanzl$ nm lib/plpgsql.so U _ AllocSetContextCreateExtended U _ BeginInternalSubTransaction U _ BlessTupleDesc...0000000000007a50 T _ Pg_magic_func000000000001c86c s _ Pg_magic_func.Pg_magic_data...0000000000007e10 T _ pg_finfo_plpgsql_call_handler000000000001c888 s _ pg_finfo_plpgsql_call_handler.my_finfo...0000000000007e20 T _ plpgsql_call_handler...

T stands for The symbol is in the text (code) section.

S stands for The symbol is in an uninitialized data section for small objects.

This article focuses on the actions when the module is loaded, involving the above two objects: _ Pg_magic_func and _ Pg_magic_func.Pg_magic_data

Take a look at the code of a PG module (excerpted from src/pl/plpgsql/src/pl_handler.c):

Static void plpgsql_extra_errors_assign_hook (const char * newvalue, void * extra); PG_MODULE_MAGIC;/* Custom GUC variable * / static const struct config_enum_entry variable_conflict_options [] = {{"error", PLPGSQL_RESOLVE_ERROR, false}, {"use_variable", PLPGSQL_RESOLVE_VARIABLE, false}, {"use_column", PLPGSQL_RESOLVE_COLUMN, false}, {NULL, 0, false}};.

Note here that PG_MODULE_MAGIC, its definition (src/include/fmgr.h):

. / * The actual data block contents * / # define PG_MODULE_MAGIC_DATA\ {\ sizeof (Pg_magic_struct),\ PG_VERSION_NUM / 100,\ FUNC_MAX_ARGS,\ INDEX_MAX_KEYS,\ NAMEDATALEN,\ FLOAT4PASSBYVAL \ FLOAT8PASSBYVAL\}. # define PG_MAGIC_FUNCTION_NAME Pg_magic_func#define PG_MAGIC_FUNCTION_NAME_STRING "Pg_magic_func" # define PG_MODULE_MAGIC\ extern PGDLLEXPORT const Pg_magic_struct * PG_MAGIC_FUNCTION_NAME (void) \ const Pg_magic_struct *\ PG_MAGIC_FUNCTION_NAME (void)\ {\ static const Pg_magic_struct Pg_magic_data = PG_MODULE_MAGIC_DATA;\ return & Pg_magic_data;\}\ extern int no_such_variable

As you can see, here is the function definition of Pg_magic_func (please check the information why symbol has an underscore), as well as its static variable Pg_magic_data.

The function of the Pg_magic_struct structure is to check whether the module matches the current database kernel at load time, preventing libraries of different versions or non-PG modules from loading.

Internal_load_library code excerpt of the dynamic library loading function (src/backend/utils/fmgr/dfmgr.c):

Magic_func = (PGModuleMagicFunction) pg_dlsym (file_scanner- > handle, PG_MAGIC_FUNCTION_NAME_STRING); if (magic_func) {const Pg_magic_struct * magic_data_ptr = (* magic_func) ();

If the Pg_magic_func is not found, or if its return value is not consistent with the current database server, the load is refused.

PG_MODULE_MAGIC_DATA for structure initialization, including

1. Structure size

2. Version number

3. The maximum number of parameters of the function

4. Maximum number of columns in the index

5. Single-precision floating point value transfer

6. Double precision floating point value transfer

The above is how to load external modules in PostgreSQL. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report