DEV Community

Michael
Michael

Posted on • Originally published at gbase8.cn

Query GBase 8a Cluster Locks and Failover Info via Python UDFs

Normally, you need the gcadmin command‑line tool to inspect cluster locks (showlock) or failover information (showfailover) in a gbase database cluster. However, GBase 8a's built‑in Python UDF capability lets you wrap these commands as SQL functions, enabling you to call them directly from a query. This article provides two ready‑to‑use UDF definitions and demonstrates their execution.

Querying Cluster Locks with a UDF

Create a showlock() function that runs gcadmin showlock internally and returns its output as a string.

DROP FUNCTION IF EXISTS showlock;
CREATE FUNCTION showlock()
RETURNS VARCHAR
$$
try:
    import commands
    output = commands.getoutput("gcadmin showlock")
except:
    return None
return str(output)
$$ LANGUAGE plpythonu;
Enter fullscreen mode Exit fullscreen mode

Example output when you call the function:

gbase> SELECT showlock();
+-------------------+
| showlock()        |
+-------------------+
|  +=================================================================+
 |                          GCLUSTER LOCK                          |
 +=================================================================+
 +-------------+----------+-------------+--------------+------+----+
 |  Lock name  |  owner   |   content   | create time  |locked|type|
 +-------------+----------+-------------+--------------+------+----+
 |gc-event-lock|10.0.2.201|global master|20250113120320| TRUE | E  |
 +-------------+----------+-------------+--------------+------+----+
 Total : 1 |
+--------------------+
1 row in set (Elapsed: 00:00:00.63)
Enter fullscreen mode Exit fullscreen mode

Querying Failover Information with a UDF

Similarly, wrap gcadmin showfailover.

DROP FUNCTION IF EXISTS showfailover;
CREATE FUNCTION showfailover()
RETURNS VARCHAR
$$
try:
    import commands
    output = commands.getoutput("gcadmin showfailover")
except:
    return None
return str(output)
$$ LANGUAGE plpythonu;
Enter fullscreen mode Exit fullscreen mode

When there is currently no failover activity, the output looks like this:

gbase> SELECT showfailover();
+--------------------------------------------------------------+
| showfailover()                                               |
+--------------------------------------------------------------+
| gcadmin showfailover: no gcluster failover information now   |
+--------------------------------------------------------------+
1 row in set (Elapsed: 00:00:00.63)
Enter fullscreen mode Exit fullscreen mode

Summary

By using Python UDFs, you can turn gcadmin commands into SQL functions that are accessible from any standard SQL client. This improves both usability and security of your gbase database environment. If you need XML output, check the -f flag in gcadmin --help and adapt the UDF accordingly.

Top comments (0)