Skip to content

Commit

Permalink
Only create neon extension in postgres database; (#5918)
Browse files Browse the repository at this point in the history
Create neon extension in neon schema.
  • Loading branch information
lubennikovaav authored Nov 26, 2023
1 parent 6b1c4cc commit 87b8ac3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 89 deletions.
6 changes: 2 additions & 4 deletions compute_tools/src/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,13 +693,12 @@ impl ComputeNode {
let spec = &compute_state.pspec.as_ref().expect("spec must be set").spec;
create_neon_superuser(spec, &mut client)?;
cleanup_instance(&mut client)?;
handle_extension_neon(self.connstr.as_str())?;
handle_roles(spec, &mut client)?;
handle_databases(spec, &mut client)?;
handle_role_deletions(spec, self.connstr.as_str(), &mut client)?;
handle_grants(spec, &mut client, self.connstr.as_str())?;
handle_extensions(spec, &mut client)?;
handle_alter_extension_neon(spec, &mut client, self.connstr.as_str())?;
handle_extension_neon(&mut client)?;
create_availability_check_data(&mut client)?;

// 'Close' connection
Expand Down Expand Up @@ -739,13 +738,12 @@ impl ComputeNode {
if spec.mode == ComputeMode::Primary {
client.simple_query("SET neon.forward_ddl = false")?;
cleanup_instance(&mut client)?;
handle_extension_neon(self.connstr.as_str())?;
handle_roles(&spec, &mut client)?;
handle_databases(&spec, &mut client)?;
handle_role_deletions(&spec, self.connstr.as_str(), &mut client)?;
handle_grants(&spec, &mut client, self.connstr.as_str())?;
handle_extensions(&spec, &mut client)?;
handle_alter_extension_neon(&spec, &mut client, self.connstr.as_str())?;
handle_extension_neon(&mut client)?;
}

// 'Close' connection
Expand Down
93 changes: 22 additions & 71 deletions compute_tools/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,78 +675,29 @@ pub fn handle_extensions(spec: &ComputeSpec, client: &mut Client) -> Result<()>
Ok(())
}

/// connect to template1 and postgres to create neon extension
/// that will be available in all databases
pub fn handle_extension_neon(connstr: &str) -> Result<()> {
for dbname in ["template1", "postgres"].iter() {
let mut conf = Config::from_str(connstr)?;
conf.dbname(dbname);
let mut template1_client = conf.connect(NoTls)?;

let create_extension_neon_query = "CREATE EXTENSION IF NOT EXISTS neon";
info!(
"creating neon extension with query: {} in db {}",
create_extension_neon_query, dbname
);
template1_client.simple_query(create_extension_neon_query)?;
}

Ok(())
}

/// Run ALTER EXTENSION neon UPDATE for each valid database
/// Run CREATE and ALTER EXTENSION neon UPDATE for postgres database
#[instrument(skip_all)]
pub fn handle_alter_extension_neon(
spec: &ComputeSpec,
client: &mut Client,
connstr: &str,
) -> Result<()> {
info!("modifying database permissions");
let existing_dbs = get_existing_dbs(client)?;

// We'd better do this at db creation time, but we don't always know when it happens.
for db in &spec.cluster.databases {
match existing_dbs.get(&db.name) {
Some(pg_db) => {
if pg_db.restrict_conn || pg_db.invalid {
info!(
"skipping grants for db {} (invalid: {}, connections not allowed: {})",
db.name, pg_db.invalid, pg_db.restrict_conn
);
continue;
}
}
None => {
bail!(
"database {} doesn't exist in Postgres after handle_databases()",
db.name
);
}
}

let mut conf = Config::from_str(connstr)?;
conf.dbname(&db.name);

let mut db_client = conf.connect(NoTls)?;

// this will be a no-op if extension is already up to date,
// which may happen in two cases:
// - extension was just installed
// - extension was already installed and is up to date
let create_extension_neon_query = "CREATE EXTENSION IF NOT EXISTS neon";
info!(
"create extension neon query for db {} : {}",
&db.name, &create_extension_neon_query
);
db_client.simple_query(create_extension_neon_query)?;

let alter_extension_neon_query = "ALTER EXTENSION neon UPDATE";
info!(
"alter extension neon query for db {} : {}",
&db.name, &alter_extension_neon_query
);
db_client.simple_query(alter_extension_neon_query)?;
}
pub fn handle_extension_neon(client: &mut Client) -> Result<()> {
info!("handle extension neon");

let mut query = "CREATE SCHEMA IF NOT EXISTS neon";
client.simple_query(query)?;

query = "CREATE EXTENSION IF NOT EXISTS neon WITH SCHEMA neon";
info!("create neon extension with query: {}", query);
client.simple_query(query)?;

query = "ALTER EXTENSION neon SET SCHEMA neon";
info!("alter neon extension schema with query: {}", query);
client.simple_query(query)?;

// this will be a no-op if extension is already up to date,
// which may happen in two cases:
// - extension was just installed
// - extension was already installed and is up to date
let query = "ALTER EXTENSION neon UPDATE";
info!("update neon extension schema with query: {}", query);
client.simple_query(query)?;

Ok(())
}
10 changes: 0 additions & 10 deletions test_runner/regress/test_neon_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,3 @@ def test_neon_extension(neon_env_builder: NeonEnvBuilder):
# If the version has changed, the test should be updated.
# Ensure that the default version is also updated in the neon.control file
assert cur.fetchone() == ("1.1",)

# create test database
cur.execute("CREATE DATABASE foodb")

# connect to test database
# test that neon extension is installed and has the correct version
with closing(endpoint_main.connect(dbname="foodb")) as conn:
with conn.cursor() as cur:
cur.execute("SELECT extversion from pg_extension where extname='neon'")
assert cur.fetchone() == ("1.1",)
9 changes: 5 additions & 4 deletions test_runner/regress/test_timeline_size.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ def wait_for_pageserver_catchup(endpoint_main: Endpoint, polling_interval=1, tim
res = endpoint_main.safe_psql(
"""
SELECT
pg_size_pretty(pg_cluster_size()),
pg_size_pretty(neon.pg_cluster_size()),
pg_wal_lsn_diff(pg_current_wal_flush_lsn(), received_lsn) as received_lsn_lag
FROM backpressure_lsns();
"""
FROM neon.backpressure_lsns();
""",
dbname="postgres",
)[0]
log.info(f"pg_cluster_size = {res[0]}, received_lsn_lag = {res[1]}")
received_lsn_lag = res[1]
Expand Down Expand Up @@ -214,7 +215,7 @@ def test_timeline_size_quota(neon_env_builder: NeonEnvBuilder):

wait_for_pageserver_catchup(endpoint_main)

cur.execute("SELECT * from pg_size_pretty(pg_cluster_size())")
cur.execute("SELECT * from pg_size_pretty(neon.pg_cluster_size())")
pg_cluster_size = cur.fetchone()
log.info(f"pg_cluster_size = {pg_cluster_size}")

Expand Down

1 comment on commit 87b8ac3

@github-actions
Copy link

@github-actions github-actions bot commented on 87b8ac3 Nov 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2492 tests run: 2357 passed, 0 failed, 135 skipped (full report)


Flaky tests (3)

Postgres 16

  • test_ondemand_download_timetravel[mock_s3]: debug

Postgres 14

  • test_branch_creation_heavy_write[20]: release
  • test_ondemand_download_timetravel[mock_s3]: debug

Code coverage (full report)

  • functions: 54.1% (8997 of 16635 functions)
  • lines: 81.5% (52455 of 64384 lines)

The comment gets automatically updated with the latest test results
87b8ac3 at 2023-11-27T07:56:21.451Z :recycle:

Please sign in to comment.