简体   繁体   English

PostgreSQL,Pl/pgsql - 如何访问执行我所在的存储过程的查询字符串?

[英]PostgreSQL, Pl/pgsql - How to access a query string that executed the stored procedure i'm in?

is there a way to access the query string from within a stored procedure?有没有办法从存储过程中访问查询字符串? i mean i'd like to add some debugging to a lot of stored procedures and it would be brilliant if i had some constant accessible from the body of the procedure, which had the query string.我的意思是我想为很多存储过程添加一些调试,如果我可以从具有查询字符串的过程主体中访问一些常量,那就太好了。

something which would work with EXECUTE.可以与 EXECUTE 一起使用的东西。

i've read the docs and cannot see anything like that...我读过文档,看不到类似的东西......

thanks!谢谢!

I agree with Pavel that your requirement is not real clear.我同意 Pavel 的观点,即您的要求并不明确。 However, I guess that you want the to get the statement that called the procedure currently running.但是,我猜您希望获取调用当前正在运行的过程的语句。 If so then there may be a built in function: Current_Query().如果是这样,那么可能有一个内置的 function:Current_Query()。 Following is an example of its use.以下是它的使用示例。

 create or replace function what_called_me()
    returns text
    language sql
  as $$
      select current_query();
  $$; 

  select 1 num, 'A' col, what_called_me() sql_statement;   

I don't understand well, what you need, but maybe you need plpgsql plugin API.我不太明白,你需要什么,但也许你需要plpgsql插件API。 This API is not well documented, but there is lot of PostgreSQL extensions that use this API - PLdebugger, plpgsql_chec, plprofiler and maybe other.这个 API 没有很好的文档记录,但是有很多 PostgreSQL 扩展使用这个 API - PLdebugger、plpgsql_chec、plprofiler 和其他。

/*
 * A PLpgSQL_plugin structure represents an instrumentation plugin.
 * To instrument PL/pgSQL, a plugin library must access the rendezvous
 * variable "PLpgSQL_plugin" and set it to point to a PLpgSQL_plugin struct.
 * Typically the struct could just be static data in the plugin library.
 * We expect that a plugin would do this at library load time (_PG_init()).
 * It must also be careful to set the rendezvous variable back to NULL
 * if it is unloaded (_PG_fini()).
 *
 * This structure is basically a collection of function pointers --- at
 * various interesting points in pl_exec.c, we call these functions
 * (if the pointers are non-NULL) to give the plugin a chance to watch
 * what we are doing.
 *
 * func_setup is called when we start a function, before we've initialized
 * the local variables defined by the function.
 *
 * func_beg is called when we start a function, after we've initialized
 * the local variables.
 *
 * func_end is called at the end of a function.
 *
 * stmt_beg and stmt_end are called before and after (respectively) each
 * statement.
 *
 * Also, immediately before any call to func_setup, PL/pgSQL fills in the
 * error_callback and assign_expr fields with pointers to its own
 * plpgsql_exec_error_callback and exec_assign_expr functions.  This is
 * a somewhat ad-hoc expedient to simplify life for debugger plugins.
 */
typedef struct PLpgSQL_plugin
{
    /* Function pointers set up by the plugin */
    void        (*func_setup) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
    void        (*func_beg) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
    void        (*func_end) (PLpgSQL_execstate *estate, PLpgSQL_function *func);
    void        (*stmt_beg) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);
    void        (*stmt_end) (PLpgSQL_execstate *estate, PLpgSQL_stmt *stmt);

    /* Function pointers set by PL/pgSQL itself */
    void        (*error_callback) (void *arg);
    void        (*assign_expr) (PLpgSQL_execstate *estate, PLpgSQL_datum *target,
                                PLpgSQL_expr *expr);
} PLpgSQL_plugin;

This is necessary when you really need to detail info about what is inside.当您确实需要详细了解内部内容时,这是必要的。

Maybe you need information just about executed queries - then you can look on extension auto_explain , when you set auto_explain.log_nested_statements to on , then the queries from procedure will be logged.也许您只需要有关已执行查询的信息 - 然后您可以查看扩展auto_explain ,当您将auto_explain.log_nested_statements设置为on ,将记录来自过程的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何限制PostgreSQL(使用SQL和PL/pgSQL)只能通过某些存储过程来更改表数据的方式? - How to restrict in PostgreSQL (using SQL and PL/pgSQL) way of changing table data only by means of certain stored procedure? PostgreSQL PL / pgSQL:查询存储在表中(营业时间) - PostgreSQL PL/pgSQL : query stored within a table (opening hours) Oracle PL / SQL存储过程编译器与PostgreSQL PGSQL存储过程编译器 - Oracle PL/SQL stored procedure compiler vs PostgreSQL PGSQL stored procedure compiler 将数组传递给PL / pgSQL存储过程时出错 - Error passing an array to PL/pgSQL stored procedure oracle存储过程OUT参数与postgresql pl / pgsql函数OUT参数 - oracle stored procedure OUT parameter vs. postgresql pl/pgsql function OUT parameter 如何从Sublime编译Postgresql(pl / pgsql)函数? - How Can I Compile Postgresql (pl/pgsql) Functions from Sublime? 如何将 PL/PgSQL 过程转换为动态过程? - How to convert a PL/PgSQL procedure into a dynamic one? 如何从pl / pgsql中的深层UDT访问数组? - How can I access arrays from deep UDTs in pl/pgsql? 在PL / pgSQL中声明存储过程中的临时表变量 - Declare a variable of temporary table in stored procedure in PL/pgSQL PostgreSQL存储过程:如何枚举查询 - PostgreSQL stored procedure: how enumerate query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM