Updated on 2025-08-05 GMT+08:00

DBE_FILE

The DBE_FILE package provides the capabilities of reading and writing OS text files for stored procedures.

API Description

Table 1 lists all APIs supported by the DBE_FILE package.

Table 1 DBE_FILE

API

Description

DBE_FILE.OPEN

Opens a file based on the specified directory and file name.

DBE_FILE.IS_CLOSE

Checks whether a file is closed.

DBE_FILE.IS_OPEN

Checks whether a file is opened.

DBE_FILE.READ_LINE

Reads a line of a specified length from an open file handle.

DBE_FILE.WRITE

Writes data to an open file.

DBE_FILE.NEW_LINE

Writes one or more line terminators to an open file.

DBE_FILE.WRITE_LINE

Writes data to an open file and automatically appends a line terminator.

DBE_FILE.FORMAT_WRITE

Writes data in a specified format to the buffer of an open file.

DBE_FILE.GET_RAW

Reads RAW data from an open file.

DBE_FILE.PUT_RAW

Writes RAW data to an open file.

DBE_FILE.FLUSH

Writes cached data to a physical file.

DBE_FILE.CLOSE

Closes an open file handle.

DBE_FILE.CLOSE_ALL

Closes all file handles opened in a session.

DBE_FILE.REMOVE

Deletes a disk file. To perform this operation, you must have required permissions.

DBE_FILE.RENAME

Renames a disk file, which is similar to the mv command of Unix.

DBE_FILE.COPY

Copies data in a continuous area to a new file. If start_line and end_line are omitted, the entire file is copied.

DBE_FILE.GET_ATTR

Reads and returns the attributes of a disk file.

DBE_FILE.SEEK

Adjusts the position of a file pointer forward or backward based on the specified number of bytes.

DBE_FILE.GET_POS

Returns the current offset of a file, in bytes.

  • DBE_FILE.OPEN

    This function opens a file. You can specify the maximum number of characters in each line. A maximum of 50 files can be opened at a time. This function returns a handle of the INTEGER type.

    The prototype of the DBE_FILE.OPEN function is as follows:

    1
    2
    3
    4
    5
    6
    DBE_FILE.OPEN (
    dir             IN    VARCHAR2,
    file_name       IN    VARCHAR2,
    open_mode       IN    VARCHAR2,
    max_line_size   IN    INTEGER DEFAULT 1024)
    RETURN INTEGER;
    
    Table 2 DBE_FILE.OPEN API parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    dir

    VARCHAR2

    IN

    No

    Directory of a file. It is a string, indicating an object name.

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use an advanced package to read and write files in the file path specified by safe_data_path.

    file_name

    VARCHAR2

    IN

    No

    File name with an extension (file type), excluding the path name. A path contained in a file name is ignored in the OPEN function. In Unix, the file name cannot end with the combination of a slash and a dot (/.).

    open_mode

    VARCHAR2

    IN

    No

    File opening mode, including r (read), w (write), and a (append).

    NOTE:

    For the write operation, the system checks the file type. If the ELF file is written, an error is reported and the system exits.

    max_line_size

    INTEGER

    IN

    Yes

    Maximum number of characters in each line, including newline characters. The minimum value is 1 and the maximum is 32767. If this parameter is not specified, the default value 1024 is used.

  • DBE_FILE.IS_CLOSE

    Checks whether a file handle is closed. A Boolean value is returned. If an invalid file handle is detected, the INVALID_FILEHANDLE exception is thrown.

    The prototype of the DBE_FILE.IS_CLOSE function is as follows:

    1
    2
    3
    DBE_FILE.IS_CLOSE (
    file      IN     INTEGER)
    RETURN BOOLEAN;
    
    Table 3 DBE_FILE.IS_CLOSE parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    file IN INTEGER

    INTEGER

    IN

    Yes

    Passes a file handle to be detected.

  • DBE_FILE.READ_LINE

    Reads data from an open file and stores the read result to the buffer. It reads data to the end of each line excluding the line terminator, to the end of the file, or to the size specified by the len parameter. The length of the data to be read cannot exceed the value of max_line_size specified when the file is opened.

    The prototype of the DBE_FILE.READ_LINE function is as follows:

    1
    2
    3
    4
    DBE_FILE.READ_LINE (
    file      IN   INTEGER,
    buffer    OUT  VARCHAR2,
    len       IN   INTEGER DEFAULT NULL)
    
    Table 4 DBE_FILE.READ_LINE parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    file

    INTEGER

    IN

    No

    File handle opened by calling the OPEN function. The file must be opened in read mode. Otherwise, the INVALID_OPERATION exception is thrown.

    buffer

    VARCHAR2

    OUT

    No

    Buffer used to receive data.

    len

    INTEGE

    IN

    Yes

    Number of bytes read from a file. The default value is NULL. If the default value NULL is used, max_line_size is used to specify the line size.

  • DBE_FILE.WRITE

    Writes buffer data to the buffer corresponding to a file. The file must be opened in write mode. This operation does not write a line terminator.

    The prototype of the DBE_FILE.WRITE function is as follows:

    1
    2
    3
    4
    DBE_FILE.WRITE (
    file     IN     INTEGER,
    buffer   IN     TEXT)
    RETURN BOOLEAN;;
    
    Table 5 DBE_FILE.WRITE parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    file

    INTEGER

    IN

    No

    This stored procedure writes data in the buffer to a file. The file must be opened in write mode. Line terminators are not written.

    buffer

    TEXT

    IN

    Yes

    Text data to be written to a file. The maximum buffer size is 32767 bytes. If no value is specified in the open state, the default value is 1024 bytes. Before the writing is performed, the buffer occupied by WRITE operations cannot exceed 32767 bytes.

    NOTE:

    For the write operation, the system checks the file type. If the ELF file is written, an error is reported and the system exits.

  • DBE_FILE.NEW_LINE

    Writes one or more line terminators to the buffer corresponding to a file. The line terminators are related to the platform used.

    The prototype of the DBE_FILE.NEW_LINE function is as follows:

    1
    2
    3
    4
    DBE_FILE.NEW_LINE (
    file         IN     INTEGER,
    line_nums    IN     INTEGER := 1)
    RETURN BOOLEAN;
    
    Table 6 DBE_FILE.NEW_LINE parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    file

    INTEGER

    IN

    No

    File handle opened using OPEN.

    line_nums

    INTEGER

    IN

    Yes

    Number of terminators written to a file. The default value is 1.

  • DBE_FILE.WRITE_LINE

    Writes buffer data to the buffer corresponding to a file. The file must be opened in write mode. This operation automatically adds a line terminator.

    The prototype of the DBE_FILE.WRITE_LINE function is as follows:

    1
    2
    3
    4
    5
    DBE_FILE.WRITE_LINE(
    file       IN     INTEGER,
    buffer     IN     TEXT,
    flush      IN     BOOLEAN DEFAULT FALSE)
    RETURN BOOLEAN;
    
    Table 7 DBE_FILE.WRITE_LINE parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    file

    INTEGER

    IN

    No

    File handle opened using OPEN.

    buffer

    TEXT

    IN

    Yes

    Text data to be written to a file. The maximum buffer size is 32767 bytes. If no value is specified during the open state, the default value is 1024 bytes. Before the writing is performed, the buffer occupied by PUT operations cannot exceed 32767 bytes.

    NOTE:

    For the write operation, the system checks the file type. If the ELF file is written, an error is reported and the system exits.

    flush

    BOOLEAN

    IN

    Yes

    Specifies whether to flush data to the disk after the writing.

  • DBE_FILE.FORMAT_WRITE

    Writes formatted data to the buffer corresponding to an open file. It is a DBE_FILE.WRITE API that allows formatting.

    The prototype of the DBE_FILE.FORMAT_WRITE function is as follows:

    1
    2
    3
    4
    5
    6
    7
    DBE_FILE.FORMAT_WRITE (
    file   IN INTEGER,
    format IN VARCHAR2,
    arg1  IN VARCHAR2 DEFAULT NULL,
    . . .
    arg6 IN VARCHAR2 DEFAULT NULL])
    RETURN BOOLEAN;
    
    Table 8 DBE_FILE.FORMAT_WRITE parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    file

    INTEGER

    IN

    No

    File handle opened using OPEN.

    format

    VARCHAR2

    IN

    Yes

    A string to be formatted, containing the text and format characters \n and %s.

    [arg1. . .arg6]

    VARCHAR2

    IN

    Yes

    Six optional parameters. The parameters and the positions of characters to be formatted are in one-to-one correspondence. If the parameter corresponding to a character to be formatted is not provided, an empty string is used to replace %s.

  • DBE_FILE.GET_RAW

    Reads RAW data from an open file, stores the read result in the buffer, and returns the result from r.

    The prototype of the DBE_FILE.GET_RAW function is as follows:

    1
    2
    3
    4
    DBE_FILE.GET_RAW (
    file    IN   INTEGER,
    r       OUT  RAW,
    length  IN   INTEGER DEFAULT NULL);
    
    Table 9 DBE_FILE.GET_RAW parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    file

    INTEGER

    IN

    No

    File handle opened using OPEN.

    r

    RAW

    OUT

    No

    Output binary data.

    length

    INTEGER

    IN

    Yes

    Length of the file to be read. The default value is NULL. All data in the file is read. The maximum length is 1 GB.

  • DBE_FILE.PUT_RAW

    Writes raw data to the buffer corresponding to a file.

    The prototype of the DBE_FILE.PUT_RAW function is as follows:

    1
    2
    3
    4
    5
    DBE_FILE.PUT_RAW (
    file    IN  INTEGER,
    r       IN  RAW,
    flush   IN  BOOLEAN DEFAULT FALSE)
    RETURN BOOLEAN;
    
    Table 10 DBE_FILE.PUT_RAW parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    file

    INTEGER

    IN

    No

    File handle opened using OPEN.

    r

    RAW

    IN

    No

    Output binary data.

    NOTE:

    For the write operation, the system checks the file type. If the ELF file is written, an error is reported and the system exits.

    flush

    BOOLEAN

    IN

    Yes

    Specifies whether to flush data to a file. The default value is false.

  • DBE_FILE.FLUSH

    Writes cached data to a physical file. The cached data must have a line terminator.

    The prototype of the DBE_FILE.FLUSH function is as follows:

    1
    2
    3
    DBE_FILE.FLUSH (
    file     IN     INTEGER)
    RETURN VOID;
    
    Table 11 DBE_FILE.FLUSH parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    file

    INTEGER

    IN

    No

    File handle opened using OPEN.

  • DBE_FILE.CLOSE

    Closes an open file. When this function is called, if there is cached data to be written, exception information may be received.

    The prototype of the DBE_FILE.CLOSE function is as follows:

    1
    2
    3
    DBE_FILE.CLOSE (
    file IN INTEGER
    )RETURN INTEGER;
    
    Table 12 DBE_FILE.CLOSE parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    file

    INTEGER

    IN

    No

    File handle opened using OPEN.

  • DBE_FILE.CLOSE_ALL

    Closes all file handles opened in a session. This function can be used for emergency cleanup.

    The prototype of the DBE_FILE.CLOSE_ALL function is as follows:

    1
    2
    DBE_FILE.CLOSE_ALL()
    RETRUN VOID;
    
    Table 13 DBE_FILE.CLOSE_ALL API parameters

    Parameter

    Description

    None.

    None.

  • DBE_FILE.REMOVE

    Deletes a disk file. To use this function, you must have the required permission.

    The prototype of the DBE_FILE.REMOVE function is as follows:

    1
    2
    3
    4
    DBE_FILE.REMOVE (
    dir           IN     VARCHAR2,
    file_name     IN     VARCHAR2)
    RETURN VOID;
    
    Table 14 DBE_FILE.REMOVE parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    dir

    VARCHAR2

    IN

    No

    Directory of a file. It is a string, indicating an object name.

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use an advanced package to read and write files in the file path specified by safe_data_path.

    file_name

    VARCHAR2

    IN

    No

    File name.

  • DBE_FILE.RENAME

    Renames a disk file. This function is similar to the mv command of Unix.

    The prototype of the DBE_FILE.RENAME function is as follows:

    1
    2
    3
    4
    5
    6
    7
    DBE_FILE.RENAME (
    src_dir        IN   VARCHAR2,
    src_file_name  IN   VARCHAR2,
    dest_dir       IN   VARCHAR2,
    dest_file_name IN   VARCHAR2,
    overwrite      IN   BOOLEAN DEFAULT FALSE)
    RETURN VOID;
    
    Table 15 DBE_FILE.RENAME parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    src_dir

    VARCHAR2

    IN

    No

    Directory of the original file (case-sensitive)

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use an advanced package to read and write files in the file path specified by safe_data_path.

    src_file_name

    VARCHAR2

    IN

    No

    Original file to be renamed

    dest_dir

    VARCHAR2

    IN

    No

    Target directory (case-sensitive).

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use an advanced package to read and write files in the file path specified by safe_data_path.

    dest_file_name

    VARCHAR2

    IN

    No

    New file name

    overwrite

    BOOLEAN

    IN

    Yes

    Specifies whether to overwrite the file. If the parameter is left empty or not specified, the parameter is not overwritten. If a file with the same name exists in the target directory, the file will not be overwritten.

  • DBE_FILE.COPY

    Copies data in a continuous area to a new file. If start_line and end_line are omitted, the entire file is copied.

    The prototype of the DBE_FILE.COPY function is as follows:

    1
    2
    3
    4
    5
    6
    7
    8
    DBE_FILE.COPY (
    src_dir           IN     VARCHAR2,
    src_file_name     IN     VARCHAR2,
    dest_dir          IN     VARCHAR2,
    dest_file_name    IN     VARCHAR2,
    start_line        IN     INTEGER DEFAULT 1,
    end_line          IN     INTEGER DEFAULT NULL)
    RETURN VOID;
    
    Table 16 DBE_FILE.COPY parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    src_dir

    VARCHAR2

    IN

    No

    Directory of the original file

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use an advanced package to read and write files in the file path specified by safe_data_path.

    src_file_name

    VARCHAR2

    IN

    No

    File to be copied.

    dest_dir

    VARCHAR2

    IN

    No

    Directory of the destination file

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use an advanced package to read and write files in the file path specified by safe_data_path.

    dest_file_name

    VARCHAR2

    IN

    No

    Target file

    NOTE:

    For the write operation, the system checks the file type. If the ELF file is written, an error is reported and the system exits.

    start_line

    INTEGER

    IN

    No

    Number of the line where the copy starts. The default value is 1.

    end_line

    INTEGER

    IN

    Yes

    Number of the line where the copy ends. The default value is NULL, indicating the end of the file.

  • DBE_FILE.GET_ATTR

    Reads and returns the attributes of a disk file.

    The prototype of the DBE_FILE.GET_ATTR stored procedure is as follows:

    1
    2
    3
    4
    5
    6
    DBE_FILE.GET_ATTR(
    location IN text,
    filename IN text,
    OUT fexists boolean,
    OUT file_length bigint,
    OUT block_size integer);
    
    Table 17 DBE_FILE.GET_ATTR parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    location

    TEXT

    IN

    No

    File directory

    NOTE:
    • File directories need to be added to the system catalog PG_DIRECTORY. If the input path does not match the path in PG_DIRECTORY, an error indicating that the path does not exist will be reported. Functions that involve location as parameters also comply with this rule.
    • When the GUC parameter safe_data_path is enabled, you can only use an advanced package to read and write files in the file path specified by safe_data_path.

    filename

    TEXT

    IN

    No

    File name.

    fexists

    BOOLEAN

    OUT

    No

    Specifies whether the file exists.

    file_length

    BIGINT

    OUT

    No

    File length (unit: byte). If the file does not exist, NULL is returned.

    block_size

    INTEGER

    OUT

    No

    Block size of the file system (unit: byte). If the file does not exist, NULL is returned.

  • DBE_FILE.SEEK

    Adjusts the position of a file pointer forward or backward based on the specified number of bytes.

    The prototype of the DBE_FILE.SEEK function is as follows:

    1
    2
    3
    4
    5
    DBE_FILE.SEEK (
    file            IN INTEGER,
    absolute_start  IN     BIGINT DEFAULT NULL,
    relative_start  IN     BIGINT DEFAULT NULL)
    RETURN VOID;
    
    Table 18 DBE_FILE.SEEK parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    file

    INTEGER

    IN

    No

    File handle opened using OPEN.

    absolute_start

    BIGINT

    IN

    Yes

    Absolute offset of a file. The default value is NULL.

    relative_start

    BIGINT

    IN

    Yes

    Relative offset of a file. A positive number indicates forward offset and a negative number indicates backward offset. The default value is NULL. If both absolute_start and this parameter are specified, the absolute_start parameter is used.

  • DBE_FILE.GET_POS

    Returns the current offset of the file in bytes.

    The prototype of the DBE_FILE.FGETPOS function is as follows:

    1
    2
    3
    DBE_FILE.GET_POS (
    file    IN     INTEGER)
    RETURN BIGINT;
    
    Table 19 DBE_FILE.GET_POS parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    file

    INTEGER

    IN

    No

    File handle opened using OPEN.

  • DBE_FILE.IS_OPEN

    Returns the current offset of the file in bytes.

    The prototype of the DBE_FILE.IS_OPEN function is as follows:

    1
    2
    3
    DBE_FILE.IS_OPEN (
    file      IN     INTEGER)
    RETURN BOOLEAN;
    
    Table 20 DBE_FILE.IS_OPEN parameters

    Parameter

    Type

    Input/Output Parameter

    Can Be Empty

    Description

    file

    INTEGER

    IN

    No

    File handle opened using OPEN.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
-- Add the /temp/ directory to the PG_DIRECTORY system catalog as a SYSADMIN.
CREATE OR REPLACE DIRECTORY dir AS '/tmp/';
-- The expected result is as follows:
CREATE DIRECTORY

-- Open a file and write data into the file.
DECLARE
  f integer;
  dir text := 'dir';
BEGIN
  f := dbe_file.open(dir, 'sample.txt', 'w');
  PERFORM dbe_file.write_line(f, 'ABC');
  PERFORM dbe_file.write_line(f, '123'::numeric);
  PERFORM dbe_file.write_line(f, '-----');
  PERFORM dbe_file.new_line(f);
  PERFORM dbe_file.write_line(f, '*******');
  PERFORM dbe_file.new_line(f, 0);
  PERFORM dbe_file.write_line(f, '++++++++');
  PERFORM dbe_file.new_line(f, 2);
  PERFORM dbe_file.write_line(f, '#######');
  PERFORM dbe_file.write(f, 'A');
  PERFORM dbe_file.write(f, 'B');
  PERFORM dbe_file.new_line(f);
  PERFORM dbe_file.format_write(f, '[1 -> %s, 2 -> %s, 3 -> %s, 4 -> %s, 5 -> %s]', 'gaussdb', 'dbe', 'file', 'get', 'line');
  PERFORM dbe_file.new_line(f);
  PERFORM dbe_file.write_line(f, '1234567890');
  f := dbe_file.close(f);
END;
/
-- The expected result is as follows:
ANONYMOUS BLOCK EXECUTE

-- Read data from the file mentioned above.
DECLARE
  f integer;
  dir text := 'dir';
BEGIN
  f := dbe_file.open(dir, 'sample.txt', 'r');
  FOR i IN 1..11 LOOP
       RAISE INFO '[%] : %', i, dbe_file.read_line(f);
  END LOOP;
END;
/
-- The expected result is as follows:
INFO:  [1] : ABC
INFO:  [2] : 123
INFO:  [3] : -----
INFO:  [4] : 
INFO:  [5] : *******
INFO:  [6] : ++++++++
INFO:  [7] : 
INFO:  [8] : 
INFO:  [9] : #######
INFO:  [10] : AB
INFO:  [11] : [1 -> gaussdb, 2 -> dbe, 3 -> file, 4 -> get, 5 -> line]
ANONYMOUS BLOCK EXECUTE

-- Offset the file handle and obtain the current file location.
DECLARE
      l_file   integer;
      l_buffer VARCHAR2(32767);
      dir text := 'dir';
      abs_offset number := 100;
      rel_offset number := NULL;
BEGIN
      l_file := dbe_file.open(dir  => dir,  file_name  => 'sample.txt',open_mode => 'R');
      dbe_output.print_line('before seek: current position is ' || dbe_file.get_pos(file => l_file)); -- before seek: current position is 0
      dbe_file.seek(file => l_file, absolute_start=>abs_offset, relative_start=>rel_offset);
      dbe_output.print_line('fseek: current position is ' || dbe_file.get_pos(file => l_file)); -- seek: current position is 100
      l_file := dbe_file.close(file => l_file);
END;
/
-- The expected result is as follows:
before seek: current position is 0
fseek: current position is 100
ANONYMOUS BLOCK EXECUTE