Skip to content

Commit

Permalink
chore: do not use pg_settings due to a bug in the emulator
Browse files Browse the repository at this point in the history
The emulator crashes when executing a query on pg_catalog.pg_settings.
Remove the references to pg_catalog.pg_settings for now, as all the
data comes from the in-memory CTE anyways.
  • Loading branch information
olavloite committed Dec 1, 2024
1 parent f98d5d8 commit 89eb497
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ public void connectToSpanner(String database, @Nullable Credentials credentials)
@VisibleForTesting
static String buildConnectionURL(
String database, OptionsMetadata options, Properties properties) {
boolean checkForCredentials = true;
String uri =
options.hasDefaultConnectionUrl()
? options.getDefaultConnectionUrl()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,14 @@ public String buildConnectionURL(String database) {
host = environment.get(SPANNER_EMULATOR_HOST_ENV_VAR);
usePlainText = true;
}
if (propertyMap != null
&& Boolean.parseBoolean(propertyMap.getOrDefault("autoConfigEmulator", "false"))) {
usePlainText = true;
}
if (propertyMap != null
&& Boolean.parseBoolean(propertyMap.getOrDefault("usePlainText", "false"))) {
usePlainText = true;
}
String endpoint;
if (host.isEmpty()) {
endpoint = "cloudspanner:/";
Expand All @@ -1041,8 +1049,7 @@ public String buildConnectionURL(String database) {
// Note that Credentials here is the credentials file, not the actual credentials
String url = String.format("%s%s;userAgent=%s", endpoint, databaseName, DEFAULT_USER_AGENT);

if (!shouldAuthenticate()
&& Strings.isNullOrEmpty(environment.get(SPANNER_EMULATOR_HOST_ENV_VAR))) {
if (!shouldAuthenticate() && !usePlainText) {
String credentials = buildCredentialsFile();
if (!Strings.isNullOrEmpty(credentials)) {
url = String.format("%s;credentials=%s", url, credentials);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,19 @@ public String generatePGSettingsCte() {
+ "\n),\n"
+ "pg_settings_names_ as (\n"
+ "select name from pg_settings_inmem_\n"
+ "union\n"
+ "select name from pg_catalog.pg_settings\n"
// TODO: Re-enable when the pg_settings table in the emulator has been fixed.
// + "union\n"
// + "select name from pg_catalog.pg_settings\n"
+ "),\n"
+ "pg_settings as (\n"
+ "select n.name, "
+ generatePgSettingsColumnExpressions()
+ "\n"
+ "from pg_settings_names_ n\n"
+ "left join pg_settings_inmem_ s1 using (name)\n"
+ "left join pg_catalog.pg_settings s2 using (name)\n"
// TODO: Re-enable when the pg_settings table in the emulator has been fixed.
// + "left join pg_catalog.pg_settings s2 using (name)\n"
+ "left join pg_settings_inmem_ s2 using (name)\n"
+ "order by name\n"
+ ")\n";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ public void testBuildConnectionUrl() {
// Enable the autoConfigEmulator flag through the options builder.
OptionsMetadata emulatorOptions = OptionsMetadata.newBuilder().autoConfigureEmulator().build();
assertEquals(
"cloudspanner:/projects/my-project/instances/my-instance/databases/my-database;userAgent=pg-adapter;autoConfigEmulator=true;dialect=postgresql",
"cloudspanner:/projects/my-project/instances/my-instance/databases/my-database;userAgent=pg-adapter;usePlainText=true;autoConfigEmulator=true;dialect=postgresql",
buildConnectionURL(
"projects/my-project/instances/my-instance/databases/my-database",
emulatorOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,17 @@ static String getDefaultSessionStateExpression() {
+ "),\n"
+ "pg_settings_names_ as (\n"
+ "select name from pg_settings_inmem_\n"
+ "union\n"
+ "select name from pg_catalog.pg_settings\n"
// TODO: Re-enable when the pg_settings table in the emulator has been fixed.
// + "union\n"
// + "select name from pg_catalog.pg_settings\n"
+ "),\n"
+ "pg_settings as (\n"
+ "select n.name, coalesce(s1.setting, s2.setting) as setting,coalesce(s1.unit, s2.unit) as unit,coalesce(s1.category, s2.category) as category,coalesce(s1.short_desc, s2.short_desc) as short_desc,coalesce(s1.extra_desc, s2.extra_desc) as extra_desc,coalesce(s1.context, s2.context) as context,coalesce(s1.vartype, s2.vartype) as vartype,coalesce(s1.source, s2.source) as source,coalesce(s1.min_val, s2.min_val) as min_val,coalesce(s1.max_val, s2.max_val) as max_val,coalesce(s1.enumvals, s2.enumvals) as enumvals,coalesce(s1.boot_val, s2.boot_val) as boot_val,coalesce(s1.reset_val, s2.reset_val) as reset_val,coalesce(s1.sourcefile, s2.sourcefile) as sourcefile,coalesce(s1.sourceline, s2.sourceline) as sourceline,coalesce(s1.pending_restart, s2.pending_restart) as pending_restart\n"
+ "from pg_settings_names_ n\n"
+ "left join pg_settings_inmem_ s1 using (name)\n"
+ "left join pg_catalog.pg_settings s2 using (name)\n"
// TODO: Re-enable when the pg_settings table in the emulator has been fixed.
// + "left join pg_catalog.pg_settings s2 using (name)\n"
+ "left join pg_settings_inmem_ s2 using (name)\n"
+ "order by name\n"
+ ")\n";
}
Expand Down

0 comments on commit 89eb497

Please sign in to comment.