English 中文(简体)
Postgressql 中的表达式短切表达式评价
原标题:Expression short cut evaluation in Postgresql
In C, the classic short cut evalution might be something like if ( a != NULL && a->b == 0 )... to avoid evaluating a structure reference if invalid... I m trying something similar in a postgresql trigger: IF TG_TABLE_NAME::text != collection_item OR OLD.domain = HERB OR NEW.domain = HERB THEN ... blah END IF; So if the table is collection_item it contains the field domain, but other tables don t have that field. So in theory if table is not collection_item, it should short circuit and not evaluate OLD.domain. However this wasn t working, it would give an error that `ERROR: record "old" has no field "domain" So I read here about evaluation rules, https://www.postgresql.org/docs/9.0/sql-expressions.html#SYNTAX-EXPRESS-EVAL It implies that if you encounter this problem you can get around it with CASE. So I tried that... IF (CASE WHEN TG_TABLE_NAME::text != collection_item THEN true WHEN OLD.domain = HERB OR NEW.domain = HERB THEN true ELSE false END) THEN ... blah END IF; but I still get ``ERROR: record "old" has no field "domain"` I can get around it with nesting IFs... IF TG_TABLE_NAME::text = collection_item THEN IF OLD.domain = HERB OR NEW.domain = HERB THEN is_target := true; END IF; ELSE is_target := true; END IF; But I m curious why my other attempts don t work. I m expecting short cut evalution to work.
问题回答
The expressions in PL/pgSQL are "compiled" before execution. The compilation is transformation from text form to AST form. In this form it is interpreted by expression executor. In your case, the compilation stage fails. It should be fully completed without dependency on result of any operator or expression. The expression compilation is done immediately before first expression execution (evaluation) inside session. PLpgSQL uses similar steps. Before first execution in the session, the source code is "compiled" to AST form. AST is stored to session cache, and interpreted. In this stage, the expressions are not compiled. Expressions are compiled just before own execution.




相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签