You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: product_docs/docs/biganimal/release/using_cluster/06_demonstration_oracle_compatibility.mdx
+31-30Lines changed: 31 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,18 @@
1
1
---
2
2
title: "Demonstration of Oracle SQL compatible functions and syntax"
3
-
navTitle: "Demo: Oracle SQL compatibility"
3
+
navTitle: "Oracle SQL compatibility"
4
+
showInteractiveBadge: true
4
5
---
5
6
6
7
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).
7
8
8
9
Watch the video, or load up psql and follow along below!
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:
38
39
39
40
-`demo` is the user role we're connecting as. This is a user set up with select privileges on the database.
40
-
-`password` is the password for this user.
41
+
-`password` is the password for this user.
41
42
!!! Warning Passwords in connection strings.
42
43
This example illustrates a complete connection URL, including the password. This is fine for a demonstration,
43
-
and may also be acceptable for applications configuration if access to the configuration is limited.
44
+
and may also be acceptable for applications configuration if access to the configuration is limited.
44
45
Avoid this practice for admin, superuser, or other roles used interactively - psql will prompt for a password
45
-
if none is supplied.
46
-
-`p-c64p9a3h5vfavr7tfrjg.qsbilba3hlgp1vqr.biganimal.io` is the host name for the Advanced Server cluster on BigAnimal that I'm connecting to.
46
+
if none is supplied.
47
+
-`p-c64p9a3h5vfavr7tfrjg.qsbilba3hlgp1vqr.biganimal.io` is the host name for the Advanced Server cluster on BigAnimal that I'm connecting to.
47
48
-`5432` is the usual PostgreSQL port number.
48
49
-`chinook` is the name of the database.
49
50
-`sslmode=require` ensures that we establish a secure connection.
psql (13.0 (Debian 13.0-1.pgdg100+1), server 13.4.8 (Debian 13.4.8-1+deb10))
@@ -86,7 +87,7 @@ __OUTPUT__
86
87
...
87
88
```
88
89
89
-
There's an employee table, let's examine its definition:
90
+
There's an employee table, let's examine its definition:
90
91
91
92
```
92
93
\d+ employee
@@ -117,17 +118,17 @@ other employees who may in turn report to still *other* employees.
117
118
118
119
## Demo #1: exposing an organization hierarchy with `CONNECT BY`
119
120
120
-
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).
121
+
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).
121
122
122
123
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:
123
124
124
125
```sql
125
126
SELECT firstname, lastname, (
126
-
SELECT LISTAGG(lastname, ', ')
127
-
FROM employee rt
128
-
START WITH rt.employeeid=e.reportsto
127
+
SELECT LISTAGG(lastname, ', ')
128
+
FROM employee rt
129
+
START WITH rt.employeeid=e.reportsto
129
130
CONNECT BY employeeid = PRIOR reportsto
130
-
) AS"chain of command"
131
+
) AS"chain of command"
131
132
FROM employee e;
132
133
__OUTPUT__
133
134
firstname | lastname | chain of command
@@ -148,11 +149,11 @@ Now, the `LISTAGG()` function was introduced in Oracle 11g Release 2. Very few d
148
149
149
150
```sql
150
151
SELECT firstname, lastname, (
151
-
SELECT string_agg(lastname, ', ')
152
-
FROM employee rt
153
-
START WITH rt.employeeid=e.reportsto
152
+
SELECT string_agg(lastname, ', ')
153
+
FROM employee rt
154
+
START WITH rt.employeeid=e.reportsto
154
155
CONNECT BY employeeid = PRIOR reportsto
155
-
) AS"chain of command"
156
+
) AS"chain of command"
156
157
FROM employee e;
157
158
__OUTPUT__
158
159
firstname | lastname | chain of command
@@ -168,20 +169,20 @@ __OUTPUT__
168
169
(8 rows)
169
170
```
170
171
171
-
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.
172
+
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.
172
173
173
-
Let's demonstrate that.
174
+
Let's demonstrate that.
174
175
175
176
## Demo #2: group concatenation with `LISTAGG`
176
177
177
178
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.
178
179
179
180
```sql
180
-
SELECT UNIQUE title,
181
+
SELECT UNIQUE title,
181
182
ROUND(AVG(bytes) OVER (PARTITION BY mediatypeid)/1048576 ) media_avg_mb,
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.
209
210
210
211
```sql
211
-
SELECT UNIQUE title,
212
+
SELECT UNIQUE title,
212
213
ROUND(AVG(bytes) OVER (PARTITION BY mediatypeid)/1048576 ) media_avg_mb,
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
226
227
```
227
228
228
-
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/)
229
-
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`,
229
+
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/)
230
+
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`,
230
231
so this query doesn't need to change when migrating from Oracle.
231
232
232
233
## Compatibility preserves the value of your existing work
233
234
234
-
In both of the examples shown here, you probably would not use the functions and syntax demonstrated for new work; there are
235
+
In both of the examples shown here, you probably would not use the functions and syntax demonstrated for new work; there are
235
236
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
236
237
you to focus your time and expertise on solving new problems.
0 commit comments