溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PostgreSQL 中有哪些鉤子函數(shù)

發(fā)布時間:2021-08-07 16:42:24 來源:億速云 閱讀:201 作者:Leah 欄目:關(guān)系型數(shù)據(jù)庫

PostgreSQL 中有哪些鉤子函數(shù),針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

一、需求

刪除數(shù)據(jù)庫pg12db時,只能使用pg12用戶刪除,其他用戶(包括超級用戶)均不能刪除此數(shù)據(jù)庫。

二、實現(xiàn)步驟

刪除數(shù)據(jù)庫的命令是drop database,屬于Utility命令,而PG提供了ProcessUtility_hook鉤子可供使用.
實現(xiàn)一個鉤子函數(shù),判斷SQL語句是否為T_DropdbStmt,如是,則判斷數(shù)據(jù)庫名稱和用戶名稱是否匹配,如不匹配,則報錯,源碼如下:

[pg12@localhost hookdemo_dbrestrict]$ cat hookdemo_dbrestrict.c
/*
 * This is a hook demo.
 * 
 */
#include "postgres.h"
#include "miscadmin.h"
#include "tcop/utility.h"
PG_MODULE_MAGIC;
void _PG_init(void);
void _PG_fini(void);
static char *undroppabledb = "pg12db";
static char *hooksuperuser = "pg12";
static ProcessUtility_hook_type prev_utility_hook = NULL;
static void hookdemodbrestrict_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
                          ProcessUtilityContext context, ParamListInfo params,
                          QueryEnvironment *queryEnv,
                          DestReceiver *dest, char *completionTag)
{
    /* Do our custom process on drop database */
    switch(nodeTag(pstmt->utilityStmt))
    {   
        case T_DropdbStmt:
        {
            DropdbStmt *stmt = (DropdbStmt *)pstmt->utilityStmt;
            char *username = GetUserNameFromId(GetUserId(),false);
            /*
             * only user pg12 can drop pg12db.
            */
            if (strcmp(stmt->dbname, undroppabledb) == 0 &&
                    strcmp(username, hooksuperuser) != 0)
                ereport(ERROR,(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),errmsg("Only superuser \"%s\" can drop database \"%s\"",hooksuperuser,undroppabledb)));
            break;
        }
        default:
            break;
    }
    /*Standard process*/
    standard_ProcessUtility(pstmt, queryString, context, params, queryEnv, dest, completionTag);
}
/* _PG_init */
void
_PG_init(void)
{
    prev_utility_hook = ProcessUtility_hook;
    ProcessUtility_hook = hookdemodbrestrict_ProcessUtility;
}
/* Uninstall */
void
_PG_fini(void)
{
    ProcessUtility_hook = prev_utility_hook;
}
[pg12@localhost hookdemo_dbrestrict]$

三、實際效果

創(chuàng)建超級用戶superx,使用該用戶登錄,創(chuàng)建pg12db數(shù)據(jù)庫,刪除數(shù)據(jù)庫,報錯.

[local:/data/run/pg12]:5120 pg12@testdb=# create user superx with superuser password 'root';
CREATE ROLE
[local:/data/run/pg12]:5120 pg12@testdb=# \q
[pg12@localhost pg122db]$ psql -U superx
Expanded display is used automatically.
psql (12.2)
Type "help" for help.
[local:/data/run/pg12]:5120 superx@testdb=# create database pg12db;
CREATE DATABASE
[local:/data/run/pg12]:5120 superx@testdb=# drop database pg12db;
ERROR:  Only superuser "pg12" can drop database "pg12db"
[local:/data/run/pg12]:5120 superx@testdb=#

關(guān)于PostgreSQL 中有哪些鉤子函數(shù)問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI