question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prohibit DML queries in function bodies

See original GitHub issue

The proposal is to prohibit INSERT, UPDATE, and DELETE statements in function bodies.

Currently these statements cannot appear in conditional expressions. More specifically, they are executed unconditionally, so it is semantically misleading to place them in a conditional branch that may or may not be executed - so this is forbidden. There’s a specialized form of these statements that allows them to be conditionally executed #1639.

However, functions don’t have the same unconditional execution policy and if these statements were to appear in functions, this could lead to unpredictable results, especially since function calls may be optimized by the query planner based on volatility and where they appear. In order to prevent unspecified behavior we currently choose to ban data manipulation inside function bodies.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
tailhookcommented, Aug 19, 2020

mutation can also write to the database and external state. Can neither be used in constraints nor in conditionals.

I would even say that we need a statement like CALL create_new_session() or MUTATE WITH create_new_session() and this is the only place where these functions can be used. Then these functions are as apparent as INSERT/UPDATE/DELETE statements and can be used in a same way (i.e. not a part of conditionals, …etc)

2reactions
CodesInChaoscommented, Aug 18, 2020

The simplest functions are pure, deterministic, do not access the database and always terminate normally. On top of that there are a bunch of additional capabilities a function could have:

  1. Non determinism/read access to external state (randomness, current time, …)
  2. Read access to the database
  3. Write access to the database
  4. Write access to external state
  5. Throw an exception
  6. Run forever

Functions used in constraints cannot access to database and not determinism is generally a bad idea in constraints.

Functions mutating cannot be optimized out and can only be used in certain places.

IMO you should encode at least enough of the above properties in the function to decide if the function can be used in constraints or conditionals.

The minimal design which achieves that would be three types of function:

  • function (pure, deterministic, can throw exceptions and run forever. Can be used in constraints and conditionals.
  • query can also read from the database and be read external state/be non deterministic. Can not be used in constraints but can be used in conditionals.
  • mutation can also write to the database and external state. Can neither be used in constraints nor in conditionals.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Solution to "cannot perform a DML operation inside a query"?
This will run the function into an independant transaction that will be able to perform DML without raising the ORA-14551.
Read more >
Function with DML & DDL - Ask TOM
Function with DML & DDL Hi,can we write DML and DDL statements inside a function(stored procedures)?
Read more >
DML Triggers - SQL Server | Microsoft Learn
DML events include INSERT, UPDATE, or DELETE statements. DML triggers can be used to enforce business rules and data integrity, query other ...
Read more >
Chapter 6. Data Manipulation (DML) Statements - Firebird
DML — data manipulation language — is the subset of SQL that is used by applications and procedural modules to extract and change...
Read more >
User-defined functions | BigQuery - Google Cloud
In this case, the JavaScript function body can return either a JavaScript Number ... A JavaScript UDF can time out and prevent your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found