

# Working with Babelfish for Aurora PostgreSQL procedures
<a name="Appendix.Babelfish.Functions"></a>

## Overview
<a name="Appendix.Babelfish.Functions.Overview"></a>

You can use the following procedure for Amazon RDS DB instances running Babelfish for Aurora PostgreSQL for a better query performance:
+ [sp\$1babelfish\$1volatility](sp_babelfish_volatility.md)
+ [sp\$1execute\$1postgresql](sp_execute_postgresql.md)

# sp\$1babelfish\$1volatility
<a name="sp_babelfish_volatility"></a>

PostgreSQL function volatility helps the optimizer for a better query execution which when used in parts of certain clauses has a significant impact on query performance. 

## Syntax
<a name="sp_babelfish_volatility-syntax"></a>

 

```
sp_babelfish_volatility ‘function_name’, ‘volatility’
```

## Arguments
<a name="sp_babelfish_volatility-arguments"></a>

 *function\$1name (optional)*   
You can either specify the value of this argument with a two-part name as `schema_name.function_name`or only the `function_name`. If you specify only the `function_name`, the schema name is the default schema for the current user.

 *volatility (optional)*   
The valid PostgreSQL values of volatility are `stable`, `volatile`, or `immutable`. For more information, see [https://www.postgresql.org/docs/current/xfunc-volatility.html](https://www.postgresql.org/docs/current/xfunc-volatility.html)

**Note**  
When `sp_babelfish_volatility` is called with `function_name` which has multiple definitions, it will throw an error.

## Result set
<a name="sp_babelfish_volatility-return-type"></a>

If the parameters are not mentioned then the result set is displayed under the following columns: `schemaname`, `functionname`, `volatility`.

## Usage notes
<a name="sp_babelfish_volatility-usage-notes"></a>

PostgreSQL function volatility helps the optimizer for a better query execution which when used in parts of certain clauses has a significant impact on query performance. 

## Examples
<a name="sp_babelfish_volatility-examples"></a>

The following examples shows how to create simple functions and later explains how to use `sp_babelfish_volatility` on these functions using different methods.

```
1> create function f1() returns int as begin return 0 end
2> go
```

```
1> create schema test_schema
2> go
```

```
1> create function test_schema.f1() returns int as begin return 0 end
2> go
```

The following example displays volatility of the functions:

```
1> exec sp_babelfish_volatility
2> go
            
schemaname  functionname volatility
----------- ------------ ----------
dbo         f1           volatile
test_schema f1           volatile
```

The following example shows how to change the volatility of the functions:

```
1> exec sp_babelfish_volatility 'f1','stable'
2> go
1> exec sp_babelfish_volatility 'test_schema.f1','immutable'
2> go
```

When you specify only the function\$1name, it displays the schema name, function name and volatility of that function. The following example displays volatility of functions after changing the values:

```
1> exec sp_babelfish_volatility 'test_schema.f1'
2> go
            
schemaname  functionname volatility
----------- ------------ ----------
test_schema f1           immutable
```

```
1> exec sp_babelfish_volatility 'f1'
2> go
            
schemaname  functionname volatility
----------- ------------ ----------
dbo         f1           stable
```

When you don't specify any argument, it displays a list of functions (schema name, function name, volatility of the functions) present in the current database:

```
1> exec sp_babelfish_volatility 
2> go
            
schemaname  functionname volatility
----------- ------------ ----------
dbo         f1           stable
test_schema f1           immutable
```

# sp\$1execute\$1postgresql
<a name="sp_execute_postgresql"></a>

You can execute PostgreSQL statements from T-SQL endpoint. This simplifies your applications as you don’t need to exit T-SQL port to execute these statements. 

## Syntax
<a name="sp_execute_postgresql-syntax"></a>

 

```
sp_execute_postgresql [ @stmt = ] statement
```

## Arguments
<a name="sp_execute_postgresql-arguments"></a>

 *[ @stmt ] statement*   
The argument is of datatype varchar. This argument accept PG dialect statements.

**Note**  
You can only pass one PG dialect statement as an argument otherwise it will raise the following error.

```
1>exec sp_execute_postgresql 'create extension pg_stat_statements; drop extension pg_stat_statements'
2>go
```

```
Msg 33557097, Level 16, State 1, Server BABELFISH, Line 1
expected 1 statement but got 2 statements after parsing
```

## Usage notes
<a name="sp_execute_postgresql-statements"></a>

### CREATE EXTENSION
<a name="sp_execute_postgresql-statements-create"></a>

Creates and loads a new extension into the current database. 

```
1>EXEC sp_execute_postgresql 'create extension [ IF NOT EXISTS ] <extension name> [ WITH ] [SCHEMA schema_name] [VERSION version]';
2>go
```

The following example shows how to create an extension:

```
1>EXEC sp_execute_postgresql 'create extension pg_stat_statements with schema sys version "1.10"';
2>go
```

Use the following command to access extension objects:

```
1>select * from pg_stat_statements;
2>go
```

**Note**  
 If schema name is not provided explicitly during extension creation, by default the extensions are installed in the public schema. You must provide the schema qualifier to access the extension objects as mentioned below:

```
1>select * from [public].pg_stat_statements;
2>go
```

**Supported extensions**

The following extensions available with Aurora PostgreSQL works with Babelfish.
+ `pg_stat_statements`
+ `tds_fdw`
+ `fuzzystrmatch`

**Limitations**
+ Users need to have sysadmin role on T-SQL and rds\$1superuser on postgres to install the extenstions.
+ Extensions cannot be installed in user created schemas and also in dbo and guest schemas for master, tempdb and msdb database. 
+ CASCADE option is not supported.

## ALTER EXTENSION
<a name="sp_execute_postgresql-alter"></a>

You can upgrade to a new extension version using ALTER extension. 

```
1>EXEC sp_execute_postgresql 'alter extension <extension name> UPDATE TO <new_version>';
2>go
```

**Limitations**
+ You can upgrade the version of your extension only using the ALTER Extension statement. Other operations aren't supported.

## DROP EXTENSION
<a name="sp_execute_postgresql-drop"></a>

Drops the specified extension. You can also use `if exists` or `restrict` options to drop the extension. 

```
1>EXEC sp_execute_postgresql 'drop extension <extension name>';
2>go
```

**Limitations**
+ CASCADE option is not supported.