From b408d19491e1c04d3651865c2307c86c86223ca8 Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Wed, 18 Nov 2026 15:05:50 -0800 Subject: [PATCH 1/4] docs: document PostgreSQL COPY FROM STDIN ingestion --- .../protocols/postgresql-copy-in.md | 147 ++++++++++++++++++ docs/user-guide/protocols/postgresql.md | 2 +- .../protocols/postgresql-copy-in.md | 147 ++++++++++++++++++ .../user-guide/protocols/postgresql.md | 2 +- sidebars.ts | 1 + 5 files changed, 297 insertions(+), 2 deletions(-) create mode 100644 docs/user-guide/protocols/postgresql-copy-in.md create mode 100644 i18n/zh/docusaurus-plugin-content-docs/current/user-guide/protocols/postgresql-copy-in.md 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..3054a1c46 --- /dev/null +++ b/docs/user-guide/protocols/postgresql-copy-in.md @@ -0,0 +1,147 @@ +--- +keywords: [PostgreSQL, COPY FROM STDIN, CSV, 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 + +`COPY FROM STDIN` reads data from the PostgreSQL **client**, not from a file on the server. Use it to send CSV rows or to reuse an InfluxDB, Arrow, OTLP, or Prometheus payload without switching from PostgreSQL to HTTP or gRPC. The connection supplies the database and credentials. Options that the ingestion interface would normally receive as headers or query parameters go in `WITH`, using the option names listed below. + +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 | What follows `COPY` | +| --- | --- | --- | +| `csv` | CSV text | Existing table, optionally followed by a column list | +| `influxdb` | Line protocol text | No table; each measurement names its table | +| `arrow` | One Arrow IPC stream | Existing table; IPC field names identify the columns | +| `otlp` | One uncompressed protobuf export request | No table for metrics; optional table for logs or traces | +| `promwrite` | One compressed Remote Write protobuf request | No table; the series identify the metric tables | + +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 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 + +Try this in `psql`. 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` | Single-byte character; default `','` | Field separator. Must differ from `QUOTE`. | +| `QUOTE` | Single-byte character; default `'"'` | Encloses a quoted field. | +| `ESCAPE` | Single-byte character; 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. + +## InfluxDB line protocol + +Send the same text as an InfluxDB write body. For example, the HTTP query parameter `?precision=ms` becomes `precision 'ms'`. This `psql` example writes to the `cpu` measurement: + +```sql +COPY FROM STDIN WITH (FORMAT influxdb, precision 'ms'); +cpu,host=web01 usage=0.75 1735689600000 +\. +``` + +`precision` accepts `ns` (default) or `n`, `us` or `u`, `ms`, `s`, `m`, and `h`. `influxdb_line` is an alias for `influxdb`. + +## Arrow IPC + +Send an Arrow IPC **stream** (schema followed by record batches), not an IPC file or a Flight protobuf envelope. 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`, promotes attributes to tags when not using a pipeline. | + +Metrics route by metric name. 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 + +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, and supplying pipeline options with exemplars is rejected. diff --git a/docs/user-guide/protocols/postgresql.md b/docs/user-guide/protocols/postgresql.md index d80426bcb..2d56c5fa5 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, 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..8e2ed4db1 --- /dev/null +++ b/i18n/zh/docusaurus-plugin-content-docs/current/user-guide/protocols/postgresql-copy-in.md @@ -0,0 +1,147 @@ +--- +keywords: [PostgreSQL, COPY FROM STDIN, CSV, InfluxDB, Arrow IPC, OTLP, Prometheus Remote Write] +description: 通过 PostgreSQL 连接使用 COPY FROM STDIN 向 GreptimeDB 写入数据,了解各格式的数据编码、目标表和写入选项。 +--- + +# PostgreSQL COPY FROM STDIN + +`COPY FROM STDIN` 从 PostgreSQL **客户端**读取数据,而不是读取服务器上的文件。除了 CSV 行数据,还可以通过同一连接发送 InfluxDB、Arrow、OTLP 或 Prometheus 数据,无需切换到 HTTP 或 gRPC。数据库和身份凭据由连接指定。原写入接口通过请求头或 URL 参数接收的选项,在 `WITH` 子句中使用下文列出的名称传入。 + +单独执行一条 `COPY` 语句,然后通过客户端的 COPY API 发送数据并结束传输。必须显式指定 `FORMAT`:目前不支持 PostgreSQL 默认的 `text` 格式和原生的 `binary` 行格式。选项写作 `选项名 值`,中间不加等号。字符串值用单引号括起。布尔选项可以省略值,表示 `true`(例如 `HEADER`)。未知选项或重复指定的选项会导致报错。成功后服务器返回 `COPY n`,其中 `n` 为相应写入接口返回的计数。 + +| `FORMAT` | 发送的数据 | `COPY` 后如何指定目标 | +| --- | --- | --- | +| `csv` | CSV 文本 | 必须指定已有表,可附带列名列表 | +| `influxdb` | 行协议文本 | 不指定表,由每条记录的 measurement 决定表名 | +| `arrow` | 一个 Arrow IPC 流 | 必须指定已有表,列名由 IPC 字段名决定 | +| `otlp` | 一条未经压缩的 protobuf 导出请求 | 指标不能指定表,日志和链路可以指定表 | +| `promwrite` | 一条压缩后的 Remote Write protobuf 请求 | 不指定表,由时间序列决定指标表 | + +对于 InfluxDB、OTLP 和 Remote Write,**每次 COPY 缓冲一个完整请求**,发送的数据最多为 64 MiB。共享的请求内存限额也可能使较小的请求被拒绝。数据量较大时,请拆成多个完整请求,分别执行 COPY;将同一请求分多次发送不能绕过限制。CSV 和 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 + +在 `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` | 单字节字符,默认 `','` | 字段分隔符,不能与 `QUOTE` 相同。 | +| `QUOTE` | 单字节字符,默认 `'"'` | 用于包围字段的引号字符。 | +| `ESCAPE` | 单字节字符,默认为 `QUOTE` 的值 | 用于转义字段内部的引号。 | +| `NULL` | 仅支持 `''`(默认) | 空字段(包括 `""`)会被解析为 NULL。不支持自定义 NULL 标记。 | + +时间戳请使用日期时间字符串。未指定偏移量时使用会话时区。CSV 不支持写入 JSON 列和二进制列。 + +## InfluxDB 行协议 + +发送与 InfluxDB 写入请求体相同的文本。例如,HTTP 查询参数 `?precision=ms` 对应 COPY 选项 `precision 'ms'`。以下示例在 `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`。`influxdb_line` 是 `influxdb` 的别名。 + +## Arrow IPC + +发送 Arrow IPC **流**(先发送 schema,再发送记录批次),而不是 IPC 文件或 Flight protobuf 消息。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`,不使用 Pipeline 时将列出的属性提取为标签。 | + +指标按指标名称写入对应的表。例如,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 + +发送与 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,如果请求中包含 exemplar,同时指定 Pipeline 选项会导致报错。 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..104b67340 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、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', From fbadc83771e0f707c98b9d50466c4d3c3b9b81b3 Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Wed, 18 Nov 2026 15:19:35 -0800 Subject: [PATCH 2/4] docs: preview JSONL COPY and tabulate InfluxDB precision --- .../protocols/postgresql-copy-in.md | 24 ++++++++++++++++--- .../protocols/postgresql-copy-in.md | 24 ++++++++++++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/docs/user-guide/protocols/postgresql-copy-in.md b/docs/user-guide/protocols/postgresql-copy-in.md index 3054a1c46..be2f195b5 100644 --- a/docs/user-guide/protocols/postgresql-copy-in.md +++ b/docs/user-guide/protocols/postgresql-copy-in.md @@ -1,5 +1,5 @@ --- -keywords: [PostgreSQL, COPY FROM STDIN, CSV, InfluxDB, Arrow IPC, OTLP, Prometheus Remote Write] +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. --- @@ -12,6 +12,7 @@ Send the `COPY` statement on its own, then write the payload through the client' | `FORMAT` | What to send | What follows `COPY` | | --- | --- | --- | | `csv` | CSV text | Existing table, optionally followed by a column list | +| `jsonl` (pending) | One JSON object per line | Existing table, optionally followed by a column list | | `influxdb` | Line protocol text | No table; each measurement names its table | | `arrow` | One Arrow IPC stream | Existing table; IPC field names identify the columns | | `otlp` | One uncompressed protobuf export request | No table for metrics; optional table for logs or traces | @@ -65,9 +66,24 @@ SELECT * FROM readings; 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) + +**Availability:** JSONL requires a build with [GreptimeDB PR #10109](https://github.com/GreptimeTeam/greptimedb/pull/10109), which has not yet merged into Nightly. + +Send one JSON object per line to an existing table. 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:00Z"} +\. +``` + +`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 -Send the same text as an InfluxDB write body. For example, the HTTP query parameter `?precision=ms` becomes `precision 'ms'`. This `psql` example writes to the `cpu` measurement: +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'); @@ -75,7 +91,9 @@ cpu,host=web01 usage=0.75 1735689600000 \. ``` -`precision` accepts `ns` (default) or `n`, `us` or `u`, `ms`, `s`, `m`, and `h`. `influxdb_line` is an alias for `influxdb`. +| 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 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 index 8e2ed4db1..fd03889bc 100644 --- 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 @@ -1,5 +1,5 @@ --- -keywords: [PostgreSQL, COPY FROM STDIN, CSV, InfluxDB, Arrow IPC, OTLP, Prometheus Remote Write] +keywords: [PostgreSQL, COPY FROM STDIN, CSV, JSONL, InfluxDB, Arrow IPC, OTLP, Prometheus Remote Write] description: 通过 PostgreSQL 连接使用 COPY FROM STDIN 向 GreptimeDB 写入数据,了解各格式的数据编码、目标表和写入选项。 --- @@ -12,6 +12,7 @@ description: 通过 PostgreSQL 连接使用 COPY FROM STDIN 向 GreptimeDB 写 | `FORMAT` | 发送的数据 | `COPY` 后如何指定目标 | | --- | --- | --- | | `csv` | CSV 文本 | 必须指定已有表,可附带列名列表 | +| `jsonl`(尚未合入) | 每行一个 JSON 对象 | 必须指定已有表,可附带列名列表 | | `influxdb` | 行协议文本 | 不指定表,由每条记录的 measurement 决定表名 | | `arrow` | 一个 Arrow IPC 流 | 必须指定已有表,列名由 IPC 字段名决定 | | `otlp` | 一条未经压缩的 protobuf 导出请求 | 指标不能指定表,日志和链路可以指定表 | @@ -65,9 +66,24 @@ SELECT * FROM readings; 时间戳请使用日期时间字符串。未指定偏移量时使用会话时区。CSV 不支持写入 JSON 列和二进制列。 +## JSON Lines(JSONL) + +**可用范围:** JSONL 需要使用包含 [GreptimeDB PR #10109](https://github.com/GreptimeTeam/greptimedb/pull/10109) 的构建版本。该 PR 尚未合入 Nightly。 + +向已有表写入数据,每行发送一个 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:00Z"} +\. +``` + +`json` 是 `jsonl` 的别名,同样不接受由多条记录组成的 JSON 数组。`WITH` 子句中除 `FORMAT` 外没有其他选项。数据按目标列类型直接解析,不经过 Pipeline。时间戳字符串未指定偏移量时使用会话时区。不支持写入 JSON 列和二进制列。 + ## InfluxDB 行协议 -发送与 InfluxDB 写入请求体相同的文本。例如,HTTP 查询参数 `?precision=ms` 对应 COPY 选项 `precision 'ms'`。以下示例在 `psql` 中向 `cpu` 写入一条记录: +发送与 InfluxDB 写入请求体相同的文本。`influxdb_line` 是 `influxdb` 的别名。以下示例在 `psql` 中向 `cpu` 写入一条记录: ```sql COPY FROM STDIN WITH (FORMAT influxdb, precision 'ms'); @@ -75,7 +91,9 @@ cpu,host=web01 usage=0.75 1735689600000 \. ``` -`precision` 支持 `ns`(默认)或 `n`、`us` 或 `u`、`ms`、`s`、`m` 和 `h`。`influxdb_line` 是 `influxdb` 的别名。 +| 选项 | 取值与默认值 | 含义 | +| --- | --- | --- | +| `precision` | `'ns'`(默认)或 `'n'`、`'us'` 或 `'u'`、`'ms'`、`'s'`、`'m'`、`'h'` | 输入时间戳的单位。例如,HTTP 查询参数 `?precision=ms` 对应 COPY 选项 `precision 'ms'`。 | ## Arrow IPC From ecdbe30ca0848d7718e19f9c3020c1d843ac4e5e Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Wed, 18 Nov 2026 15:57:28 -0800 Subject: [PATCH 3/4] docs: explain COPY ingestion rationale and table requirements --- .../protocols/postgresql-copy-in.md | 39 ++++++++++--------- docs/user-guide/protocols/postgresql.md | 2 +- .../protocols/postgresql-copy-in.md | 39 ++++++++++--------- .../user-guide/protocols/postgresql.md | 2 +- 4 files changed, 44 insertions(+), 38 deletions(-) diff --git a/docs/user-guide/protocols/postgresql-copy-in.md b/docs/user-guide/protocols/postgresql-copy-in.md index be2f195b5..48c337fac 100644 --- a/docs/user-guide/protocols/postgresql-copy-in.md +++ b/docs/user-guide/protocols/postgresql-copy-in.md @@ -5,20 +5,25 @@ description: Stream data into GreptimeDB over a PostgreSQL connection using COPY # PostgreSQL COPY FROM STDIN -`COPY FROM STDIN` reads data from the PostgreSQL **client**, not from a file on the server. Use it to send CSV rows or to reuse an InfluxDB, Arrow, OTLP, or Prometheus payload without switching from PostgreSQL to HTTP or gRPC. The connection supplies the database and credentials. Options that the ingestion interface would normally receive as headers or query parameters go in `WITH`, using the option names listed below. +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 | What follows `COPY` | -| --- | --- | --- | -| `csv` | CSV text | Existing table, optionally followed by a column list | -| `jsonl` (pending) | One JSON object per line | Existing table, optionally followed by a column list | -| `influxdb` | Line protocol text | No table; each measurement names its table | -| `arrow` | One Arrow IPC stream | Existing table; IPC field names identify the columns | -| `otlp` | One uncompressed protobuf export request | No table for metrics; optional table for logs or traces | -| `promwrite` | One compressed Remote Write protobuf request | No table; the series identify the metric tables | +| `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 and Arrow instead write batches as they arrive. If a later batch fails, earlier batches can remain in the table. +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) @@ -45,7 +50,7 @@ Use `copy.write()` for data already encoded in the chosen format, not `copy.writ ## CSV -Try this in `psql`. 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. +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); @@ -68,9 +73,7 @@ Use date/time strings for timestamps. Strings without an offset use the session ## JSON Lines (JSONL) -**Availability:** JSONL requires a build with [GreptimeDB PR #10109](https://github.com/GreptimeTeam/greptimedb/pull/10109), which has not yet merged into Nightly. - -Send one JSON object per line to an existing table. 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: +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); @@ -83,7 +86,7 @@ COPY readings (ts, host, temperature) FROM STDIN WITH (FORMAT jsonl); ## InfluxDB line protocol -Send the same text as an InfluxDB write body. `influxdb_line` is an alias for `influxdb`. This `psql` example writes to the `cpu` measurement: +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'); @@ -97,7 +100,7 @@ cpu,host=web01 usage=0.75 1735689600000 ## Arrow IPC -Send an Arrow IPC **stream** (schema followed by record batches), not an IPC file or a Flight protobuf envelope. GreptimeDB uses the Arrow Flight bulk write path and matches fields by name. Do not supply a COPY column list. +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); @@ -134,7 +137,7 @@ with cur.copy("COPY app_logs FROM STDIN WITH (FORMAT otlp, signal 'logs')") as c | `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`, promotes attributes to tags when not using a pipeline. | -Metrics route by metric name. For example, to send the equivalent of the HTTP header `x-greptime-otlp-metric-promote-resource-attrs: service.name;host.id`: +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'); @@ -148,7 +151,7 @@ COPY app_traces FROM STDIN WITH (FORMAT otlp, signal 'traces', pipeline_name 'gr ## Prometheus Remote Write -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. +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'); diff --git a/docs/user-guide/protocols/postgresql.md b/docs/user-guide/protocols/postgresql.md index 2d56c5fa5..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 -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, InfluxDB line protocol, Arrow IPC, OTLP, or Prometheus Remote Write data over the same connection. +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 index fd03889bc..4adcc9936 100644 --- 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 @@ -5,20 +5,25 @@ description: 通过 PostgreSQL 连接使用 COPY FROM STDIN 向 GreptimeDB 写 # PostgreSQL COPY FROM STDIN -`COPY FROM STDIN` 从 PostgreSQL **客户端**读取数据,而不是读取服务器上的文件。除了 CSV 行数据,还可以通过同一连接发送 InfluxDB、Arrow、OTLP 或 Prometheus 数据,无需切换到 HTTP 或 gRPC。数据库和身份凭据由连接指定。原写入接口通过请求头或 URL 参数接收的选项,在 `WITH` 子句中使用下文列出的名称传入。 +如果应用只能通过数据库协议访问 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 流 | 必须指定已有表,列名由 IPC 字段名决定 | -| `otlp` | 一条未经压缩的 protobuf 导出请求 | 指标不能指定表,日志和链路可以指定表 | -| `promwrite` | 一条压缩后的 Remote Write protobuf 请求 | 不指定表,由时间序列决定指标表 | +| `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 和 Arrow 则按批次写入,后续批次失败时,先前的批次可能已经写入表中。 +对于 InfluxDB、OTLP 和 Remote Write,**每次 COPY 缓冲一个完整请求**,发送的数据最多为 64 MiB。共享的请求内存限额也可能使较小的请求被拒绝。数据量较大时,请拆成多个完整请求,分别执行 COPY;将同一请求分多次发送不能绕过限制。CSV、JSONL 和 Arrow 则按批次写入,后续批次失败时,先前的批次可能已经写入表中。 ## Python 客户端示例(Psycopg 3) @@ -45,7 +50,7 @@ with psycopg.connect("postgresql://greptime@localhost:4003/public", autocommit=T ## CSV -在 `psql` 中运行以下示例,单独一行 `\.` 表示输入结束。`HEADER` 会跳过首条记录,但不会按表头名称调整列顺序。请按输入顺序指定列名;省略列名列表时,使用表定义中的列顺序。 +先创建目标表,再在 `COPY` 后指定表名,如下方的 `psql` 示例所示。单独一行 `\.` 表示输入结束。`HEADER` 会跳过首条记录,但不会按表头名称调整列顺序。请按输入顺序指定列名;省略列名列表时,使用表定义中的列顺序。 ```sql CREATE TABLE readings (ts TIMESTAMP TIME INDEX, host STRING, temperature DOUBLE); @@ -68,9 +73,7 @@ SELECT * FROM readings; ## JSON Lines(JSONL) -**可用范围:** JSONL 需要使用包含 [GreptimeDB PR #10109](https://github.com/GreptimeTeam/greptimedb/pull/10109) 的构建版本。该 PR 尚未合入 Nightly。 - -向已有表写入数据,每行发送一个 JSON 对象。键名与列名匹配,键的顺序不影响写入。以下示例沿用 CSV 示例中的 `readings` 表。第二条记录没有 `temperature` 键,因此该列写入 NULL: +在 `COPY` 后指定一张已有的表,每行发送一个 JSON 对象。键名与列名匹配,键的顺序不影响写入。以下示例沿用 CSV 示例中的 `readings` 表。第二条记录没有 `temperature` 键,因此该列写入 NULL: ```sql COPY readings (ts, host, temperature) FROM STDIN WITH (FORMAT jsonl); @@ -83,7 +86,7 @@ COPY readings (ts, host, temperature) FROM STDIN WITH (FORMAT jsonl); ## InfluxDB 行协议 -发送与 InfluxDB 写入请求体相同的文本。`influxdb_line` 是 `influxdb` 的别名。以下示例在 `psql` 中向 `cpu` 写入一条记录: +不能在 `COPY` 后指定表名。每行数据中的 measurement 就是目标表名;表不存在时,GreptimeDB 默认会自动建表。发送与 InfluxDB 写入请求体相同的文本。`influxdb_line` 是 `influxdb` 的别名。以下示例在 `psql` 中向 `cpu` 写入一条记录: ```sql COPY FROM STDIN WITH (FORMAT influxdb, precision 'ms'); @@ -97,7 +100,7 @@ cpu,host=web01 usage=0.75 1735689600000 ## Arrow IPC -发送 Arrow IPC **流**(先发送 schema,再发送记录批次),而不是 IPC 文件或 Flight protobuf 消息。GreptimeDB 使用 Arrow Flight 批量写入接口,按 IPC 字段名匹配目标列。不支持在 COPY 语句中指定列名列表。 +发送 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); @@ -134,7 +137,7 @@ with cur.copy("COPY app_logs FROM STDIN WITH (FORMAT otlp, signal 'logs')") as c | `pipeline_params` | 日志、链路 | 字符串,例如 `'key1=value1&key2=value2'`。传递 Pipeline 参数,对应 `x-greptime-pipeline-params`。 | | `extract_keys` | 日志 | 用逗号分隔的字符串,例如 `'host,env'`。对应 `x-greptime-log-extract-keys`,不使用 Pipeline 时将列出的属性提取为标签。 | -指标按指标名称写入对应的表。例如,HTTP 请求头 `x-greptime-otlp-metric-promote-resource-attrs: service.name;host.id` 可改写为: +写入指标时不能在 `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'); @@ -148,7 +151,7 @@ COPY app_traces FROM STDIN WITH (FORMAT otlp, signal 'traces', pipeline_name 'gr ## Prometheus Remote Write -发送与 HTTP POST 到 `/v1/prometheus/write` 相同的压缩 protobuf 请求体。`version` 和 `zstd` 必须与客户端实际使用的编码一致。 +不能在 `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'); 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 104b67340..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 ## 写入数据 -可以使用 [SQL INSERT](/user-guide/ingest-data/for-iot/sql.md) 写入行数据,也可以在同一连接上使用 [PostgreSQL COPY FROM STDIN](postgresql-copy-in.md) 传输 CSV、InfluxDB 行协议、Arrow IPC、OTLP 或 Prometheus Remote Write 数据。 +可以使用 [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 数据。 ## 读取数据 From d0e0fc63d851d81109b8b4bc45124c4c3b82396d Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Mon, 23 Nov 2026 22:38:28 -0800 Subject: [PATCH 4/4] docs: correct PostgreSQL COPY examples and option semantics --- docs/user-guide/protocols/postgresql-copy-in.md | 12 ++++++------ .../user-guide/protocols/postgresql-copy-in.md | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/user-guide/protocols/postgresql-copy-in.md b/docs/user-guide/protocols/postgresql-copy-in.md index 48c337fac..548920e3e 100644 --- a/docs/user-guide/protocols/postgresql-copy-in.md +++ b/docs/user-guide/protocols/postgresql-copy-in.md @@ -64,9 +64,9 @@ SELECT * FROM readings; | Option | Value and default | Meaning | | --- | --- | --- | | `HEADER` | Boolean; default `false` | Skip the first input record. | -| `DELIMITER` | Single-byte character; default `','` | Field separator. Must differ from `QUOTE`. | -| `QUOTE` | Single-byte character; default `'"'` | Encloses a quoted field. | -| `ESCAPE` | Single-byte character; defaults to `QUOTE` | Escapes a quote inside a quoted field. | +| `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. @@ -78,7 +78,7 @@ Name an existing table after `COPY` and send one JSON object per line. Keys matc ```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:00Z"} +{"host":"web02","ts":"2025-01-01T00:01:01Z"} \. ``` @@ -135,7 +135,7 @@ with cur.copy("COPY app_logs FROM STDIN WITH (FORMAT otlp, signal 'logs')") as c | `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`, promotes attributes to tags when not using a pipeline. | +| `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`: @@ -165,4 +165,4 @@ COPY FROM STDIN WITH (FORMAT promwrite, version 2, zstd, physical_table 'metrics | `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, and supplying pipeline options with exemplars is rejected. +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/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 index 4adcc9936..b94fc03e9 100644 --- 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 @@ -64,9 +64,9 @@ SELECT * FROM readings; | 选项 | 取值与默认值 | 含义 | | --- | --- | --- | | `HEADER` | 布尔值,默认 `false` | 跳过首条输入记录。 | -| `DELIMITER` | 单字节字符,默认 `','` | 字段分隔符,不能与 `QUOTE` 相同。 | -| `QUOTE` | 单字节字符,默认 `'"'` | 用于包围字段的引号字符。 | -| `ESCAPE` | 单字节字符,默认为 `QUOTE` 的值 | 用于转义字段内部的引号。 | +| `DELIMITER` | 单个可打印 ASCII 字符或制表符,默认 `','` | 字段分隔符,不能与 `QUOTE` 相同。 | +| `QUOTE` | 单个可打印 ASCII 字符或制表符,默认 `'"'` | 用于包围字段的引号字符。 | +| `ESCAPE` | 单个可打印 ASCII 字符或制表符,默认为 `QUOTE` 的值 | 用于转义字段内部的引号。 | | `NULL` | 仅支持 `''`(默认) | 空字段(包括 `""`)会被解析为 NULL。不支持自定义 NULL 标记。 | 时间戳请使用日期时间字符串。未指定偏移量时使用会话时区。CSV 不支持写入 JSON 列和二进制列。 @@ -78,7 +78,7 @@ SELECT * FROM readings; ```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:00Z"} +{"host":"web02","ts":"2025-01-01T00:01:01Z"} \. ``` @@ -135,7 +135,7 @@ with cur.copy("COPY app_logs FROM STDIN WITH (FORMAT otlp, signal 'logs')") as c | `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`,不使用 Pipeline 时将列出的属性提取为标签。 | +| `extract_keys` | 日志 | 用逗号分隔的字符串,例如 `'host,env'`。对应 `x-greptime-log-extract-keys`,在直接写入日志时提取指定属性。新建的列为标签列;已有列保留原有角色,例如已有的 `host` 字段列不会变成标签列。 | 写入指标时不能在 `COPY` 后指定表名。GreptimeDB 按指标名称选择目标表,默认会在表不存在时创建它。例如,HTTP 请求头 `x-greptime-otlp-metric-promote-resource-attrs: service.name;host.id` 可改写为: @@ -165,4 +165,4 @@ COPY FROM STDIN WITH (FORMAT promwrite, version 2, zstd, physical_table 'metrics | `pipeline_name` | 可选字符串 | 为 v1 选择 Pipeline。 | | `pipeline_version` | 可选字符串 | 为 v1 选择 Pipeline 版本。 | -与 HTTP 接口一样,v2 不执行 Pipeline,如果请求中包含 exemplar,同时指定 Pipeline 选项会导致报错。 +与 HTTP 接口一样,v2 不执行 Pipeline。若 v2 请求包含 exemplar,指定 `pipeline_name` 会导致请求被拒绝。仅指定 `pipeline_version` 不会触发这项检查。