diff --git a/docs/user-guide/protocols/postgresql-copy-in.md b/docs/user-guide/protocols/postgresql-copy-in.md new file mode 100644 index 000000000..548920e3e --- /dev/null +++ b/docs/user-guide/protocols/postgresql-copy-in.md @@ -0,0 +1,168 @@ +--- +keywords: [PostgreSQL, COPY FROM STDIN, CSV, JSONL, InfluxDB, Arrow IPC, OTLP, Prometheus Remote Write] +description: Stream data into GreptimeDB over a PostgreSQL connection using COPY FROM STDIN. Choose a payload format, destination, and ingestion options. +--- + +# PostgreSQL COPY FROM STDIN + +If your application can reach GreptimeDB only through a database protocol, `COPY FROM STDIN` lets it stream data from a PostgreSQL client without opening an HTTP or gRPC connection. It also avoids converting each batch into SQL `INSERT` statements. In a [GreptimeDB ingestion benchmark](https://github.com/GreptimeTeam/greptimedb/pull/9297), COPY sustained 841,000 rows/s with 1,000-row batches versus 49,000 rows/s for PostgreSQL `INSERT` on the same workload: more than 10× the throughput. Results depend on the workload and batch size. + +[PostgreSQL developers have been working on extensible COPY formats](https://www.postgresql.org/message-id/20231204.153548.2126325458835528809.kou%40clear-code.com). GreptimeDB puts that idea into practice: an exporter can send an OTLP or Arrow payload through an existing PostgreSQL driver instead of implementing another transport. This gives client and tool authors a way to test new integrations without waiting for upstream PostgreSQL to support these formats. + +`STDIN` means the data comes from the client, not from a file on the server. The connection supplies the database and credentials. Options normally passed as HTTP headers or query parameters go in `WITH`. + +Send the `COPY` statement on its own, then write the payload through the client's COPY API and finish the copy. Specify `FORMAT` explicitly: neither PostgreSQL's default `text` format nor its native `binary` row format is supported. Use `option value`, not `option = value`. Enclose string values in single quotes. A boolean option written without a value means `true` (for example, `HEADER`). Unknown or duplicate options are rejected. On success the server returns `COPY n`, where `n` is the count reported by that ingestion interface. + +| `FORMAT` | What to send | Table name after `COPY` | Create the table first? | +| --- | --- | --- | --- | +| `csv` | CSV text | Required; optional column list | Yes | +| `jsonl` | One JSON object per line | Required; optional column list | Yes | +| `influxdb` | Line protocol text | Not supported; the measurement names the table | No, by default | +| `arrow` | One Arrow IPC stream | Required; no column list | Yes, even with `auto_create_table true` | +| `otlp` (metrics) | One uncompressed metrics export request | Not supported; the metric name determines the table | No, by default | +| `otlp` (logs or traces) | One uncompressed logs or traces export request | Optional; no column list | No, by default (including named targets) | +| `promwrite` | One compressed Remote Write protobuf request | Not supported; each series identifies its metric table | No, by default | + +InfluxDB, OTLP, and Remote Write buffer **one request per COPY**, up to 64 MiB of payload. The shared request-memory limit can reject a smaller request. For larger imports, send multiple complete requests in separate COPY operations; splitting the same payload across network writes does not bypass the limit. CSV, JSONL, and Arrow instead write batches as they arrive. If a later batch fails, earlier batches can remain in the table. + +## Python client example (Psycopg 3) + +This example writes two InfluxDB line protocol records through GreptimeDB's PostgreSQL port. You don't need to create a table first: `room_temperature` in the payload names the destination table. Install Psycopg 3: + +```shell +python -m pip install 'psycopg[binary]' +``` + +Run this script against a local GreptimeDB instance, or change the connection string for your deployment: + +```python +import psycopg + +with psycopg.connect("postgresql://greptime@localhost:4003/public", autocommit=True) as conn: + with conn.cursor() as cur: + with cur.copy("COPY FROM STDIN WITH (FORMAT influxdb, precision 'ms')") as copy: + copy.write(b"room_temperature,room=kitchen celsius=21.5 1735689600000\n") + copy.write(b"room_temperature,room=office celsius=22.0 1735689600000\n") + print(cur.statusmessage) # COPY 2 +``` + +Use `copy.write()` for data already encoded in the chosen format, not `copy.write_row()`, which encodes PostgreSQL rows. Both writes above belong to one COPY request. Leaving the `cur.copy()` block finishes the request; don't send the `\.` marker used by `psql`. + +## CSV + +Create the destination table first, then name it after `COPY`, as in this `psql` example. The line containing `\.` ends client input. `HEADER` skips the first record; it does not use the header names to reorder columns. List the columns in input order, or omit the list to use table order. + +```sql +CREATE TABLE readings (ts TIMESTAMP TIME INDEX, host STRING, temperature DOUBLE); +COPY readings (ts, host, temperature) FROM STDIN WITH (FORMAT csv, HEADER); +ts,host,temperature +2025-01-01T00:00:00Z,web01,21.5 +\. +SELECT * FROM readings; +``` + +| Option | Value and default | Meaning | +| --- | --- | --- | +| `HEADER` | Boolean; default `false` | Skip the first input record. | +| `DELIMITER` | One printable ASCII character or a tab; default `','` | Field separator. Must differ from `QUOTE`. | +| `QUOTE` | One printable ASCII character or a tab; default `'"'` | Encloses a quoted field. | +| `ESCAPE` | One printable ASCII character or a tab; defaults to `QUOTE` | Escapes a quote inside a quoted field. | +| `NULL` | Only `''` (default) | An empty field, even `""`, is NULL. Custom NULL markers are not supported. | + +Use date/time strings for timestamps. Strings without an offset use the session time zone. JSON and binary columns are not supported by the CSV decoder. + +## JSON Lines (JSONL) + +Name an existing table after `COPY` and send one JSON object per line. Keys match columns by name, not by position. For example, with the `readings` table from the CSV example, the second record sets `temperature` to NULL because the key is absent: + +```sql +COPY readings (ts, host, temperature) FROM STDIN WITH (FORMAT jsonl); +{"host":"web01","temperature":22.5,"ts":"2025-01-01T00:01:00Z"} +{"host":"web02","ts":"2025-01-01T00:01:01Z"} +\. +``` + +`json` is an alias for `jsonl`; it does not accept a JSON array of records. There are no `WITH` options beyond `FORMAT`. Values are decoded directly into the target columns without a pipeline. Timestamp strings without an offset use the session time zone. JSON and binary target columns are not supported. + +## InfluxDB line protocol + +Do not name a table after `COPY`. Each line names its measurement, and GreptimeDB creates missing measurement tables by default. Send the same text as an InfluxDB write body. `influxdb_line` is an alias for `influxdb`. This `psql` example writes to the `cpu` measurement: + +```sql +COPY FROM STDIN WITH (FORMAT influxdb, precision 'ms'); +cpu,host=web01 usage=0.75 1735689600000 +\. +``` + +| Option | Value and default | Meaning | +| --- | --- | --- | +| `precision` | `'ns'` (default) or `'n'`, `'us'` or `'u'`, `'ms'`, `'s'`, `'m'`, `'h'` | Unit of timestamps in the input. For example, the HTTP query parameter `?precision=ms` becomes `precision 'ms'`. | + +## Arrow IPC + +Send an Arrow IPC **stream** (schema followed by record batches), not an IPC file or a Flight protobuf envelope. Name an existing table after `COPY`. GreptimeDB uses the Arrow Flight bulk write path and matches fields by name. Do not supply a COPY column list. + +```sql +COPY readings FROM STDIN WITH (FORMAT arrow, auto_create_table true, insert_skip_wal false); +``` + +| Option | Value and default | Meaning | +| --- | --- | --- | +| `auto_create_table` | Boolean; omit to use the existing schema-on-write policy | Allow missing columns to be added. The target table must already exist even when this option is `true`. | +| `insert_skip_wal` | Boolean; omit to keep the current WAL setting | Set to `true` to skip the write-ahead log for this COPY, reducing durability. | + +COPY returns one final row count rather than Flight's per-batch acknowledgments. + +## OpenTelemetry (OTLP) + +Set `signal` to match the protobuf message you send: `'metrics'` (default) for `ExportMetricsServiceRequest`, `'logs'` for `ExportLogsServiceRequest`, or `'traces'` for `ExportTraceServiceRequest`. Send the serialized message itself, without HTTP headers, compression, or a gRPC frame. `otlp_metrics` is an alias for `otlp`. + +For example, given a Psycopg 3 cursor `cur` and an OpenTelemetry `ExportLogsServiceRequest` named `request`, send the raw bytes rather than using the driver's row encoder: + +```python +with cur.copy("COPY app_logs FROM STDIN WITH (FORMAT otlp, signal 'logs')") as copy: + copy.write(request.SerializeToString()) +``` + +| Option | Signal | Value and meaning | +| --- | --- | --- | +| `signal` | All | `'metrics'` (default), `'logs'`, or `'traces'`. Selects the protobuf message type. | +| `promote_all_resource_attrs` | Metrics | Boolean, default `false`. Promote all resource attributes except those listed in `resource_attrs`. | +| `resource_attrs` | Metrics | Semicolon-separated string, e.g. `'service.name;host.id'`. Lists attributes to keep, or to exclude if `promote_all_resource_attrs` is `true`. | +| `promote_scope_attrs` | Metrics | Boolean, default `false`. Promote scope attributes. | +| `translation_strategy` | Metrics | String, default `'UnderscoreEscapingWithSuffixes'`. See [accepted metric and label name translation strategies](/user-guide/ingest-data/for-observability/opentelemetry.md#prometheus-compatibility). | +| `table_name` | Logs, traces | String. Overrides the destination, like `x-greptime-log-table-name` or `x-greptime-trace-table-name`. See the routing rules below. | +| `pipeline_name` | Logs, traces | String. Selects a pipeline, like `x-greptime-pipeline-name`. Required for traces; omit for direct log ingestion. | +| `pipeline_version` | Logs, traces | String. Selects a pipeline version, like `x-greptime-pipeline-version`. | +| `pipeline_params` | Logs, traces | String, e.g. `'key1=value1&key2=value2'`. Passes parameters like `x-greptime-pipeline-params`. | +| `extract_keys` | Logs | Comma-separated string, e.g. `'host,env'`. Like `x-greptime-log-extract-keys`, extracts attributes during direct log ingestion. New columns are tags; existing columns keep their role (for example, a `host` field remains a field). | + +For metrics, do not name a table after `COPY`. GreptimeDB routes by metric name and creates missing tables by default. For example, to send the equivalent of the HTTP header `x-greptime-otlp-metric-promote-resource-attrs: service.name;host.id`: + +```sql +COPY FROM STDIN WITH (FORMAT otlp, signal 'metrics', resource_attrs 'service.name;host.id'); +``` + +For logs and traces, `table_name` overrides the table named after `COPY`; a qualified COPY target still selects the schema. If neither is given, the defaults are `opentelemetry_logs` and `opentelemetry_traces`. These tables need not exist beforehand. No signal accepts a column list. For example, to write spans to `app_traces` with the v2 trace pipeline: + +```sql +COPY app_traces FROM STDIN WITH (FORMAT otlp, signal 'traces', pipeline_name 'greptime_trace_v2'); +``` + +## Prometheus Remote Write + +Do not name a table after `COPY`. The metric name in each series determines the logical table; GreptimeDB creates missing metric tables by default. Use `physical_table` to select the backing table, not to override the series name. Send the same compressed protobuf body that you would POST to `/v1/prometheus/write`. Set `version` and `zstd` to match the bytes the client has already encoded. + +```sql +COPY FROM STDIN WITH (FORMAT promwrite, version 2, zstd, physical_table 'metrics'); +``` + +| Option | Value and default | Meaning | +| --- | --- | --- | +| `version` | `1` (default) or `2`; also accepts `'v1'` or `'v2'` | Remote Write protobuf message version. | +| `zstd` | Boolean; default `false` (Snappy) | Set to `true` for a Zstandard-compressed body, like `Content-Encoding: zstd`. | +| `physical_table` | Optional string | Like the HTTP `physical_table` query parameter, selects the backing physical table, not the logical table for each series. | +| `pipeline_name` | Optional string | Selects a pipeline for v1. | +| `pipeline_version` | Optional string | Selects the pipeline version for v1. | + +As on the HTTP endpoint, v2 does not apply pipelines. A v2 request with exemplars is rejected if `pipeline_name` is set. Setting only `pipeline_version` does not trigger this check. diff --git a/docs/user-guide/protocols/postgresql.md b/docs/user-guide/protocols/postgresql.md index d80426bcb..421e38cd0 100644 --- a/docs/user-guide/protocols/postgresql.md +++ b/docs/user-guide/protocols/postgresql.md @@ -24,7 +24,7 @@ Please refer to [Table Management](/user-guide/deployments-administration/manage ## Ingest data -Please refer to [SQL](/user-guide/ingest-data/for-iot/sql.md). +Use [SQL INSERT](/user-guide/ingest-data/for-iot/sql.md) to write rows, or [PostgreSQL COPY FROM STDIN](postgresql-copy-in.md) to stream CSV, JSONL, InfluxDB line protocol, Arrow IPC, OTLP, or Prometheus Remote Write data over the same connection. ## Query data diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/user-guide/protocols/postgresql-copy-in.md b/i18n/zh/docusaurus-plugin-content-docs/current/user-guide/protocols/postgresql-copy-in.md new file mode 100644 index 000000000..b94fc03e9 --- /dev/null +++ b/i18n/zh/docusaurus-plugin-content-docs/current/user-guide/protocols/postgresql-copy-in.md @@ -0,0 +1,168 @@ +--- +keywords: [PostgreSQL, COPY FROM STDIN, CSV, JSONL, InfluxDB, Arrow IPC, OTLP, Prometheus Remote Write] +description: 通过 PostgreSQL 连接使用 COPY FROM STDIN 向 GreptimeDB 写入数据,了解各格式的数据编码、目标表和写入选项。 +--- + +# PostgreSQL COPY FROM STDIN + +如果应用只能通过数据库协议访问 GreptimeDB,可以用 `COPY FROM STDIN` 从 PostgreSQL 客户端流式写入数据,无需另建 HTTP 或 gRPC 连接,也不用把每批数据转换成 SQL `INSERT` 语句。在 [GreptimeDB 的写入基准测试](https://github.com/GreptimeTeam/greptimedb/pull/9297)中,每批写入 1,000 行时,COPY 的吞吐量为每秒 84.1 万行,而相同负载下通过 PostgreSQL 协议执行 `INSERT` 为每秒 4.9 万行,前者是后者的 10 倍以上。实际吞吐量取决于负载和批次大小。 + +[PostgreSQL 社区正在探索如何让 COPY 支持可扩展的格式](https://www.postgresql.org/message-id/20231204.153548.2126325458835528809.kou%40clear-code.com)。GreptimeDB 把这个想法付诸实践:例如,导出工具可以用已有的 PostgreSQL 驱动直接发送 OTLP 或 Arrow 数据,无需再实现一套传输协议。客户端和工具开发者不必等待 PostgreSQL 上游支持这些格式,就可以开发并验证新的集成方式。 + +`STDIN` 表示数据来自客户端,而不是服务器上的文件。数据库和身份凭据由连接指定,原本通过 HTTP 请求头或 URL 参数传入的选项写在 `WITH` 子句中。 + +单独执行一条 `COPY` 语句,然后通过客户端的 COPY API 发送数据并结束传输。必须显式指定 `FORMAT`:目前不支持 PostgreSQL 默认的 `text` 格式和原生的 `binary` 行格式。选项写作 `选项名 值`,中间不加等号。字符串值用单引号括起。布尔选项可以省略值,表示 `true`(例如 `HEADER`)。未知选项或重复指定的选项会导致报错。成功后服务器返回 `COPY n`,其中 `n` 为相应写入接口返回的计数。 + +| `FORMAT` | 发送的数据 | `COPY` 后是否指定表名 | 是否需要提前建表 | +| --- | --- | --- | --- | +| `csv` | CSV 文本 | 必须指定,可附带列名列表 | 需要 | +| `jsonl` | 每行一个 JSON 对象 | 必须指定,可附带列名列表 | 需要 | +| `influxdb` | 行协议文本 | 不支持,由 measurement 决定表名 | 默认不需要 | +| `arrow` | 一个 Arrow IPC 流 | 必须指定,不支持列名列表 | 需要,即使设置了 `auto_create_table true` | +| `otlp`(指标) | 一条未经压缩的指标导出请求 | 不支持,由指标名称决定表名 | 默认不需要 | +| `otlp`(日志或链路) | 一条未经压缩的日志或链路导出请求 | 可选,不支持列名列表 | 默认不需要,指定表名时也一样 | +| `promwrite` | 一条压缩后的 Remote Write protobuf 请求 | 不支持,由各时间序列的指标名称决定表名 | 默认不需要 | + +对于 InfluxDB、OTLP 和 Remote Write,**每次 COPY 缓冲一个完整请求**,发送的数据最多为 64 MiB。共享的请求内存限额也可能使较小的请求被拒绝。数据量较大时,请拆成多个完整请求,分别执行 COPY;将同一请求分多次发送不能绕过限制。CSV、JSONL 和 Arrow 则按批次写入,后续批次失败时,先前的批次可能已经写入表中。 + +## Python 客户端示例(Psycopg 3) + +下面的示例通过 GreptimeDB 的 PostgreSQL 端口写入两条 InfluxDB 行协议记录。无需提前建表,记录中的 `room_temperature` 就是目标表名。先安装 Psycopg 3: + +```shell +python -m pip install 'psycopg[binary]' +``` + +以下脚本连接本地 GreptimeDB 实例。要连接其他实例,请修改连接字符串: + +```python +import psycopg + +with psycopg.connect("postgresql://greptime@localhost:4003/public", autocommit=True) as conn: + with conn.cursor() as cur: + with cur.copy("COPY FROM STDIN WITH (FORMAT influxdb, precision 'ms')") as copy: + copy.write(b"room_temperature,room=kitchen celsius=21.5 1735689600000\n") + copy.write(b"room_temperature,room=office celsius=22.0 1735689600000\n") + print(cur.statusmessage) # COPY 2 +``` + +如果数据已经按所选格式编码,请使用 `copy.write()`,不要使用会按 PostgreSQL 行格式编码的 `copy.write_row()`。示例中的两次写入属于同一个 COPY 请求。退出 `cur.copy()` 代码块即结束传输,不需要发送 `psql` 使用的 `\.` 标记。 + +## CSV + +先创建目标表,再在 `COPY` 后指定表名,如下方的 `psql` 示例所示。单独一行 `\.` 表示输入结束。`HEADER` 会跳过首条记录,但不会按表头名称调整列顺序。请按输入顺序指定列名;省略列名列表时,使用表定义中的列顺序。 + +```sql +CREATE TABLE readings (ts TIMESTAMP TIME INDEX, host STRING, temperature DOUBLE); +COPY readings (ts, host, temperature) FROM STDIN WITH (FORMAT csv, HEADER); +ts,host,temperature +2025-01-01T00:00:00Z,web01,21.5 +\. +SELECT * FROM readings; +``` + +| 选项 | 取值与默认值 | 含义 | +| --- | --- | --- | +| `HEADER` | 布尔值,默认 `false` | 跳过首条输入记录。 | +| `DELIMITER` | 单个可打印 ASCII 字符或制表符,默认 `','` | 字段分隔符,不能与 `QUOTE` 相同。 | +| `QUOTE` | 单个可打印 ASCII 字符或制表符,默认 `'"'` | 用于包围字段的引号字符。 | +| `ESCAPE` | 单个可打印 ASCII 字符或制表符,默认为 `QUOTE` 的值 | 用于转义字段内部的引号。 | +| `NULL` | 仅支持 `''`(默认) | 空字段(包括 `""`)会被解析为 NULL。不支持自定义 NULL 标记。 | + +时间戳请使用日期时间字符串。未指定偏移量时使用会话时区。CSV 不支持写入 JSON 列和二进制列。 + +## JSON Lines(JSONL) + +在 `COPY` 后指定一张已有的表,每行发送一个 JSON 对象。键名与列名匹配,键的顺序不影响写入。以下示例沿用 CSV 示例中的 `readings` 表。第二条记录没有 `temperature` 键,因此该列写入 NULL: + +```sql +COPY readings (ts, host, temperature) FROM STDIN WITH (FORMAT jsonl); +{"host":"web01","temperature":22.5,"ts":"2025-01-01T00:01:00Z"} +{"host":"web02","ts":"2025-01-01T00:01:01Z"} +\. +``` + +`json` 是 `jsonl` 的别名,同样不接受由多条记录组成的 JSON 数组。`WITH` 子句中除 `FORMAT` 外没有其他选项。数据按目标列类型直接解析,不经过 Pipeline。时间戳字符串未指定偏移量时使用会话时区。不支持写入 JSON 列和二进制列。 + +## InfluxDB 行协议 + +不能在 `COPY` 后指定表名。每行数据中的 measurement 就是目标表名;表不存在时,GreptimeDB 默认会自动建表。发送与 InfluxDB 写入请求体相同的文本。`influxdb_line` 是 `influxdb` 的别名。以下示例在 `psql` 中向 `cpu` 写入一条记录: + +```sql +COPY FROM STDIN WITH (FORMAT influxdb, precision 'ms'); +cpu,host=web01 usage=0.75 1735689600000 +\. +``` + +| 选项 | 取值与默认值 | 含义 | +| --- | --- | --- | +| `precision` | `'ns'`(默认)或 `'n'`、`'us'` 或 `'u'`、`'ms'`、`'s'`、`'m'`、`'h'` | 输入时间戳的单位。例如,HTTP 查询参数 `?precision=ms` 对应 COPY 选项 `precision 'ms'`。 | + +## Arrow IPC + +发送 Arrow IPC **流**(先发送 schema,再发送记录批次),而不是 IPC 文件或 Flight protobuf 消息。必须在 `COPY` 后指定已有表。GreptimeDB 使用 Arrow Flight 批量写入接口,按 IPC 字段名匹配目标列。不支持在 COPY 语句中指定列名列表。 + +```sql +COPY readings FROM STDIN WITH (FORMAT arrow, auto_create_table true, insert_skip_wal false); +``` + +| 选项 | 取值与默认值 | 含义 | +| --- | --- | --- | +| `auto_create_table` | 布尔值,省略时沿用现有的写入时 schema 演进策略 | 允许添加缺少的列。即使设为 `true`,目标表也必须已经存在。 | +| `insert_skip_wal` | 布尔值,省略时沿用现有 WAL 设置 | 设为 `true` 会在此次 COPY 中跳过预写日志,降低数据持久性。 | + +与 Flight 按批次确认不同,COPY 只在结束时返回总行数。 + +## OpenTelemetry (OTLP) + +`signal` 必须与发送的 protobuf 消息类型一致:`'metrics'`(默认)对应 `ExportMetricsServiceRequest`,`'logs'` 对应 `ExportLogsServiceRequest`,`'traces'` 对应 `ExportTraceServiceRequest`。直接发送序列化后的消息,不要压缩,也不要附加 HTTP 请求头或 gRPC 帧头。`otlp_metrics` 是 `otlp` 的别名。 + +例如,假设 `cur` 是 Psycopg 3 游标,`request` 是 OpenTelemetry 的 `ExportLogsServiceRequest`。直接写入消息字节,不要使用驱动的行编码接口: + +```python +with cur.copy("COPY app_logs FROM STDIN WITH (FORMAT otlp, signal 'logs')") as copy: + copy.write(request.SerializeToString()) +``` + +| 选项 | 信号 | 取值与含义 | +| --- | --- | --- | +| `signal` | 全部 | `'metrics'`(默认)、`'logs'` 或 `'traces'`。选择 protobuf 消息类型。 | +| `promote_all_resource_attrs` | 指标 | 布尔值,默认 `false`。设为 `true` 时保留除 `resource_attrs` 所列属性之外的全部资源属性。 | +| `resource_attrs` | 指标 | 用分号分隔的字符串,例如 `'service.name;host.id'`。列出要保留的属性;当 `promote_all_resource_attrs` 为 `true` 时,则列出要排除的属性。 | +| `promote_scope_attrs` | 指标 | 布尔值,默认 `false`。指定是否保留 scope 属性。 | +| `translation_strategy` | 指标 | 字符串,默认 `'UnderscoreEscapingWithSuffixes'`。可选值见[指标和标签名称的转换策略](/user-guide/ingest-data/for-observability/opentelemetry.md#兼容-prometheus)。 | +| `table_name` | 日志、链路 | 字符串。覆盖目标表名,对应 `x-greptime-log-table-name` 或 `x-greptime-trace-table-name`。目标表的选择规则见下文。 | +| `pipeline_name` | 日志、链路 | 字符串。选择 Pipeline,对应 `x-greptime-pipeline-name`。写入链路时必须指定;直接写入日志时可省略。 | +| `pipeline_version` | 日志、链路 | 字符串。选择 Pipeline 版本,对应 `x-greptime-pipeline-version`。 | +| `pipeline_params` | 日志、链路 | 字符串,例如 `'key1=value1&key2=value2'`。传递 Pipeline 参数,对应 `x-greptime-pipeline-params`。 | +| `extract_keys` | 日志 | 用逗号分隔的字符串,例如 `'host,env'`。对应 `x-greptime-log-extract-keys`,在直接写入日志时提取指定属性。新建的列为标签列;已有列保留原有角色,例如已有的 `host` 字段列不会变成标签列。 | + +写入指标时不能在 `COPY` 后指定表名。GreptimeDB 按指标名称选择目标表,默认会在表不存在时创建它。例如,HTTP 请求头 `x-greptime-otlp-metric-promote-resource-attrs: service.name;host.id` 可改写为: + +```sql +COPY FROM STDIN WITH (FORMAT otlp, signal 'metrics', resource_attrs 'service.name;host.id'); +``` + +对于日志和链路,`table_name` 优先于 `COPY` 后的表名;如果在 `COPY` 目标中指定了 schema,该 schema 仍然有效。两者均未指定时,日志和链路分别写入 `opentelemetry_logs` 和 `opentelemetry_traces`。这些表无需提前创建。所有信号均不支持指定列名列表。例如,使用 v2 链路 Pipeline 将 span 写入 `app_traces`: + +```sql +COPY app_traces FROM STDIN WITH (FORMAT otlp, signal 'traces', pipeline_name 'greptime_trace_v2'); +``` + +## Prometheus Remote Write + +不能在 `COPY` 后指定表名。每个时间序列的指标名称决定其逻辑表;指标表不存在时,GreptimeDB 默认会自动建表。`physical_table` 选项用于选择底层物理表,不会覆盖时间序列的名称。发送与 HTTP POST 到 `/v1/prometheus/write` 相同的压缩 protobuf 请求体。`version` 和 `zstd` 必须与客户端实际使用的编码一致。 + +```sql +COPY FROM STDIN WITH (FORMAT promwrite, version 2, zstd, physical_table 'metrics'); +``` + +| 选项 | 取值与默认值 | 含义 | +| --- | --- | --- | +| `version` | `1`(默认)或 `2`,也支持 `'v1'` 或 `'v2'` | Remote Write protobuf 消息版本。 | +| `zstd` | 布尔值,默认 `false`(Snappy) | 对于 Zstandard 压缩的请求体,设为 `true`,对应 `Content-Encoding: zstd`。 | +| `physical_table` | 可选字符串 | 对应同名 HTTP 查询参数,选择存储指标的物理表,而不是每个时间序列对应的逻辑表。 | +| `pipeline_name` | 可选字符串 | 为 v1 选择 Pipeline。 | +| `pipeline_version` | 可选字符串 | 为 v1 选择 Pipeline 版本。 | + +与 HTTP 接口一样,v2 不执行 Pipeline。若 v2 请求包含 exemplar,指定 `pipeline_name` 会导致请求被拒绝。仅指定 `pipeline_version` 不会触发这项检查。 diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/user-guide/protocols/postgresql.md b/i18n/zh/docusaurus-plugin-content-docs/current/user-guide/protocols/postgresql.md index 8f7adb501..a4b1d4718 100644 --- a/i18n/zh/docusaurus-plugin-content-docs/current/user-guide/protocols/postgresql.md +++ b/i18n/zh/docusaurus-plugin-content-docs/current/user-guide/protocols/postgresql.md @@ -23,7 +23,7 @@ psql -h -p 4003 -U -d public ## 写入数据 -请参考[写入数据](/user-guide/ingest-data/for-iot/sql.md). +可以使用 [SQL INSERT](/user-guide/ingest-data/for-iot/sql.md) 写入行数据,也可以在同一连接上使用 [PostgreSQL COPY FROM STDIN](postgresql-copy-in.md) 传输 CSV、JSONL、InfluxDB 行协议、Arrow IPC、OTLP 或 Prometheus Remote Write 数据。 ## 读取数据 diff --git a/sidebars.ts b/sidebars.ts index ed357a117..8ae956df6 100644 --- a/sidebars.ts +++ b/sidebars.ts @@ -197,6 +197,7 @@ const sidebars: SidebarsConfig = { 'user-guide/protocols/opentelemetry', 'user-guide/protocols/mysql', 'user-guide/protocols/postgresql', + 'user-guide/protocols/postgresql-copy-in', 'user-guide/protocols/http', 'user-guide/protocols/grpc', 'user-guide/protocols/opentsdb',