{ "cells": [ { "cell_type": "markdown", "id": "42ccd0b9-6096-4542-bd7f-1ac4068feef5", "metadata": { "ExecuteTime": { "end_time": "2025-06-16T22:54:18.053297Z", "start_time": "2025-06-16T22:54:18.051238Z" } }, "source": [ "# Extract Metadata from AWS Redshift\n", "\n", "本文详细介绍如何从 AWS Redshift 的 System Table 中提取所有 Schema, Table, Column 的完整元数据信息, 以便将这些结构化数据编码后提供给 LLM, 帮助我们生成更准确的 SQL 查询语句." ] }, { "cell_type": "markdown", "id": "35ad45c6-243c-44da-9d02-d397fc343757", "metadata": {}, "source": [ "## Setup Helpers\n", "\n", "在开始元数据提取工作之前, 我们首先配置必要的工具和连接环境. 这些工具将帮助我们在 Notebook 中便捷地执行 SQL 查询并直观地预览查询结果." ] }, { "cell_type": "code", "execution_count": 35, "id": "53cf7591-bd92-483d-bdee-35a6178b3aa2", "metadata": { "ExecuteTime": { "end_time": "2025-06-16T22:55:38.748583Z", "start_time": "2025-06-16T22:55:38.652371Z" } }, "outputs": [], "source": [ "import polars as pl\n", "import sqlalchemy as sa\n", "from rich import print as rprint\n", "from rich.console import Console\n", "from rich.syntax import Syntax\n", "import simple_aws_redshift.api as aws_rs\n", "from boto_session_manager import BotoSesManager\n", "\n", "from mcp_ohmy_sql.tests.aws.constants import aws_profile, database_name, namespace_name, workgroup_name" ] }, { "cell_type": "markdown", "id": "c0194ddd-8d55-44cd-adf0-8658dd6460cd", "metadata": {}, "source": [ "接下来创建与 Redshift Serverless 的数据库连接:" ] }, { "cell_type": "code", "execution_count": 36, "id": "9cf56083-6879-4fa9-9a6f-d233f1f21346", "metadata": { "ExecuteTime": { "end_time": "2025-06-16T22:56:40.896066Z", "start_time": "2025-06-16T22:56:40.231678Z" } }, "outputs": [], "source": [ "# Create sqlalchemy engine.\n", "bsm = BotoSesManager(profile_name=aws_profile)\n", "params = aws_rs.RedshiftServerlessConnectionParams.new(\n", " redshift_serverless_client=bsm.redshiftserverless_client,\n", " namespace_name=namespace_name,\n", " workgroup_name=workgroup_name,\n", ")\n", "engine = params.get_engine()" ] }, { "cell_type": "markdown", "id": "4556ded3-b84a-444c-a7f6-abea0cc237fb", "metadata": {}, "source": [ "为了提高开发效率, 我们定义一个通用的 SQL 执行和结果展示函数:" ] }, { "cell_type": "code", "execution_count": 37, "id": "d38d06cb-43d5-4acd-9a36-d7ab13088424", "metadata": { "ExecuteTime": { "end_time": "2025-06-16T23:00:32.672336Z", "start_time": "2025-06-16T23:00:32.617442Z" } }, "outputs": [ { "data": { "text/html": [ "
  1 SELECT 1 as value;                                                                                             \n",
       "
\n" ], "text/plain": [ "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m1 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mSELECT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m1\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mas\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mvalue\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m;\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "shape: (1, 1)
value
i64
1
" ], "text/plain": [ "shape: (1, 1)\n", "┌───────┐\n", "│ value │\n", "│ --- │\n", "│ i64 │\n", "╞═══════╡\n", "│ 1 │\n", "└───────┘" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 定义一个用来运行 SQL 以及展示结果的 helper 函数\n", "console = Console()\n", "pl.Config(tbl_rows=20)\n", "\n", "def run_sql(sql: str)-> pl.DataFrame:\n", " syntax = Syntax(sql, \"sql\", line_numbers=True)\n", " console.print(syntax)\n", " with engine.connect() as conn:\n", " rows = conn.execute(sa.text(sql)).fetchall()\n", " df = pl.DataFrame(rows)\n", " return df\n", "\n", "sql = \"SELECT 1 as value;\"\n", "run_sql(sql)" ] }, { "cell_type": "markdown", "id": "de34e343-14b4-43c7-8ce9-2ca7ecdf0de7", "metadata": {}, "source": [ "## Explore Key System Tables\n", "\n", "Redshift 继承了 PostgreSQL 的架构设计, 采用 Schema → Table → Column 的三层数据组织结构. 我们的目标是找到三个核心 SQL 查询, 分别用于获取:\n", "\n", "- Schema 信息 - 每行代表一个 Schema 及其属性\n", "- Table 信息 - 每行代表一个 Table 及其属性\n", "- Column 信息 - 每行代表一个 Column 及其详细属性\n", "\n", "在编写复杂的元数据查询之前, 我们需要先了解 Redshift 中可用的系统表及其数据结构. 值得注意的是, Redshift 在 PostgreSQL 原有系统表基础上进行了扩展, 添加了许多 _info 后缀的视图 (如 pg_user_info, pg_class_info), 这些视图对 Redshift 用户更加友好, 我们将优先使用这些增强版本." ] }, { "cell_type": "markdown", "id": "dfb676e2-d91f-4047-b105-7e774df72580", "metadata": {}, "source": [ "### pg_user_info\n", "\n", "[pg_user_info](https://docs.aws.amazon.com/redshift/latest/dg/pg_user_info.html) 是 Amazon Redshift 提供的系统视图, 显示用户信息包括用户 ID 和密码过期时间等详细信息.\n", "需要注意的是, 只有超级用户才能访问 PG_USER_INFO 视图. 对于我们的元数据提取需求, 使用基础的 pg_user 表就足够了, 因为我们主要需要获取用户名信息用于权限过滤." ] }, { "cell_type": "code", "execution_count": 38, "id": "900cab01-842a-43c4-ae38-ffbd3e0591e3", "metadata": {}, "outputs": [], "source": [ "query = \"\"\"\n", "SELECT id, name\n", "FROM users\n", "WHERE active = 1;\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 39, "id": "1026f327-3a44-4534-9b52-a35fc973e4a6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
  1                                                                                                                \n",
       "  2 SELECT                                                                                                         \n",
       "  3     *                                                                                                          \n",
       "  4 FROM pg_user t                                                                                                 \n",
       "  5 LIMIT 10;                                                                                                      \n",
       "  6                                                                                                                \n",
       "
\n" ], "text/plain": [ "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m1 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m2 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mSELECT\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m3 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m*\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m4 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mFROM\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_user\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mt\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m5 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIMIT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m10\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m;\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m6 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "shape: (4, 8)
usenameusesysidusecreatedbusesuperusecatupdpasswdvaluntiluseconfig
stri64boolboolboolstrstrnull
"rdsdb"1truetruetrue"********""infinity"null
"admin"100truetruefalse"********"nullnull
"IAMR:cdk-hnb659fds-cfn-exec-ro…101truetruefalse"********"nullnull
"IAM:sanhe"102falsefalsefalse"********"nullnull
" ], "text/plain": [ "shape: (4, 8)\n", "┌────────────────┬──────────┬─────────────┬──────────┬───────────┬──────────┬──────────┬───────────┐\n", "│ usename ┆ usesysid ┆ usecreatedb ┆ usesuper ┆ usecatupd ┆ passwd ┆ valuntil ┆ useconfig │\n", "│ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", "│ str ┆ i64 ┆ bool ┆ bool ┆ bool ┆ str ┆ str ┆ null │\n", "╞════════════════╪══════════╪═════════════╪══════════╪═══════════╪══════════╪══════════╪═══════════╡\n", "│ rdsdb ┆ 1 ┆ true ┆ true ┆ true ┆ ******** ┆ infinity ┆ null │\n", "│ admin ┆ 100 ┆ true ┆ true ┆ false ┆ ******** ┆ null ┆ null │\n", "│ IAMR:cdk-hnb65 ┆ 101 ┆ true ┆ true ┆ false ┆ ******** ┆ null ┆ null │\n", "│ 9fds-cfn-exec- ┆ ┆ ┆ ┆ ┆ ┆ ┆ │\n", "│ ro… ┆ ┆ ┆ ┆ ┆ ┆ ┆ │\n", "│ IAM:sanhe ┆ 102 ┆ false ┆ false ┆ false ┆ ******** ┆ null ┆ null │\n", "└────────────────┴──────────┴─────────────┴──────────┴───────────┴──────────┴──────────┴───────────┘" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sql = \"\"\"\n", "SELECT\n", " *\n", "FROM pg_user t\n", "LIMIT 10;\n", "\"\"\"\n", "run_sql(sql)" ] }, { "cell_type": "markdown", "id": "e3c9fe87-6068-4769-a660-8316cdb1cebc", "metadata": {}, "source": [ "### pg_description\n", "\n", "[pg_description](https://www.postgresql.org/docs/current/catalog-pg-description.html) 表是 PostgreSQL 的标准系统表, 用于存储数据库对象的注释信息 (也称为描述信息). 这些注释通过 SQL 的 COMMENT 语句定义, 相当于将文档直接嵌入到数据库结构中." ] }, { "cell_type": "code", "execution_count": 40, "id": "cb80ecb1-27dd-4677-a8e6-1d7be118060c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
  1                                                                                                                \n",
       "  2 SELECT                                                                                                         \n",
       "  3     *                                                                                                          \n",
       "  4 FROM pg_description t                                                                                          \n",
       "  5 LIMIT 10;                                                                                                      \n",
       "  6                                                                                                                \n",
       "
\n" ], "text/plain": [ "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m1 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m2 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mSELECT\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m3 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m*\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m4 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mFROM\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_description\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mt\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m5 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIMIT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m10\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m;\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m6 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "shape: (10, 4)
objoidclassoidobjsubiddescription
i64i64i64str
124212550"I/O"
124312550"I/O"
124412550"I/O"
3112550"I/O"
124512550"I/O"
3312550"I/O"
3412550"I/O"
3512550"I/O"
3812550"I/O"
3912550"I/O"
" ], "text/plain": [ "shape: (10, 4)\n", "┌────────┬──────────┬──────────┬─────────────┐\n", "│ objoid ┆ classoid ┆ objsubid ┆ description │\n", "│ --- ┆ --- ┆ --- ┆ --- │\n", "│ i64 ┆ i64 ┆ i64 ┆ str │\n", "╞════════╪══════════╪══════════╪═════════════╡\n", "│ 1242 ┆ 1255 ┆ 0 ┆ I/O │\n", "│ 1243 ┆ 1255 ┆ 0 ┆ I/O │\n", "│ 1244 ┆ 1255 ┆ 0 ┆ I/O │\n", "│ 31 ┆ 1255 ┆ 0 ┆ I/O │\n", "│ 1245 ┆ 1255 ┆ 0 ┆ I/O │\n", "│ 33 ┆ 1255 ┆ 0 ┆ I/O │\n", "│ 34 ┆ 1255 ┆ 0 ┆ I/O │\n", "│ 35 ┆ 1255 ┆ 0 ┆ I/O │\n", "│ 38 ┆ 1255 ┆ 0 ┆ I/O │\n", "│ 39 ┆ 1255 ┆ 0 ┆ I/O │\n", "└────────┴──────────┴──────────┴─────────────┘" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sql = \"\"\"\n", "SELECT\n", " *\n", "FROM pg_description t\n", "LIMIT 10;\n", "\"\"\"\n", "run_sql(sql)" ] }, { "cell_type": "markdown", "id": "3d21c6fa-1d73-41e8-95a8-27812f83963e", "metadata": {}, "source": [ "我们可以通过 JOIN 操作将 pg_description 与其他系统表结合, 获取带有描述信息的完整元数据:" ] }, { "cell_type": "code", "execution_count": 41, "id": "8b05e347-d94f-4e4b-ac8c-f3e47888638c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
   1                                                                                                               \n",
       "   2 -- oid is the hidden database object id                                                                       \n",
       "   3 SELECT                                                                                                        \n",
       "   4     pg_namespace.oid as object_id,                                                                            \n",
       "   5     pg_namespace.nspname as schema_name,                                                                      \n",
       "   6     pg_description.description as description                                                                 \n",
       "   7 FROM pg_namespace                                                                                             \n",
       "   8 JOIN pg_description                                                                                           \n",
       "   9     ON pg_namespace.oid = pg_description.objoid                                                               \n",
       "  10 WHERE                                                                                                         \n",
       "  11     pg_namespace.nspname NOT IN ('information_schema', 'catalog_history')                                     \n",
       "  12     AND pg_namespace.nspname NOT LIKE 'pg_%'                                                                  \n",
       "  13 LIMIT 20;                                                                                                     \n",
       "  14                                                                                                               \n",
       "
\n" ], "text/plain": [ "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 1 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 2 \u001b[0m\u001b[38;2;149;144;119;48;2;39;40;34m-- oid is the hidden database object id\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 3 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mSELECT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 4 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34moid\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mas\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mobject_id\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 5 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mas\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mschema_name\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 6 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_description\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mdescription\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mas\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mdescription\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 7 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mFROM\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 8 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mJOIN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_description\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 9 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mON\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34moid\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_description\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mobjoid\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m10 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHERE\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m11 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mNOT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mIN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m(\u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'information_schema'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'catalog_history'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m)\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m12 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAND\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mNOT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIKE\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'pg_%'\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m13 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIMIT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m20\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m;\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m14 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "shape: (1, 3)
object_idschema_namedescription
i64strstr
2200"public""Standard public schema"
" ], "text/plain": [ "shape: (1, 3)\n", "┌───────────┬─────────────┬────────────────────────┐\n", "│ object_id ┆ schema_name ┆ description │\n", "│ --- ┆ --- ┆ --- │\n", "│ i64 ┆ str ┆ str │\n", "╞═══════════╪═════════════╪════════════════════════╡\n", "│ 2200 ┆ public ┆ Standard public schema │\n", "└───────────┴─────────────┴────────────────────────┘" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sql = \"\"\"\n", "-- oid is the hidden database object id\n", "SELECT \n", " pg_namespace.oid as object_id, \n", " pg_namespace.nspname as schema_name,\n", " pg_description.description as description\n", "FROM pg_namespace\n", "JOIN pg_description\n", " ON pg_namespace.oid = pg_description.objoid\n", "WHERE\n", " pg_namespace.nspname NOT IN ('information_schema', 'catalog_history')\n", " AND pg_namespace.nspname NOT LIKE 'pg_%'\n", "LIMIT 20;\n", "\"\"\"\n", "run_sql(sql)" ] }, { "cell_type": "markdown", "id": "06c773b1-9784-4083-9a4f-b7162dd4802c", "metadata": {}, "source": [ "### pg_namespace\n", "\n", "[pg_namespace](https://www.postgresql.org/docs/8.0/catalog-pg-namespace.html) 是 PostgreSQL 的核心系统表. 在数据库理论中, namespace 是 SQL schema 的底层实现结构, 每个 namespace 可以包含独立的关系 (表), 类型等对象, 且不会产生命名冲突.\n", "重要特征:\n", "\n", "- 来源: PostgreSQL 原生系统表\n", "- 用户友好性: 是, 自动根据当前用户权限过滤结果\n", "- 数据粒度: 表中每一行代表一个 Schema" ] }, { "cell_type": "code", "execution_count": 42, "id": "01aae6d7-0ec6-43c4-9f38-d82d4d999a1d", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
  1                                                                                                                \n",
       "  2 SELECT *                                                                                                       \n",
       "  3 FROM pg_namespace t                                                                                            \n",
       "  4 LIMIT 20;                                                                                                      \n",
       "  5                                                                                                                \n",
       "
\n" ], "text/plain": [ "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m1 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m2 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mSELECT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m*\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m3 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mFROM\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mt\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m4 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIMIT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m20\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m;\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m5 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "shape: (10, 3)
nspnamenspownernspacl
stri64str
"pg_toast"1null
"pg_internal"1null
"catalog_history"1null
"pg_mv"1null
"pg_automv"1null
"pg_auto_copy"1null
"pg_s3"1null
"pg_catalog"1"{rdsdb=UCDA/rdsdb,=U/rdsdb}"
"public"1"{rdsdb=UCDA/rdsdb,=UC/rdsdb}"
"information_schema"1"{rdsdb=UCDA/rdsdb,=U/rdsdb}"
" ], "text/plain": [ "shape: (10, 3)\n", "┌────────────────────┬──────────┬──────────────────────────────┐\n", "│ nspname ┆ nspowner ┆ nspacl │\n", "│ --- ┆ --- ┆ --- │\n", "│ str ┆ i64 ┆ str │\n", "╞════════════════════╪══════════╪══════════════════════════════╡\n", "│ pg_toast ┆ 1 ┆ null │\n", "│ pg_internal ┆ 1 ┆ null │\n", "│ catalog_history ┆ 1 ┆ null │\n", "│ pg_mv ┆ 1 ┆ null │\n", "│ pg_automv ┆ 1 ┆ null │\n", "│ pg_auto_copy ┆ 1 ┆ null │\n", "│ pg_s3 ┆ 1 ┆ null │\n", "│ pg_catalog ┆ 1 ┆ {rdsdb=UCDA/rdsdb,=U/rdsdb} │\n", "│ public ┆ 1 ┆ {rdsdb=UCDA/rdsdb,=UC/rdsdb} │\n", "│ information_schema ┆ 1 ┆ {rdsdb=UCDA/rdsdb,=U/rdsdb} │\n", "└────────────────────┴──────────┴──────────────────────────────┘" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sql = \"\"\"\n", "SELECT *\n", "FROM pg_namespace t\n", "LIMIT 20;\n", "\"\"\"\n", "df = run_sql(sql)\n", "df" ] }, { "cell_type": "markdown", "id": "7ac396fe-2901-44d7-b88a-fc881fa97c14", "metadata": {}, "source": [ "为了获取用户自定义的 Schema (排除系统 Schema), 我们使用以下过滤条件:" ] }, { "cell_type": "code", "execution_count": 43, "id": "4d1cb0b5-7e6d-4a40-a5e8-6d6167419998", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
   1                                                                                                               \n",
       "   2 -- oid is the hidden database object id                                                                       \n",
       "   3 SELECT                                                                                                        \n",
       "   4     t.oid as object_id,                                                                                       \n",
       "   5     t.*                                                                                                       \n",
       "   6 FROM pg_namespace t                                                                                           \n",
       "   7 WHERE                                                                                                         \n",
       "   8     t.nspname NOT IN ('information_schema', 'catalog_history')                                                \n",
       "   9     AND t.nspname NOT LIKE 'pg_%'                                                                             \n",
       "  10 LIMIT 20;                                                                                                     \n",
       "  11                                                                                                               \n",
       "
\n" ], "text/plain": [ "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 1 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 2 \u001b[0m\u001b[38;2;149;144;119;48;2;39;40;34m-- oid is the hidden database object id\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 3 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mSELECT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 4 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mt\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34moid\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mas\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mobject_id\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 5 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mt\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m*\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 6 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mFROM\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mt\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 7 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHERE\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 8 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mt\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mNOT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mIN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m(\u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'information_schema'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'catalog_history'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m)\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 9 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAND\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mt\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mNOT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIKE\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'pg_%'\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m10 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIMIT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m20\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m;\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m11 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "shape: (1, 4)
object_idnspnamenspownernspacl
i64stri64str
2200"public"1"{rdsdb=UCDA/rdsdb,=UC/rdsdb}"
" ], "text/plain": [ "shape: (1, 4)\n", "┌───────────┬─────────┬──────────┬──────────────────────────────┐\n", "│ object_id ┆ nspname ┆ nspowner ┆ nspacl │\n", "│ --- ┆ --- ┆ --- ┆ --- │\n", "│ i64 ┆ str ┆ i64 ┆ str │\n", "╞═══════════╪═════════╪══════════╪══════════════════════════════╡\n", "│ 2200 ┆ public ┆ 1 ┆ {rdsdb=UCDA/rdsdb,=UC/rdsdb} │\n", "└───────────┴─────────┴──────────┴──────────────────────────────┘" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sql = \"\"\"\n", "-- oid is the hidden database object id\n", "SELECT \n", " t.oid as object_id, \n", " t.*\n", "FROM pg_namespace t \n", "WHERE\n", " t.nspname NOT IN ('information_schema', 'catalog_history')\n", " AND t.nspname NOT LIKE 'pg_%'\n", "LIMIT 20;\n", "\"\"\"\n", "run_sql(sql)" ] }, { "cell_type": "markdown", "id": "4fcd3b09-c1e2-43f1-9f68-f450553bd645", "metadata": {}, "source": [ "这个 SQL 就是我们要的, 给出了所有的 Schema 和 Description." ] }, { "cell_type": "code", "execution_count": 44, "id": "cb7eecd5-932f-4fde-982f-e1c571429472", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
   1                                                                                                               \n",
       "   2 SELECT                                                                                                        \n",
       "   3     pg_namespace.nspname as schema_name,                                                                      \n",
       "   4     pg_description.description as description                                                                 \n",
       "   5 FROM pg_namespace                                                                                             \n",
       "   6 JOIN pg_description                                                                                           \n",
       "   7     ON pg_namespace.oid = pg_description.objoid                                                               \n",
       "   8 WHERE                                                                                                         \n",
       "   9     pg_namespace.nspname NOT IN ('information_schema', 'catalog_history')                                     \n",
       "  10     AND pg_namespace.nspname NOT LIKE 'pg_%'                                                                  \n",
       "  11 ;                                                                                                             \n",
       "  12                                                                                                               \n",
       "
\n" ], "text/plain": [ "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 1 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 2 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mSELECT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 3 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mas\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mschema_name\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 4 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_description\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mdescription\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mas\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mdescription\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 5 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mFROM\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 6 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mJOIN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_description\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 7 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mON\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34moid\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_description\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mobjoid\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 8 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHERE\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 9 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mNOT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mIN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m(\u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'information_schema'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'catalog_history'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m)\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m10 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAND\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mNOT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIKE\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'pg_%'\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m11 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m;\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m12 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "shape: (1, 2)
schema_namedescription
strstr
"public""Standard public schema"
" ], "text/plain": [ "shape: (1, 2)\n", "┌─────────────┬────────────────────────┐\n", "│ schema_name ┆ description │\n", "│ --- ┆ --- │\n", "│ str ┆ str │\n", "╞═════════════╪════════════════════════╡\n", "│ public ┆ Standard public schema │\n", "└─────────────┴────────────────────────┘" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sql = \"\"\"\n", "SELECT \n", " pg_namespace.nspname as schema_name,\n", " pg_description.description as description\n", "FROM pg_namespace\n", "JOIN pg_description\n", " ON pg_namespace.oid = pg_description.objoid\n", "WHERE\n", " pg_namespace.nspname NOT IN ('information_schema', 'catalog_history')\n", " AND pg_namespace.nspname NOT LIKE 'pg_%'\n", ";\n", "\"\"\"\n", "run_sql(sql)" ] }, { "cell_type": "markdown", "id": "b3ac370f-5d37-4ef3-bde1-5794cf9eff7d", "metadata": {}, "source": [ "### pg_class_info\n", "\n", "[PG_CLASS_INFO](https://docs.aws.amazon.com/redshift/latest/dg/r_PG_CLASS_INFO.html) 是 Amazon Redshift 专有的增强视图, 包含表的创建时间和当前分布样式等详细信息.\n", "重要特征:\n", "\n", "- 来源: Redshift 专有 (非 PostgreSQL 原生)\n", "- 用户友好性: 是, 只显示当前用户有权限访问的表\n", "- 数据粒度: 表中每一行代表一个 Table 及其属性" ] }, { "cell_type": "code", "execution_count": 45, "id": "10a4a0ef-8b50-4d80-909c-c67ad21aa149", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
  1                                                                                                                \n",
       "  2 SELECT *                                                                                                       \n",
       "  3 FROM PG_CLASS_INFO                                                                                             \n",
       "  4 LIMIT 5;                                                                                                       \n",
       "  5                                                                                                                \n",
       "
\n" ], "text/plain": [ "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m1 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m2 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mSELECT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m*\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m3 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mFROM\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mPG_CLASS_INFO\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m4 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIMIT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m5\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m;\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m5 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "shape: (5, 33)
reloidrelnamerelnamespacereltyperelownerrelamrelfilenodereltablespacerelpagesreltuplesreltoastrelidreltoastidxidrelhasindexrelissharedrelkindrelnattsrelexternidrelisreplicatedrelispinnedreldiststylerelprojbaseidrelchecksreltriggersrelukeysrelfkeysrelrefsrelhasoidsrelhaspkeyrelhasrulesrelhassubclassrelaclreleffectivediststylerelcreationtime
i64stri64i64i64i64i64i64i64f64i64i64boolboolstri64i64boolbooli64i64i64i64i64i64i64boolboolboolboolnulli64null
376"pg_xactlock"110100166400.000falsetrue"s"14294967295falsefalse0000000falsefalsefalsefalsenull0null
16774"pg_aggregate_fnoid_index"110140316774000.000falsefalse"i"14294967295falsefalse0000000falsefalsefalsefalsenull0null
16775"pg_am_name_index"110140316775000.000falsefalse"i"14294967295falsefalse0000000falsefalsefalsefalsenull0null
16776"pg_am_oid_index"110140316776000.000falsefalse"i"14294967295falsefalse0000000falsefalsefalsefalsenull0null
16777"pg_amop_opc_strat_index"110140316777000.000falsefalse"i"34294967295falsefalse0000000falsefalsefalsefalsenull0null
" ], "text/plain": [ "shape: (5, 33)\n", "┌────────┬─────────────┬─────────────┬─────────┬───┬────────────┬────────┬────────────┬────────────┐\n", "│ reloid ┆ relname ┆ relnamespac ┆ reltype ┆ … ┆ relhassubc ┆ relacl ┆ releffecti ┆ relcreatio │\n", "│ --- ┆ --- ┆ e ┆ --- ┆ ┆ lass ┆ --- ┆ vediststyl ┆ ntime │\n", "│ i64 ┆ str ┆ --- ┆ i64 ┆ ┆ --- ┆ null ┆ e ┆ --- │\n", "│ ┆ ┆ i64 ┆ ┆ ┆ bool ┆ ┆ --- ┆ null │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ i64 ┆ │\n", "╞════════╪═════════════╪═════════════╪═════════╪═══╪════════════╪════════╪════════════╪════════════╡\n", "│ 376 ┆ pg_xactlock ┆ 11 ┆ 0 ┆ … ┆ false ┆ null ┆ 0 ┆ null │\n", "│ 16774 ┆ pg_aggregat ┆ 11 ┆ 0 ┆ … ┆ false ┆ null ┆ 0 ┆ null │\n", "│ ┆ e_fnoid_ind ┆ ┆ ┆ ┆ ┆ ┆ ┆ │\n", "│ ┆ ex ┆ ┆ ┆ ┆ ┆ ┆ ┆ │\n", "│ 16775 ┆ pg_am_name_ ┆ 11 ┆ 0 ┆ … ┆ false ┆ null ┆ 0 ┆ null │\n", "│ ┆ index ┆ ┆ ┆ ┆ ┆ ┆ ┆ │\n", "│ 16776 ┆ pg_am_oid_i ┆ 11 ┆ 0 ┆ … ┆ false ┆ null ┆ 0 ┆ null │\n", "│ ┆ ndex ┆ ┆ ┆ ┆ ┆ ┆ ┆ │\n", "│ 16777 ┆ pg_amop_opc ┆ 11 ┆ 0 ┆ … ┆ false ┆ null ┆ 0 ┆ null │\n", "│ ┆ _strat_inde ┆ ┆ ┆ ┆ ┆ ┆ ┆ │\n", "│ ┆ x ┆ ┆ ┆ ┆ ┆ ┆ ┆ │\n", "└────────┴─────────────┴─────────────┴─────────┴───┴────────────┴────────┴────────────┴────────────┘" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sql = \"\"\"\n", "SELECT * \n", "FROM PG_CLASS_INFO\n", "LIMIT 5;\n", "\"\"\"\n", "run_sql(sql)" ] }, { "cell_type": "markdown", "id": "e2067645-ec81-42bb-99c2-da6b827f3e92", "metadata": {}, "source": [ "通过与 pg_namespace 进行 JOIN 操作, 我们可以获得完整的 schema_name.table_name 信息, 并且包含 Redshift 特有的分布键信息:" ] }, { "cell_type": "code", "execution_count": 46, "id": "e93e6778-6b52-4f0a-8950-08df0a8dff05", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
   1                                                                                                               \n",
       "   2 SELECT                                                                                                        \n",
       "   3     pg_class_info.reloid AS table_id,                                                                         \n",
       "   4     trim(pg_namespace.nspname) AS schema_name,                                                                \n",
       "   5     trim(pg_class_info.relname) AS table_name,                                                                \n",
       "   6     pg_class_info.reldiststyle,                                                                               \n",
       "   7     pg_class_info.releffectivediststyle,                                                                      \n",
       "   8     CASE WHEN pg_class_info.reldiststyle = 0 THEN 'EVEN'::text                                                \n",
       "   9         WHEN pg_class_info.reldiststyle = 1 THEN 'KEY'::text                                                  \n",
       "  10         WHEN pg_class_info.reldiststyle = 8 THEN 'ALL'::text                                                  \n",
       "  11         WHEN pg_class_info.releffectivediststyle = 10 THEN 'AUTO(ALL)'::text                                  \n",
       "  12         WHEN pg_class_info.releffectivediststyle = 11 THEN 'AUTO(EVEN)'::text                                 \n",
       "  13         WHEN pg_class_info.releffectivediststyle = 12 THEN 'AUTO(KEY)'::text ELSE '<<UNKNOWN>>'::text END AS d\n",
       "  14     pg_class_info.relcreationtime AS creation_time,                                                           \n",
       "  15     pg_class_info.relowner AS owner                                                                           \n",
       "  16 FROM pg_class_info                                                                                            \n",
       "  17 LEFT JOIN pg_namespace                                                                                        \n",
       "  18     ON pg_class_info.relnamespace = pg_namespace.oid                                                          \n",
       "  19 WHERE                                                                                                         \n",
       "  20     pg_namespace.nspname NOT IN ('information_schema', 'catalog_history')                                     \n",
       "  21     AND pg_namespace.nspname NOT LIKE 'pg_%'                                                                  \n",
       "  22 LIMIT 100;                                                                                                    \n",
       "  23                                                                                                               \n",
       "
\n" ], "text/plain": [ "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 1 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 2 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mSELECT\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 3 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreloid\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtable_id\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 4 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mtrim\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m(\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m)\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mschema_name\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 5 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mtrim\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m(\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mrelname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m)\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mtable_name\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 6 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreldiststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 7 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreleffectivediststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 8 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mCASE\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreldiststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m0\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mTHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'EVEN'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 9 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreldiststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m1\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mTHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'KEY'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m10 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreldiststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m8\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mTHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'ALL'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m11 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreleffectivediststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m10\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mTHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'AUTO(ALL)'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m12 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreleffectivediststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m11\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mTHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'AUTO(EVEN)'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m13 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreleffectivediststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m12\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mTHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'AUTO(KEY)'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mELSE\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'<>'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mEND\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34md\u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m14 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mrelcreationtime\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mcreation_time\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m15 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mrelowner\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mowner\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m16 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mFROM\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m17 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLEFT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mJOIN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m18 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mON\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mrelnamespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34moid\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m19 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHERE\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m20 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mNOT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mIN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m(\u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'information_schema'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'catalog_history'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m)\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m21 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAND\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mNOT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIKE\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'pg_%'\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m22 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIMIT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m100\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m;\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m23 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "shape: (23, 8)
table_idschema_nametable_namereldiststylereleffectivediststylediststylecreation_timeowner
i64strstri64i64strdatetime[μs]i64
135770"public""artist_pkey"00"EVEN"null102
135774"public""album_pkey"00"EVEN"null102
135782"public""genre_pkey"00"EVEN"null102
135786"public""mediatype_pkey"00"EVEN"null102
135790"public""track_pkey"00"EVEN"null102
135772"public""album"11"KEY"2025-06-16 20:18:56.956046102
135784"public""mediatype"88"ALL"2025-06-16 20:18:57.083479102
135780"public""genre"88"ALL"2025-06-16 20:18:57.024100102
135806"public""playlist_pkey"00"EVEN"null102
135810"public""playlisttrack_pkey"00"EVEN"null102
135830"public""customer_pkey"00"EVEN"null102
135820"public""employee"88"ALL"2025-06-16 20:18:57.340479102
135838"public""invoice_pkey"00"EVEN"null102
135828"public""customer"11"KEY"2025-06-16 20:18:57.409248102
135846"public""invoiceline_pkey"00"EVEN"null102
135836"public""invoice"11"KEY"2025-06-16 20:18:57.477920102
135844"public""invoiceline"11"KEY"2025-06-16 20:18:57.536679102
135788"public""track"11"KEY"2025-06-16 20:18:57.143263102
135856"public""albumsalesstats"00"EVEN"null102
135768"public""artist"88"ALL"2025-06-16 20:18:56.862195102
" ], "text/plain": [ "shape: (23, 8)\n", "┌──────────┬─────────────┬─────────────┬─────────────┬────────────┬───────────┬────────────┬───────┐\n", "│ table_id ┆ schema_name ┆ table_name ┆ reldiststyl ┆ releffecti ┆ diststyle ┆ creation_t ┆ owner │\n", "│ --- ┆ --- ┆ --- ┆ e ┆ vediststyl ┆ --- ┆ ime ┆ --- │\n", "│ i64 ┆ str ┆ str ┆ --- ┆ e ┆ str ┆ --- ┆ i64 │\n", "│ ┆ ┆ ┆ i64 ┆ --- ┆ ┆ datetime[μ ┆ │\n", "│ ┆ ┆ ┆ ┆ i64 ┆ ┆ s] ┆ │\n", "╞══════════╪═════════════╪═════════════╪═════════════╪════════════╪═══════════╪════════════╪═══════╡\n", "│ 135770 ┆ public ┆ artist_pkey ┆ 0 ┆ 0 ┆ EVEN ┆ null ┆ 102 │\n", "│ 135774 ┆ public ┆ album_pkey ┆ 0 ┆ 0 ┆ EVEN ┆ null ┆ 102 │\n", "│ 135782 ┆ public ┆ genre_pkey ┆ 0 ┆ 0 ┆ EVEN ┆ null ┆ 102 │\n", "│ 135786 ┆ public ┆ mediatype_p ┆ 0 ┆ 0 ┆ EVEN ┆ null ┆ 102 │\n", "│ ┆ ┆ key ┆ ┆ ┆ ┆ ┆ │\n", "│ 135790 ┆ public ┆ track_pkey ┆ 0 ┆ 0 ┆ EVEN ┆ null ┆ 102 │\n", "│ 135772 ┆ public ┆ album ┆ 1 ┆ 1 ┆ KEY ┆ 2025-06-16 ┆ 102 │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 20:18:56.9 ┆ │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 56046 ┆ │\n", "│ 135784 ┆ public ┆ mediatype ┆ 8 ┆ 8 ┆ ALL ┆ 2025-06-16 ┆ 102 │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 20:18:57.0 ┆ │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 83479 ┆ │\n", "│ 135780 ┆ public ┆ genre ┆ 8 ┆ 8 ┆ ALL ┆ 2025-06-16 ┆ 102 │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 20:18:57.0 ┆ │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 24100 ┆ │\n", "│ 135806 ┆ public ┆ playlist_pk ┆ 0 ┆ 0 ┆ EVEN ┆ null ┆ 102 │\n", "│ ┆ ┆ ey ┆ ┆ ┆ ┆ ┆ │\n", "│ 135810 ┆ public ┆ playlisttra ┆ 0 ┆ 0 ┆ EVEN ┆ null ┆ 102 │\n", "│ ┆ ┆ ck_pkey ┆ ┆ ┆ ┆ ┆ │\n", "│ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │\n", "│ 135830 ┆ public ┆ customer_pk ┆ 0 ┆ 0 ┆ EVEN ┆ null ┆ 102 │\n", "│ ┆ ┆ ey ┆ ┆ ┆ ┆ ┆ │\n", "│ 135820 ┆ public ┆ employee ┆ 8 ┆ 8 ┆ ALL ┆ 2025-06-16 ┆ 102 │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 20:18:57.3 ┆ │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 40479 ┆ │\n", "│ 135838 ┆ public ┆ invoice_pke ┆ 0 ┆ 0 ┆ EVEN ┆ null ┆ 102 │\n", "│ ┆ ┆ y ┆ ┆ ┆ ┆ ┆ │\n", "│ 135828 ┆ public ┆ customer ┆ 1 ┆ 1 ┆ KEY ┆ 2025-06-16 ┆ 102 │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 20:18:57.4 ┆ │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 09248 ┆ │\n", "│ 135846 ┆ public ┆ invoiceline ┆ 0 ┆ 0 ┆ EVEN ┆ null ┆ 102 │\n", "│ ┆ ┆ _pkey ┆ ┆ ┆ ┆ ┆ │\n", "│ 135836 ┆ public ┆ invoice ┆ 1 ┆ 1 ┆ KEY ┆ 2025-06-16 ┆ 102 │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 20:18:57.4 ┆ │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 77920 ┆ │\n", "│ 135844 ┆ public ┆ invoiceline ┆ 1 ┆ 1 ┆ KEY ┆ 2025-06-16 ┆ 102 │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 20:18:57.5 ┆ │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 36679 ┆ │\n", "│ 135788 ┆ public ┆ track ┆ 1 ┆ 1 ┆ KEY ┆ 2025-06-16 ┆ 102 │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 20:18:57.1 ┆ │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 43263 ┆ │\n", "│ 135856 ┆ public ┆ albumsaless ┆ 0 ┆ 0 ┆ EVEN ┆ null ┆ 102 │\n", "│ ┆ ┆ tats ┆ ┆ ┆ ┆ ┆ │\n", "│ 135768 ┆ public ┆ artist ┆ 8 ┆ 8 ┆ ALL ┆ 2025-06-16 ┆ 102 │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 20:18:56.8 ┆ │\n", "│ ┆ ┆ ┆ ┆ ┆ ┆ 62195 ┆ │\n", "└──────────┴─────────────┴─────────────┴─────────────┴────────────┴───────────┴────────────┴───────┘" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sql = \"\"\"\n", "SELECT\n", " pg_class_info.reloid AS table_id,\n", " trim(pg_namespace.nspname) AS schema_name,\n", " trim(pg_class_info.relname) AS table_name,\n", " pg_class_info.reldiststyle,\n", " pg_class_info.releffectivediststyle,\n", " CASE WHEN pg_class_info.reldiststyle = 0 THEN 'EVEN'::text\n", " WHEN pg_class_info.reldiststyle = 1 THEN 'KEY'::text\n", " WHEN pg_class_info.reldiststyle = 8 THEN 'ALL'::text\n", " WHEN pg_class_info.releffectivediststyle = 10 THEN 'AUTO(ALL)'::text\n", " WHEN pg_class_info.releffectivediststyle = 11 THEN 'AUTO(EVEN)'::text\n", " WHEN pg_class_info.releffectivediststyle = 12 THEN 'AUTO(KEY)'::text ELSE '<>'::text END AS diststyle,\n", " pg_class_info.relcreationtime AS creation_time,\n", " pg_class_info.relowner AS owner\n", "FROM pg_class_info\n", "LEFT JOIN pg_namespace\n", " ON pg_class_info.relnamespace = pg_namespace.oid\n", "WHERE\n", " pg_namespace.nspname NOT IN ('information_schema', 'catalog_history')\n", " AND pg_namespace.nspname NOT LIKE 'pg_%'\n", "LIMIT 100;\n", "\"\"\"\n", "run_sql(sql)" ] }, { "cell_type": "markdown", "id": "f05253b1-1869-4aed-a8fa-3cc04d8025eb", "metadata": {}, "source": [ "这个 SQL 就是我们要的, 给出了所有的 Table, Distribution Key 信息, 和 Owner 信息" ] }, { "cell_type": "code", "execution_count": 47, "id": "937cdff5-85b2-4274-a36b-0824531abe04", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
   1                                                                                                               \n",
       "   2 SELECT                                                                                                        \n",
       "   3     trim(pg_namespace.nspname) AS schema_name,                                                                \n",
       "   4     trim(pg_class_info.relname) AS table_name,                                                                \n",
       "   5     CASE WHEN pg_class_info.reldiststyle = 0 THEN 'EVEN'::text                                                \n",
       "   6         WHEN pg_class_info.reldiststyle = 1 THEN 'KEY'::text                                                  \n",
       "   7         WHEN pg_class_info.reldiststyle = 8 THEN 'ALL'::text                                                  \n",
       "   8         WHEN pg_class_info.releffectivediststyle = 10 THEN 'AUTO(ALL)'::text                                  \n",
       "   9         WHEN pg_class_info.releffectivediststyle = 11 THEN 'AUTO(EVEN)'::text                                 \n",
       "  10         WHEN pg_class_info.releffectivediststyle = 12 THEN 'AUTO(KEY)'::text ELSE '<<UNKNOWN>>'::text END AS d\n",
       "  11     pg_user.usename AS owner_name,                                                                            \n",
       "  12     pg_description.description as description                                                                 \n",
       "  13 FROM pg_class_info                                                                                            \n",
       "  14 LEFT JOIN pg_namespace                                                                                        \n",
       "  15     ON pg_class_info.relnamespace = pg_namespace.oid                                                          \n",
       "  16 LEFT JOIN pg_description                                                                                      \n",
       "  17     ON pg_namespace.oid = pg_description.objoid                                                               \n",
       "  18 LEFT JOIN pg_user                                                                                             \n",
       "  19     ON pg_class_info.relowner = pg_user.usesysid                                                              \n",
       "  20 WHERE                                                                                                         \n",
       "  21     pg_namespace.nspname NOT IN ('information_schema', 'catalog_history')                                     \n",
       "  22     AND pg_namespace.nspname NOT LIKE 'pg_%'                                                                  \n",
       "  23 LIMIT 100;                                                                                                    \n",
       "  24                                                                                                               \n",
       "
\n" ], "text/plain": [ "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 1 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 2 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mSELECT\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 3 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mtrim\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m(\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m)\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mschema_name\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 4 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mtrim\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m(\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mrelname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m)\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mtable_name\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 5 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mCASE\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreldiststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m0\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mTHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'EVEN'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 6 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreldiststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m1\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mTHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'KEY'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 7 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreldiststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m8\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mTHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'ALL'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 8 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreleffectivediststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m10\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mTHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'AUTO(ALL)'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 9 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreleffectivediststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m11\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mTHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'AUTO(EVEN)'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m10 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mreleffectivediststyle\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m12\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mTHEN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'AUTO(KEY)'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mELSE\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'<>'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m:\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtext\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mEND\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34md\u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m11 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_user\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34musename\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mowner_name\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m12 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_description\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mdescription\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mas\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mdescription\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m13 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mFROM\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m14 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLEFT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mJOIN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m15 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mON\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mrelnamespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34moid\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m16 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLEFT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mJOIN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_description\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m17 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mON\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34moid\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_description\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mobjoid\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m18 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLEFT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mJOIN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_user\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m19 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mON\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_class_info\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mrelowner\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;255;70;137;48;2;39;40;34m=\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_user\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34musesysid\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m20 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHERE\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m21 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mNOT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mIN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m(\u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'information_schema'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'catalog_history'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m)\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m22 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAND\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_namespace\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mnspname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mNOT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIKE\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'pg_%'\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m23 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIMIT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m100\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m;\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m24 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "shape: (23, 5)
schema_nametable_namediststyleowner_namedescription
strstrstrstrstr
"public""artist_pkey""EVEN""IAM:sanhe""Standard public schema"
"public""album_pkey""EVEN""IAM:sanhe""Standard public schema"
"public""genre_pkey""EVEN""IAM:sanhe""Standard public schema"
"public""mediatype_pkey""EVEN""IAM:sanhe""Standard public schema"
"public""track_pkey""EVEN""IAM:sanhe""Standard public schema"
"public""album""KEY""IAM:sanhe""Standard public schema"
"public""mediatype""ALL""IAM:sanhe""Standard public schema"
"public""genre""ALL""IAM:sanhe""Standard public schema"
"public""playlist_pkey""EVEN""IAM:sanhe""Standard public schema"
"public""playlisttrack_pkey""EVEN""IAM:sanhe""Standard public schema"
"public""customer_pkey""EVEN""IAM:sanhe""Standard public schema"
"public""employee""ALL""IAM:sanhe""Standard public schema"
"public""invoice_pkey""EVEN""IAM:sanhe""Standard public schema"
"public""customer""KEY""IAM:sanhe""Standard public schema"
"public""invoiceline_pkey""EVEN""IAM:sanhe""Standard public schema"
"public""invoice""KEY""IAM:sanhe""Standard public schema"
"public""invoiceline""KEY""IAM:sanhe""Standard public schema"
"public""track""KEY""IAM:sanhe""Standard public schema"
"public""albumsalesstats""EVEN""IAM:sanhe""Standard public schema"
"public""artist""ALL""IAM:sanhe""Standard public schema"
" ], "text/plain": [ "shape: (23, 5)\n", "┌─────────────┬────────────────────┬───────────┬────────────┬────────────────────────┐\n", "│ schema_name ┆ table_name ┆ diststyle ┆ owner_name ┆ description │\n", "│ --- ┆ --- ┆ --- ┆ --- ┆ --- │\n", "│ str ┆ str ┆ str ┆ str ┆ str │\n", "╞═════════════╪════════════════════╪═══════════╪════════════╪════════════════════════╡\n", "│ public ┆ artist_pkey ┆ EVEN ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ album_pkey ┆ EVEN ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ genre_pkey ┆ EVEN ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ mediatype_pkey ┆ EVEN ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ track_pkey ┆ EVEN ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ album ┆ KEY ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ mediatype ┆ ALL ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ genre ┆ ALL ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ playlist_pkey ┆ EVEN ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ playlisttrack_pkey ┆ EVEN ┆ IAM:sanhe ┆ Standard public schema │\n", "│ … ┆ … ┆ … ┆ … ┆ … │\n", "│ public ┆ customer_pkey ┆ EVEN ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ employee ┆ ALL ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ invoice_pkey ┆ EVEN ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ customer ┆ KEY ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ invoiceline_pkey ┆ EVEN ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ invoice ┆ KEY ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ invoiceline ┆ KEY ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ track ┆ KEY ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ albumsalesstats ┆ EVEN ┆ IAM:sanhe ┆ Standard public schema │\n", "│ public ┆ artist ┆ ALL ┆ IAM:sanhe ┆ Standard public schema │\n", "└─────────────┴────────────────────┴───────────┴────────────┴────────────────────────┘" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sql = \"\"\"\n", "SELECT\n", " trim(pg_namespace.nspname) AS schema_name,\n", " trim(pg_class_info.relname) AS table_name,\n", " CASE WHEN pg_class_info.reldiststyle = 0 THEN 'EVEN'::text\n", " WHEN pg_class_info.reldiststyle = 1 THEN 'KEY'::text\n", " WHEN pg_class_info.reldiststyle = 8 THEN 'ALL'::text\n", " WHEN pg_class_info.releffectivediststyle = 10 THEN 'AUTO(ALL)'::text\n", " WHEN pg_class_info.releffectivediststyle = 11 THEN 'AUTO(EVEN)'::text\n", " WHEN pg_class_info.releffectivediststyle = 12 THEN 'AUTO(KEY)'::text ELSE '<>'::text END AS diststyle,\n", " pg_user.usename AS owner_name,\n", " pg_description.description as description\n", "FROM pg_class_info\n", "LEFT JOIN pg_namespace\n", " ON pg_class_info.relnamespace = pg_namespace.oid\n", "LEFT JOIN pg_description\n", " ON pg_namespace.oid = pg_description.objoid\n", "LEFT JOIN pg_user\n", " ON pg_class_info.relowner = pg_user.usesysid\n", "WHERE\n", " pg_namespace.nspname NOT IN ('information_schema', 'catalog_history')\n", " AND pg_namespace.nspname NOT LIKE 'pg_%'\n", "LIMIT 100;\n", "\"\"\"\n", "run_sql(sql)" ] }, { "cell_type": "markdown", "id": "aac18e0f-1571-4901-a464-1e4c1231c7db", "metadata": {}, "source": [ "### pg_table_def\n", "\n", "[pg_table_def](https://docs.aws.amazon.com/redshift/latest/dg/r_PG_TABLE_DEF.html) 是 Redshift 中最重要的元数据表之一, 包含了所有表和列的详细定义信息.\n", "重要特征:\n", "\n", "- 来源: Redshift 专有 (非 PostgreSQL 原生)\n", "- 用户友好性: 是, 只显示当前用户有权限访问的表\n", "- 数据粒度: 表中每一行代表一个 Column 的详细信息\n", "\n", "这个表是我们进行列级元数据提取的核心数据源, 下面这个 Query 就已经够用了." ] }, { "cell_type": "code", "execution_count": 48, "id": "17a84567-39cf-492e-973a-e041c7ddaee5", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
   1                                                                                                               \n",
       "   2 SELECT                                                                                                        \n",
       "   3     td.schemaname AS schema_name,                                                                             \n",
       "   4     td.tablename AS table_name,                                                                               \n",
       "   5     td.column AS column_name,                                                                                 \n",
       "   6     td.type AS column_type,                                                                                   \n",
       "   7     td.encoding AS column_encoding,                                                                           \n",
       "   8     td.distkey AS is_column_a_distkey,                                                                        \n",
       "   9     td.sortkey AS sortkey_position,                                                                           \n",
       "  10     td.notnull AS is_column_notnull                                                                           \n",
       "  11 FROM pg_table_def td                                                                                          \n",
       "  12 WHERE td.schemaname NOT IN ('pg_catalog', 'information_schema')                                               \n",
       "  13 LIMIT 20;                                                                                                     \n",
       "  14                                                                                                               \n",
       "
\n" ], "text/plain": [ "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 1 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 2 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mSELECT\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 3 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtd\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mschemaname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mschema_name\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 4 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtd\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtablename\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mtable_name\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 5 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtd\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mcolumn\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mcolumn_name\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 6 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtd\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mtype\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mcolumn_type\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 7 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtd\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mencoding\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mcolumn_encoding\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 8 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtd\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mdistkey\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mis_column_a_distkey\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m 9 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtd\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34msortkey\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34msortkey_position\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m10 \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtd\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mnotnull\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mAS\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mis_column_notnull\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m11 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mFROM\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mpg_table_def\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtd\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m12 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mWHERE\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mtd\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m.\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34mschemaname\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mNOT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mIN\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m(\u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'pg_catalog'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m,\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;230;219;116;48;2;39;40;34m'information_schema'\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m)\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m13 \u001b[0m\u001b[38;2;102;217;239;48;2;39;40;34mLIMIT\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m \u001b[0m\u001b[38;2;174;129;255;48;2;39;40;34m20\u001b[0m\u001b[38;2;248;248;242;48;2;39;40;34m;\u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n", "\u001b[1;38;2;227;227;221;48;2;39;40;34m \u001b[0m\u001b[38;2;101;102;96;48;2;39;40;34m14 \u001b[0m\u001b[48;2;39;40;34m \u001b[0m\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "shape: (20, 8)
schema_nametable_namecolumn_namecolumn_typecolumn_encodingis_column_a_distkeysortkey_positionis_column_notnull
strstrstrstrstrbooli64bool
"public""album""albumid""integer""none"false1true
"public""album""title""character varying(255)""lzo"false0true
"public""album""artistid""integer""none"true2true
"public""albumsalesstats""albumid""integer""none"false0false
"public""albumsalesstats""albumtitle""character varying(255)""none"false0false
"public""albumsalesstats""artistname""character varying(255)""none"false0false
"public""albumsalesstats""totalsales""integer""none"false0false
"public""albumsalesstats""totalquantity""integer""none"false0false
"public""albumsalesstats""totalrevenue""numeric(10,2)""none"false0false
"public""albumsalesstats""avgtrackprice""numeric(10,2)""none"false0false
"public""albumsalesstats""tracksinalbum""integer""none"false0false
"public""artist""artistid""integer""none"false1true
"public""artist""name""character varying(255)""lzo"false0false
"public""customer""customerid""integer""none"true1true
"public""customer""firstname""character varying(255)""lzo"false0true
"public""customer""lastname""character varying(255)""lzo"false0true
"public""customer""company""character varying(255)""lzo"false0false
"public""customer""address""character varying(500)""lzo"false0false
"public""customer""city""character varying(100)""none"false3false
"public""customer""state""character varying(100)""lzo"false0false
" ], "text/plain": [ "shape: (20, 8)\n", "┌────────────┬────────────┬────────────┬───────────┬───────────┬───────────┬───────────┬───────────┐\n", "│ schema_nam ┆ table_name ┆ column_nam ┆ column_ty ┆ column_en ┆ is_column ┆ sortkey_p ┆ is_column │\n", "│ e ┆ --- ┆ e ┆ pe ┆ coding ┆ _a_distke ┆ osition ┆ _notnull │\n", "│ --- ┆ str ┆ --- ┆ --- ┆ --- ┆ y ┆ --- ┆ --- │\n", "│ str ┆ ┆ str ┆ str ┆ str ┆ --- ┆ i64 ┆ bool │\n", "│ ┆ ┆ ┆ ┆ ┆ bool ┆ ┆ │\n", "╞════════════╪════════════╪════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡\n", "│ public ┆ album ┆ albumid ┆ integer ┆ none ┆ false ┆ 1 ┆ true │\n", "│ public ┆ album ┆ title ┆ character ┆ lzo ┆ false ┆ 0 ┆ true │\n", "│ ┆ ┆ ┆ varying(2 ┆ ┆ ┆ ┆ │\n", "│ ┆ ┆ ┆ 55) ┆ ┆ ┆ ┆ │\n", "│ public ┆ album ┆ artistid ┆ integer ┆ none ┆ true ┆ 2 ┆ true │\n", "│ public ┆ albumsales ┆ albumid ┆ integer ┆ none ┆ false ┆ 0 ┆ false │\n", "│ ┆ stats ┆ ┆ ┆ ┆ ┆ ┆ │\n", "│ public ┆ albumsales ┆ albumtitle ┆ character ┆ none ┆ false ┆ 0 ┆ false │\n", "│ ┆ stats ┆ ┆ varying(2 ┆ ┆ ┆ ┆ │\n", "│ ┆ ┆ ┆ 55) ┆ ┆ ┆ ┆ │\n", "│ public ┆ albumsales ┆ artistname ┆ character ┆ none ┆ false ┆ 0 ┆ false │\n", "│ ┆ stats ┆ ┆ varying(2 ┆ ┆ ┆ ┆ │\n", "│ ┆ ┆ ┆ 55) ┆ ┆ ┆ ┆ │\n", "│ public ┆ albumsales ┆ totalsales ┆ integer ┆ none ┆ false ┆ 0 ┆ false │\n", "│ ┆ stats ┆ ┆ ┆ ┆ ┆ ┆ │\n", "│ public ┆ albumsales ┆ totalquant ┆ integer ┆ none ┆ false ┆ 0 ┆ false │\n", "│ ┆ stats ┆ ity ┆ ┆ ┆ ┆ ┆ │\n", "│ public ┆ albumsales ┆ totalreven ┆ numeric(1 ┆ none ┆ false ┆ 0 ┆ false │\n", "│ ┆ stats ┆ ue ┆ 0,2) ┆ ┆ ┆ ┆ │\n", "│ public ┆ albumsales ┆ avgtrackpr ┆ numeric(1 ┆ none ┆ false ┆ 0 ┆ false │\n", "│ ┆ stats ┆ ice ┆ 0,2) ┆ ┆ ┆ ┆ │\n", "│ public ┆ albumsales ┆ tracksinal ┆ integer ┆ none ┆ false ┆ 0 ┆ false │\n", "│ ┆ stats ┆ bum ┆ ┆ ┆ ┆ ┆ │\n", "│ public ┆ artist ┆ artistid ┆ integer ┆ none ┆ false ┆ 1 ┆ true │\n", "│ public ┆ artist ┆ name ┆ character ┆ lzo ┆ false ┆ 0 ┆ false │\n", "│ ┆ ┆ ┆ varying(2 ┆ ┆ ┆ ┆ │\n", "│ ┆ ┆ ┆ 55) ┆ ┆ ┆ ┆ │\n", "│ public ┆ customer ┆ customerid ┆ integer ┆ none ┆ true ┆ 1 ┆ true │\n", "│ public ┆ customer ┆ firstname ┆ character ┆ lzo ┆ false ┆ 0 ┆ true │\n", "│ ┆ ┆ ┆ varying(2 ┆ ┆ ┆ ┆ │\n", "│ ┆ ┆ ┆ 55) ┆ ┆ ┆ ┆ │\n", "│ public ┆ customer ┆ lastname ┆ character ┆ lzo ┆ false ┆ 0 ┆ true │\n", "│ ┆ ┆ ┆ varying(2 ┆ ┆ ┆ ┆ │\n", "│ ┆ ┆ ┆ 55) ┆ ┆ ┆ ┆ │\n", "│ public ┆ customer ┆ company ┆ character ┆ lzo ┆ false ┆ 0 ┆ false │\n", "│ ┆ ┆ ┆ varying(2 ┆ ┆ ┆ ┆ │\n", "│ ┆ ┆ ┆ 55) ┆ ┆ ┆ ┆ │\n", "│ public ┆ customer ┆ address ┆ character ┆ lzo ┆ false ┆ 0 ┆ false │\n", "│ ┆ ┆ ┆ varying(5 ┆ ┆ ┆ ┆ │\n", "│ ┆ ┆ ┆ 00) ┆ ┆ ┆ ┆ │\n", "│ public ┆ customer ┆ city ┆ character ┆ none ┆ false ┆ 3 ┆ false │\n", "│ ┆ ┆ ┆ varying(1 ┆ ┆ ┆ ┆ │\n", "│ ┆ ┆ ┆ 00) ┆ ┆ ┆ ┆ │\n", "│ public ┆ customer ┆ state ┆ character ┆ lzo ┆ false ┆ 0 ┆ false │\n", "│ ┆ ┆ ┆ varying(1 ┆ ┆ ┆ ┆ │\n", "│ ┆ ┆ ┆ 00) ┆ ┆ ┆ ┆ │\n", "└────────────┴────────────┴────────────┴───────────┴───────────┴───────────┴───────────┴───────────┘" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sql = \"\"\"\n", "SELECT\n", " td.schemaname AS schema_name,\n", " td.tablename AS table_name,\n", " td.column AS column_name,\n", " td.type AS column_type,\n", " td.encoding AS column_encoding,\n", " td.distkey AS is_column_a_distkey,\n", " td.sortkey AS sortkey_position,\n", " td.notnull AS is_column_notnull\n", "FROM pg_table_def td\n", "WHERE td.schemaname NOT IN ('pg_catalog', 'information_schema')\n", "LIMIT 20;\n", "\"\"\"\n", "run_sql(sql)" ] }, { "cell_type": "markdown", "id": "3eadbbbc-58a2-4fd9-a923-2f20b74f3e0f", "metadata": {}, "source": [ "### Summary\n", "\n", "通过探索这些关键的系统表, 我们现在掌握了从 Redshift 中提取完整元数据的方法:\n", "\n", "- Schema 层级: 使用 pg_namespace 获取所有用户定义的 Schema\n", "- Table 层级: 使用 pg_class_info 结合 pg_namespace 获取表的完整信息\n", "- Column 层级: 使用 pg_table_def 获取列的详细定义\n", "\n", "这些查询的结果可以被结构化处理后提供给 LLM, 帮助生成更准确和上下文相关的 SQL 语句. 在后续的实现中, 我们将基于这些基础查询构建完整的元数据提取流程." ] }, { "cell_type": "code", "execution_count": null, "id": "bd610a6e-f5b4-488e-8576-4de7b3193853", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.8" } }, "nbformat": 4, "nbformat_minor": 5 }