If your workload mainly uses trajectory IDs for queries and is not sensitive to storage usage, you can use vertex tables to store data. After you run a query on a vertex table, the query results must be aggregated into trajectories to perform the required operations.

In this example, a vertex table is used to store a user trajectory on a travel app. The following data is collected at each sampling point: user ID, date and time, longitude, latitude, altitude, and signal intensity of the sampling device.
  1. Create a table.
    CREATE TABLE sample_points(
        userid numeric, -- User ID
        sample_time timestamp, -- Sampling point
        x double precision, -- Longitude
        y double precision, -- Latitude
        z double precision, -- Altitude
        intensity int -- Signal intensity of the sampling device
    );
  2. Insert test data into the table.
    INSERT INTO sample_points VALUES
    (1,'2020-04-11 17:42:30',114.35, 39.28, 4, 80),
    (1,'2020-04-11 17:43:30',114.36, 39.28, 4, 30),
    (1,'2020-04-11 17:45:00',114.35, 39.29, 4, 50),
    (2,'2020-04-11 17:42:30',114.3, 39, 34, 60),
    (2,'2020-04-11 17:43:30',114.3, 39, 38, 58);
  3. Create an index for the userid column.
    CREATE INDEX on sample_points USING btree(userid);
  4. Extract the trajectory points of the user whose userid is 1 at all sampling points. The trajectory points are arranged based on the chronological order of sampling points.
    SELECT * FROM sample_points WHERE userid = 1 ORDER BY sample_time;
You can process the extracted trajectory points. For example, you can delete drift points that are not accurate.
SELECT ST_removeDriftPoints( -- Deletes inaccurate drift points.
    ST_SetSRID( -- Changes the spatial reference identifier (SRID) of a trajectory.
        ST_MakeTrajectory( -- Constructs a trajectory.
            array_agg(
                ROW(traj.sample_time, traj.x, traj.y, traj.z, traj.intensity) -- Selects the columns that you want to use to construct a trajectory.
            ), true, '{"intensity"}'::cstring[]),4326), 40, 10, '1 minute'::interval
) FROM (SELECT * FROM sample_points WHERE userid = 1 ORDER BY sample_time) traj;
Note
  • In the example, trajectory points are aggregated into a trajectory. Then, the ST_removeDriftPoints function is used to remove trajectory points that are not required. For more information about the parameters of trajectory functions, see ST_makeTrajectory, ST_SetSRID, and ST_removeDriftPoints.
  • In the ROW function, the first column must be of the TIMESTAMP type, and the second and third columns must be of the DOUBLE PRECISION type. If the value for the second parameter of the ST_MakeTrajectory function is true, the fourth column of the ROW function must also be of the DOUBLE PRECISION type. The value true indicates that trajectory points contain altitudes. If any column of the ROW function is not of the DOUBLE PRECISION type, you can use Variable name::double precision or cast(Variable name as double precision) to explicitly convert the data type of the column into DOUBLE PRECISION.
  • For more information about the data types of time values, see Data types for time values in PostgreSQL.
The ST_removeDriftPoint function returns trajectories. If you want to convert trajectories into vertex tables, use the ST_AsTable function.
WITH removed as
(
    SELECT ST_removeDriftPoints(
        ST_SetSRID(
            ST_MakeTrajectory(
                array_agg(
                    ROW(traj.sample_time, traj.x, traj.y, traj.z, traj.intensity)), 
                true, '{"intensity"}'::cstring[]), 4326), 40, 10, '1 minute'::interval) AS trajcol
    FROM (SELECT * FROM sample_points WHERE userid = 1 ORDER BY sample_time) traj
)
SELECT f.* FROM removed, 
ST_AsTable(removed.trajcol) AS -- Converts a trajectory into a vertex table.
f(sample_time timestamp,x double precision, y double precision, z double precision, intensity int);
Note
  • The ST_removeDriftPoints function requires that the SRID of a trajectory is 4326, which specifies the World Geodetic System 1984 (WGS84). Therefore, you must make sure that the SRID of the input trajectory for the ST_removeDriftPoints function is 4326. Otherwise, the SRID of the trajectory that is returned by the ST_removeDriftPoint function is 0. The value 0 indicates that the SRID of the returned trajectory is unknown.
  • If you use Data Management (DMS) to connect your client to your ApsaraDB RDS instance and execute this statement, compatibility issues may occur. If a message is displayed, indicating that unexpected results are returned, use another client.