Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8652263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:16:10+00:00 2026-06-12T14:16:10+00:00

I have a PostgreSQL database that has multiple entries for the objectid , on

  • 0

I have a PostgreSQL database that has multiple entries for the objectid, on multiple devicenames, but there is a unique timestamp for each entry. The table looks something like this:

address | devicename | objectid      |  timestamp       
--------+------------+---------------+------------------------------
1.1.1.1 | device1    | vs_hub.ch1_25 | 2012-10-02 17:36:41.011629+00
1.1.1.2 | device2    | vs_hub.ch1_25 | 2012-10-02 17:48:01.755559+00
1.1.1.1 | device1    | vs_hub.ch1_25 | 2012-10-03 15:37:09.06065+00
1.1.1.2 | device2    | vs_hub.ch1_25 | 2012-10-03 15:48:33.93128+00
1.1.1.1 | device1    | vs_hub.ch1_25 | 2012-10-05 16:01:59.266779+00
1.1.1.2 | device2    | vs_hub.ch1_25 | 2012-10-05 16:13:46.843113+00
1.1.1.1 | device1    | vs_hub.ch1_25 | 2012-10-06 01:11:45.853361+00
1.1.1.2 | device2    | vs_hub.ch1_25 | 2012-10-06 01:23:21.204324+00

I want to delete all but the oldest entry for each odjectid and devicename. In this case I want to delete all but:

1.1.1.1 | device1 | vs_hub.ch1_25 | 2012-10-02 17:36:41.011629+00
1.1.1.2 | device2 | vs_hub.ch1_25 | 2012-10-02 17:48:01.755559+00

Is there a way do this? Or is it possible to select the oldest entries for both “objectid and devicename” into a temp table?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:16:11+00:00Added an answer on June 12, 2026 at 2:16 pm

    To distill the described result, this would probably simplest and fastest:

    SELECT DISTINCT ON (devicename, objectid) *
    FROM   tbl
    ORDER  BY devicename, objectid, ts DESC;
    

    Details and explanation in this related answer.

    From your sample data, I conclude that you are going to delete large portions of the original table. It is probably faster to just TRUNCATE the table (or DROP & recreate, since you should add a surrogate pk column anyway) and write the remaining rows to it. This also provides you with a pristine table, implicitly clustered (ordered) the way it’s best for your queries and save the work that VACUUM would have to do otherwise. And it’s probably still faster overall:

    I would also strongly advise to add a surrogate primary key to your table, preferably a serial column.

    BEGIN;
    
    CREATE TEMP TABLE tmp_tbl ON COMMIT DROP AS
    SELECT DISTINCT ON (devicename, objectid) *
    FROM   tbl
    ORDER  BY devicename, objectid, ts DESC;
    
    TRUNCATE tbl;
    ALTER TABLE tbl ADD column tbl_id serial PRIMARY KEY;
    
    -- or, if you can afford to drop & recreate:
    -- DROP TABLE tbl;
    -- CREATE TABLE tbl (
    --   tbl_id serial PRIMARY KEY
    -- , address text
    -- , devicename text
    -- , objectid text
    -- , ts timestamp);
    
    INSERT INTO tbl (address, devicename, objectid, ts)
    SELECT address, devicename, objectid, ts
    FROM   tmp_tbl;
    
    COMMIT;
    

    Do it all within a transaction to make sure you are not going to fail half way through.

    This is fast as long as your setting for temp_buffers is big enough to hold the temporary table. Else the system will start swapping data to disk and performance takes a dive. You can set temp_buffers just for the current session like this:

    SET temp_buffers = 1000MB;
    

    So you don’t waste RAM that you don’t normally need for temp_buffers. Has to be set before the first use of any temporary objects in the session. More information in this related answer.

    Also, as the INSERT follows a TRUNCATE inside a transaction, it will be easy on the Write Ahead Log – improving performance.

    Consider CREATE TABLE AS for the alternative route:

    • What causes large INSERT to slow down and disk usage to explode?

    The only downside: You need an exclusive lock on the table. This may be a problem in databases with heavy concurrent load.

    Finally, never use timestamp as column name. It’s a reserved word in every SQL standard and a type name in PostgreSQL. I used ts instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a client that has a PostgreSQL database and he cannot remember the
Background In a PostgreSQL 9.0 database, there are various tables that have many-to-many relationships.
I have a PostgreSQL database with events. Each event has a datetime or an
I have a postgresql database that has this column structure: Author id name Book
I have a large readonly Wordnet PostgreSQL database that I'd like to use from
I Have to get a movie from a PostgreSQL database that matches a given
Background: I have a PostgreSQL (v8.3) database that is heavily optimized for OLTP. I
I have a PostgreSQL database that contains traffic tickets written by a few jurisdictions.
I have a database table that has a structure like the one shown below:
I have a PostgreSQL database at work that is a single table of data.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.