postgresql - 什么是 PostgreSQL 函数,我什么时候必须使用它们?

标签 postgresql function

我想知道什么是 PostgreSQL 函数。
我什么时候必须写它们?
我该怎么写?
我该如何称呼他们?

最佳答案

定义,from wikipedia :

A stored procedure is a subroutine available to applications that access a relational database system.

一般存储过程的优点,from wikipedia :

Overhead: Because stored procedure statements are stored directly in the database, they may remove all or part of the compilation overhead that is typically required in situations where software applications send inline (dynamic) SQL queries to a database. (...)

Avoidance of network traffic: A major advantage with stored procedures is that they can run directly within the database engine. In a production system, this typically means that the procedures run entirely on a specialized database server, which has direct access to the data being accessed. The benefit here is that network communication costs can be avoided completely. This becomes particularly important for complex series of SQL statements.

Encapsulation of business logic: Stored procedures allow programmers to embed business logic as an API in the database, which can simplify data management and reduce the need to encode the logic elsewhere in client programs. (...)

Delegation of access-rights: In many systems, stored procedures can be granted access rights to the database that users who execute those procedures do not directly have.

Some protection from SQL injection attacks: Stored procedures can be used to protect against injection attacks. Stored procedure parameters will be treated as data even if an attacker inserts SQL commands. (...)

在 PostgresSQL 中,存储过程称为用户定义函数。 定义示例:

CREATE FUNCTION somefunc(quantity integer) RETURNS integer AS $$
DECLARE
    myvariable integer := 2;
BEGIN   
    RETURN quantity * myvariable;
END;
$$ LANGUAGE plpgsql;

(您可以使用其他语言在PostgreSQL中定义存储函数)

调用示例:

SELECT somefunc(100);

更多信息:http://www.postgresql.org/docs/9.1/static/server-programming.html

关于postgresql - 什么是 PostgreSQL 函数,我什么时候必须使用它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7963380/

相关文章:

postgresql - Airflow psycopg2.OperationalError : FATAL: sorry, 已经有太多客户端

python - SQLAlchemy 通过 JSON 子字段加入

sql - Postgres - 将两列聚合成一个项目

sql - 查找看过相同节目的观众(每个匹配多行)

ruby-on-rails - 查询所有后代表的多态关联?

function - Powershell保存功能

PHP 类具有来自 Traits 的冲突构造函数定义

php - 是否可以覆盖PHP中的函数

c++ - 函数返回临时对象行为不当

sql - 返回表与重复查询的函数