在 Postgres 数据库中存储用户生成的报告

Storing User Generated Reports in Postgres DB

我正在寻找一些关于将数据生成的报告存储在我的 Postgresql 数据库中的建议。我想为用户提供将输出存储为 csv 的选项,但我也希望能够将其保存在数据库中。前者不是什么大问题,但对于后者,我不确定将 csv 文件作为二进制文件 (bytea) 存储在数据库中还是将内容存储在行中会更好。我愿意听取有关完成这项工作的最佳方式的建议。

我想说这完全取决于您对如何处理数据库中“报告”的要求以及报告的大小限制。

如果您确定报告永远不会超过 1GB,那么您可以将完整的报告存储在 text 列中(我不会将 bytea 用于文本数据)。所以你会得到一个简单的 table:

create table report
(
  id integer primary key,
  title text not null,
  created_at timestamp not null,
  content text not null
);

另一种选择是存储报告的每个 这样您就不会受到整个报告可能的大小限制的影响(只有一行的限制)

create table report
(
  id integer not null, 
  title text not null
  created_at timestamp not null,
);

create table report_content
(
  report_id integer not null references report,
  line_no integer not null,
  content text not null, 
  primary key (report_id, line_no)
);