DBE_PLDEBUGGER Schema
DBE_PLDEBUGGER system functions are used to debug stored procedures in a standalone system. This chapter describes the interfaces supported by DBE_PLDEBUGGER. Only the administrator has the permission to execute these debugging interfaces, but does not have the permission to modify or create functions.
NOTICE:
When a user is created in the function body, the plaintext password is returned when attach, next, continue, info_code, step, info_breakpoint, backtrace or finish is called. You are not advised to create a user in the function body.
The administrator can run the following command to grant the gs_role_pldebugger role and debugger permissions to a user:
GRANT gs_role_pldebugger to user;
Two clients are required to connect to the database. One client is responsible for executing the debugging interface as the debug end, and the other client is responsible for executing the debugging function to control the execution of stored procedures on the server. The following is an example.
Prepare for debugging.
Use PG_PROC to find the OID of the stored procedure to be debugged and execute DBE_PLDEBUGGER.turn_on(oid). In this case, the client functions as the server.
openGauss=# CREATE OR REPLACE PROCEDURE test_debug ( IN x INT) AS BEGIN INSERT INTO t1 (a) VALUES (x); DELETE FROM t1 WHERE a = x; END; / CREATE PROCEDURE openGauss=# SELECT OID FROM PG_PROC WHERE PRONAME='test_debug'; oid ------- 16389 (1 row) openGauss=# SELECT * FROM DBE_PLDEBUGGER.turn_on(16389); nodename | port ----------+------ datanode | 0 (1 row)
Start debugging.
When the server executes the stored procedure, the server hangs before the first SQL statement in the stored procedure and waits for the debugging message sent by the debug end. Debugging is supported only by directly executing a stored procedure and cannot be achieved by invoking an executed stored procedure through a trigger.
openGauss=# call test_debug(1);
Start another client as the debug end and invoke DBE_PLDEBUGGER.attach to attach with the stored procedure for debugging based on the data returned by turn_on.
openGauss=# SELECT * FROM DBE_PLDEBUGGER.attach('datanode',0); funcoid | funcname | lineno | query ---------+------------+--------+---------------------------------- 16389 | test_debug | 3 | INSERT INTO t1 (a) VALUES (x); (1 row)
Execute the next statement on the client where the attach operation is performed.
openGauss=# SELECT * FROM DBE_PLDEBUGGER.next(); funcoid | funcname | lineno | query ---------+------------+--------+---------------------- 16389 | test_debug | 0 | [EXECUTION FINISHED] (1 row)
Execute the following variable operations on the client where the attach command is performed.
openGauss=# SELECT * FROM DBE_PLDEBUGGER.info_locals(); -- Print all variables. varname | vartype | value | package_name | isconst ---------+---------+-------+--------------+--------- x | int4 | 1 | | f (1 row) openGauss=# SELECT * FROM DBE_PLDEBUGGER.set_var('x', 2); -- Assign a value to a variable. set_var --------- t (1 row) openGauss=# SELECT * FROM DBE_PLDEBUGGER.print_var('x'); -- Print a single variable. varname | vartype | value | package_name | isconst ---------+---------+-------+--------------+--------- x | int4 | 2 | | f (1 row)
Directly execute the stored procedure that is being debugged.
openGauss=# SELECT * FROM DBE_PLDEBUGGER.continue(); funcoid | funcname | lineno | query ---------+------------+--------+---------------------- 16389 | test_debug | 0 | [EXECUTION FINISHED] (1 row)
Exit the stored procedure that is being debugged and do not execute statements that have not been executed before.
openGauss=# SELECT * FROM DBE_PLDEBUGGER.abort(); abort ------- t (1 row)
View the code information on the client and identify the line number of the breakpoint that can be set.
openGauss=# SELECT * FROM DBE_PLDEBUGGER.info_code(16389); lineno | query | canbreak --------+-----------------------------------------------------------+---------- | CREATE OR REPLACE PROCEDURE public.test_debug( IN x INT) | f 1 | AS DECLARE | f 2 | BEGIN | f 3 | INSERT INTO t1 (a) VALUES (x); | t 4 | DELETE FROM t1 WHERE a = x; | t 5 | END; | f 6 | / | f (7 rows)
Sets a breakpoint.
openGauss=# SELECT * FROM DBE_PLDEBUGGER.add_breakpoint(16389,4); lineno | query | canbreak --------+-----------------------------------------------------------+---------- | CREATE OR REPLACE PROCEDURE public.test_debug( IN x INT) | f 1 | AS DECLARE | f 2 | BEGIN | f 3 | INSERT INTO t1 (a) VALUES (x); | t 4 | DELETE FROM t1 WHERE a = x; | t 5 | END; | f 6 | / | f (7 rows)
View the breakpoint information.
openGauss=# SELECT * FROM DBE_PLDEBUGGER.info_breakpoints(); breakpointno | funcoid | lineno | query | enable --------------+---------+--------+---------------------------------+-------- 0 | 16389 | 4 | DELETE FROM t1 WHERE a = x; | t (1 row)
Execute to the breakpoint.
openGauss=# SELECT * FROM DBE_PLDEBUGGER.continue(); funcoid | funcname | lineno | query ---------+------------+--------+--------------------------------- 16389 | test_debug | 4 | DELETE FROM t1 WHERE a = x; (1 row)
After the stored procedure is executed, the debugging automatically exits. To debug the stored procedure again, you need to attach again. If the server does not need to be debugged, run the turn_off command to disable the debugging or exit the session. For details about the debugging interfaces, see the following table.
Table 1 DBE_PLDEBUGGER