Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 300 Vote(s) - 3.61 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is there a way to set an "expiry" time, after which a data entry is automatically deleted in PostgreSQL?

#1
Is there any way to set some sort of "expiry" time on data entries in [PostgreSQL][1]? I'm thinking about something equivalent to [`EXPIRE` in Redis][2].

I'm not looking to store a timestamp and then manually code some sort of [cron][3] job to check what entries have expired.

I'm trying to find out if there's any native feature in PostgreSQL that would provide this kind of functionality, or if it would make sense to request such feature for future releases.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#2
No. There is no such feature.

I can't see what it does more than either (1) just an "expired" timestamp does or (2) timestamp + cron-job/pgAgent.

It doesn't sound like a general feature that would be added to the core. You could quite simply code an [extension][1] to handle this sort of thing, with either a tick called from a cron-job or perhaps a [background-worker][2] process.

I don't see anything on [pgxn][3], so presumably there's not been much demand for it yet.


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#3
There is no built in expiration feature but if your goal is to automatically expire fields and have the logic contained within your database (and thus no outside dependency like a cron job) then you can always write a trigger. Below is an example of a trigger that deletes rows from a table that have a timestamp of older than 1 minute. It is executed whenever a new row is inserted into that same table. You can obviously set the trigger to execute on other conditions and for various expiration dates as need be. I used the following website as a basis for this:

[To see links please register here]


CREATE TABLE expire_table (
timestamp timestamp NOT NULL DEFAULT NOW(),
name TEXT NOT NULL
);

INSERT INTO expire_table (name) VALUES ('a');
INSERT INTO expire_table (name) VALUES ('b');
INSERT INTO expire_table (name) VALUES ('c');

select * from expire_table;
timestamp | name
----------------------------+------
2014-09-26 15:33:43.243356 | a
2014-09-26 15:33:45.222202 | b
2014-09-26 15:33:47.347131 | c
(3 rows)

CREATE FUNCTION expire_table_delete_old_rows() RETURNS trigger
LANGUAGE plpgsql
AS $$
BEGIN
DELETE FROM expire_table WHERE timestamp < NOW() - INTERVAL '1 minute';
RETURN NEW;
END;
$$;

CREATE TRIGGER expire_table_delete_old_rows_trigger
AFTER INSERT ON expire_table
EXECUTE PROCEDURE expire_table_delete_old_rows();

INSERT INTO expire_table (name) VALUES ('d');

select * from expire_table;
timestamp | name
----------------------------+------
2014-09-26 15:36:56.132596 | d
(1 row)
Reply

#4
Nope, PG does not.
But you do get these with Google Cloud Spanner. It does have an SQL interface and currently support postgres dialect too.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through