Repository navigation
Description
Is your feature request related to a problem? Please describe.
Assuming we have our infrastructure for detecting the changes/modifications in the cluster during a range of LSNs with us (Refer #649). We now want to extract those changes (either the complete changed file or better only the changed blocks of a file) from the postgres server.
Describe the solution you'd like
A clear solution path is yet to be decided but here are some breadcrumbs for some potential approaches:
- Get the database cluster directory and using
relfilenodeinformation copy the associated file. [require SUPERUSER role] - Leverage the
pgmoneta_extfunctions to receive a file. [require SUPERUSER permissions] - Perform a base backup, keep it as a local temporary copy and then extract relevant files and blocks out of it and prune the local base backup copy. [slow compute]
Solution
Approach-1
Use the admin function pg_read_binary_file() which is used to provide native access to files on the machine hosting the server. [Some sources claim it works only for local cluster, have to check]
Function Signature:
pg_read_binary_file(filename text [, offset bigint, length bigint ] [, missing_ok boolean ] ) → bytea
Limitations:
- Only files within the database cluster directory and the
log_directorycan be accessed. - By default, only
SUPERUSERcan execute this, but some non-root user with rolepg_read_server_filescan also perform this command. - Grant users the
EXECUTEprivilege onpg_read_binary_file(), when granting privilege on these functions, note that the table entries showing optional parameters are mostly implemented as several physical functions with different parameter lists. Privilege must be granted separately on each such function, if it is to be used (therefore:EXECUTEonpg_read_binary_file(text)is different fromEXECUTEonpg_read_binary_file(text, bigint, bigint, boolean))
Highlights:
- Since the function can accept
offsetandlengthas parameters, we can request a block (modified) from the relation file. [Most Desired] - Don't really need the absolute path to database cluster, relative path inside the database cluster can be used to make a query for example
pg_read_binary_file('base/1/112')will work.
A glimpse:
- GRANT the required roles and permissions to
repluser.
GRANT pg_read_server_files TO repl;
GRANT EXECUTE on function pg_read_binary_file(text, bigint, bigint, boolean) to repl;
GRANT EXECUTE on function pg_read_binary_file(text) to repl;
This means we can use pg_read_binary_file either with all parameters (text, bigint, bigint, boolean) or with single parameter (text)
- Run the command (to extract first 100 bytes from the file 'base/1/112')
SELECT pg_read_binary_file('base/1/112', 0, 100, false);
Output:
pg_read_binary_file
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\x00000000e0170001000000004800f01ff01f0420000000006231050004000000000000000000000000000000000000000000000000000000000000000000f0bf010000000000000000000000000000000000000000000000000000000000000000000000
(1 row)
- Transform the
byteaoutput to relevant format for storage
bytea internally stores the raw binary format, but send the data as hex when are queried. Therefore we might have to decode the hex into binary for storage.
For example when the a field stores the following data: 01001000 01100101 01101100 01101100 01101111 as bytea is queried, the server will send \x48656c6c6f to the client, This hex is created by breaking the binary octet into half and converting each half to hex.
Resources:
- https://www.postgresql.org/docs/current/datatype-binary.html
- https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-GENFILE
- https://pgpedia.info/p/pg_read_binary_file.html
Credits:
I want to thank:
I only found this solution because of the discussions that followed.
@jesperpedersen, @Jubilee101 PTAL
Activity
Jubilee101 commentedon Jul 31, 2025
Hi @ashu3103 , extension is the way to go. And I don't think you need superuser to fetch files. Think of extension as a part of postgres and it in fact has unlimited access to postgres server data. So we get to define what roles users should assume to access the data through the extension. I think for our extension all you need is the
replattribute. If you need to you can also expand on that with predefined roles: https://www.postgresql.org/docs/current/predefined-roles.htmlashu3103 commentedon Jul 31, 2025
Hi @Jubilee101,
The function of interest here is
pgmoneta_ext_receive_file_chunk()frompgmoneta_extwhich helps client receive the chunks of a file from the server.In the
doc/manual/user-02-functions.md- it's written, we needSUPERUSERprivilege to execute this then I went on to the implementation and there, we are enforcingSUPERUSERprivilege for the functionmaybe we can remove this check and see if we really require SUPERUSER role.
Jubilee101 commentedon Jul 31, 2025
I see. In that case we can loosen the requirement. Can you check if
pg_read_server_filesis enough? Feel free to submit a PR on the extension side, Jesper and I will review it.jesperpedersen commentedon Jul 31, 2025
pg_read_server_filesis 14+ so we need security checks per function that checks the role against the server versionashu3103 commentedon Jul 31, 2025
There is this page https://www.postgresql.org/docs/13/default-roles.html which lists the Default Roles for the users, this contains the
pg_read_server_files.I think they have just changed the title of the page from default to predefined for 14+
ashu3103 commentedon Jul 31, 2025
I can also explore some other admin functions (refer this) like -
pg_read_binary_file ( filename text [, offset bigint, length bigint ] [, missing_ok boolean ] ) → byteaThis function can also accept offset and length so maybe we can do a block query too!
jesperpedersen commentedon Jul 31, 2025
@ashu3103 Ok, so go in the
pg_read_server_filesrouteJubilee101 commentedon Jul 31, 2025
Sounds promising, let us know if you get it to work -- ideally without need for superuser.
ashu3103 commentedon Aug 1, 2025
Hi, @jesperpedersen, @Jubilee101
Shall I prepare a patch for this approach?
jesperpedersen commentedon Aug 1, 2025
@ashu3103 It is necessary to
GRANTonpg_read_binary_fileif the user has the role ?But, yes, go in this direction
ashu3103 commentedon Aug 1, 2025
Yes, it's necessary to explicitly
GRANTthe user on this function.Okay, on it!!
[pgmoneta#709] Request a relation file from the postgres server
[pgmoneta#709] Request a relation file from the postgres server
ashu3103 commentedon Aug 20, 2025
Hi @jesperpedersen, @Jubilee101,
Any other task remaining with this issue?
Jubilee101 commentedon Aug 21, 2025
I think this is good for now. Let's open another issue if we find ourselves in need of other solutions.