AlloyDB データベースで外部クエリ SELECT customer_id, MIN(order_date) AS
first_order_date FROM orders GROUP BY customer_id を実行し、EXTERNAL_QUERY 関数によって各お客様の最初の注文日を取得します。
SELECTc.customer_id,c.name,rq.first_order_dateFROMmydataset.customersAScLEFTOUTERJOINEXTERNAL_QUERY('us.connection_id','''SELECT customer_id, MIN(order_date) AS first_order_date FROM orders GROUP BY customer_id''')ASrqONrq.customer_id=c.customer_idGROUPBYc.customer_id,c.name,rq.first_order_date;
問題: データベース サーバーに接続できない: Invalid table-valued function EXTERNAL_QUERY Connect to PostgreSQL server failed: server closed the connection unexpectedly. This probably means the server terminated abnormally before or while processing the request.
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-08-17 UTC。"],[],[],null,["# AlloyDB federated queries\n=========================\n\nAs a data analyst, you can query data in AlloyDB for PostgreSQL from BigQuery\nusing [federated queries](/bigquery/docs/federated-queries-intro).\n\nBigQuery AlloyDB federation lets BigQuery\nquery data residing in AlloyDB in real time without copying or\nmoving the data.\n\nBefore you begin\n----------------\n\n- Ensure that your BigQuery administrator has created an [AlloyDB connection](/bigquery/docs/connect-to-alloydb#create-alloydb-connection) and [shared](/bigquery/docs/connect-to-alloydb#share_connections) it with you.\n-\n\n To get the permissions that\n you need to query an AlloyDB instance,\n\n ask your administrator to grant you the\n\n\n [BigQuery Connection User](/iam/docs/roles-permissions/bigquery#bigquery.connectionUser) (`roles/bigquery.connectionUser`)\n IAM role on your project.\n\n\n For more information about granting roles, see [Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\n\n You might also be able to get\n the required permissions through [custom\n roles](/iam/docs/creating-custom-roles) or other [predefined\n roles](/iam/docs/roles-overview#predefined).\n\nQuery data\n----------\n\nTo send a federated query to AlloyDB from a\nGoogleSQL query, use the\n[`EXTERNAL_QUERY` function](/bigquery/docs/reference/standard-sql/federated_query_functions#external_query).\n\nSuppose that you store a customer table in BigQuery, while\nstoring a sales table in AlloyDB, and want to join the two tables in\na single query. The following example makes a federated query to an\nAlloyDB table named `orders` and joins the results with a\nBigQuery table named `mydataset.customers`.\n\nThe example query includes 3 parts:\n\n1. Run the external query `SELECT customer_id, MIN(order_date) AS\n first_order_date FROM orders GROUP BY customer_id` in the AlloyDB database\n to get the first order date for each customer through\n the `EXTERNAL_QUERY` function.\n\n2. Join the external query results table with the customers table in\n BigQuery by `customer_id`.\n\n3. Select customer information and first order date in the final result set.\n\n SELECT c.customer_id, c.name, rq.first_order_date\n FROM mydataset.customers AS c\n LEFT OUTER JOIN EXTERNAL_QUERY(\n 'us.connection_id',\n '''SELECT customer_id, MIN(order_date) AS first_order_date\n FROM orders\n GROUP BY customer_id''') AS rq ON rq.customer_id = c.customer_id\n GROUP BY c.customer_id, c.name, rq.first_order_date;\n\nView an AlloyDB table schema\n----------------------------\n\nYou can use the `EXTERNAL_QUERY` function to query `information_schema` tables\nto access database metadata. For example, you can list all of the tables in the\ndatabase or view the table schema. For more information, see [PostgreSQL information_schema tables](https://www.postgresql.org/docs/9.1/information-schema.html). \n\n -- List all tables in a database.\n SELECT * FROM EXTERNAL_QUERY(\"region.connection_id\",\n \"select * from information_schema.tables;\");\n\n -- List all columns in a table.\n SELECT * FROM EXTERNAL_QUERY(\"region.connection_id\",\n \"select * from information_schema.columns where table_name='x';\");\n\nTrack BigQuery federated queries\n--------------------------------\n\nWhen you run a federated query against AlloyDB,\nBigQuery annotates the query with a comment similar to the following: \n\n```\n/* Federated query from BigQuery. Project ID: PROJECT_ID, BigQuery Job ID: JOB_ID. */\n```\n\nIf you are monitoring logs for query usage, the following annotation can help you\nidentify queries coming from BigQuery.\n\n1. Go to the **Logs Explorer** page.\n\n [Go to the Logs Explorer](https://console.cloud.google.com/logs/query)\n2. In the **Query** tab, enter the following query:\n\n resource.type=\"alloydb.googleapis.com/Instance\"\n textPayload=~\"Federated query from BigQuery\"\n\n3. Click **Run query**.\n\n If there are records available for BigQuery federated queries,\n a list of records similar to the following appears in **Query results**. \n\n ```\n YYYY-MM-DD hh:mm:ss.millis UTC [3210064]: [4-1]\n db=DATABASE, user=USER_ACCOUNT\n STATEMENT: SELECT 1 FROM (SELECT FROM company_name_table) t;\n /* Federated query from BigQuery.\n Project ID: PROJECT_ID, BigQuery Job ID: JOB_ID\n */\n\n YYYY-MM-DD hh:mm:ss.millis UTC [3210532]: [2-1]\n db=DATABASE, user=USER_ACCOUNT\n STATEMENT: SELECT \"company_id\", \"company type_id\" FROM\n (SELECT FROM company_name_table) t;\n /* Federated query from BigQuery.\n Project ID: PROJECT_ID, BigQuery Job ID: JOB_ID\n */\n ```\n\n For more information about Cloud Logging, see [Cloud Logging](/logging/docs/view/logs-explorer-interface).\n\nTroubleshooting\n---------------\n\nThis section describes potential errors you might encounter when sending a\nfederated query to AlloyDB and provides possible troubleshooting\nresolutions.\n\n**Issue:** Failed to connect to the database server with this error:\n`Invalid table-valued function EXTERNAL_QUERY Connect to PostgreSQL server failed: server closed the connection unexpectedly. This probably means the server terminated abnormally before or while processing the request.`\n\n**Resolution:** Ensure that you used valid credentials and followed all prerequisites\nwhile creating the [connection to AlloyDB](/bigquery/docs/connect-to-alloydb#create-alloydb-connection).\nCheck if the service account that is automatically created\nwhen a connection to AlloyDB is\ncreated has the AlloyDB Client (`roles/alloydb.client`) role.\nFor more information, see\n[Grant access to the service account](/bigquery/docs/connect-to-alloydb#access-alloydb).\n\nWhat's next\n-----------\n\n- Learn about [federated queries](/bigquery/docs/federated-queries-intro).\n- Learn about [PostgreSQL to BigQuery data type mapping](/bigquery/docs/reference/standard-sql/federated_query_functions#postgresql_mapping).\n- Learn about [unsupported data types](/bigquery/docs/reference/standard-sql/federated_query_functions#unsupported_data_types)."]]