<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PinkDB Archives - Philipp Salvisberg&#039;s Blog</title>
	<atom:link href="https://www.salvis.com/blog/tag/pinkdb/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.salvis.com/blog/tag/pinkdb/</link>
	<description>Database-centric development</description>
	<lastBuildDate>Mon, 12 Oct 2026 18:40:08 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.5</generator>

<image>
	<url>https://www.salvis.com/blog/wp-content/uploads/2014/04/favicon.png</url>
	<title>PinkDB Archives - Philipp Salvisberg&#039;s Blog</title>
	<link>https://www.salvis.com/blog/tag/pinkdb/</link>
	<width>32</width>
	<height>32</height>
</image> 
<atom:link rel="hub" href="https://pubsubhubbub.appspot.com"/>
<atom:link rel="hub" href="https://pubsubhubbub.superfeedr.com"/>
<atom:link rel="hub" href="https://websubhub.com/hub"/>
<atom:link rel="self" href="https://www.salvis.com/blog/tag/pinkdb/feed/"/>
	<item>
		<title>PL/SQL vs. JavaScript in the Oracle Database 23ai #JoelKallmanDay</title>
		<link>https://www.salvis.com/blog/2024/10/16/pl-sql-vs-javascript-in-the-oracle-database-23ai-joelkallmanday/</link>
					<comments>https://www.salvis.com/blog/2024/10/16/pl-sql-vs-javascript-in-the-oracle-database-23ai-joelkallmanday/#comments</comments>
		
		<dc:creator><![CDATA[Philipp Salvisberg]]></dc:creator>
		<pubDate>Wed, 16 Oct 2024 02:22:28 +0000</pubDate>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[JoelKallmanDay]]></category>
		<category><![CDATA[Oracle 26ai]]></category>
		<category><![CDATA[PinkDB]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[SmartDB]]></category>
		<guid isPermaLink="false">https://www.salvis.com/blog/?p=13635</guid>

					<description><![CDATA[<p>JavaScript is the first language supported by the Multilingual Engine (MLE) in Oracle Database 23ai. Having additional languages in the Oracle Database allows us to use existing libraries within the database. Also, it makes it easier for those without PL/SQL skills to get started with database development. Wasn&#8217;t that also the argument<span class="excerpt-hellip"> […]</span></p>
<p>The post <a href="https://www.salvis.com/blog/2024/10/16/pl-sql-vs-javascript-in-the-oracle-database-23ai-joelkallmanday/">PL/SQL vs. JavaScript in the Oracle Database 23ai #JoelKallmanDay</a> appeared first on <a href="https://www.salvis.com/blog">Philipp Salvisberg&#039;s Blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">JavaScript is the first language supported by the Multilingual Engine (MLE) in Oracle Database 23ai. Having additional languages in the Oracle Database allows us to use existing libraries within the database. Also, it makes it easier for those without PL/SQL skills to get started with database development. Wasn&#8217;t that also the argument for Java in the database? What is easier and better in JavaScript than in Java? How performant are JavaScript modules? When is JavaScript a good alternative to PL/SQL and when is it not?</p>



<p class="wp-block-paragraph">This is a translation of my German article &#8220;PL/SQL oder JavaScript in der Oracle Datenbank 23ai?&#8221; published in the <a href="https://www.salvis.com/blog/wp-content/uploads/2024/10/06_2024-Red_Stack_Magazin-DTk-WEB-Philipp_Salvisberg_PLSQL_oder_JavaScript_in_der_Oracl.pdf">Red Stack Magazine</a> No. 6/2024 on 11 October 2024.</p>



<h2 class="wp-block-heading">Why Do We Need Code in the Database?</h2>



<p class="wp-block-paragraph"></p>



<p class="wp-block-paragraph">I see the following reasons for this.</p>



<ol class="wp-block-list">
<li>We bring the code to the data rather than the data to the code. This allows us to process the data efficiently on the database server and deliver the result to the client in just a few network round trips. This uses fewer resources, is more cost-effective and faster than if we had to transport the data to the client and process it there.</li>



<li>We take responsibility for the quality of the data stored in the database. Typically, we write data once and read it often. Therefore, we should store data correctly so that consumers can rely on the data when they read it. In this sense, the logic for validating the data belongs in the database. This logic is often more extensive than what today&#8217;s database constraints provide. In other words, we need code in the database as part of an API to keep our data consistent and correct.</li>
</ol>



<p class="wp-block-paragraph">Even if your database applications do not follow the principles of SmartDB or <a href="https://www.salvis.com/blog/2018/07/18/the-pink-database-paradigm-pinkdb-2/">PinkDB</a>, there are benefits to selectively using code in the database. And if your company policy categorically forbids code in the database, it is probably time to reconsider.</p>



<h2 class="wp-block-heading">PL/SQL Without SQL</h2>



<p class="wp-block-paragraph">Let&#8217;s pretend we need a function to convert a timestamp to Unix time. Wikipedia defines the <a href="https://en.wikipedia.org/wiki/Unix_time">Unix time</a> as follows.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">Unix time is a date and time representation widely used in&nbsp;computing. It measures time by the number of non-leap seconds that have elapsed since 00:00:00 UTC on 1 January 1970, the Unix epoch.</p>
</blockquote>



<p class="wp-block-paragraph">Listing 1 shows how we can implement this in PL/SQL.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Listing 1: to_epoch_plsql</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>create or replace function to_epoch_plsql(
   in_ts in timestamp
) return number is
   co_epoch_date constant timestamp with time zone := 
      timestamp '1970-01-01 00:00:00 UTC';
   l_interval    interval day(9) to second (3);
begin
   l_interval := in_ts - co_epoch_date;
   return 1000 * (extract(second from l_interval)
         + extract(minute from l_interval) * 60
         + extract(hour from l_interval) * 60 * 60
         + extract(day from l_interval) * 60 * 60 * 24);
end;
/</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> function to_epoch_plsql(</span></span>
<span class="line"><span style="color: #D4D4D4">   in_ts </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">timestamp</span></span>
<span class="line"><span style="color: #D4D4D4">) </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">is</span></span>
<span class="line"><span style="color: #D4D4D4">   co_epoch_date </span><span style="color: #569CD6">constant</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">timestamp with time zone</span><span style="color: #D4D4D4"> := </span></span>
<span class="line"><span style="color: #D4D4D4">      </span><span style="color: #569CD6">timestamp</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&#39;1970-01-01 00:00:00 UTC&#39;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #9CDCFE">l_interval</span><span style="color: #D4D4D4">    interval day(</span><span style="color: #B5CEA8">9</span><span style="color: #D4D4D4">) to second (</span><span style="color: #B5CEA8">3</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #569CD6">begin</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #9CDCFE">l_interval</span><span style="color: #D4D4D4"> := in_ts - co_epoch_date;</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">1000</span><span style="color: #D4D4D4"> * (</span><span style="color: #DCDCAA">extract</span><span style="color: #D4D4D4">(second </span><span style="color: #569CD6">from</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">l_interval</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">         + </span><span style="color: #DCDCAA">extract</span><span style="color: #D4D4D4">(minute </span><span style="color: #569CD6">from</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">l_interval</span><span style="color: #D4D4D4">) * </span><span style="color: #B5CEA8">60</span></span>
<span class="line"><span style="color: #D4D4D4">         + </span><span style="color: #DCDCAA">extract</span><span style="color: #D4D4D4">(hour </span><span style="color: #569CD6">from</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">l_interval</span><span style="color: #D4D4D4">) * </span><span style="color: #B5CEA8">60</span><span style="color: #D4D4D4"> * </span><span style="color: #B5CEA8">60</span></span>
<span class="line"><span style="color: #D4D4D4">         + </span><span style="color: #DCDCAA">extract</span><span style="color: #D4D4D4">(day </span><span style="color: #569CD6">from</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">l_interval</span><span style="color: #D4D4D4">) * </span><span style="color: #B5CEA8">60</span><span style="color: #D4D4D4"> * </span><span style="color: #B5CEA8">60</span><span style="color: #D4D4D4"> * </span><span style="color: #B5CEA8">24</span><span style="color: #D4D4D4">);</span></span>
<span class="line"><span style="color: #569CD6">end</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span></code></pre></div>



<p class="wp-block-paragraph">The <code>to_epoch_plsql</code> function expects a <code>timestamp</code>, a <code>timestamp with timezone</code> would be better. We have omitted this to keep the example as simple as possible. Although the solution may seem simple, we are reimplementing existing functionality. We had to find out how Unix time works, what role time zones play, what leap seconds are for, and that Unix time is used in milliseconds, not seconds.</p>



<p class="wp-block-paragraph">Wouldn&#8217;t it be nice to be able to use an existing, tested function in the database to keep our application&#8217;s code to a minimum? Even though there is no <code>to_epoch</code> function in SQL, the Java Development Kit (JDK) offers such functionality. The Oracle Java Virtual Machine (OJVM) is an embedded component of the Oracle Database 23.5. It&#8217;s a JDK version 11.</p>



<h2 class="wp-block-heading">Java Without SQL</h2>



<p class="wp-block-paragraph">The Oracle Database has supported Java stored procedures since version 8i Release 1. This means that we can provide a <code>to_epoch_java</code> function as shown in Listing 2.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Listing 2: to_epoch_java</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>create or replace and compile java source named "Util" as
public class Util {
   public static long toEpoch(java.sql.Timestamp ts) {
      return ts.getTime();
   }
}
/
create or replace function to_epoch_java(in_ts in timestamp) 
  return number is language java name 
    'Util.toEpoch(java.sql.Timestamp) return java.lang.long';
/</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">and</span><span style="color: #D4D4D4"> compile </span><span style="color: #569CD6">java</span><span style="color: #D4D4D4"> source named </span><span style="color: #CE9178">&quot;Util&quot;</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">as</span></span>
<span class="line"><span style="color: #D4D4D4">public class Util {</span></span>
<span class="line"><span style="color: #D4D4D4">   public static </span><span style="color: #569CD6">long</span><span style="color: #D4D4D4"> toEpoch(</span><span style="color: #569CD6">java</span><span style="color: #D4D4D4">.</span><span style="color: #569CD6">sql</span><span style="color: #D4D4D4">.</span><span style="color: #569CD6">Timestamp</span><span style="color: #D4D4D4"> ts) {</span></span>
<span class="line"><span style="color: #D4D4D4">      </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> ts.getTime();</span></span>
<span class="line"><span style="color: #D4D4D4">   }</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span>
<span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> function to_epoch_java(in_ts </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">timestamp</span><span style="color: #D4D4D4">) </span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">is</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">language</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">java</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">name</span><span style="color: #D4D4D4"> </span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #CE9178">&#39;Util.toEpoch(java.sql.Timestamp) return java.lang.long&#39;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span></code></pre></div>



<p class="wp-block-paragraph">For Java, we need to create a class and a call specification. The purpose of the call specification is, among other things, to map the types of input and output data between SQL and Java. For example, to map the return value of <code>java.lang.long</code> to <code>number</code>.</p>



<p class="wp-block-paragraph">The code no longer contains the formula for converting a timestamp to Unix time, but it is quite extensive. Is there an easy way in JavaScript?</p>



<h2 class="wp-block-heading">JavaScript Without SQL</h2>



<p class="wp-block-paragraph">With Oracle Database 23ai, we can create JavaScript modules.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Listing 3: to_epoch_js</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>create or replace mle module util_mod language javascript as   
export function toEpoch(ts) {
   return ts.valueOf();
}
/
create or replace function to_epoch_js(in_ts in timestamp) 
   return number is 
      mle module util_mod 
      signature 'toEpoch(Date)';
/</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> mle module util_mod </span><span style="color: #569CD6">language</span><span style="color: #D4D4D4"> javascript </span><span style="color: #569CD6">as</span><span style="color: #D4D4D4">   </span></span>
<span class="line"><span style="color: #D4D4D4">export function toEpoch(ts) {</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> ts.valueOf();</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span>
<span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> function to_epoch_js(in_ts </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">timestamp</span><span style="color: #D4D4D4">) </span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">is</span><span style="color: #D4D4D4"> </span></span>
<span class="line"><span style="color: #D4D4D4">      mle module util_mod </span></span>
<span class="line"><span style="color: #D4D4D4">      signature </span><span style="color: #CE9178">&#39;toEpoch(Date)&#39;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span></code></pre></div>



<p class="wp-block-paragraph">The implementation of to_<code>epoch_js</code> in Listing 3 is similar to <code>to_epoch_java</code>. A module in JavaScript and then an MLE call specification. However, it is no longer necessary to fully map the input data types in JavaScript. The Oracle Database defines default values. These can be overridden, but do not need to be explicitly defined as in Java. It is not possible to map the output data type. In this case, it must be possible to convert the return value to a <code>number</code>, otherwise, a runtime error will occur.</p>



<p class="wp-block-paragraph">However, the implementation for this simple case is quite extensive. Oracle has probably realised this and provided an alternative.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Listing 4: to_epoch_js2 – inline MLE call specification</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>create or replace function to_epoch_js2("in_ts" in timestamp)
   return number is
      mle language javascript ' return in_ts.valueOf();';
/</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> function to_epoch_js2(</span><span style="color: #CE9178">&quot;in_ts&quot;</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">timestamp</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">is</span></span>
<span class="line"><span style="color: #D4D4D4">      mle </span><span style="color: #569CD6">language</span><span style="color: #D4D4D4"> javascript </span><span style="color: #CE9178">&#39; return in_ts.valueOf();&#39;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span></code></pre></div>



<p class="wp-block-paragraph">The <code>to_epoch_js2</code> function in Listing 4 is equivalent to <code>to_epoch_js</code> but significantly simpler than any of the other variants. However, an inline MLE call specification is only applicable to JavaScript code without dependencies on other modules, this means for JavaScript code without an import command.</p>



<h2 class="wp-block-heading">Performance of <code>to_epoch_...</code></h2>



<p class="wp-block-paragraph">Anyone who has worked with Java stored procedures in the database knows that the initialisation of the OJVM in a new database session slows down the response time considerably. This is not the case with MLE because it uses a GraalVM native image. Simply put, it reads only the memory contents of a file, similar to waking your laptop from hibernation. This makes it possible to start a Java program within a millisecond. The native image is integrated into the database as a shared library <code>$ORACLE_HOME/lib/libmle.so</code>. This means that the MLE provides JavaScript via Java, but is completely independent of the OJVM.</p>



<p class="wp-block-paragraph">In Figure 1 we compare the runtimes of 100,000 function calls. Instead of seconds, we use a normalised unit of time, which makes comparing easier and results less dependent on the hardware stack used.</p>



<p class="wp-block-paragraph">All experiments were performed using the Oracle Database 23.5 Free Edition on an AMD Ryzen R1600 processor-based system. The shortest time of five repetitions was taken into account. You can reproduce these experiments using the scripts in <a href="https://github.com/PhilippSalvisberg/js23c">this GitHub repository</a>.</p>



<figure class="wp-block-image size-large is-style-default"><a href="https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure1.png"><img fetchpriority="high" decoding="async" width="1024" height="440" src="https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure1-1024x440.png" alt="Figure 1: Runtime of 100K calls of to_epoch_..." class="wp-image-13656" srcset="https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure1-1024x440.png 1024w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure1-300x129.png 300w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure1-768x330.png 768w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure1-1536x660.png 1536w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure1-2048x880.png 2048w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure1-150x64.png 150w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure1-480x206.png 480w" sizes="(max-width:767px) 480px, (max-width:1024px) 100vw, 1024px" /></a><figcaption class="wp-element-caption"><em>Figure 1: Runtime of 100K calls of to_epoch_&#8230;</em></figcaption></figure>



<p class="wp-block-paragraph">The <code>to_epoch_plsql</code>, <code>to_epoch_java</code> and <code>to_epoch_js</code> variants are called from a PL/SQL loop. This means 100,000 context switches between PL/SQL and Java or JavaScript. The fourth variant, <code>to_epoch_jsloop</code>, calls <code>toEpoch</code> from a JavaScript loop. In this case, the context switching between PL/SQL and JavaScript makes processing about 50 times slower.</p>



<p class="wp-block-paragraph">Based on these results, we should avoid context switching between PL/SQL and JavaScript if possible. The performance of JavaScript in the database is impressive in this case. Quite different from the OJVM.</p>



<h2 class="wp-block-heading">Memory Usage of <code>to_epoch_...</code></h2>



<p class="wp-block-paragraph">Figure 2 shows the maximum memory used at the end of a <code>to_epoch_…</code> function call. The call was made in a new database session and contains the memory requirements of the measuring instruments.</p>



<figure class="wp-block-image size-large"><a href="https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure2.png"><img decoding="async" width="1024" height="440" src="https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure2-1024x440.png" alt="Max. Memory Usage After Single Call" class="wp-image-13659" srcset="https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure2-1024x440.png 1024w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure2-300x129.png 300w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure2-768x330.png 768w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure2-1536x660.png 1536w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure2-2048x880.png 2048w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure2-150x64.png 150w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure2-480x206.png 480w" sizes="(max-width:767px) 480px, (max-width:1024px) 100vw, 1024px" /></a><figcaption class="wp-element-caption"><em>Figure 2: Max. memory usage after a single call</em></figcaption></figure>



<p class="wp-block-paragraph">JavaScript uses significantly more memory than PL/SQL. If you take this into account when sizing the database server and connection pools, this should not be a problem nowadays.</p>



<h2 id="using-a-3rd-party-javascript-library" class="wp-block-heading">Using a 3rd Party JavaScript Library</h2>



<p class="wp-block-paragraph">Let&#8217;s say we want to validate email addresses in our database without actually sending a test email. The rules for a valid <a href="https://en.wikipedia.org/wiki/Email_address">email address</a> are quite extensive. In the JavaScript ecosystem, we can find open-source libraries for such requirements that can be used in the database without modification. For this example, we use <a href="https://www.npmjs.com/package/validator">validator.js</a>, which can validate not only email addresses but also credit card numbers, EAN, IBAN and much more. Using SQLcl&#8217;s script command, we can <a href="https://www.salvis.com/blog/2023/11/26/installing-mle-modules-in-the-oracle-database/">load npm modules</a> directly into the database.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Listing 5: Load validator.js from npm as validator_mod into the database</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>script https://raw.githubusercontent.com/PhilippSalvisberg/mle-sqlcl/main/mle.js install validator_mod https://esm.run/validator@13.12.0 13.12.0

select version, language_name, length(module)
  from user_mle_modules
 where module_name = 'VALIDATOR_MOD';</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">script https://</span><span style="color: #569CD6">raw</span><span style="color: #D4D4D4">.githubusercontent.com/PhilippSalvisberg/mle-sqlcl/main/mle.js install validator_mod https://esm.run/validator@</span><span style="color: #B5CEA8">13.12</span><span style="color: #D4D4D4">.</span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">13.12</span><span style="color: #D4D4D4">.</span><span style="color: #B5CEA8">0</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">select</span><span style="color: #D4D4D4"> version, language_name, </span><span style="color: #DCDCAA">length</span><span style="color: #D4D4D4">(module)</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #569CD6">from</span><span style="color: #D4D4D4"> user_mle_modules</span></span>
<span class="line"><span style="color: #D4D4D4"> </span><span style="color: #569CD6">where</span><span style="color: #D4D4D4"> module_name = </span><span style="color: #CE9178">&#39;VALIDATOR_MOD&#39;</span><span style="color: #D4D4D4">;</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>VERSION    LANGUAGE_NAME       LENGTH(MODULE)
---------- ---------------- ----------------- 
13.12.0    JAVASCRIPT                  123260</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">VERSION    LANGUAGE_NAME       LENGTH(MODULE)</span></span>
<span class="line"><span style="color: #D4D4D4">---------- ---------------- ----------------- </span></span>
<span class="line"><span style="color: #D4D4D4">13.12.0    JAVASCRIPT                  123260</span></span></code></pre></div>



<p class="wp-block-paragraph">The script <code>mle.js</code> in Listing 5 is not read from the local file system as usual but via a URL from GitHub. The script creates a JavaScript module <code>validator_mod</code> with the contents of the URL <a href="https://esm.run/validator@13.12.0">https://esm.run/validator@13.12.0</a>, which is a minimised, browser-optimised version of the validator.js module in the <a href="https://www.npmjs.com/package/validator">npm</a> software registry. The last parameter <code>13.12.0</code> is the version of the module stored in the Oracle Data Dictionary.</p>



<p class="wp-block-paragraph">In Listing 6, we create the MLE call specification in a PL/SQL package. The <code>is_mail</code> function accepts only a string as a parameter. The validator options are defined in the package body. This simplifies uniform use in the database application.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Listing 6: PL/SQL package validator_api </span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>create or replace package validator_api is
   function is_email(
      in_email in varchar2
   ) return boolean deterministic;
end validator_api;
/
create or replace package body validator_api is
   function is_email_internal(
      in_email   in varchar2,
      in_options in json
   ) return boolean deterministic as mle module validator_mod 
   signature 'default.isEmail(string, any)';

   function is_email(
      in_email in varchar2
   ) return boolean deterministic is
   begin
      return is_email_internal(
                in_email   => in_email,
                in_options => json('
                   {
                      "allow_display_name": false,
                      "allow_undescores": false,
                      "require_display_name": false,
                      "allow_utf8_local_part": true,
                      "require_tld": true,
                      "allow_ip_domain": false,
                      "domain_specific_validation": false,
                      "blacklisted_chars": "",
                      "ignore_max_length": false,
                      "host_blacklist": &#91;"dubious.com"&#93;,
                      "host_whitelist": []
                   }
                ')
             );
   end is_email;
end validator_api;
/</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">package</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">validator_api</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">is</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">function</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">is_email</span><span style="color: #D4D4D4">(</span></span>
<span class="line"><span style="color: #D4D4D4">      in_email </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">varchar2</span></span>
<span class="line"><span style="color: #D4D4D4">   ) </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">boolean</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">deterministic</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #569CD6">end</span><span style="color: #D4D4D4"> validator_api;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span>
<span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">package body</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">validator_api</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">is</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">function</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">is_email_internal</span><span style="color: #D4D4D4">(</span></span>
<span class="line"><span style="color: #D4D4D4">      in_email   </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">varchar2</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">      in_options </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> json</span></span>
<span class="line"><span style="color: #D4D4D4">   ) </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">boolean</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">deterministic</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">as</span><span style="color: #D4D4D4"> mle module validator_mod </span></span>
<span class="line"><span style="color: #D4D4D4">   signature </span><span style="color: #CE9178">&#39;default.isEmail(string, any)&#39;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">function</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">is_email</span><span style="color: #D4D4D4">(</span></span>
<span class="line"><span style="color: #D4D4D4">      in_email </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">varchar2</span></span>
<span class="line"><span style="color: #D4D4D4">   ) </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">boolean</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">deterministic</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">is</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">begin</span></span>
<span class="line"><span style="color: #D4D4D4">      </span><span style="color: #C586C0">return</span><span style="color: #D4D4D4"> is_email_internal(</span></span>
<span class="line"><span style="color: #D4D4D4">                in_email   =&gt; in_email,</span></span>
<span class="line"><span style="color: #D4D4D4">                in_options =&gt; json(</span><span style="color: #CE9178">&#39;</span></span>
<span class="line"><span style="color: #CE9178">                   {</span></span>
<span class="line"><span style="color: #CE9178">                      &quot;allow_display_name&quot;: false,</span></span>
<span class="line"><span style="color: #CE9178">                      &quot;allow_undescores&quot;: false,</span></span>
<span class="line"><span style="color: #CE9178">                      &quot;require_display_name&quot;: false,</span></span>
<span class="line"><span style="color: #CE9178">                      &quot;allow_utf8_local_part&quot;: true,</span></span>
<span class="line"><span style="color: #CE9178">                      &quot;require_tld&quot;: true,</span></span>
<span class="line"><span style="color: #CE9178">                      &quot;allow_ip_domain&quot;: false,</span></span>
<span class="line"><span style="color: #CE9178">                      &quot;domain_specific_validation&quot;: false,</span></span>
<span class="line"><span style="color: #CE9178">                      &quot;blacklisted_chars&quot;: &quot;&quot;,</span></span>
<span class="line"><span style="color: #CE9178">                      &quot;ignore_max_length&quot;: false,</span></span>
<span class="line"><span style="color: #CE9178">                      &quot;host_blacklist&quot;: &#91;&quot;dubious.com&quot;&#93;,</span></span>
<span class="line"><span style="color: #CE9178">                      &quot;host_whitelist&quot;: []</span></span>
<span class="line"><span style="color: #CE9178">                   }</span></span>
<span class="line"><span style="color: #CE9178">                &#39;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">             );</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">end</span><span style="color: #D4D4D4"> is_email;</span></span>
<span class="line"><span style="color: #569CD6">end</span><span style="color: #D4D4D4"> validator_api;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span></code></pre></div>



<p class="wp-block-paragraph">Listing 7 shows the use of the validator in SQL. The second email address is invalid because of the <code>allow_display_name</code> option. The third e-mail address is formally correct, but it uses a domain listed under <code>host_blacklist</code>.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Listing 7: Validate email addresses</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>select e_mail, validator_api.is_email(e_mail) as is_valid
  from (values
          ('esther.muster@example.com'),
          ('Esther Muster &lt;esther.muster@example.com>'),
          ('esther.muster@dubious.com')
       ) test_data (e_mail);</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">select</span><span style="color: #D4D4D4"> e_mail, validator_api.is_email(e_mail) </span><span style="color: #569CD6">as</span><span style="color: #D4D4D4"> is_valid</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #569CD6">from</span><span style="color: #D4D4D4"> (</span><span style="color: #569CD6">values</span></span>
<span class="line"><span style="color: #D4D4D4">          (</span><span style="color: #CE9178">&#39;esther.muster@example.com&#39;</span><span style="color: #D4D4D4">),</span></span>
<span class="line"><span style="color: #D4D4D4">          (</span><span style="color: #CE9178">&#39;Esther Muster &lt;esther.muster@example.com&gt;&#39;</span><span style="color: #D4D4D4">),</span></span>
<span class="line"><span style="color: #D4D4D4">          (</span><span style="color: #CE9178">&#39;esther.muster@dubious.com&#39;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">       ) test_data (e_mail);</span></span></code></pre></div>



<div class="wp-block-kevinbatdorf-code-block-pro" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>E_MAIL                                      IS_VALID
----------------------------------------- ----------
esther.muster@example.com                          1
Esther Muster &lt;esther.muster@example.com>          0
esther.muster@dubious.com                          0</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">E_MAIL                                      IS_VALID</span></span>
<span class="line"><span style="color: #D4D4D4">----------------------------------------- ----------</span></span>
<span class="line"><span style="color: #D4D4D4">esther.muster@example.com                          1</span></span>
<span class="line"><span style="color: #D4D4D4">Esther Muster &lt;esther.muster@example.com&gt;          0</span></span>
<span class="line"><span style="color: #D4D4D4">esther.muster@dubious.com                          0</span></span></code></pre></div>



<h2 class="wp-block-heading">JavaScript With SQL</h2>



<p class="wp-block-paragraph">The MLE provides a global variable <code>session</code> of type <a href="https://oracle-samples.github.io/mle-modules/docs/mle-js-oracledb/23ai/classes/api.IConnection.html">IConnection</a> to communicate with the current database session. Listing 8 shows an example of a simple update statement using bind variables.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Listing 8: increase_salary_js </span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>create or replace mle module increase_salary_mod 
language javascript as
export function increase_salary(deptno, by_percent) {
   session.execute(`
      update emp
         set sal = sal + sal * :by_percent / 100
         where deptno = :deptno`, &#91;by_percent, deptno&#93;);
}
/
create or replace procedure increase_salary_js(
   in_deptno     in number,
   in_by_percent in number
) as mle module increase_salary_mod
signature 'increase_salary(number, number)';
/</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #9CDCFE">create</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">or</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">replace</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">mle</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">module</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">increase_salary_mod</span><span style="color: #D4D4D4"> </span></span>
<span class="line"><span style="color: #4EC9B0">language</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">javascript</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">as</span></span>
<span class="line"><span style="color: #C586C0">export</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">function</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">increase_salary</span><span style="color: #D4D4D4">(</span><span style="color: #9CDCFE">deptno</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">by_percent</span><span style="color: #D4D4D4">) {</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #9CDCFE">session</span><span style="color: #D4D4D4">.</span><span style="color: #DCDCAA">execute</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">`</span></span>
<span class="line"><span style="color: #CE9178">      update emp</span></span>
<span class="line"><span style="color: #CE9178">         set sal = sal + sal * :by_percent / 100</span></span>
<span class="line"><span style="color: #CE9178">         where deptno = :deptno`</span><span style="color: #D4D4D4">, &#91;</span><span style="color: #9CDCFE">by_percent</span><span style="color: #D4D4D4">, </span><span style="color: #9CDCFE">deptno</span><span style="color: #D4D4D4">&#93;);</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span>
<span class="line"><span style="color: #9CDCFE">create</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">or</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">replace</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">procedure</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">increase_salary_js</span><span style="color: #D4D4D4">(</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #9CDCFE">in_deptno</span><span style="color: #D4D4D4">     </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">number</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #9CDCFE">in_by_percent</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #9CDCFE">number</span></span>
<span class="line"><span style="color: #D4D4D4">) </span><span style="color: #C586C0">as</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">mle</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">module</span><span style="color: #D4D4D4"> </span><span style="color: #4EC9B0">increase_salary_mod</span></span>
<span class="line"><span style="color: #9CDCFE">signature</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&#39;increase_salary(number, number)&#39;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span></code></pre></div>



<h2 class="wp-block-heading">PL/SQL With SQL</h2>



<p class="wp-block-paragraph">Listing 9 shows the PL/SQL counterpart to the JavaScript code in Listing 8, using dynamic SQL with bind variables.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Listing 9: increase_salary_dplsql</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>create or replace procedure increase_salary_dplsql(
   in_deptno     in number,
   in_by_percent in number
) is
begin
   execute immediate '
      update emp
         set sal = sal + sal * :by_percent / 100
      where deptno = :deptno' using in_by_percent, in_deptno;
end increase_salary_dplsql;
/</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> procedure increase_salary_dplsql(</span></span>
<span class="line"><span style="color: #D4D4D4">   in_deptno     </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">   in_by_percent </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span></span>
<span class="line"><span style="color: #D4D4D4">) </span><span style="color: #569CD6">is</span></span>
<span class="line"><span style="color: #569CD6">begin</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #DCDCAA">execute immediate</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&#39;</span></span>
<span class="line"><span style="color: #CE9178">      update emp</span></span>
<span class="line"><span style="color: #CE9178">         set sal = sal + sal * :by_percent / 100</span></span>
<span class="line"><span style="color: #CE9178">      where deptno = :deptno&#39;</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">using</span><span style="color: #D4D4D4"> in_by_percent, in_deptno;</span></span>
<span class="line"><span style="color: #569CD6">end</span><span style="color: #D4D4D4"> increase_salary_dplsql;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span></code></pre></div>



<p class="wp-block-paragraph">Experienced PL/SQL developers would not write it this way, as syntax and semantic errors are only thrown at runtime. In addition, it is more expensive for the Oracle Database to execute dynamic SQL, and the use of database objects is not stored in the Oracle Data Dictionary. Instead, experienced PL/SQL developers use static SQL whenever possible and sensible. The code is shorter and SQL injection is impossible. Listing 10 shows the static SQL variant.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Listing 10: increase_salary_plsql</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>create or replace procedure increase_salary_plsql(
   in_deptno     in number,
   in_by_percent in number
) is
begin
   update emp
      set sal = sal + sal * in_by_percent / 100
    where deptno = in_deptno;
end increase_salary_plsql;
/</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> procedure increase_salary_plsql(</span></span>
<span class="line"><span style="color: #D4D4D4">   in_deptno     </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">   in_by_percent </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span></span>
<span class="line"><span style="color: #D4D4D4">) </span><span style="color: #569CD6">is</span></span>
<span class="line"><span style="color: #569CD6">begin</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">update</span><span style="color: #D4D4D4"> emp</span></span>
<span class="line"><span style="color: #D4D4D4">      </span><span style="color: #569CD6">set</span><span style="color: #D4D4D4"> sal = sal + sal * in_by_percent / </span><span style="color: #B5CEA8">100</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #569CD6">where</span><span style="color: #D4D4D4"> deptno = in_deptno;</span></span>
<span class="line"><span style="color: #569CD6">end</span><span style="color: #D4D4D4"> increase_salary_plsql;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span></code></pre></div>



<h2 class="wp-block-heading">Performance of <code>increase_salary_...</code></h2>



<p class="wp-block-paragraph">Figure 3 compares the runtimes of 100,000 procedure calls in a PL/SQL loop. Only <code>increase_salary_jsloop</code> uses a JavaScript loop. This avoids 100,000 context switches between PL/SQL and JavaScript. In other words, the difference between <code>increase_salary_js</code> and <code>increase_salary_jsloop</code> is the cost of 100,000 context switches.</p>



<figure class="wp-block-image size-large"><a href="https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure3.png"><img decoding="async" width="1024" height="440" src="https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure3-1024x440.png" alt="Runtime of 100,000 procedure calls" class="wp-image-13673" srcset="https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure3-1024x440.png 1024w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure3-300x129.png 300w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure3-768x330.png 768w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure3-1536x660.png 1536w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure3-2048x880.png 2048w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure3-150x64.png 150w, https://www.salvis.com/blog/wp-content/uploads/2024/10/Figure3-480x206.png 480w" sizes="(max-width:767px) 480px, (max-width:1024px) 100vw, 1024px" /></a><figcaption class="wp-element-caption"><em>Figure 3: Runtime of 100,000 procedure calls</em></figcaption></figure>



<p class="wp-block-paragraph">In the Oracle Database version 23.5, JavaScript is about 5 to 6 times slower than PL/SQL in this example when we use dynamic SQL. In the Oracle Database version 23.3, the difference was a factor of 7, which makes me optimistic that we can expect further performance improvements in future versions.</p>



<p class="wp-block-paragraph">Based on these experiments, it is difficult to make general statements about the performance differences between PL/SQL and JavaScript. However, it appears that PL/SQL code with SQL statements has an advantage over JavaScript.</p>



<h2 class="wp-block-heading">MLE Environment</h2>



<p class="wp-block-paragraph">Accessing the network using the <a href="https://oracle-samples.github.io/mle-modules/docs/mle-js-fetch/23ai/">JavaScript Fetch API</a> is possible, If the appropriate permissions have been granted using the PL/SQL package <code><a href="https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/DBMS_NETWORK_ACL_ADMIN.html">dbms_network_acl_admin</a></code>. However, for security reasons, JavaScript cannot access the database server&#8217;s file system.</p>



<p class="wp-block-paragraph">JavaScript runtime environments such as Node.js, Deno, Bun or web browsers, access the file system to import other JavaScript modules. For that, you need an MLE environment in the Oracle Database. Listing 11 shows how to create and use it.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(234, 191, 191, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">Listing 11: Using an MLE environment</span><span role="button" tabindex="0" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><pre class="code-block-pro-copy-button-pre" aria-hidden="true"><textarea class="code-block-pro-copy-button-textarea" tabindex="-1" aria-hidden="true" readonly>create or replace mle env demo_env
    imports(
        'increase_salary' module increase_salary_mod,
        'validator'       module validator_mod,
        'util'            module util_mod
    )
    language options  'js.strict=true, js.console=false,
                       js.polyglot-builtin=true'
;
create or replace mle module increase_salary_loop_mod 
language javascript as   
import {increase_salary} from "increase_salary";
export function increase_salary_loop(deptno,by_percent,times){
   for (let i=0; i&lt;times; i++) {
      increase_salary(deptno, by_percent);
   }
}
/
create or replace procedure increase_salary_jsloop(
   in_deptno     in number,
   in_by_percent in number,
   in_times      in number
) as mle module increase_salary_loop_mod
env demo_env
signature 'increase_salary_loop(number, number, number)';
/</textarea></pre><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line cbp-line-highlight"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> mle env demo_env</span></span>
<span class="line"><span style="color: #D4D4D4">    imports(</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">        </span><span style="color: #CE9178">&#39;increase_salary&#39;</span><span style="color: #D4D4D4"> module increase_salary_mod,</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #CE9178">&#39;validator&#39;</span><span style="color: #D4D4D4">       module validator_mod,</span></span>
<span class="line"><span style="color: #D4D4D4">        </span><span style="color: #CE9178">&#39;util&#39;</span><span style="color: #D4D4D4">            module util_mod</span></span>
<span class="line"><span style="color: #D4D4D4">    )</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #569CD6">language</span><span style="color: #D4D4D4"> options  </span><span style="color: #CE9178">&#39;js.strict=true, js.console=false,</span></span>
<span class="line"><span style="color: #CE9178">                       js.polyglot-builtin=true&#39;</span></span>
<span class="line"><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> mle module increase_salary_loop_mod </span></span>
<span class="line"><span style="color: #569CD6">language</span><span style="color: #D4D4D4"> javascript </span><span style="color: #569CD6">as</span><span style="color: #D4D4D4">   </span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">import {increase_salary} </span><span style="color: #569CD6">from</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;increase_salary&quot;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">export function increase_salary_loop(deptno,by_percent,times){</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #C586C0">for</span><span style="color: #D4D4D4"> (let i=</span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">; i&lt;times; i++) {</span></span>
<span class="line"><span style="color: #D4D4D4">      increase_salary(deptno, by_percent);</span></span>
<span class="line"><span style="color: #D4D4D4">   }</span></span>
<span class="line"><span style="color: #D4D4D4">}</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span>
<span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> procedure increase_salary_jsloop(</span></span>
<span class="line"><span style="color: #D4D4D4">   in_deptno     </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">   in_by_percent </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">   in_times      </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span></span>
<span class="line"><span style="color: #D4D4D4">) </span><span style="color: #569CD6">as</span><span style="color: #D4D4D4"> mle module increase_salary_loop_mod</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">env demo_env</span></span>
<span class="line"><span style="color: #D4D4D4">signature </span><span style="color: #CE9178">&#39;increase_salary_loop(number, number, number)&#39;</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span></code></pre></div>



<p class="wp-block-paragraph">The MLE environment <code>demo_env</code> maps the import name <code>increase_salary</code> to the MLE module <code>increase_salary_mod</code>. This import is used in the MLE module <code>increase_salary_loop_mod</code>. However, the MLE environment is not assigned there. This is only done in the MLE call specification <code>increase_salary_jsloop</code>.</p>



<p class="wp-block-paragraph">MLE environments allow JavaScript code to be structured in the same way inside and outside the database. In most cases, a single MLE environment will be sufficient for an application. Multiple MLE environments are required if different language options are used per module, or if different versions of a module are to be loaded with the same import name.</p>



<h2 class="wp-block-heading">Is Tom Kyte’s Mantra Still Valid?</h2>



<p class="wp-block-paragraph">One of the things Tom Kyte is famous for is his mantra. There are several variations, but all have the same message. This variant is from Expert Oracle Database Architecture, Third Edition, 2014. On page 3 he writes:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">I have a pretty simple mantra when it comes to developing database software, one that has been consistent for many years:</p>



<ul class="wp-block-list">
<li>You should do it in a single SQL statement if at all possible. And believe it or not, it is almost always possible. This statement is even truer as time goes on. SQL is an extremely powerful language.</li>



<li>If you can’t do it in a single SQL Statement, do it in PL/SQL—as little PL/SQL as possible! Follow the saying that goes &#8220;more code = more bugs, less code = less bugs.&#8221;</li>



<li>If you can’t do it in PL/SQL, try a Java stored procedure. The times this is necessary are extremely rare nowadays with Oracle9i and above. PL/SQL is an extremely competent, fully featured 3GL.</li>



<li>If you can’t do it in Java, do it in a C external procedure. This is most frequently the approach when raw speed or using a third-party API written in C is needed.</li>



<li>If you can’t do it in a C external routine, you might want to seriously think about why it is you need to do it.</li>
</ul>
</blockquote>



<p class="wp-block-paragraph">With Oracle Database 23ai, I would put JavaScript on the same level as PL/SQL. Before Java, definitely. Furthermore, it is not just a question of whether something can be done in SQL or PL/SQL. If we need a functionality that already exists in the JavaScript ecosystem, we should consider using it rather than reimplementing it in SQL or PL/SQL just because it&#8217;s possible. Ultimately, it is also about the maintainability of the application and the technical debt we are incurring.</p>



<h2 class="wp-block-heading">Conclusion</h2>



<p class="wp-block-paragraph">MLE was introduced as an experimental feature at Oracle Open World 2017. Since then, MLE and the underlying GraalVM technology have been continuously improved and have reached a good, production-ready state in Oracle Database 23ai. It is ideally suited for integrating existing, tested functionality from the JavaScript ecosystem into the Oracle Database.</p>



<p class="wp-block-paragraph">We still need to figure out how to develop, test, debug and deploy JavaScript with SQL. In any case, JavaScript is a real alternative to PL/SQL, even if PL/SQL scores with static SQL and better performance.</p>



<p class="wp-block-paragraph"></p>
<p>The post <a href="https://www.salvis.com/blog/2024/10/16/pl-sql-vs-javascript-in-the-oracle-database-23ai-joelkallmanday/">PL/SQL vs. JavaScript in the Oracle Database 23ai #JoelKallmanDay</a> appeared first on <a href="https://www.salvis.com/blog">Philipp Salvisberg&#039;s Blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.salvis.com/blog/2024/10/16/pl-sql-vs-javascript-in-the-oracle-database-23ai-joelkallmanday/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>optimizer_secure_view_merging and plsql_declarations</title>
		<link>https://www.salvis.com/blog/2022/10/30/optimizer_secure_view_merging-and-plsql_declarations/</link>
					<comments>https://www.salvis.com/blog/2022/10/30/optimizer_secure_view_merging-and-plsql_declarations/#comments</comments>
		
		<dc:creator><![CDATA[Philipp Salvisberg]]></dc:creator>
		<pubDate>Sun, 30 Oct 2022 11:56:32 +0000</pubDate>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Autonomous Database]]></category>
		<category><![CDATA[PinkDB]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://www.salvis.com/blog/?p=11897</guid>

					<description><![CDATA[<p>The Original Problem A customer is currently upgrading some Oracle databases from 11.2 to 19c. One query was extremely slow on the new test system and my job was to find out why. The root cause was that the database parameter optimizer_secure_view_merging was set to a different value. In 19c true and<span class="excerpt-hellip"> […]</span></p>
<p>The post <a href="https://www.salvis.com/blog/2022/10/30/optimizer_secure_view_merging-and-plsql_declarations/">optimizer_secure_view_merging and plsql_declarations</a> appeared first on <a href="https://www.salvis.com/blog">Philipp Salvisberg&#039;s Blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">The Original Problem</h2>



<p class="wp-block-paragraph">A customer is currently upgrading some Oracle databases from 11.2 to 19c. One query was extremely slow on the new test system and my job was to find out why. The root cause was that the database parameter <a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/refrn/OPTIMIZER_SECURE_VIEW_MERGING.html">optimizer_secure_view_merging</a> was set to a different value. In 19c <code>true</code> and <code>false</code> in 11.2. This led to a different and in fact bad execution plan in 19c.</p>



<p class="wp-block-paragraph">Now the question was, should the customer keep the default value of <code>optimizer_secure_view_merging</code> in 19c and rewrite the slow query or change the parameter to <code>false</code> as in 11.2 to get a good performance without a code change?</p>



<h2 class="wp-block-heading">What About the <code>opt_param</code> Hint?</h2>



<p class="wp-block-paragraph">Actually, the first thing I tried was the <code>opt_param('optimizer_secure_view_merging','false')</code> hint. Unfortunately, this does not work in 19c. It&#8217;s a known bug 28504113. Fixed in 23c. However, I can&#8217;t really recommend waiting for 23c, right?</p>



<h2 class="wp-block-heading">What About the <code>merge view</code> Privilege?</h2>



<p class="wp-block-paragraph">The <a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/GRANT.html#GUID-20B4E2C0-A7F8-4BC8-A5E8-BE61BDC41AC3__BGBHCCFH">merge any view</a> privilege is a good option for highly privileged users and roles. But it should not be granted lightly to any ordinary role or user.</p>



<p class="wp-block-paragraph">The <a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/GRANT.html#d404029e6564">merge view</a> privilege can be granted per view to a user or role. This has a similar scope as a hint in the subquery of a view without having to change the code. In fact, it is an excellent option to override the <code>optimizer_secure_view_merging</code> setting for a view. We could <code>grant merge view on &lt;owner&gt;.&lt;view_name&gt; to public</code> to mimic the scope of a&nbsp;hint in the subquery of a view.</p>



<p class="wp-block-paragraph">However, the customer uses a metadata-driven approach to generate the grants for end-user roles as part of the application. And it would require a change of the application to handle this exceptional case. Of course, this grant can easily be hard coded for the view in question, but this is something the customer would like to avoid.</p>



<h2 class="wp-block-heading">Christian Antognini&#8217;s Recommendation</h2>



<p class="wp-block-paragraph">Chris explains <code>optimizer_secure_view_merging</code> on pages 289 to 291 in <a href="https://antognini.ch/top/">Troubleshooting Oracle Performance, 2nd Edition</a>. On page 291 he writes the following:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p class="wp-block-paragraph">If you’re neither using views nor VPD for security purposes, I advise you to set the <code>optimizer_secure_view_merging</code> initialization parameter to <code>FALSE</code>.</p>
</blockquote>



<p class="wp-block-paragraph">In my case, the customer uses views and protects them with Virtual Private Database policies. According to Chris, the customer should keep the default value <code>true</code> for <code>optimizer_secure_view_merging</code>. A sound advice.</p>



<h2 class="wp-block-heading">What Security Risk Are We Talking About?</h2>



<p class="wp-block-paragraph"><a href="https://antognini.ch/top/">Troubleshooting Oracle Performance, 2nd Edition</a> comes with an <a href="https://antognini.ch/downloads/top2/allfiles.zip">allfiles.zip</a> file. It contains a script <code>optimizer_secure_view_merging.sql</code> in the folder <code>chapter09</code>. Chris used this script to explain the impact of <code>optimizer_secure_view_merging</code> in his book. I reused this script here with minor changes.</p>



<p class="wp-block-paragraph">Let&#8217;s connect as user <code>sys</code> and create a database user <code>u1</code> for the application data and code and a user <code>u2</code> as connect user (with passwords which work in Autonomous Databases). We also disable <code>optimizer_secure_view_merging</code>.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">1) Setup as sys</span><span role="button" tabindex="0" data-code="create user u1 identified by &quot;AppOwner2022&quot;    default tablespace users quota unlimited on users;
create user u2 identified by &quot;ConnectUser2022&quot; default tablespace users quota unlimited on users;

grant create session, create table, create procedure, create view, create public synonym to u1;
grant create session, create procedure to u2;

alter system set optimizer_secure_view_merging=false scope=memory;" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">create</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">user</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">u1</span><span style="color: #D4D4D4"> identified </span><span style="color: #569CD6">by</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;AppOwner2022&quot;</span><span style="color: #D4D4D4">    </span><span style="color: #569CD6">default</span><span style="color: #D4D4D4"> tablespace users quota </span><span style="color: #569CD6">unlimited</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">on</span><span style="color: #D4D4D4"> users;</span></span>
<span class="line"><span style="color: #569CD6">create</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">user</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">u2</span><span style="color: #D4D4D4"> identified </span><span style="color: #569CD6">by</span><span style="color: #D4D4D4"> </span><span style="color: #CE9178">&quot;ConnectUser2022&quot;</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">default</span><span style="color: #D4D4D4"> tablespace users quota </span><span style="color: #569CD6">unlimited</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">on</span><span style="color: #D4D4D4"> users;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">grant</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">create</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">session</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">create</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">table</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">create</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">procedure</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">create</span><span style="color: #D4D4D4"> view, </span><span style="color: #569CD6">create</span><span style="color: #D4D4D4"> public </span><span style="color: #569CD6">synonym</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">to</span><span style="color: #D4D4D4"> u1;</span></span>
<span class="line"><span style="color: #569CD6">grant</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">create</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">session</span><span style="color: #D4D4D4">, </span><span style="color: #569CD6">create</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">procedure</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">to</span><span style="color: #D4D4D4"> u2;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">alter</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">system</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">set</span><span style="color: #D4D4D4"> optimizer_secure_view_merging=false scope=memory;</span></span></code></pre></div>



<p class="wp-block-paragraph">Now we connect as user <code>u1</code> and create a table <code>t</code> with 6 rows. and a function <code>f</code> to filter rows in the view <code>v</code>.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">2) Setup as u1</span><span role="button" tabindex="0" data-code="create table t (
  id    number(10) primary key,
  class number(10),
  pad   varchar2(10)
);

execute dbms_random.seed(0)

insert into t (id, class, pad)
select rownum, mod(rownum, 3), dbms_random.string('a', 10)
  from dual
connect by level <= 6;

execute dbms_stats.gather_table_stats(user, 't')

create or replace function f(in_class in number) return number as
begin
   if in_class = 1 then
      return 1;
   else
      return 0;
   end if;
end;
/

create or replace view v as
   select *
     from t
    where f(class) = 1;

grant select on v to u2;

create or replace public synonym v for u1.v;" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">create</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">table</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">t</span><span style="color: #D4D4D4"> (</span></span>
<span class="line"><span style="color: #D4D4D4">  id    </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4">(</span><span style="color: #B5CEA8">10</span><span style="color: #D4D4D4">) </span><span style="color: #569CD6">primary key</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">  class </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4">(</span><span style="color: #B5CEA8">10</span><span style="color: #D4D4D4">),</span></span>
<span class="line"><span style="color: #D4D4D4">  pad   </span><span style="color: #569CD6">varchar2</span><span style="color: #D4D4D4">(</span><span style="color: #B5CEA8">10</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">execute</span><span style="color: #D4D4D4"> dbms_random.seed(</span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">insert into</span><span style="color: #D4D4D4"> t (id, class, pad)</span></span>
<span class="line"><span style="color: #569CD6">select</span><span style="color: #D4D4D4"> rownum, mod(rownum, </span><span style="color: #B5CEA8">3</span><span style="color: #D4D4D4">), dbms_random.string(</span><span style="color: #CE9178">&#39;a&#39;</span><span style="color: #D4D4D4">, </span><span style="color: #B5CEA8">10</span><span style="color: #D4D4D4">)</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #569CD6">from</span><span style="color: #D4D4D4"> dual</span></span>
<span class="line"><span style="color: #569CD6">connect</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">by</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">level</span><span style="color: #D4D4D4"> &lt;= </span><span style="color: #B5CEA8">6</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">execute</span><span style="color: #D4D4D4"> dbms_stats.gather_table_stats(user, </span><span style="color: #CE9178">&#39;t&#39;</span><span style="color: #D4D4D4">)</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">function</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">f</span><span style="color: #D4D4D4">(in_class </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4">) </span><span style="color: #569CD6">return</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">as</span></span>
<span class="line"><span style="color: #569CD6">begin</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">if</span><span style="color: #D4D4D4"> in_class = </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">then</span></span>
<span class="line"><span style="color: #D4D4D4">      </span><span style="color: #569CD6">return</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">else</span></span>
<span class="line"><span style="color: #D4D4D4">      </span><span style="color: #569CD6">return</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">0</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">end</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">if</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #569CD6">end</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">view</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">v</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">as</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">select</span><span style="color: #D4D4D4"> *</span></span>
<span class="line"><span style="color: #D4D4D4">     </span><span style="color: #569CD6">from</span><span style="color: #D4D4D4"> t</span></span>
<span class="line"><span style="color: #D4D4D4">    </span><span style="color: #569CD6">where</span><span style="color: #D4D4D4"> f(class) = </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">grant</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">select</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">on</span><span style="color: #D4D4D4"> v </span><span style="color: #569CD6">to</span><span style="color: #D4D4D4"> u2;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">create</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">or</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">replace</span><span style="color: #D4D4D4"> public </span><span style="color: #569CD6">synonym</span><span style="color: #D4D4D4"> v </span><span style="color: #569CD6">for</span><span style="color: #D4D4D4"> u1.v;</span></span></code></pre></div>



<p class="wp-block-paragraph">Let&#8217;s connect as user <code>u2</code> to query the view.</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">3) Query view v as u2</span><span role="button" tabindex="0" data-code="select id, pad
  from v
 where id between 1 and 5;

        ID PAD       
---------- ----------
         1 DrMLTDXxxq
         4 AszBGEUGEL" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">select</span><span style="color: #D4D4D4"> id, pad</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #569CD6">from</span><span style="color: #D4D4D4"> v</span></span>
<span class="line"><span style="color: #D4D4D4"> </span><span style="color: #569CD6">where</span><span style="color: #D4D4D4"> id </span><span style="color: #569CD6">between</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">and</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">5</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        ID PAD       </span></span>
<span class="line"><span style="color: #6A9955">---------- ----------</span></span>
<span class="line"><span style="color: #D4D4D4">         </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4"> DrMLTDXxxq</span></span>
<span class="line"><span style="color: #D4D4D4">         </span><span style="color: #B5CEA8">4</span><span style="color: #D4D4D4"> AszBGEUGEL</span></span></code></pre></div>



<p class="wp-block-paragraph">Only two of five rows are returned due to the where clause in the view. So far so good.</p>



<p class="wp-block-paragraph">The user <code>u2</code> has the right to create own functions. And that is a security risk. Why? Because the user can write a <code>spy</code>&nbsp;function like in the next example:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(234, 191, 191, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">4) Accessing protected data as u2</span><span role="button" tabindex="0" data-code="create or replace function spy(
   in_id  in number,
   in_pad in varchar2
) return number as
begin
   dbms_output.put_line('id='
      || in_id
      || ' pad='
      || in_pad);
   return 1;
end;
/

set serveroutput on size unlimited
select id, pad
  from v
 where id between 1 and 5
   and spy(id, pad) = 1;

        ID PAD       
---------- ----------
         1 DrMLTDXxxq
         4 AszBGEUGEL

id=1 pad=DrMLTDXxxq
id=2 pad=XOZnqYRJwI
id=3 pad=nlGfGBTxNk
id=4 pad=AszBGEUGEL
id=5 pad=qTSRnFjRGb" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">function</span><span style="color: #D4D4D4"> </span><span style="color: #DCDCAA">spy</span><span style="color: #D4D4D4">(</span></span>
<span class="line"><span style="color: #D4D4D4">   in_id  </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">   in_pad </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">varchar2</span></span>
<span class="line"><span style="color: #D4D4D4">) </span><span style="color: #569CD6">return</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">as</span></span>
<span class="line"><span style="color: #569CD6">begin</span></span>
<span class="line"><span style="color: #D4D4D4">   dbms_output.put_line(</span><span style="color: #CE9178">&#39;id=&#39;</span></span>
<span class="line"><span style="color: #D4D4D4">      || in_id</span></span>
<span class="line"><span style="color: #D4D4D4">      || </span><span style="color: #CE9178">&#39; pad=&#39;</span></span>
<span class="line"><span style="color: #D4D4D4">      || in_pad);</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">return</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #569CD6">end</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">set</span><span style="color: #D4D4D4"> serveroutput </span><span style="color: #569CD6">on</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">size</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">unlimited</span></span>
<span class="line"><span style="color: #569CD6">select</span><span style="color: #D4D4D4"> id, pad</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #569CD6">from</span><span style="color: #D4D4D4"> v</span></span>
<span class="line"><span style="color: #D4D4D4"> </span><span style="color: #569CD6">where</span><span style="color: #D4D4D4"> id </span><span style="color: #569CD6">between</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">and</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">5</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">and</span><span style="color: #D4D4D4"> spy(id, pad) = </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        ID PAD       </span></span>
<span class="line"><span style="color: #6A9955">---------- ----------</span></span>
<span class="line"><span style="color: #D4D4D4">         </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4"> DrMLTDXxxq</span></span>
<span class="line"><span style="color: #D4D4D4">         </span><span style="color: #B5CEA8">4</span><span style="color: #D4D4D4"> AszBGEUGEL</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">id=</span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4"> pad=DrMLTDXxxq</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">id=</span><span style="color: #B5CEA8">2</span><span style="color: #D4D4D4"> pad=XOZnqYRJwI</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">id=</span><span style="color: #B5CEA8">3</span><span style="color: #D4D4D4"> pad=nlGfGBTxNk</span></span>
<span class="line"><span style="color: #D4D4D4">id=</span><span style="color: #B5CEA8">4</span><span style="color: #D4D4D4"> pad=AszBGEUGEL</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">id=</span><span style="color: #B5CEA8">5</span><span style="color: #D4D4D4"> pad=qTSRnFjRGb</span></span></code></pre></div>



<p class="wp-block-paragraph">Look at the server output for id 3, 4 and 5. By using the spy function in the where clause the user can get access to all rows in table <code>t</code>. This is only possible because</p>



<ul class="wp-block-list">
<li>the database parameter <code>optimizer_secure_view_merging</code> is set to <code>false</code>,</li>



<li>the optimizer applies the <code>spy</code> function to an intermediate result and</li>



<li>the user <code>u2</code> has the <code>create procedure</code> privilege.</li>
</ul>



<p class="wp-block-paragraph">When you call <code>alter system set optimizer_secure_view_merging=true scope=memory;</code> then the result of the previous query looks like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">5) Result with optimizer_secure_view_merging=true</span><span role="button" tabindex="0" data-code="        ID PAD       
---------- ----------
         1 DrMLTDXxxq
         4 AszBGEUGEL

id=1 pad=DrMLTDXxxq
id=4 pad=AszBGEUGEL" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #D4D4D4">        ID PAD       </span></span>
<span class="line"><span style="color: #6A9955">---------- ----------</span></span>
<span class="line"><span style="color: #D4D4D4">         </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4"> DrMLTDXxxq</span></span>
<span class="line"><span style="color: #D4D4D4">         </span><span style="color: #B5CEA8">4</span><span style="color: #D4D4D4"> AszBGEUGEL</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">id=</span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4"> pad=DrMLTDXxxq</span></span>
<span class="line"><span style="color: #D4D4D4">id=</span><span style="color: #B5CEA8">4</span><span style="color: #D4D4D4"> pad=AszBGEUGEL</span></span></code></pre></div>



<p class="wp-block-paragraph">The <code>spy</code> function does not reveal protected data anymore. Thanks to <code>optimizer_secure_view_merging=true</code>.</p>



<h2 class="wp-block-heading">The Next Problem</h2>



<p class="wp-block-paragraph">The customer&#8217;s connect users do not have <code>create procedure</code> privileges. After all, It&#8217;s a <a href="https://www.salvis.com/blog/2018/07/18/the-pink-database-paradigm-pinkdb/">PinkDB</a> application. Hence I could recommend to set <code>optimizer_secure_view_merging=false</code>, because the connect users would not be able to write their own <code>spy</code> functions, right?</p>



<p class="wp-block-paragraph">Wrong. For two reasons.</p>



<p class="wp-block-paragraph">Firstly, the user could have access to an existing function that might be misused, e.g. a logger function.</p>



<p class="wp-block-paragraph">Secondly, we are on 19c. And since 12.1 we have <a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SELECT.html#GUID-CFA006CA-6FF1-4972-821E-6996142A51C6__GUID-28DA0E1D-87BF-462E-BCB8-8F77921022F9">plsql_declarations</a> to write PL/SQL functions and procedures in the&nbsp;<a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SELECT.html#GUID-CFA006CA-6FF1-4972-821E-6996142A51C6__GUID-7BFCEBF8-6459-4328-8CFC-6FF044E8A147">with_clause</a> of a <a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/SELECT.html#GUID-CFA006CA-6FF1-4972-821E-6996142A51C6">select statement</a>. As a result, I can write a <code>spy</code> function without the <code>create procedure</code> privilege. For example like this:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(234, 191, 191, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">6) Spy function in the with_clause</span><span role="button" tabindex="0" data-code="set serveroutput on size unlimited
with
   function spy(
      in_id  in number,
      in_pad in varchar2
   ) return number as
   begin
      dbms_output.put_line('id='
         || in_id
         || ' pad='
         || in_pad);
      return 1;
   end;
select id, pad
  from v
 where id between 1 and 5
   and spy(id, pad) = 1;
/

        ID PAD       
---------- ----------
         1 DrMLTDXxxq
         4 AszBGEUGEL

id=1 pad=DrMLTDXxxq
id=2 pad=XOZnqYRJwI
id=3 pad=nlGfGBTxNk
id=4 pad=AszBGEUGEL
id=5 pad=qTSRnFjRGb" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">set</span><span style="color: #D4D4D4"> serveroutput </span><span style="color: #569CD6">on</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">size</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">unlimited</span></span>
<span class="line"><span style="color: #569CD6">with</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">function</span><span style="color: #D4D4D4"> spy(</span></span>
<span class="line"><span style="color: #D4D4D4">      in_id  </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">      in_pad </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">varchar2</span></span>
<span class="line"><span style="color: #D4D4D4">   ) </span><span style="color: #569CD6">return</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">number</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">as</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">begin</span></span>
<span class="line"><span style="color: #D4D4D4">      dbms_output.put_line(</span><span style="color: #CE9178">&#39;id=&#39;</span></span>
<span class="line"><span style="color: #D4D4D4">         || in_id</span></span>
<span class="line"><span style="color: #D4D4D4">         || </span><span style="color: #CE9178">&#39; pad=&#39;</span></span>
<span class="line"><span style="color: #D4D4D4">         || in_pad);</span></span>
<span class="line"><span style="color: #D4D4D4">      </span><span style="color: #569CD6">return</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">end</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #569CD6">select</span><span style="color: #D4D4D4"> id, pad</span></span>
<span class="line"><span style="color: #D4D4D4">  </span><span style="color: #569CD6">from</span><span style="color: #D4D4D4"> v</span></span>
<span class="line"><span style="color: #D4D4D4"> </span><span style="color: #569CD6">where</span><span style="color: #D4D4D4"> id </span><span style="color: #569CD6">between</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">and</span><span style="color: #D4D4D4"> </span><span style="color: #B5CEA8">5</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #569CD6">and</span><span style="color: #D4D4D4"> spy(id, pad) = </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">        ID PAD       </span></span>
<span class="line"><span style="color: #6A9955">---------- ----------</span></span>
<span class="line"><span style="color: #D4D4D4">         </span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4"> DrMLTDXxxq</span></span>
<span class="line"><span style="color: #D4D4D4">         </span><span style="color: #B5CEA8">4</span><span style="color: #D4D4D4"> AszBGEUGEL</span></span>
<span class="line"></span>
<span class="line"><span style="color: #D4D4D4">id=</span><span style="color: #B5CEA8">1</span><span style="color: #D4D4D4"> pad=DrMLTDXxxq</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">id=</span><span style="color: #B5CEA8">2</span><span style="color: #D4D4D4"> pad=XOZnqYRJwI</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">id=</span><span style="color: #B5CEA8">3</span><span style="color: #D4D4D4"> pad=nlGfGBTxNk</span></span>
<span class="line"><span style="color: #D4D4D4">id=</span><span style="color: #B5CEA8">4</span><span style="color: #D4D4D4"> pad=AszBGEUGEL</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">id=</span><span style="color: #B5CEA8">5</span><span style="color: #D4D4D4"> pad=qTSRnFjRGb</span></span></code></pre></div>



<p class="wp-block-paragraph">Again, look at the server output for id 3, 4 and 5. Protected data is revealed, even if the user has only the <code>create session</code> privilege and <code>optimizer_secure_view_merging</code> is set to <code>true</code>. IMO this is clearly a security bug.</p>



<h2 class="wp-block-heading">What Database Versions Are Affected?</h2>



<p class="wp-block-paragraph">I assume that all Oracle Database versions from 12.1 onwards are affected. Including Autonomous Databases. I have explicitly tested the following versions:</p>



<ul class="wp-block-list">
<li>OCI as of 2022-10-30:
<ul class="wp-block-list">
<li>Autonomous Database 21c (ATP)</li>



<li>Autonomous Database 19c (ADW, AJD)</li>
</ul>
</li>



<li>On-Premises
<ul class="wp-block-list">
<li>Oracle Database XE 21c</li>



<li>Oracle Database Enterprise Edition 19c (19.16)</li>
</ul>
</li>
</ul>



<h2 class="wp-block-heading">What Can We Do?</h2>



<p class="wp-block-paragraph">I created SR 3-31087264311 for this issue. I expect that either a workaround is provided or a bug is opened and a fix will be available soon. I&#8217;ll update this blog post accordingly. <em>(Update on 2023-02-27: Bug 34777606 is referenced in the SR, but not visible. However, it&#8217;s an accepted security bug and fixed in 23c. I&#8217;ve tested it successfully in 23.2. It&#8217;s not planned to include a fix as part of an RU for 19c. However, you can request a one-off patch via SR.).</em></p>



<p class="wp-block-paragraph">In any case, if you have views or VPD policies for security purposes, set <code>optimizer_secure_view_merging=true</code> and ensure that the connect users do not have the <code>create procedure</code> privilege. Follow the <a href="https://en.wikipedia.org/wiki/Principle_of_least_privilege">principle of least privileges</a>.</p>



<p class="wp-block-paragraph">As a workaround, you can use <a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/dbseg/configuring-audit-policies.html#GUID-B706FF6F-13A6-4944-AFCB-29971F5076FD">Fine-Grained Auditing</a> to identify statements with <code>plsql_declarations</code>. You can either log them or abort the execution as in the following example:</p>



<div class="wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(234, 191, 191, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#2b2b2b;color:#c7c7c7">7) Abort execution of statements with plsql_declarations (as sys)</span><span role="button" tabindex="0" data-code="create or replace procedure u1.suppress_plsql_delcarations_handler(
   in_object_owner in varchar2,
   in_object_name  in varchar2,
   in_policy_name  in varchar2
) is
begin
   if regexp_like(sys_context('userenv', 'current_sql'), 'with[^fp]+(function|procedure)', 'i') then
      raise_application_error(-20942, 'plsql_declarations are not allowed in the with_clause. See policy '
         || in_policy_name
         || ' for '
         || in_object_owner
         || '.'
         || in_object_name);
   end if;
end;
/

begin
   dbms_fga.add_policy(
      object_schema   =&gt; 'U1',
      object_name     =&gt; 'V',
      policy_name     =&gt; 'SUPPRESS_PLSQL_DELCARATIONS_POLICY',
      handler_schema  =&gt; 'U1',
      handler_module  =&gt; 'SUPPRESS_PLSQL_DELCARATIONS_HANDLER',
      statement_types =&gt; 'SELECT,INSERT,UPDATE,DELETE'
   );
end;
/" style="color:#D4D4D4;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki dark-plus" style="background-color: #1E1E1E" tabindex="0"><code><span class="line"><span style="color: #569CD6">create or replace</span><span style="color: #D4D4D4"> procedure u1.suppress_plsql_delcarations_handler(</span></span>
<span class="line"><span style="color: #D4D4D4">   in_object_owner </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">varchar2</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">   in_object_name  </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">varchar2</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">   in_policy_name  </span><span style="color: #569CD6">in</span><span style="color: #D4D4D4"> </span><span style="color: #569CD6">varchar2</span></span>
<span class="line"><span style="color: #D4D4D4">) </span><span style="color: #569CD6">is</span></span>
<span class="line"><span style="color: #569CD6">begin</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #C586C0">if</span><span style="color: #D4D4D4"> regexp_like(</span><span style="color: #DCDCAA">sys_context</span><span style="color: #D4D4D4">(</span><span style="color: #CE9178">&#39;userenv&#39;</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&#39;current_sql&#39;</span><span style="color: #D4D4D4">), </span><span style="color: #CE9178">&#39;with[^fp]+(function|procedure)&#39;</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&#39;i&#39;</span><span style="color: #D4D4D4">) </span><span style="color: #569CD6">then</span></span>
<span class="line"><span style="color: #D4D4D4">      </span><span style="color: #DCDCAA">raise_application_error</span><span style="color: #D4D4D4">(-</span><span style="color: #B5CEA8">20942</span><span style="color: #D4D4D4">, </span><span style="color: #CE9178">&#39;plsql_declarations are not allowed in the with_clause. See policy &#39;</span></span>
<span class="line"><span style="color: #D4D4D4">         || in_policy_name</span></span>
<span class="line"><span style="color: #D4D4D4">         || </span><span style="color: #CE9178">&#39; for &#39;</span></span>
<span class="line"><span style="color: #D4D4D4">         || in_object_owner</span></span>
<span class="line"><span style="color: #D4D4D4">         || </span><span style="color: #CE9178">&#39;.&#39;</span></span>
<span class="line"><span style="color: #D4D4D4">         || in_object_name);</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #C586C0">end if</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #569CD6">end</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span>
<span class="line"></span>
<span class="line"><span style="color: #569CD6">begin</span></span>
<span class="line"><span style="color: #D4D4D4">   </span><span style="color: #DCDCAA">dbms_fga.</span><span style="color: #4EC9B0">add_policy</span><span style="color: #D4D4D4">(</span></span>
<span class="line"><span style="color: #D4D4D4">      object_schema   =&gt; </span><span style="color: #CE9178">&#39;U1&#39;</span><span style="color: #D4D4D4">,</span></span>
<span class="line cbp-line-highlight"><span style="color: #D4D4D4">      object_name     =&gt; </span><span style="color: #CE9178">&#39;V&#39;</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">      policy_name     =&gt; </span><span style="color: #CE9178">&#39;SUPPRESS_PLSQL_DELCARATIONS_POLICY&#39;</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">      handler_schema  =&gt; </span><span style="color: #CE9178">&#39;U1&#39;</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">      handler_module  =&gt; </span><span style="color: #CE9178">&#39;SUPPRESS_PLSQL_DELCARATIONS_HANDLER&#39;</span><span style="color: #D4D4D4">,</span></span>
<span class="line"><span style="color: #D4D4D4">      statement_types =&gt; </span><span style="color: #CE9178">&#39;SELECT,INSERT,UPDATE,DELETE&#39;</span></span>
<span class="line"><span style="color: #D4D4D4">   );</span></span>
<span class="line"><span style="color: #569CD6">end</span><span style="color: #D4D4D4">;</span></span>
<span class="line"><span style="color: #D4D4D4">/</span></span></code></pre></div>



<p class="has-text-align-left wp-block-paragraph">You have to register a policy for each view. This can be easily generated. However, you should consider that such a policy has an impact on performance. Therefore, you should define such a policy only when needed.</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p class="has-text-align-left wp-block-paragraph"><em>Updated on 2022-11-02, documented workaround via <a href="https://docs.oracle.com/en/database/oracle/oracle-database/19/arpls/DBMS_FGA.html#GUID-DC7B93AE-4390-4D27-9CAC-43DC932D8622">DBMS_FGA</a>.</em></p>



<p class="has-text-align-left wp-block-paragraph"><em>Updated on 2022-11-03, updated workaround regarding comments before <code>plsql_declarations</code> and support for all DML statements.</em></p>



<p class="has-text-align-left wp-block-paragraph"><em>Updated on 2023-03-13, added reference to bug 34777606 (not visible), fixed in 23c, no information regarding backport yet.</em></p>



<p class="has-text-align-left wp-block-paragraph"><em>Updated on 2023-03-27,&nbsp; &#8220;Bug 34255928 &#8211; INCORRECT SECURE VIEW MERGING FOR A QUERY WITH A FUNCTION DEFINED IN ITS WITH CLAUSE&#8221; fixed in 23c, backport possible as a one-off patch via SR. No plans to include it in an RU for 19c.</em></p>



<p class="has-text-align-left wp-block-paragraph"><em>Updated on 2023-04-05: Tested successfully in 23c, <span class="s1">Developer-Release Version 23.2.0.0.0</span></em></p>
<p>The post <a href="https://www.salvis.com/blog/2022/10/30/optimizer_secure_view_merging-and-plsql_declarations/">optimizer_secure_view_merging and plsql_declarations</a> appeared first on <a href="https://www.salvis.com/blog">Philipp Salvisberg&#039;s Blog</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.salvis.com/blog/2022/10/30/optimizer_secure_view_merging-and-plsql_declarations/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>DOAG2022 Highlights</title>
		<link>https://www.salvis.com/blog/2022/09/25/doag2022-highlights/</link>
		
		<dc:creator><![CDATA[Philipp Salvisberg]]></dc:creator>
		<pubDate>Sun, 25 Sep 2022 11:18:27 +0000</pubDate>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[PinkDB]]></category>
		<category><![CDATA[PL/SQL]]></category>
		<category><![CDATA[SQL]]></category>
		<guid isPermaLink="false">https://www.salvis.com/blog/?p=11658</guid>

					<description><![CDATA[<p>Nobody knows what kind of restrictions we will experience later this year. That&#8217;s why the DOAG Conference + Exhibition 2022 took place in September instead of November. The organizers wanted an in-person event. While you can access some chosen content remotely an in-person event has a lot of advantages, like getting in<span class="excerpt-hellip"> […]</span></p>
<p>The post <a href="https://www.salvis.com/blog/2022/09/25/doag2022-highlights/">DOAG2022 Highlights</a> appeared first on <a href="https://www.salvis.com/blog">Philipp Salvisberg&#039;s Blog</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">Nobody knows what kind of restrictions we will experience later this year. That&#8217;s why the DOAG Conference + Exhibition 2022 took place in September instead of November. The organizers wanted an in-person event. While you can <a href="https://en.shop.doag.org/shop/prd.404.doag-2022-conference-exhibition-on-demand/">access some chosen content remotely</a> an in-person event has a lot of advantages, like getting in touch with speakers and attendees. I enjoyed it very much. Many thanks to DOAG and all the people behind the scenes who made this possible.</p>



<p class="wp-block-paragraph">In this blog post, I summarize some of my personal highlights in chronological order.</p>



<h2 class="wp-block-heading">Day 1 &#8211; Theme Day</h2>



<p class="wp-block-paragraph">The very first day of the conference was a theme day. Themes were</p>



<ul class="wp-block-list">
<li>PL/SQL &amp; APEX</li>



<li>Oracle Forms</li>



<li>Automation</li>



<li>Ransomware</li>



<li>Database Migrations</li>



<li>Multitenant Architecture</li>



<li>PostgreSQL</li>



<li>Autonomous Database for DevOps</li>



<li>Softskills</li>
</ul>



<p class="wp-block-paragraph">Most of the themes were organized as mini-conferences with sessions you could join. Others were more like workshops with lightning talks as introductions. And there was the opportunity to book some 1:1 time slots with developer experts for questions regarding Oracle database, performance, JSON, XML, blockchain, frontend testing, JavaScript, NodeJS, cloud, containers, microservices, mobile UI, PWA, low code, SSO, APEX good practices, APEX API, APEX plugins, REST, spatial and more.</p>



<p class="wp-block-paragraph">AFAIK all sessions were in German on this day. Participants and speakers without knowledge of German probably felt a bit lost and took the opportunity to talk over a drink or a meal.</p>



<h3 class="wp-block-heading">PostgreSQL Features Oracle People Will Like</h3>



<figure class="wp-block-image"><a href="https://www.salvis.com/blog/wp-content/uploads/2022/09/7D87F5B6-5AB2-451F-A2C9-08E6A1E26E3B_1_105_c.jpeg"><img loading="lazy" decoding="async" width="1024" height="769" src="https://www.salvis.com/blog/wp-content/uploads/2022/09/7D87F5B6-5AB2-451F-A2C9-08E6A1E26E3B_1_105_c.jpeg" alt="" class="wp-image-11664" srcset="https://www.salvis.com/blog/wp-content/uploads/2022/09/7D87F5B6-5AB2-451F-A2C9-08E6A1E26E3B_1_105_c.jpeg 1024w, https://www.salvis.com/blog/wp-content/uploads/2022/09/7D87F5B6-5AB2-451F-A2C9-08E6A1E26E3B_1_105_c-300x225.jpeg 300w, https://www.salvis.com/blog/wp-content/uploads/2022/09/7D87F5B6-5AB2-451F-A2C9-08E6A1E26E3B_1_105_c-768x577.jpeg 768w, https://www.salvis.com/blog/wp-content/uploads/2022/09/7D87F5B6-5AB2-451F-A2C9-08E6A1E26E3B_1_105_c-194x146.jpeg 194w, https://www.salvis.com/blog/wp-content/uploads/2022/09/7D87F5B6-5AB2-451F-A2C9-08E6A1E26E3B_1_105_c-50x38.jpeg 50w, https://www.salvis.com/blog/wp-content/uploads/2022/09/7D87F5B6-5AB2-451F-A2C9-08E6A1E26E3B_1_105_c-100x75.jpeg 100w, https://www.salvis.com/blog/wp-content/uploads/2022/09/7D87F5B6-5AB2-451F-A2C9-08E6A1E26E3B_1_105_c-1x1.jpeg 1w" sizes="auto, (max-width:767px) 480px, (max-width:1024px) 100vw, 1024px" /></a></figure>



<p class="wp-block-paragraph">A highlight of this day was the talk by <a href="https://twitter.com/postgresql_007">Hans-Jürgen Schönig</a>. He is Austrian, a Viennese to be exact. I can imagine him as a stand-up comedian. Rarely have I laughed so hard at a technical presentation. He presented some features where PostgreSQL shines and the Oracle database does not look so good. The intention was clear. He wanted to make Oracle database fans jealous. If you have the chance to see Hans-Jürgen live, don&#8217;t miss it.</p>



<h2 class="wp-block-heading">Day 2 and 3 &#8211; The Conference</h2>



<p class="wp-block-paragraph">The second day was the start of the real conference. 20 parallel streams with 4 to 6 English talks per time slot. The first session was at 08:00 and the last one at 17:00. A 15-minute break between the 45-minute sessions. There were for sure more attendees than at the theme day, but fewer than in 2019 &#8211; the last in-person DOAG conference. And some exhibitors were missing. It hurts to see Robotron at the spot where the Trivadis booth was the previous years. Let&#8217;s hope for more exhibitors next year.</p>



<p class="wp-block-paragraph">These are my highlights of the main conference.</p>



<h3 class="wp-block-heading">Design Patterns in PL/SQL &amp; SQL</h3>



<figure class="wp-block-image"><a href="https://www.salvis.com/blog/wp-content/uploads/2022/09/07B8AA14-BE0A-42C0-AB0F-A15D9D6B38D3_1_105_c.jpeg"><img loading="lazy" decoding="async" width="1024" height="769" src="https://www.salvis.com/blog/wp-content/uploads/2022/09/07B8AA14-BE0A-42C0-AB0F-A15D9D6B38D3_1_105_c.jpeg" alt="" class="wp-image-11665" srcset="https://www.salvis.com/blog/wp-content/uploads/2022/09/07B8AA14-BE0A-42C0-AB0F-A15D9D6B38D3_1_105_c.jpeg 1024w, https://www.salvis.com/blog/wp-content/uploads/2022/09/07B8AA14-BE0A-42C0-AB0F-A15D9D6B38D3_1_105_c-300x225.jpeg 300w, https://www.salvis.com/blog/wp-content/uploads/2022/09/07B8AA14-BE0A-42C0-AB0F-A15D9D6B38D3_1_105_c-768x577.jpeg 768w, https://www.salvis.com/blog/wp-content/uploads/2022/09/07B8AA14-BE0A-42C0-AB0F-A15D9D6B38D3_1_105_c-194x146.jpeg 194w, https://www.salvis.com/blog/wp-content/uploads/2022/09/07B8AA14-BE0A-42C0-AB0F-A15D9D6B38D3_1_105_c-50x38.jpeg 50w, https://www.salvis.com/blog/wp-content/uploads/2022/09/07B8AA14-BE0A-42C0-AB0F-A15D9D6B38D3_1_105_c-100x75.jpeg 100w, https://www.salvis.com/blog/wp-content/uploads/2022/09/07B8AA14-BE0A-42C0-AB0F-A15D9D6B38D3_1_105_c-1x1.jpeg 1w" sizes="auto, (max-width:767px) 480px, (max-width:1024px) 100vw, 1024px" /></a></figure>



<p class="wp-block-paragraph"><a href="https://twitter.com/DBoriented">Oren Nakdimon</a> had the bad luck to get the 08:00 slot. However, I was lucky to meet him on the bus to the conference center. He convinced me to attend his session (of course he was already on the shortlist). He used a data model based on artists, genres, albums and songs for all examples and presented the following patterns:</p>



<table id="tablepress-18" class="tablepress tablepress-id-18">
<thead>
<tr class="row-1">
	<th class="column-1">Pattern</th><th class="column-2">Generic Problem</th><th class="column-3">Example</th>
</tr>
</thead>
<tbody class="row-striping row-hover">
<tr class="row-2">
	<td class="column-1">Overlap check</td><td class="column-2">Find rows that (partially or fully) overlap a given range.</td><td class="column-3">Find all artists that were living between two dates.</td>
</tr>
<tr class="row-3">
	<td class="column-1">Extendable LOV</td><td class="column-2">Look-up a reference table and add new values if not exist.</td><td class="column-3">Get genre ID by its name. If it doesn&#8217;t exist, add it.</td>
</tr>
<tr class="row-4">
	<td class="column-1">Multi-value parameters</td><td class="column-2">Pass multiple entities to procedures</td><td class="column-3">Add an album with its songs.</td>
</tr>
<tr class="row-5">
	<td class="column-1">The guardian trigger</td><td class="column-2">Whenever some DML is done on some table T, another piece of code should be executed.</td><td class="column-3">Enforce the rule: an album must have at least one song.</td>
</tr>
<tr class="row-6">
	<td class="column-1">Conditional uniqueness</td><td class="column-2">Enforce a business rule that requires uniqueness of an attribute (column) within a subset of the entity instances (table rows).</td><td class="column-3">Enforce the rule: each album may have at most one favourite song.</td>
</tr>
<tr class="row-7">
	<td class="column-1">Correlated results</td><td class="column-2">Return correlated data based on various inputs.</td><td class="column-3">Return albums – and all their songs – based on various inputs.</td>
</tr>
<tr class="row-8">
	<td class="column-1">Arc relationship</td><td class="column-2">Implement an arc relationship from a generic entity.</td><td class="column-3">Implement an arc relationship from images to artists and albums.</td>
</tr>
</tbody>
</table>
<!-- #tablepress-18 from cache -->


<p class="wp-block-paragraph">For each pattern, Oren explained the problem and built the suggested solution step by step. He considered also various corner cases.</p>



<p class="wp-block-paragraph">Have a look at his <a href="http://db-oriented.com/presentations/">website</a> where you find the slides (search for &#8220;Design Patterns in PL/SQL and SQL&#8221;). I&#8217;m sure that some of us have solved one or the other problem less elegantly.</p>



<h3 class="wp-block-heading">Oracle Analytics Server, Make It Right from the Beginning</h3>



<figure class="wp-block-image"><a href="https://www.salvis.com/blog/wp-content/uploads/2022/09/D88C89D3-6222-47E2-9D04-0A2F865210D4_1_105_c.jpeg"><img loading="lazy" decoding="async" width="1024" height="769" src="https://www.salvis.com/blog/wp-content/uploads/2022/09/D88C89D3-6222-47E2-9D04-0A2F865210D4_1_105_c.jpeg" alt="" class="wp-image-11669" srcset="https://www.salvis.com/blog/wp-content/uploads/2022/09/D88C89D3-6222-47E2-9D04-0A2F865210D4_1_105_c.jpeg 1024w, https://www.salvis.com/blog/wp-content/uploads/2022/09/D88C89D3-6222-47E2-9D04-0A2F865210D4_1_105_c-300x225.jpeg 300w, https://www.salvis.com/blog/wp-content/uploads/2022/09/D88C89D3-6222-47E2-9D04-0A2F865210D4_1_105_c-768x577.jpeg 768w, https://www.salvis.com/blog/wp-content/uploads/2022/09/D88C89D3-6222-47E2-9D04-0A2F865210D4_1_105_c-194x146.jpeg 194w, https://www.salvis.com/blog/wp-content/uploads/2022/09/D88C89D3-6222-47E2-9D04-0A2F865210D4_1_105_c-50x38.jpeg 50w, https://www.salvis.com/blog/wp-content/uploads/2022/09/D88C89D3-6222-47E2-9D04-0A2F865210D4_1_105_c-100x75.jpeg 100w, https://www.salvis.com/blog/wp-content/uploads/2022/09/D88C89D3-6222-47E2-9D04-0A2F865210D4_1_105_c-1x1.jpeg 1w" sizes="auto, (max-width:767px) 480px, (max-width:1024px) 100vw, 1024px" /></a></figure>



<p class="wp-block-paragraph"><a href="https://twitter.com/G_Ceresa">Gianni Ceresa</a>&#8216;s talk was about installing the Oracle Analytics Server the right way. However, a lot of his talk was independent of the product. Simply put it was about reading the documentation, automation and testing. &#8220;Script as much as you can&#8221; was one of Gianni&#8217;s messages. There was also an interesting side story about how a cloud provider can mess up provisioning its own product. See <a href="https://gianniceresa.com/2021/12/oracle-analytics-server-on-marketplace-yes-but-no/">this blog post</a> to learn more.</p>



<h3 class="wp-block-heading">The Future of the Oracle Database</h3>



<figure class="wp-block-image"><a href="https://www.salvis.com/blog/wp-content/uploads/2022/09/D5DFA022-DB6A-4FAA-8F0A-E5B804763DEB_1_105_c.jpeg"><img loading="lazy" decoding="async" width="1024" height="769" src="https://www.salvis.com/blog/wp-content/uploads/2022/09/D5DFA022-DB6A-4FAA-8F0A-E5B804763DEB_1_105_c.jpeg" alt="" class="wp-image-11672" srcset="https://www.salvis.com/blog/wp-content/uploads/2022/09/D5DFA022-DB6A-4FAA-8F0A-E5B804763DEB_1_105_c.jpeg 1024w, https://www.salvis.com/blog/wp-content/uploads/2022/09/D5DFA022-DB6A-4FAA-8F0A-E5B804763DEB_1_105_c-300x225.jpeg 300w, https://www.salvis.com/blog/wp-content/uploads/2022/09/D5DFA022-DB6A-4FAA-8F0A-E5B804763DEB_1_105_c-768x577.jpeg 768w, https://www.salvis.com/blog/wp-content/uploads/2022/09/D5DFA022-DB6A-4FAA-8F0A-E5B804763DEB_1_105_c-194x146.jpeg 194w, https://www.salvis.com/blog/wp-content/uploads/2022/09/D5DFA022-DB6A-4FAA-8F0A-E5B804763DEB_1_105_c-50x38.jpeg 50w, https://www.salvis.com/blog/wp-content/uploads/2022/09/D5DFA022-DB6A-4FAA-8F0A-E5B804763DEB_1_105_c-100x75.jpeg 100w, https://www.salvis.com/blog/wp-content/uploads/2022/09/D5DFA022-DB6A-4FAA-8F0A-E5B804763DEB_1_105_c-1x1.jpeg 1w" sizes="auto, (max-width:767px) 480px, (max-width:1024px) 100vw, 1024px" /></a></figure>



<p class="wp-block-paragraph">The Oracle Database version 23c is the next long-term release. The release is expected in April 2023. The beta program will start in October and end in February 2023.  <a href="https://twitter.com/GeraldVenzl">Gerald Venzl</a> revealed in this session some new features. He presented the following features with some examples:</p>



<ul class="wp-block-list">
<li>Schema Level Privileges</li>



<li>SQL Domains</li>



<li>4096 Columns</li>



<li>JSON Schema</li>



<li>Boolean Data Type</li>



<li>UPDATE via JOIN</li>



<li>JavaScript Stored Procedures</li>



<li>IF [NOT] EXISTS</li>



<li>GROUP BY Alias / Column Position</li>



<li>Table Value Constructor</li>



<li>Better RETURNING CLAUSE</li>



<li>Developer Role</li>



<li>SELECT without FROM</li>



<li>Annotations</li>



<li>Much Better Error Messages</li>
</ul>



<p class="wp-block-paragraph">See this <a href="https://twitter.com/phsalvisberg/status/1572885995070300160?s=20&amp;t=d9H6PPPf7q5DMO9Onwmm2Q">Twitter thread</a> for some pictures per feature. More details will probably be available after CloudWorld. I&#8217;m really looking forward to getting access to this release.</p>



<h2 class="wp-block-heading">TAPI vs. XAPI</h2>



<figure class="wp-block-image"><a href="https://www.salvis.com/blog/wp-content/uploads/2022/09/7B453E63-DA77-4594-AA99-77894EE2140F_1_105_c.jpeg"><img loading="lazy" decoding="async" width="1024" height="769" src="https://www.salvis.com/blog/wp-content/uploads/2022/09/7B453E63-DA77-4594-AA99-77894EE2140F_1_105_c.jpeg" alt="" class="wp-image-11674" srcset="https://www.salvis.com/blog/wp-content/uploads/2022/09/7B453E63-DA77-4594-AA99-77894EE2140F_1_105_c.jpeg 1024w, https://www.salvis.com/blog/wp-content/uploads/2022/09/7B453E63-DA77-4594-AA99-77894EE2140F_1_105_c-300x225.jpeg 300w, https://www.salvis.com/blog/wp-content/uploads/2022/09/7B453E63-DA77-4594-AA99-77894EE2140F_1_105_c-768x577.jpeg 768w, https://www.salvis.com/blog/wp-content/uploads/2022/09/7B453E63-DA77-4594-AA99-77894EE2140F_1_105_c-194x146.jpeg 194w, https://www.salvis.com/blog/wp-content/uploads/2022/09/7B453E63-DA77-4594-AA99-77894EE2140F_1_105_c-50x38.jpeg 50w, https://www.salvis.com/blog/wp-content/uploads/2022/09/7B453E63-DA77-4594-AA99-77894EE2140F_1_105_c-100x75.jpeg 100w, https://www.salvis.com/blog/wp-content/uploads/2022/09/7B453E63-DA77-4594-AA99-77894EE2140F_1_105_c-1x1.jpeg 1w" sizes="auto, (max-width:767px) 480px, (max-width:1024px) 100vw, 1024px" /></a></figure>



<p class="wp-block-paragraph"><a href="https://www.linkedin.com/in/j%C3%BCrgen-sieben-36b817/">Jürgen Sieben</a> explained based on a simple example the problems of accessing tables directly and using row-level triggers to apply business logic. He improved the situation by using a table API. The main disadvantage of a table API is, that it exposes the implementation such as the structure and the column names. A solution is to use a view layer for read-only access and a transaction API for write access. The table API can still be used, but only internally by the transaction API itself. Finally, Jürgen explains that a CRUD-based API is a technical interface that obscures the intention. To cancel an order you could delete the order with its positions. But most probably you would want to store the reason for the cancellation and change the state of the order. So, a transactional API should be designed from a business perspective.</p>



<p class="wp-block-paragraph">I liked this talk very much. Not only because I totally agree with Jürgen, but also because I like the way how he explains things in a concise and easy-to-follow way.</p>



<h2 class="wp-block-heading">Summary</h2>



<p class="wp-block-paragraph">I enjoyed this conference very much. Thanks to everyone who helped make this conference happen. I&#8217;m already looking forward to DOAG2023.</p>
<p>The post <a href="https://www.salvis.com/blog/2022/09/25/doag2022-highlights/">DOAG2022 Highlights</a> appeared first on <a href="https://www.salvis.com/blog">Philipp Salvisberg&#039;s Blog</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
