Skip to content

Instantly share code, notes, and snippets.

@mbj
Created October 15, 2020 15:10
Show Gist options
  • Save mbj/1f34de553642c595917b52b5b92e5d2c to your computer and use it in GitHub Desktop.
Save mbj/1f34de553642c595917b52b5b92e5d2c to your computer and use it in GitHub Desktop.
Postgresql logical implication operator
CREATE FUNCTION
logical_implication(a boolean, b boolean)
RETURNS
boolean
LANGUAGE
sql
IMMUTABLE
PARALLEL SAFE
RETURNS NULL ON NULL INPUT
COST 1
AS $$
SELECT NOT a OR b
$$
;
CREATE OPERATOR ->
( function = logical_implication
, leftarg = boolean
, rightarg = boolean
)
;
@mbj
Copy link
Author

mbj commented Oct 15, 2020

Note that when you wish to use this operator in a join condition, or indexes you should define additional properties of the operator to allow these operations to be executed efficiently: https://www.postgresql.org/docs/12/xoper-optimization.html

@icon-ramico
Copy link

you can simply 'OR NOT'

@mbj
Copy link
Author

mbj commented Mar 22, 2022

Not checked the boolean table for that one yet, still I think that OR NOT is "hard" on the human mind in regular SQL, especially as the human than had to triple check precedence rules. I'd still prefer the operator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment