Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
872 views
in Technique[技术] by (71.8m points)

postgresql - Read data from a text file inside a trigger

Is there any way by which data on an text file can be accessed inside a trigger function?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use standard plpgsql functions for this.

COPY

If we are talking about CSV files (or similar) and the file is on the db server you use COPY. I quote the manual here.

COPY with a file name instructs the PostgreSQL server to directly read from or write to a file. The file must be accessible to the server and the name must be specified from the viewpoint of the server.

For more complex operations you might want to use a temporary table as staging table, COPY the file into it and work with plain SQL commands from there. Similar to this example. Be sure you don't run into conflicts with trying to create the same table in the same session multiple times, though ...

pg_read_file()

There are also generic file access functions. For security reasons their use is rather restricted:

Only files within the database cluster directory and the log_directory can be accessed. Use a relative path for files in the cluster directory, and a path matching the log_directory configuration setting for log files. Use of these functions is restricted to superusers.

I use this to read in small XML files and process inside PostgreSQL. Demo:

CREATE OR REPLACE FUNCTION f_import_from_file()
  RETURNS boolean AS
$BODY$
DECLARE
   myxml    xml;
   datafile text := 'path/relative/to/db_cluster/myfile.xml';
BEGIN

myxml := pg_read_file(datafile, 0, 10000000);   -- 10 MB max.

-- do stuff, like INSERT ...

$BODY$

You can overcome the path restriction with a symlink from your db directory to any other directory. Be wary of possible security implications, though.

file_fdw

Finally, you could use a foreign data wrapper to access data files in the server's file system. You need the additional module file_fdw for this. Install once per database with:

CREATE EXTENSION file_fdw;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...