arrow_back

Administering an AlloyDB Database

Sign in Join
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

Administering an AlloyDB Database

Lab 1 hour 30 minutes universal_currency_alt 1 Credit show_chart Introductory
info This lab may incorporate AI tools to support your learning.
Test and share your knowledge with our community!
done
Get access to over 700 hands-on labs, skill badges, and courses

GSP1086

Google Cloud self-paced labs logo

Overview

AlloyDB for PostgreSQL is a fully managed PostgreSQL-compatible database service for your most demanding enterprise database workloads. AlloyDB combines the best of Google with one of the most popular open-source database engines, PostgreSQL, for superior performance, scale, and availability.

In this lab, you perform administrative tasks that are essential to the optimal use of an AlloyDB for PostgreSQL database.

What you'll do

In this lab, you learn how to perform the following tasks:

  • Examine a Database Flag
  • Setup a Database Extension
  • Create a Read Pool Instance for an Existing Cluster
  • Setup Backups
  • Examine Monitoring in the AlloyDB Console

Setup and requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Cloud console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

    The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username below and paste it into the Sign in dialog.

    {{{user_0.username | "Username"}}}

    You can also find the Username in the Lab Details panel.

  4. Click Next.

  5. Copy the Password below and paste it into the Welcome dialog.

    {{{user_0.password | "Password"}}}

    You can also find the Password in the Lab Details panel.

  6. Click Next.

    Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  7. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Google Cloud console opens in this tab.

Note: To view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left. Navigation menu icon

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

When you are connected, you are already authenticated, and the project is set to your Project_ID, . The output contains a line that declares the Project_ID for this session:

Your Cloud Platform project in this session is set to {{{project_0.project_id | "PROJECT_ID"}}}

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

Output:

ACTIVE: * ACCOUNT: {{{user_0.username | "ACCOUNT"}}} To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = {{{project_0.project_id | "PROJECT_ID"}}} Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Task 1. Examine a Database Flag

  1. An AlloyDB cluster and instance were provisioned when you started the lab. On the Cloud Console Navigation menu (console_nav_small.png), click VIEW ALL PRODUCTS. In Databases section click AlloyDB then Clusters to examine the cluster's details.

  2. The cluster is named lab-cluster and the instance is named lab-instance.

  3. The instance takes a while to be fully created and initialized. Please wait until you see a Status of Ready to proceed.

  4. Please make note of the Private IP address in the instances section. Copy the Private IP address to a text file so that you can paste the value in a later step.

  5. The instance was also configured with the enable_pgaudit database flag already set. Pgaudit is a popular feature of PostgreSQL that provides detailed session and object audit logging via the standard logging facility. To fully enable pgaudit you must also enable the corresponding database extension which you will do in the next section.

  6. In the Instances in your cluster section, select the lab-instance, and then click Edit Primary.

  7. To add a database flag to your instance, expand Advanced Configuration Options and click on Add a Database Flag.

  8. Browse the list of avaiable flags (inside Choose a flag) to get a sense of the supported options. You will not add an additional flag as part of this lab.

  9. Click Cancel twice to exit the Edit Primary instance screen.

Task 2. Setup a Database Extension

  1. Continuing from the previous you will setup a database extension to fully enable the pgaudit feature for your AlloyDB cluster.

  2. Unlike configuring a flag, you must connect to your instance via the psql client to enable a database extension.

  3. On the Navigation menu (Navigation menu icon), under Compute Engine click VM instances.

  4. For the instance named alloydb-client, in the Connect column, click SSH to open a terminal window.

  5. Set the following environment variable, replacing ALLOYDB_ADDRESS with the Private IP address of the AlloyDB instance.

export ALLOYDB=ALLOYDB_ADDRESS
  1. Run the following command to store the Private IP address of the AlloyDB instance on the AlloyDB client VM so that it will persist throughout the lab.
echo $ALLOYDB > alloydbip.txt
  1. Use the following command to launch the PostgreSQL (psql) client. You will be prompted to provide the postgres user's password (Change3Me) which you entered when you created the cluster.
psql -h $ALLOYDB -U postgres
  1. You will be presented the psql terminal prompt as shown below.
psql (14.5 (Debian 14.5-1.pgdg110+1), server 14.4) SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off) Type "help" for help. postgres=>
  1. Input and run the following SQL commands separately to enable the extension.
\c postgres CREATE EXTENSION IF NOT EXISTS PGAUDIT;
  1. Run the following system query to see details on the pgaudit extension.
select extname, extversion from pg_extension where extname = 'pgaudit'; extname | extversion ---------+------------ pgaudit | 1.6.1 (1 row)
  1. Type \q to exit the psql client.

  2. Type exit to close the terminal window.

  3. Click Check my progress to verify the objective.

Enable the pgaudit extension

Task 3. Create a Read Pool Instance for an Existing Cluster

  1. One key feature of AlloyDB for PostgreSQL is the implementation of read pool instances. A read pool instance increases your cluster’s read capacity by aggregating nodes, which you can scale, enabling highly available reads.

You are not required to have any read pool instances in a cluster, but they provide better support for data analytics workloads than do primary instances. Therefore they are the best choice for your data analytics needs.

  1. To add a read pool instance click Add Read Pool or Add Read Pool Instance in the Instances in your cluster section of the Overview page for your cluster.

  2. For Read pool instance ID input lab-instance-rp1. Set the Node count to 2.

  3. Select 2 vCPU, 16 GB as your machine type.

  4. Click Create Read Pool.

  5. Read pool instance creation will take approximately 8 to 11 minutes.

  6. Your read pool instance now appears on the Overview page. Notice the Private IP is in the same pool of addresses as the Primary Instance. The direct IP address allows you to funnel read-specific queries to the read pool, thereby enhancing overall cluster performance.

  7. Click Check my progress to verify the objective.

Create a read pool instance

Task 4. Setup Backups

  1. Automatic backups are configured by default when every AlloyDB cluster is created. You can however create backups as needed, on-demand for additional recovery options based on workload requirements.

  2. On the Cloud Console Navigation menu (console_nav_small.png), click on VIEW ALL PRODUCTS and under Databases click AlloyDB then Backups to launch the Backups page.

  3. Your instance was too recently created to have any automatic backups so you will proceed with creating an on-demand backup. Click Create backup.

  4. Ensure lab-cluster is selected as the source for the backup.

  5. Enter a unique ID for the backup. In this case enter lab-backup.

  6. Click Create.

AlloyDB checks that the source cluster is in the Ready state and then starts a long-running operation to perform the backup. The Backups page shows the backup with a status of In progress until the operation finshes. The speed varies based on the size of the instance but in the lab environment the backup should be created quickly -- within 1 minute.

  1. In the Cloud Shell, run the command below to see more details on your backup.
gcloud beta alloydb backups list
  1. Recovery of a backup is very simple. Click the Restore link at the end of the backup row. Inspect the information for the backup and the proposed target for recovery. For this lab you will not restore the backup you just created. Click Cancel to close the wizard.

  2. Click Check my progress to verify the objective.

Create a backup

Task 5. Examine Monitoring in the AlloyDB Console

  1. The AlloyDB Monitoring dashboard contains a great deal of information about the usage, size, and performance of the clusters and instances. The dashboard displays metrics of the resources that you use and lets you monitor any trends that result.

  2. From the Cluster Overview select the Monitoring link on the left side of the page.

  3. Very little activity has occurred on the lab-instance so the metrics displayed at the moment will not offer much insight. You will utilize the Postgres tool pgbench to generate a synthetic dataset and run a simulated workload to place the lab-instance under a representative burden.

  4. On the Navigation menu (Navigation menu icon), under Compute Engine click VM instances.

  5. For the instance named alloydb-client, in the Connect column, click SSH to open a terminal window.

  6. Set the ALLOYDB environment variable by running the following command.

export ALLOYDB=$(cat alloydbip.txt)
  1. The first step of using pgbench is to create and populate the sample tables. Run the following command to create a set of four tables. You will be prompted for the postgres user's password which is Change3Me.

The largest table pgbench_accounts will be loaded with 5 million rows. The operation should be very brief.

pgbench -h $ALLOYDB -U postgres -i -s 50 -F 90 -n postgres pgbench create dropping old tables... NOTICE: table "pgbench_accounts" does not exist, skipping NOTICE: table "pgbench_branches" does not exist, skipping NOTICE: table "pgbench_history" does not exist, skipping NOTICE: table "pgbench_tellers" does not exist, skipping creating tables... generating data (client-side)... 5000000 of 5000000 tuples (100%) done (elapsed 9.49 s, remaining 0.00 s) creating primary keys... done in 13.71 s (drop tables 0.00 s, create tables 0.01 s, client-side generate 9.98 s, primary keys 3.72 s).
  1. Connect to the psql client and run the following query to verify the row count in the pgbench_accounts table. You will be prompted for the postgres user's password which is Change3Me.
psql -h $ALLOYDB -U postgres select count (*) from pgbench_accounts; count --------- 5000000 (1 row)
  1. Type \q to exit the psql client.

  2. Run the following pgbench operation to simulate a workload against lab-instance. The operation corresponds to a load of fifty (50) clients, across two (2) threads, polling every thirty (30) seconds, over the course of three (3) minutes. You will be prompted for the postgres user's password which is Change3Me.

pgbench -h $ALLOYDB -U postgres -c 50 -j 2 -P 30 -T 180 postgres
  1. The workload operation will complete and report statistics for the run. The details will appear similar to the following:
pgbench (14.5 (Debian 14.5-1.pgdg110+1), server 14.4) starting vacuum...end. progress: 30.0 s, 1412.2 tps, lat 34.433 ms stddev 25.836 progress: 60.0 s, 1426.6 tps, lat 35.040 ms stddev 25.459 progress: 90.0 s, 1393.2 tps, lat 35.863 ms stddev 33.101 progress: 120.0 s, 1429.8 tps, lat 34.968 ms stddev 31.735 progress: 150.0 s, 1335.4 tps, lat 37.406 ms stddev 30.922 progress: 180.0 s, 1424.8 tps, lat 35.118 ms stddev 28.440 transaction type: scaling factor: 50 query mode: simple number of clients: 50 number of threads: 2 duration: 180 s number of transactions actually processed: 252710 latency average = 35.458 ms latency stddev = 29.391 ms initial connection time = 801.012 ms tps = 1409.393040 (without initial connection time)
  1. Return to the AlloyDB Monitoring dashboard and set the time horizon to 1 hour. In the tiles below you will see details on Mean CPU utilization, Minimum available memory, Connections, Transactions per second, Cluster storage, Maximum replication lag, and Active nodes.

  2. Click the Query insights link on the left for details on the queries that the pgbench operation issued against the instance.

  3. Under Top Queries and Tags, you will see an ordering of queries. In the image below the query UPDATE pgbench_branches SET ... was the top query by Load by total time. Your results may vary.

TopQuery.png

  1. The Load by total time column has a selector option. You may also view the queries by Load by CPU, Load by IO wait, and Load by lock wait.

  2. Feel free to explore queries in detail by clicking on any of the values in the Query column in the Top Queries and Tags section or links in other areas of the dashboard.

Congratulations!

You have now performed administrative tasks that are essential to the optimal use of an AlloyDB for PostgreSQL database.

Manual Last Updated August 28, 2024

Lab Last Tested August 28, 2024

Copyright 2024 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

This content is not currently available

We will notify you via email when it becomes available

Great!

We will contact you via email if it becomes available