39.6. Правила и статус команд
Сервер Postgres Pro возвращает строку состояния команды, например, INSERT 149592 1
, для каждой получаемой команды. Это довольно прозрачно, когда не задействуются правила, но что произойдёт, если правила перезапишут запрос?
Правила влияют на состояния команды следующим образом:
Если с запросом не связано безусловное правило
INSTEAD
, то выполняется заданный исходный запрос и его статус выдаётся как обычно. (Но если определены какие-то условные правилаINSTEAD
, к исходному запросу добавляется условие, обратное их условиям применения. Это может повлиять на число обрабатываемых строк и выводимый статус команды.)Если с запросом связано безусловное правило
INSTEAD
, исходный запрос не выполняется вовсе. В этом случае сервер возвратит статус команды от последнего запроса, вставленного правиломINSTEAD
(условным или безусловным), и тип команды исходного запроса (INSERT
,UPDATE
илиDELETE
). Если правила не добавили подходящего запроса, в возвращённом статусе команды показывается исходный тип запроса и нули вместо количества строк и OID.
Программист может добиться, чтобы статус команды во втором случае устанавливало нужное правило INSTEAD
, назначив ему имя, стоящее по алфавиту после других активных правил, чтобы это правило применялось последним.
39.6. Rules and Command Status
The Postgres Pro server returns a command status string, such as INSERT 149592 1
, for each command it receives. This is simple enough when there are no rules involved, but what happens when the query is rewritten by rules?
Rules affect the command status as follows:
If there is no unconditional
INSTEAD
rule for the query, then the originally given query will be executed, and its command status will be returned as usual. (But note that if there were any conditionalINSTEAD
rules, the negation of their qualifications will have been added to the original query. This might reduce the number of rows it processes, and if so the reported status will be affected.)If there is any unconditional
INSTEAD
rule for the query, then the original query will not be executed at all. In this case, the server will return the command status for the last query that was inserted by anINSTEAD
rule (conditional or unconditional) and is of the same command type (INSERT
,UPDATE
, orDELETE
) as the original query. If no query meeting those requirements is added by any rule, then the returned command status shows the original query type and zeroes for the row-count and OID fields.
The programmer can ensure that any desired INSTEAD
rule is the one that sets the command status in the second case, by giving it the alphabetically last rule name among the active rules, so that it gets applied last.