简体   繁体   English

在plpgsql(postgresql)或python类上创建函数更好?

[英]What's better to create a function in plpgsql (postgresql) or on a python class?

I have this problem, I don't know if I should make the functions on Postgresql DB with plpgsql or if I should make it inside a class on python/java whatever language programming might work. 我有这个问题,我不知道是否应该使用plpgsql在Postgresql DB上创建函数,或者是否应该在python / java的类中创建函数,无论哪种语言编程都可以。

My boss asked me why I was writing the functions on plpgsql and not on python and I coulnd't give him a reliable answer of why it was better. 我的老板问我为什么要在plpgsql而不是python上编写函数,而我却不能给他一个可靠的答案,为什么它更好。

I have google it and I couldn't find any information about this. 我已经在Google上搜索到了,但找不到任何信息。 The only thing that I can imagine about doing the function on plpgsql is that if I change of programming language the only thing that I would need to do is to call the function and not rewriting it on the new language. 我可以想象在plpgsql上执行该功能的唯一一件事就是,如果我更改编程语言,那么我唯一需要做的就是调用该函数而不是在新语言上重写它。

This is an example of a plpgsql that I write with the help of a stackoverflow member, should I rewrite it in a python class or leave it inside the db as a function and just call the function from a python class? 这是我在stackoverflow成员的帮助下编写的plpgsql的示例,是将其重写为python类还是将其作为函数保留在db中,而只是从python类中调用该函数?

CREATE OR REPLACE FUNCTION createid(idslist varchar[], objecttype varchar)
RETURNS TABLE(original_id varchar, new_id varchar) as
$$
declare 
l_prefix text;
BEGIN
IF LOWER(objectType) = 'global' THEN
    l_prefix := 'GID';
ELSE
    l_prefix := 'ORG';
END IF;
RETURN QUERY
INSERT INTO idstable(original_id, new_id)
select t.x, l_prefix||nextval('mapSquema.globalid')::TEXT
from unnest(idslist) as t(x)
returning *
END;
$$ LANGUAGE plpgsql;

There are some advantages of stored procedures generally: 通常,存储过程有一些优点:

  • is possible to minimize moving data to client side, 可以最大程度地减少将数据移动到客户端,
  • is possible to reduce network traffic, 可以减少网络流量,
  • the server side code is accessible simply from different environments, 可以从不同的环境访问服务器端代码,
  • it helps with decomposition on data processing part and data presentation part, 它有助于数据处理部分和数据表示部分的分解,
  • with secure definer functions you can increase a security to level impossible without stored procedures. 使用安全的定义器功能,可以将安全性提高到没有存储过程无法达到的水平。

Sometimes these features are interesting and helps to develop very fast and well maintained applications. 有时,这些功能很有趣,有助于开发非常快速且维护良好的应用程序。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM