Skip to content

Commit 661df53

Browse files
chore: refactoring pg sql queries to be fully qualified (#574)
Co-authored-by: Sophia Chu <[email protected]>
1 parent ef55235 commit 661df53

18 files changed

+70
-62
lines changed

examples/aws_driver_example/aws_iam_authentication_postgresql_example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const client = new AwsPGClient({
3838
// Attempt connection
3939
try {
4040
await client.connect();
41-
const result = await client.query("select aurora_db_instance_identifier()");
41+
const result = await client.query("select pg_catalog.aurora_db_instance_identifier()");
4242
console.log(result.rows[0]); // { aurora_db_instance_identifier: "instance-id" }
4343
} finally {
4444
await client.end();

examples/aws_driver_example/aws_limitless_postgresql_example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const client = new AwsPGClient({
3939
// Attempt connection
4040
try {
4141
await client.connect();
42-
const result = await client.query("select aurora_db_instance_identifier()");
42+
const result = await client.query("select pg_catalog.aurora_db_instance_identifier()");
4343
console.log(result);
4444
} finally {
4545
await client.end();

examples/aws_driver_example/aws_simple_connection_postgresql_example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const client = new AwsPGClient({
3434
// Attempt connection.
3535
try {
3636
await client.connect();
37-
const result = await client.query("select aurora_db_instance_identifier()");
37+
const result = await client.query("select pg_catalog.aurora_db_instance_identifier()");
3838
console.log(result.rows[0]); // { aurora_db_instance_identifier: "instance-id" }
3939
} finally {
4040
await client.end();

examples/aws_driver_example/telemetry_metrics_otlp_example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const client = new AwsPGClient({
8383

8484
try {
8585
await client.connect();
86-
const result = (await client.query("select * from aurora_db_instance_identifier()")).rows[0];
86+
const result = (await client.query("select * from pg_catalog.aurora_db_instance_identifier()")).rows[0];
8787
console.log(result);
8888
} finally {
8989
await client.end();

examples/aws_driver_example/telemetry_tracing_xray_example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const client = new AwsPGClient({
8080

8181
try {
8282
await client.connect();
83-
const result = (await client.query("select * from aurora_db_instance_identifier()")).rows[0];
83+
const result = (await client.query("select * from pg_catalog.aurora_db_instance_identifier()")).rows[0];
8484
console.log(result);
8585
} finally {
8686
await client.end();

examples/javascript_example/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
22
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
3+
44
Licensed under the Apache License, Version 2.0 (the "License").
55
You may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
7-
7+
88
http://www.apache.org/licenses/LICENSE-2.0
9-
9+
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
1212
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -35,7 +35,7 @@ async function main() {
3535

3636
try {
3737
await client.connect();
38-
const result = await client.query("SELECT aurora_db_instance_identifier() as current_id");
38+
const result = await client.query("SELECT pg_catalog.aurora_db_instance_identifier() as current_id");
3939
console.log(`Currently connected to ${result.rows[0].current_id}`);
4040
} finally {
4141
await client.end();

pg/lib/dialect/aurora_pg_database_dialect.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,31 @@ import { ClientWrapper } from "../../../common/lib/client_wrapper";
2424
import { DatabaseDialectCodes } from "../../../common/lib/database_dialect/database_dialect_codes";
2525
import { LimitlessDatabaseDialect } from "../../../common/lib/database_dialect/limitless_database_dialect";
2626
import { WrapperProperties } from "../../../common/lib/wrapper_property";
27-
import {
28-
MonitoringRdsHostListProvider
29-
} from "../../../common/lib/host_list_provider/monitoring/monitoring_host_list_provider";
27+
import { MonitoringRdsHostListProvider } from "../../../common/lib/host_list_provider/monitoring/monitoring_host_list_provider";
3028
import { PluginService } from "../../../common/lib/plugin_service";
3129
import { BlueGreenDialect, BlueGreenResult } from "../../../common/lib/database_dialect/blue_green_dialect";
3230

3331
export class AuroraPgDatabaseDialect extends PgDatabaseDialect implements TopologyAwareDatabaseDialect, LimitlessDatabaseDialect, BlueGreenDialect {
3432
private static readonly VERSION = process.env.npm_package_version;
3533
private static readonly TOPOLOGY_QUERY: string =
36-
"SELECT server_id, CASE WHEN SESSION_ID = 'MASTER_SESSION_ID' THEN TRUE ELSE FALSE END AS is_writer, " +
34+
"SELECT server_id, CASE WHEN SESSION_ID OPERATOR(pg_catalog.=) 'MASTER_SESSION_ID' THEN TRUE ELSE FALSE END AS is_writer, " +
3735
"CPU, COALESCE(REPLICA_LAG_IN_MSEC, 0) AS lag, LAST_UPDATE_TIMESTAMP " +
38-
"FROM aurora_replica_status() " +
36+
"FROM pg_catalog.aurora_replica_status() " +
3937
// filter out nodes that haven't been updated in the last 5 minutes
40-
"WHERE EXTRACT(EPOCH FROM(NOW() - LAST_UPDATE_TIMESTAMP)) <= 300 OR SESSION_ID = 'MASTER_SESSION_ID' " +
38+
"WHERE EXTRACT(EPOCH FROM(pg_catalog.NOW() OPERATOR(pg_catalog.-) LAST_UPDATE_TIMESTAMP)) OPERATOR(pg_catalog.<=) 300 OR SESSION_ID OPERATOR(pg_catalog.=) 'MASTER_SESSION_ID' " +
4139
"OR LAST_UPDATE_TIMESTAMP IS NULL";
4240
private static readonly EXTENSIONS_SQL: string =
43-
"SELECT (setting LIKE '%aurora_stat_utils%') AS aurora_stat_utils FROM pg_settings WHERE name='rds.extensions'";
44-
private static readonly HOST_ID_QUERY: string = "SELECT aurora_db_instance_identifier() as host";
45-
private static readonly IS_READER_QUERY: string = "SELECT pg_is_in_recovery() as is_reader";
41+
"SELECT (setting LIKE '%aurora_stat_utils%') AS aurora_stat_utils FROM pg_catalog.pg_settings WHERE name OPERATOR(pg_catalog.=) 'rds.extensions'";
42+
private static readonly HOST_ID_QUERY: string = "SELECT pg_catalog.aurora_db_instance_identifier() as host";
43+
private static readonly IS_READER_QUERY: string = "SELECT pg_catalog.pg_is_in_recovery() as is_reader";
4644
private static readonly IS_WRITER_QUERY: string =
47-
"SELECT server_id " + "FROM aurora_replica_status() " + "WHERE SESSION_ID = 'MASTER_SESSION_ID' AND SERVER_ID = aurora_db_instance_identifier()";
45+
"SELECT server_id " +
46+
"FROM pg_catalog.aurora_replica_status() " +
47+
"WHERE SESSION_ID OPERATOR(pg_catalog.=) 'MASTER_SESSION_ID' AND SERVER_ID OPERATOR(pg_catalog.=) pg_catalog.aurora_db_instance_identifier()";
4848

49-
private static readonly BG_STATUS_QUERY: string = `SELECT * FROM get_blue_green_fast_switchover_metadata('aws_advanced_nodejs_wrapper-${AuroraPgDatabaseDialect.VERSION}')`;
49+
private static readonly BG_STATUS_QUERY: string = `SELECT * FROM pg_catalog.get_blue_green_fast_switchover_metadata('aws_advanced_nodejs_wrapper-${AuroraPgDatabaseDialect.VERSION}')`;
5050

51-
private static readonly TOPOLOGY_TABLE_EXIST_QUERY: string = "SELECT 'get_blue_green_fast_switchover_metadata'::regproc";
51+
private static readonly TOPOLOGY_TABLE_EXIST_QUERY: string = "SELECT pg_catalog.'get_blue_green_fast_switchover_metadata'::regproc";
5252

5353
getHostListProvider(props: Map<string, any>, originalUrl: string, hostListProviderService: HostListProviderService): HostListProvider {
5454
if (WrapperProperties.PLUGINS.get(props).includes("failover2")) {

pg/lib/dialect/pg_database_dialect.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
import { DatabaseDialect, DatabaseType } from "../../../common/lib/database_dialect/database_dialect";
1818
import { HostListProviderService } from "../../../common/lib/host_list_provider_service";
1919
import { HostListProvider } from "../../../common/lib/host_list_provider/host_list_provider";
20-
import {
21-
ConnectionStringHostListProvider
22-
} from "../../../common/lib/host_list_provider/connection_string_host_list_provider";
20+
import { ConnectionStringHostListProvider } from "../../../common/lib/host_list_provider/connection_string_host_list_provider";
2321
import { AwsWrapperError, HostRole, TransactionIsolationLevel, UnsupportedMethodError } from "../../../common/lib";
2422
import { DatabaseDialectCodes } from "../../../common/lib/database_dialect/database_dialect_codes";
2523
import { ClientWrapper } from "../../../common/lib/client_wrapper";
@@ -41,7 +39,7 @@ export class PgDatabaseDialect implements DatabaseDialect {
4139
}
4240

4341
getHostAliasQuery(): string {
44-
return "SELECT CONCAT(inet_server_addr(), ':', inet_server_port())";
42+
return "SELECT pg_catalog.CONCAT(pg_catalog.inet_server_addr(), ':', pg_catalog.inet_server_port())";
4543
}
4644

4745
async getHostAliasAndParseResults(targetClient: ClientWrapper): Promise<string> {
@@ -56,7 +54,7 @@ export class PgDatabaseDialect implements DatabaseDialect {
5654
}
5755

5856
getServerVersionQuery(): string {
59-
return "SELECT 'version', VERSION()";
57+
return "SELECT 'version', pg_catalog.VERSION()";
6058
}
6159

6260
getSetReadOnlyQuery(readOnly: boolean): string {
@@ -98,7 +96,7 @@ export class PgDatabaseDialect implements DatabaseDialect {
9896

9997
async isDialect(targetClient: ClientWrapper): Promise<boolean> {
10098
return await targetClient
101-
.query("SELECT 1 FROM pg_proc LIMIT 1")
99+
.query("SELECT 1 FROM pg_catalog.pg_proc LIMIT 1")
102100
.then((result: { rows: any }) => {
103101
return !!result.rows[0];
104102
})

pg/lib/dialect/rds_multi_az_pg_database_dialect.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ import { ErrorHandler } from "../../../common/lib/error_handler";
2727
import { MultiAzPgErrorHandler } from "../multi_az_pg_error_handler";
2828
import { WrapperProperties } from "../../../common/lib/wrapper_property";
2929
import { PluginService } from "../../../common/lib/plugin_service";
30-
import {
31-
MonitoringRdsHostListProvider
32-
} from "../../../common/lib/host_list_provider/monitoring/monitoring_host_list_provider";
30+
import { MonitoringRdsHostListProvider } from "../../../common/lib/host_list_provider/monitoring/monitoring_host_list_provider";
3331

3432
export class RdsMultiAZClusterPgDatabaseDialect extends PgDatabaseDialect implements TopologyAwareDatabaseDialect {
3533
constructor() {
@@ -38,13 +36,13 @@ export class RdsMultiAZClusterPgDatabaseDialect extends PgDatabaseDialect implem
3836
private static readonly VERSION = process.env.npm_package_version;
3937
private static readonly TOPOLOGY_QUERY: string = `SELECT id, endpoint, port FROM rds_tools.show_topology('aws_advanced_nodejs_wrapper-"${RdsMultiAZClusterPgDatabaseDialect.VERSION}"')`;
4038
private static readonly WRITER_HOST_FUNC_EXIST_QUERY: string =
41-
"SELECT 1 AS tmp FROM information_schema.routines WHERE routine_schema='rds_tools' AND routine_name='multi_az_db_cluster_source_dbi_resource_id'";
39+
"SELECT 1 AS tmp FROM information_schema.routines WHERE routine_schema OPERATOR(pg_catalog.=) 'rds_tools' AND routine_name OPERATOR(pg_catalog.=) 'multi_az_db_cluster_source_dbi_resource_id'";
4240
private static readonly FETCH_WRITER_HOST_QUERY: string =
4341
"SELECT multi_az_db_cluster_source_dbi_resource_id FROM rds_tools.multi_az_db_cluster_source_dbi_resource_id()";
4442
private static readonly FETCH_WRITER_HOST_QUERY_COLUMN_NAME: string = "multi_az_db_cluster_source_dbi_resource_id";
4543
private static readonly HOST_ID_QUERY: string = "SELECT dbi_resource_id FROM rds_tools.dbi_resource_id()";
4644
private static readonly HOST_ID_QUERY_COLUMN_NAME: string = "dbi_resource_id";
47-
private static readonly IS_READER_QUERY: string = "SELECT pg_is_in_recovery() AS is_reader";
45+
private static readonly IS_READER_QUERY: string = "SELECT pg_catalog.pg_is_in_recovery() AS is_reader";
4846
private static readonly IS_READER_QUERY_COLUMN_NAME: string = "is_reader";
4947

5048
async isDialect(targetClient: ClientWrapper): Promise<boolean> {

pg/lib/dialect/rds_pg_database_dialect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class RdsPgDatabaseDialect extends PgDatabaseDialect implements BlueGreen
2424

2525
private static readonly EXTENSIONS_SQL: string =
2626
"SELECT (setting LIKE '%rds_tools%') AS rds_tools, (setting LIKE '%aurora_stat_utils%') AS aurora_stat_utils " +
27-
"FROM pg_settings WHERE name='rds.extensions'";
27+
"FROM pg_catalog.pg_settings WHERE name OPERATOR(pg_catalog.=) 'rds.extensions'";
2828

2929
private static readonly BG_STATUS_QUERY: string = `SELECT * FROM rds_tools.show_topology('aws_advanced_nodejs_wrapper-${RdsPgDatabaseDialect.VERSION}')`;
3030

0 commit comments

Comments
 (0)