PostgreSQL 14.5 pg_read_binary_file could not open file for reading: Invalid argument

448 Views Asked by At

Yesterday I installed PostgreSQL 14.5 on a Windows 10 laptop. I then ran an old script to load images into a table. The script uses the pg_read_binary_file function. Some of the images are .jpg files and some are .png files. Of the 34 files, only 5 were successfully processed (1 .jpg and 4 .png). The other 29 failed with the following error:

[Exception, Error code 0, SQLState XX000] ERROR: could not open file "file absolute path" for reading: Invalid argument

For instance, the following statement executes without errors

select pg_read_binary_file('C:\Users\Jorge\OneDrive\Documents\000\020-logos\adalid.png') as adalid_png;

... and the following statement fails

select pg_read_binary_file('C:\Users\Jorge\OneDrive\Documents\000\020-logos\oper.png') as oper_png;

... with the following error message

[Exception, Error code 0, SQLState XX000] ERROR: could not open file "C:/Users/Jorge/OneDrive/Documents/000/020-logos/oper.png" for reading: Invalid argument

So far, I have not been able to identify any difference in the files that could be the cause of the error. Also, I'm pretty sure the script works on earlier releases of version 14. Unfortunately I have not been able to find a website to download any of those earlier releases to test it again.

Has anyone else found this problem, and its solution?

1

There are 1 best solutions below

2
Jorge Campins On

I think the issue is somehow caused by OneDrive. This laptop is new. When I logged in with my Microsoft account, the OneDrive directory was automatically created and updated. Apparently this operation only updates the directory entries, leaving the contents of the files in the cloud until they are opened. When I zipped the directory that contains all my images, a message from OneDrive appeared saying that in that moment it will restore some files. After that, all the commands in my scripts work.

My theory is that pg_read_binary_file gets the file entry from the directory, so it doesn't give the "No such file or directory" message; but then fails reading the contents, giving the "Invalid argument" message instead.

The unanswered question would be: why does 7-Zip make OneDrive restore the files but pg_read_binary_file does not?

UPDATE

After more testing, and reading Save disk space with OneDrive Files On-Demand for Windows, now I am sure that pg_read_binary_file could fail and send the message "Invalid argument" when the OneDrive file is not a locally available file. In Windows File Explorer such file has a blue cloud icon next to it.