86得票2回答
如果索引不存在,则创建索引。

我正在处理一个函数,如果索引不存在则允许我添加。我遇到的问题是无法获取索引列表进行比较。有什么想法吗? 这与创建列的类似问题类似,可以使用以下代码解决: https://stackoverflow.com/a/12603892/368511

73得票4回答
功能表现

从MySQL的背景来看,存储过程 性能(旧文章) 和 可用性 都有问题,我正在评估是否在我的公司的新产品中使用PostgreSQL。 我想做的其中一件事是将一些 应用逻辑 移入存储过程中,所以我在这里询问关于在PostgreSQL(9.0)中使用函数时的做和不做(最佳实践),尤其是与性能陷阱...

62得票7回答
"Stored Procedures" and "Stored Functions" are both database objects that contain a set of SQL statements. However, there are some key differences between the two. Firstly, the main purpose of a Stored Procedure is to perform an action or a series of actions in the database. It can be used to insert, update, delete, or retrieve data from tables. On the other hand, a Stored Function is designed to return a single value or a table of values. It is often used in calculations or as part of a query. Secondly, Stored Procedures can have input and output parameters, allowing them to accept values and return results. This makes them more flexible and versatile compared to Stored Functions, which can only return values. Another difference lies in their usage within SQL statements. Stored Procedures are called using the EXECUTE statement, while Stored Functions are typically used in SELECT statements or as part of expressions. Furthermore, Stored Procedures can modify the database schema, such as creating or altering tables, whereas Stored Functions cannot make any changes to the database structure. In terms of performance, Stored Functions are generally faster than Stored Procedures because they are precompiled and cached by the database engine. However, this advantage comes at the cost of limited functionality. In summary, while both Stored Procedures and Stored Functions serve important roles in database management, their differences lie in their purpose, usage, parameters, ability to modify the database, and performance characteristics.

所以,这个问题的一个评论提到,在PostgreSQL中,“存储过程”和“存储函数”之间有一些细微的差别。 评论中链接了一个wikipedia文章,但其中一些似乎不适用(例如,它们可以在SELECT语句中使用)。 语法本身似乎有点令人困惑: CREATE FUNCTION emp_stam...

46得票1回答
Postgres函数与预编译查询中的SQL注入问题

在Postgres中,准备好的查询和用户定义函数作为防止SQL注入的机制是否等效?这两种方法之间有特定的优势吗?

41得票3回答
在PostgreSQL中实现UPSERT的惯用方式 在PostgreSQL中,实现UPSERT(插入或更新)操作有几种常见的方法。以下是其中一些常用的方法: 1. 使用INSERT ... ON CONFLICT DO UPDATE语句:这是PostgreSQL 9.5版本引入的新功能。它允许您在插入冲突时执行更新操作。您可以指定冲突解决的列和要更新的值。 2. 使用MERGE语句:尽管PostgreSQL没有内置的MERGE语句,但您可以使用WITH子句和UPDATE语句来模拟类似的行为。首先,您可以使用WITH子句创建一个临时表,然后使用UPDATE语句将数据从源表合并到目标表。 3. 使用EXCLUDE约束:PostgreSQL 9.5还引入了EXCLUDE约束,它允许您定义一组排除条件,以避免插入重复的数据。通过在表上创建适当的EXCLUDE约束,您可以确保插入操作不会导致冲突。 4. 使用触发器:如果您需要更高级的控制,您可以使用触发器来实现UPSERT操作。通过在表上创建一个BEFORE INSERT触发器,您可以检查要插入的数据是否已存在,并根据需要执行插入或更新操作。 这些都是在PostgreSQL中实现UPSERT操作的常见方法。您可以根据具体的需求选择适合您的方法。

我已经阅读了关于 PostgreSQL 中不同 UPSERT 实现的文章,但所有这些解决方案都相对较旧或相对奇特(例如使用可写 CTE)。 而且我并不是一个精通 psql 的专家,无法立即确定这些解决方案之所以过时是否是因为它们被广泛推荐,还是它们(几乎全部)只是不适合生产环境的玩具示例。 在...

40得票1回答
将Postgres函数中的查询结果分配给多个变量

我需要在Postgres函数中为两个变量赋值,如下所示。 a := select col1 from tbl where ... b := select col2 from tbl where ... 如何在一行命令中为两个变量分配两个值? 像这样: a,b := select co...

32得票1回答
EXPLAIN ANALYZE在plpgsql函数内部的查询中没有显示任何细节。

我正在使用PostgreSQL 9.3中的PL/pgSQL函数,其中包含几个复杂的查询。 create function f1() returns integer as $$ declare event tablename%ROWTYPE; .... .... begin FOR ...

25得票2回答
在PL/pgSQL中,有没有一种简单的方法来检查查询是否返回了结果呢?

我目前正在尝试使用PL/pgSQL,并想知道是否有更优雅的方法来执行类似这样的操作: select c.data into data from doc c where c.doc_id = id and c.group_cur > group_cur order by c.id des...

24得票2回答
触发器:将已删除的行移动到归档表

我在我的PostgreSQL数据库中有一个小表(约10行),名为restrictions,其中的值每天都会被删除和插入。 我想要一个名为restrictions_deleted的表,每当从restrictions中删除一行时,它将自动存储。由于restrictions具有序列id,所以不会有...