Skip to content

Commit

Permalink
Merge pull request #2047 from EnterpriseDB/feature/rob/merchandising-…
Browse files Browse the repository at this point in the history
…BA-oracle-demo
  • Loading branch information
robert-stringer authored Nov 18, 2021
2 parents f26c942 + e371119 commit 5a74b10
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
---
title: "Demonstration of Oracle SQL compatible functions and syntax"
navTitle: "Demo: Oracle SQL compatibility"
navTitle: "Oracle SQL compatibility"
showInteractiveBadge: true
---

BigAnimal lets you run Oracle SQL queries in the cloud via [EDB Postgres Advanced Server](https://www.enterprisedb.com/docs/epas/latest/epas_compat_ora_dev_guide/). This topic demonstrates two Oracle SQL-syntax queries running unmodified on a BigAnimal test cluster, populated with the [Chinook sample database](https://github.com/lerocha/chinook-database).

Watch the video, or load up psql and follow along below!

<figure class="embed-responsive embed-responsive-16by9">
<iframe src="https://www.youtube.com/embed/lV4QQ53kgew"
<iframe src="https://www.youtube.com/embed/lV4QQ53kgew"
title="Video recording of this demonstration"
class="embed-responsive-item"
frameborder="0"
class="embed-responsive-item"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</figure>

Expand All @@ -37,20 +38,20 @@ postgres://demo:[email protected]:54
In case you're unfamiliar with [PostgreSQL connection URIs](https://www.postgresql.org/docs/current/libpq-connect.html#id-1.7.3.8.3.6), let's break that down:

- `demo` is the user role we're connecting as. This is a user set up with select privileges on the database.
- `password` is the password for this user.
- `password` is the password for this user.
!!! Warning Passwords in connection strings.
This example illustrates a complete connection URL, including the password. This is fine for a demonstration,
and may also be acceptable for applications configuration if access to the configuration is limited.
and may also be acceptable for applications configuration if access to the configuration is limited.
Avoid this practice for admin, superuser, or other roles used interactively - psql will prompt for a password
if none is supplied.
- `p-c64p9a3h5vfavr7tfrjg.qsbilba3hlgp1vqr.biganimal.io` is the host name for the Advanced Server cluster on BigAnimal that I'm connecting to.
if none is supplied.
- `p-c64p9a3h5vfavr7tfrjg.qsbilba3hlgp1vqr.biganimal.io` is the host name for the Advanced Server cluster on BigAnimal that I'm connecting to.
- `5432` is the usual PostgreSQL port number.
- `chinook` is the name of the database.
- `sslmode=require` ensures that we establish a secure connection.

With that in hand, we can launch psql:

```shell
```shell
psql postgres://demo:[email protected]:5432/chinook?sslmode=require
__OUTPUT__
psql (13.0 (Debian 13.0-1.pgdg100+1), server 13.4.8 (Debian 13.4.8-1+deb10))
Expand Down Expand Up @@ -86,7 +87,7 @@ __OUTPUT__
...
```

There's an employee table, let's examine its definition:
There's an employee table, let's examine its definition:

```
\d+ employee
Expand Down Expand Up @@ -117,17 +118,17 @@ other employees who may in turn report to still *other* employees.

## Demo #1: exposing an organization hierarchy with `CONNECT BY`

Let's construct a [hierarchical query](https://www.enterprisedb.com/docs/epas/latest/epas_compat_ora_dev_guide/03_advanced_concepts/05_hierarchical_queries/) to expose this [chain of command](https://en.wikipedia.org/wiki/Chain_of_command).
Let's construct a [hierarchical query](https://www.enterprisedb.com/docs/epas/latest/epas_compat_ora_dev_guide/03_advanced_concepts/05_hierarchical_queries/) to expose this [chain of command](https://en.wikipedia.org/wiki/Chain_of_command).

Modern SQL would use a recursive CTE for this, as those are widely supported. But Oracle has, for decades, supported an alternative mechanism for querying hierarchy in the form of `CONNECT BY` - let's put that into action:

```sql
SELECT firstname, lastname, (
SELECT LISTAGG(lastname, ', ')
FROM employee rt
START WITH rt.employeeid=e.reportsto
SELECT LISTAGG(lastname, ', ')
FROM employee rt
START WITH rt.employeeid=e.reportsto
CONNECT BY employeeid = PRIOR reportsto
) AS "chain of command"
) AS "chain of command"
FROM employee e;
__OUTPUT__
firstname | lastname | chain of command
Expand All @@ -148,11 +149,11 @@ Now, the `LISTAGG()` function was introduced in Oracle 11g Release 2. Very few d

```sql
SELECT firstname, lastname, (
SELECT string_agg(lastname, ', ')
FROM employee rt
START WITH rt.employeeid=e.reportsto
SELECT string_agg(lastname, ', ')
FROM employee rt
START WITH rt.employeeid=e.reportsto
CONNECT BY employeeid = PRIOR reportsto
) AS "chain of command"
) AS "chain of command"
FROM employee e;
__OUTPUT__
firstname | lastname | chain of command
Expand All @@ -168,20 +169,20 @@ __OUTPUT__
(8 rows)
```

But [the semantics of the two functions are different for even slightly less-trivial uses](https://www.enterprisedb.com/blog/how-workaround-oracle-listagg-function-postgresql), specifically when using the grouping construct.
But [the semantics of the two functions are different for even slightly less-trivial uses](https://www.enterprisedb.com/blog/how-workaround-oracle-listagg-function-postgresql), specifically when using the grouping construct.

Let's demonstrate that.
Let's demonstrate that.

## Demo #2: group concatenation with `LISTAGG`

As we saw above, this database has "album" and "track" tables containing metadata on digital recordings. We can use some analytic functions, including `LISTAGG`, to put together a report on average track storage requirements for albums with "baby" in the title.

```sql
SELECT UNIQUE title,
SELECT UNIQUE title,
ROUND(AVG(bytes) OVER (PARTITION BY mediatypeid)/1048576 ) media_avg_mb,
LISTAGG(t.name || ' (' || ROUND(bytes/1048576) || ' mb)', chr(10))
LISTAGG(t.name || ' (' || ROUND(bytes/1048576) || ' mb)', chr(10))
WITHIN GROUP (ORDER BY trackid)
OVER (PARTITION BY title) track_list
OVER (PARTITION BY title) track_list
FROM track t
JOIN album USING (albumid)
JOIN mediatype USING (mediatypeid)
Expand All @@ -208,11 +209,11 @@ __OUTPUT__
If we try replacing `LISTAGG` with `string_agg` in this example, it's going to fail - the [expression syntax for `string_agg`](https://www.postgresql.org/docs/current/sql-expressions.html#SYNTAX-AGGREGATES) is different.

```sql
SELECT UNIQUE title,
SELECT UNIQUE title,
ROUND(AVG(bytes) OVER (PARTITION BY mediatypeid)/1048576 ) media_avg_mb,
string_agg(t.name || ' (' || ROUND(bytes/1048576) || ' mb)', chr(10))
string_agg(t.name || ' (' || ROUND(bytes/1048576) || ' mb)', chr(10))
WITHIN GROUP (ORDER BY trackid)
OVER (PARTITION BY title) track_list
OVER (PARTITION BY title) track_list
FROM track t
JOIN album USING (albumid)
JOIN mediatype USING (mediatypeid)
Expand All @@ -225,13 +226,13 @@ LINE 3: string_agg(t.name || ' (' || ROUND(bytes/1048576) || ...
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
```

Now, this isn't terribly difficult to correct, but it requires restructuring the query to replace the grouping construct - such work can quickly accumulate errors. Fortunately, [EDB Postgres Advanced Server](https://www.enterprisedb.com/docs/epas/latest/)
supports [`LISTAGG`](https://www.enterprisedb.com/docs/epas/latest/epas_compat_reference/02_the_sql_language/03_functions_and_operators/11_aggregate_functions/#listagg) AND `string_agg`,
Now, this isn't terribly difficult to correct, but it requires restructuring the query to replace the grouping construct - such work can quickly accumulate errors. Fortunately, [EDB Postgres Advanced Server](https://www.enterprisedb.com/docs/epas/latest/)
supports [`LISTAGG`](https://www.enterprisedb.com/docs/epas/latest/epas_compat_reference/02_the_sql_language/03_functions_and_operators/11_aggregate_functions/#listagg) AND `string_agg`,
so this query doesn't need to change when migrating from Oracle.

## Compatibility preserves the value of your existing work

In both of the examples shown here, you probably would not use the functions and syntax demonstrated for new work; there are
In both of the examples shown here, you probably would not use the functions and syntax demonstrated for new work; there are
better, more familiar or at least more widely-available equivalents provided natively by PostgreSQL (and many other databases). But by supporting them, EDB Advanced Server gives you the ability to reuse existing logic with minimal modification, allowing
you to focus your time and expertise on solving new problems.

Expand Down
Binary file modified src/images/screen-demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
104 changes: 74 additions & 30 deletions src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from "react";
import { Container } from "react-bootstrap";
import Icon, { iconNames } from "../components/icon/";
import cliImg from "../images/screen-demo.gif";
import { Footer, IndexSubNav, Layout, Link, MainContent } from "../components";

const IndexCard = ({ iconName, headingText, children }) => (
Expand Down Expand Up @@ -43,50 +42,86 @@ const Page = () => (
<MainContent searchNavLogo={true}>
{/* Sign Post */}

<div className="new-thing-header" aria-roledescription="badge">
<span className="badge-text font-weight-bold">Interactive Demo</span>
</div>
<div className="container">
<div className="row">
<div className="col-sm mb-3 mr-1">
<div className="mb-2">
<div className="new-thing-header" aria-roledescription="badge">
<span className="badge-text font-weight-bold">Demo</span>
</div>
</div>

<div className="full-width mt-3 mb-5">
<div className="pt-0">
<div className="d-flex pb-0 align-items-center">
<div className="mr-3">
<Link to="/kubernetes/cloud_native_postgresql/interactive_demo/">
<img
src={cliImg}
alt="Illustration of a Kubernetes Terminal Command"
className="img-fluid shadow rounded card"
/>
</Link>
<div className="mb-3">
<h3 className="card-title mb-2 font-weight-bold">
<Link
className="homepage-headling-link"
to="/biganimal/latest/using_cluster/06_demonstration_oracle_compatibility/"
>
Demonstration of Oracle SQL <br />
compatibility in BigAnimal
</Link>
</h3>
<p>
BigAnimal lets you run Oracle SQL queries in the cloud via EDB
Postgres Advanced Server. Watch the video, or load up psql and
follow along.
</p>
<div className="d-flex align-items-center">
<p>
<Link
className="btn btn-info btn-sm"
to="/biganimal/latest/using_cluster/06_demonstration_oracle_compatibility/"
>
Watch demo
</Link>
</p>
<p>
<Link className="btn-sm ml-2" to="/biganimal/latest/">
Find out more &rarr;
</Link>
</p>
</div>
</div>
<div className="flex-fill pl-3">
<h2 className="card-title mb-2 font-weight-bold">
<span className="text-muted font-weight-normal">
Cloud Native Postgres
</div>
<div className="col-sm mb-3">
<div className="mb-2">
<div className="new-thing-header" aria-roledescription="badge">
<span className="badge-text font-weight-bold">
Interactive Demo
</span>
<br />
Install, Configure and Deploy PostgreSQL with Kubernetes
</h2>
</div>
</div>

<p className="pt-2 pb-1 balance-text">
Test drive Cloud Native Postgres in the browser.
<div className="mb-3">
<h3 className="card-title mb-2 font-weight-bold">
<Link
className="homepage-headling-link"
to="/kubernetes/cloud_native_postgresql/interactive_demo/"
>
Install, Configure and Deploy PostgreSQL <br />
with Kubernetes
</Link>
</h3>
<p>
Want to see what it takes to get the Cloud Native PostgreSQL
Operator up and running? Try in the browser now, no downloads
required.
</p>

<div className="d-flex align-items-center">
<p>
<Link
className="btn btn-info"
className="btn btn-info btn-sm"
to="/kubernetes/cloud_native_postgresql/interactive_demo/"
>
Try the Interactive Demo &rarr;
Try it now
</Link>
</p>
<p className="ml-3">
<p>
<Link
className="btn-sm ml-2"
to="/kubernetes/cloud_native_postgresql/"
className="border-bottom"
>
Learn More
Find out more &rarr;
</Link>
</p>
</div>
Expand All @@ -109,6 +144,15 @@ const Page = () => (

<IndexCard iconName={iconNames.CLOUD_DB} headingText="Cloud">
<IndexCardLink to="/biganimal/latest">BigAnimal</IndexCardLink>
<IndexCardLink
to="/biganimal/latest/using_cluster/06_demonstration_oracle_compatibility/"
className="nested-link"
>
Oracle SQL Compatiblity
<span className="new-thing" title="Interactive Demo">
Demo
</span>
</IndexCardLink>
</IndexCard>

<IndexCard iconName={iconNames.KUBERNETES} headingText="Kubernetes">
Expand Down
6 changes: 6 additions & 0 deletions src/styles/_docs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,9 @@ html.katacoda-panel-active .katacoda-exec-button {
font-size: 0.7rem;
}
}

.homepage-headling-link {
&:hover {
text-decoration: underline;
}
}

0 comments on commit 5a74b10

Please sign in to comment.