Skip to content

Request a relation file from the server #709

Closed

Description

@ashu3103

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 relfilenode information copy the associated file. [require SUPERUSER role]
  • Leverage the pgmoneta_ext functions 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_directory can be accessed.
  • By default, only SUPERUSER can execute this, but some non-root user with role pg_read_server_files can also perform this command.
  • Grant users the EXECUTE privilege on pg_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: EXECUTE on pg_read_binary_file(text) is different from EXECUTE on pg_read_binary_file(text, bigint, bigint, boolean))

Highlights:

  • Since the function can accept offset and length as 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 repl user.
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 bytea output 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:

Credits:

I want to thank:

I only found this solution because of the discussions that followed.

@jesperpedersen, @Jubilee101 PTAL

Activity

  1. self-assigned this
    on Jul 30, 2025
  2. Jubilee101 commented on Jul 31, 2025

    @Jubilee101
    Member

    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 repl attribute. If you need to you can also expand on that with predefined roles: https://www.postgresql.org/docs/current/predefined-roles.html

  3. ashu3103 commented on Jul 31, 2025

    @ashu3103
    CollaboratorAuthor

    Hi @Jubilee101,

    And I don't think you need superuser to fetch files

    The function of interest here is pgmoneta_ext_receive_file_chunk() from pgmoneta_ext which helps client receive the chunks of a file from the server.

    In the doc/manual/user-02-functions.md - it's written, we need SUPERUSER privilege to execute this then I went on to the implementation and there, we are enforcing SUPERUSER privilege for the function

    if (privileges & PRIVILEGE_SUPERUSER)
    {
        // do something
    }
    else
    {
       // error
    }
    

    maybe we can remove this check and see if we really require SUPERUSER role.

  4. Jubilee101 commented on Jul 31, 2025

    @Jubilee101
    Member

    I see. In that case we can loosen the requirement. Can you check if pg_read_server_files is enough? Feel free to submit a PR on the extension side, Jesper and I will review it.

  5. jesperpedersen commented on Jul 31, 2025

    @jesperpedersen
    Member

    pg_read_server_files is 14+ so we need security checks per function that checks the role against the server version

  6. ashu3103 commented on Jul 31, 2025

    @ashu3103
    CollaboratorAuthor

    pg_read_server_files is 14+ so we need security checks per function that checks the role against the server version

    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+

  7. ashu3103 commented on Jul 31, 2025

    @ashu3103
    CollaboratorAuthor

    I can also explore some other admin functions (refer this) like -

    pg_read_binary_file ( filename text [, offset bigint, length bigint ] [, missing_ok boolean ] ) → bytea

    This function can also accept offset and length so maybe we can do a block query too!

  8. jesperpedersen commented on Jul 31, 2025

    @jesperpedersen
    Member

    @ashu3103 Ok, so go in the pg_read_server_files route

  9. Jubilee101 commented on Jul 31, 2025

    @Jubilee101
    Member

    This function can also accept offset and length so maybe we can do a block query too!

    Sounds promising, let us know if you get it to work -- ideally without need for superuser.

  10. ashu3103 commented on Aug 1, 2025

    @ashu3103
    CollaboratorAuthor

    Approach-1

    Hi, @jesperpedersen, @Jubilee101

    Shall I prepare a patch for this approach?

  11. jesperpedersen commented on Aug 1, 2025

    @jesperpedersen
    Member

    @ashu3103 It is necessary to GRANT on pg_read_binary_file if the user has the role ?

    But, yes, go in this direction

  12. ashu3103 commented on Aug 1, 2025

    @ashu3103
    CollaboratorAuthor

    @ashu3103 It is necessary to GRANT on pg_read_binary_file if the user has the role ?

    Yes, it's necessary to explicitly GRANT the user on this function.

    But, yes, go in this direction

    Okay, on it!!

  13. added 2 commits that reference this issue on Aug 9, 2025
  14. ashu3103 commented on Aug 20, 2025

    @ashu3103
    CollaboratorAuthor

    Hi @jesperpedersen, @Jubilee101,

    Any other task remaining with this issue?

  15. Jubilee101 commented on Aug 21, 2025

    @Jubilee101
    Member

    Any other task remaining with this issue?

    I think this is good for now. Let's open another issue if we find ourselves in need of other solutions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

feature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions