PostgreSQL: Table creation time











up vote
33
down vote

favorite
5












How can I find the table creation time in Postgresql.



Example ,



If I created a file I can find the file creation time like that I want to know the table creation time.










share|improve this question




























    up vote
    33
    down vote

    favorite
    5












    How can I find the table creation time in Postgresql.



    Example ,



    If I created a file I can find the file creation time like that I want to know the table creation time.










    share|improve this question


























      up vote
      33
      down vote

      favorite
      5









      up vote
      33
      down vote

      favorite
      5






      5





      How can I find the table creation time in Postgresql.



      Example ,



      If I created a file I can find the file creation time like that I want to know the table creation time.










      share|improve this question















      How can I find the table creation time in Postgresql.



      Example ,



      If I created a file I can find the file creation time like that I want to know the table creation time.







      postgresql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 5 '10 at 11:31









      Milen A. Radev

      42.8k169094




      42.8k169094










      asked Apr 5 '10 at 6:20









      kiruthika

      92462031




      92462031
























          7 Answers
          7






          active

          oldest

          votes

















          up vote
          14
          down vote



          accepted










          I had a look through the pg_* tables, and I couldn't find any creation times in there. It's possible to locate the table files, but then on Linux you can't get file creation time. So I think the answer is that you can only find this information on Windows, using the following steps:




          • get the database id with select datname, datdba from pg_database;

          • get the table filenode id with select relname, relfilenode from pg_class;

          • find the table file and look up its creation time; I think the location should be something like <PostgreSQL folder>/main/base/<database id>/<table filenode id> (not sure what it is on Windows).






          share|improve this answer

















          • 4




            There are some operations on a table, such as CLUSTER, that will generate a new file and not re-use the old one. So this is not a reliable method.
            – Magnus Hagander
            Apr 5 '10 at 9:15










          • @Alex Korban: Fully automatized this here: stackoverflow.com/questions/18849756/…
            – Stefan Steiger
            Sep 17 '13 at 15:09










          • @Quandary: Interesting, thanks. Looks like there's still no bulletproof method to do it, other than storing creation times yourself.
            – Alex Korban
            Sep 19 '13 at 4:42










          • I need to be a master user? Can you express a are a query (that works), like the @Manoj's?
            – Peter Krauss
            Jan 13 '14 at 0:01










          • Have a look at @Quandary's link: stackoverflow.com/questions/18849756/…
            – Alex Korban
            Jan 13 '14 at 4:07


















          up vote
          17
          down vote













          You can't - the information isn't recorded anywhere. Looking at the table files won't necessarily give you the right information - there are table operations that will create a new file for you, in which case the date would reset.






          share|improve this answer




























            up vote
            2
            down vote













            I don't think it's possible from within PostgreSQL, but you'll probably find it in the underlying table file's creation time.






            share|improve this answer




























              up vote
              0
              down vote













              Suggested here :



              SELECT oid FROM pg_database WHERE datname = 'mydb';


              Then (assuming the oid is 12345) :



              ls -l $PGDATA/base/12345/PG_VERSION


              This workaround assumes that PG_VERSION is the least likely to be modified after the creation.



              NB : If PGDATA is not defined, check Where does PostgreSQL store the database?






              share|improve this answer






























                up vote
                0
                down vote













                I'm trying to follow a different way for obtain this.
                Starting from this discussion my solution was:



                DROP TABLE IF EXISTS t_create_history CASCADE;
                CREATE TABLE t_create_history (
                gid serial primary key,
                object_type varchar(20),
                schema_name varchar(50),
                object_identity varchar(200),
                creation_date timestamp without time zone
                );



                --delete event trigger before dropping function
                DROP EVENT TRIGGER IF EXISTS t_create_history_trigger;

                --create history function
                DROP FUNCTION IF EXISTS public.t_create_history_func();

                CREATE OR REPLACE FUNCTION t_create_history_func()
                RETURNS event_trigger
                LANGUAGE plpgsql
                AS $$
                DECLARE
                obj record;
                BEGIN
                FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands () WHERE command_tag in ('SELECT INTO','CREATE TABLE','CREATE TABLE AS')
                LOOP
                INSERT INTO public.t_create_history (object_type, schema_name, object_identity, creation_date) SELECT obj.object_type, obj.schema_name, obj.object_identity, now();
                END LOOP;

                END;
                $$;


                --ALTER EVENT TRIGGER t_create_history_trigger DISABLE;
                --DROP EVENT TRIGGER t_create_history_trigger;

                CREATE EVENT TRIGGER t_create_history_trigger ON ddl_command_end
                WHEN TAG IN ('SELECT INTO','CREATE TABLE','CREATE TABLE AS')
                EXECUTE PROCEDURE t_create_history_func();


                In this way you obtain a table that records all the creation tables.






                share|improve this answer




























                  up vote
                  -3
                  down vote













                  You can get this from pg_stat_last_operation. Here is how to do it:



                  select * from pg_stat_last_operation where objid = 'table_name'::regclass order by statime;


                  This table stores following operations:



                  select distinct staactionname from pg_stat_last_operation;

                  staactionname
                  ---------------
                  ALTER

                  ANALYZE

                  CREATE

                  PARTITION

                  PRIVILEGE

                  VACUUM
                  (6 rows)





                  share|improve this answer



















                  • 5




                    ERROR: "pg_stat_last_operation not exist", I am using pg9.2.4.
                    – Peter Krauss
                    Jan 12 '14 at 23:58






                  • 3




                    pg_stat_last_operation is Greenplum.
                    – Peter Eisentraut
                    Apr 15 '14 at 4:38










                  • Table pg_stat_all_tables could also help.
                    – Hans Ginzel
                    Jun 15 '15 at 8:12






                  • 1




                    this does not work in PostgreSQL
                    – a1an
                    Jun 19 '15 at 14:34


















                  up vote
                  -3
                  down vote













                  --query



                  select pslo.stasubtype, pc.relname, pslo.statime
                  from pg_stat_last_operation pslo
                  join pg_class pc on(pc.relfilenode = pslo.objid)
                  and pslo.staactionname = 'CREATE'
                  Order By pslo.statime desc


                  will help to accomplish desired results



                  (tried it on greenplum)






                  share|improve this answer





















                  • This is the same answer as Manoj's and greenplum specific.
                    – dezso
                    Nov 7 '14 at 14:12










                  • I am using two tables
                    – Gurupreet Singh Bhatia
                    Dec 8 '14 at 10:59






                  • 1




                    this does not work in PostgreSQL
                    – a1an
                    Jun 19 '15 at 14:34











                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2577168%2fpostgresql-table-creation-time%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  7 Answers
                  7






                  active

                  oldest

                  votes








                  7 Answers
                  7






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  14
                  down vote



                  accepted










                  I had a look through the pg_* tables, and I couldn't find any creation times in there. It's possible to locate the table files, but then on Linux you can't get file creation time. So I think the answer is that you can only find this information on Windows, using the following steps:




                  • get the database id with select datname, datdba from pg_database;

                  • get the table filenode id with select relname, relfilenode from pg_class;

                  • find the table file and look up its creation time; I think the location should be something like <PostgreSQL folder>/main/base/<database id>/<table filenode id> (not sure what it is on Windows).






                  share|improve this answer

















                  • 4




                    There are some operations on a table, such as CLUSTER, that will generate a new file and not re-use the old one. So this is not a reliable method.
                    – Magnus Hagander
                    Apr 5 '10 at 9:15










                  • @Alex Korban: Fully automatized this here: stackoverflow.com/questions/18849756/…
                    – Stefan Steiger
                    Sep 17 '13 at 15:09










                  • @Quandary: Interesting, thanks. Looks like there's still no bulletproof method to do it, other than storing creation times yourself.
                    – Alex Korban
                    Sep 19 '13 at 4:42










                  • I need to be a master user? Can you express a are a query (that works), like the @Manoj's?
                    – Peter Krauss
                    Jan 13 '14 at 0:01










                  • Have a look at @Quandary's link: stackoverflow.com/questions/18849756/…
                    – Alex Korban
                    Jan 13 '14 at 4:07















                  up vote
                  14
                  down vote



                  accepted










                  I had a look through the pg_* tables, and I couldn't find any creation times in there. It's possible to locate the table files, but then on Linux you can't get file creation time. So I think the answer is that you can only find this information on Windows, using the following steps:




                  • get the database id with select datname, datdba from pg_database;

                  • get the table filenode id with select relname, relfilenode from pg_class;

                  • find the table file and look up its creation time; I think the location should be something like <PostgreSQL folder>/main/base/<database id>/<table filenode id> (not sure what it is on Windows).






                  share|improve this answer

















                  • 4




                    There are some operations on a table, such as CLUSTER, that will generate a new file and not re-use the old one. So this is not a reliable method.
                    – Magnus Hagander
                    Apr 5 '10 at 9:15










                  • @Alex Korban: Fully automatized this here: stackoverflow.com/questions/18849756/…
                    – Stefan Steiger
                    Sep 17 '13 at 15:09










                  • @Quandary: Interesting, thanks. Looks like there's still no bulletproof method to do it, other than storing creation times yourself.
                    – Alex Korban
                    Sep 19 '13 at 4:42










                  • I need to be a master user? Can you express a are a query (that works), like the @Manoj's?
                    – Peter Krauss
                    Jan 13 '14 at 0:01










                  • Have a look at @Quandary's link: stackoverflow.com/questions/18849756/…
                    – Alex Korban
                    Jan 13 '14 at 4:07













                  up vote
                  14
                  down vote



                  accepted







                  up vote
                  14
                  down vote



                  accepted






                  I had a look through the pg_* tables, and I couldn't find any creation times in there. It's possible to locate the table files, but then on Linux you can't get file creation time. So I think the answer is that you can only find this information on Windows, using the following steps:




                  • get the database id with select datname, datdba from pg_database;

                  • get the table filenode id with select relname, relfilenode from pg_class;

                  • find the table file and look up its creation time; I think the location should be something like <PostgreSQL folder>/main/base/<database id>/<table filenode id> (not sure what it is on Windows).






                  share|improve this answer












                  I had a look through the pg_* tables, and I couldn't find any creation times in there. It's possible to locate the table files, but then on Linux you can't get file creation time. So I think the answer is that you can only find this information on Windows, using the following steps:




                  • get the database id with select datname, datdba from pg_database;

                  • get the table filenode id with select relname, relfilenode from pg_class;

                  • find the table file and look up its creation time; I think the location should be something like <PostgreSQL folder>/main/base/<database id>/<table filenode id> (not sure what it is on Windows).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 5 '10 at 7:42









                  Alex Korban

                  12.1k53853




                  12.1k53853








                  • 4




                    There are some operations on a table, such as CLUSTER, that will generate a new file and not re-use the old one. So this is not a reliable method.
                    – Magnus Hagander
                    Apr 5 '10 at 9:15










                  • @Alex Korban: Fully automatized this here: stackoverflow.com/questions/18849756/…
                    – Stefan Steiger
                    Sep 17 '13 at 15:09










                  • @Quandary: Interesting, thanks. Looks like there's still no bulletproof method to do it, other than storing creation times yourself.
                    – Alex Korban
                    Sep 19 '13 at 4:42










                  • I need to be a master user? Can you express a are a query (that works), like the @Manoj's?
                    – Peter Krauss
                    Jan 13 '14 at 0:01










                  • Have a look at @Quandary's link: stackoverflow.com/questions/18849756/…
                    – Alex Korban
                    Jan 13 '14 at 4:07














                  • 4




                    There are some operations on a table, such as CLUSTER, that will generate a new file and not re-use the old one. So this is not a reliable method.
                    – Magnus Hagander
                    Apr 5 '10 at 9:15










                  • @Alex Korban: Fully automatized this here: stackoverflow.com/questions/18849756/…
                    – Stefan Steiger
                    Sep 17 '13 at 15:09










                  • @Quandary: Interesting, thanks. Looks like there's still no bulletproof method to do it, other than storing creation times yourself.
                    – Alex Korban
                    Sep 19 '13 at 4:42










                  • I need to be a master user? Can you express a are a query (that works), like the @Manoj's?
                    – Peter Krauss
                    Jan 13 '14 at 0:01










                  • Have a look at @Quandary's link: stackoverflow.com/questions/18849756/…
                    – Alex Korban
                    Jan 13 '14 at 4:07








                  4




                  4




                  There are some operations on a table, such as CLUSTER, that will generate a new file and not re-use the old one. So this is not a reliable method.
                  – Magnus Hagander
                  Apr 5 '10 at 9:15




                  There are some operations on a table, such as CLUSTER, that will generate a new file and not re-use the old one. So this is not a reliable method.
                  – Magnus Hagander
                  Apr 5 '10 at 9:15












                  @Alex Korban: Fully automatized this here: stackoverflow.com/questions/18849756/…
                  – Stefan Steiger
                  Sep 17 '13 at 15:09




                  @Alex Korban: Fully automatized this here: stackoverflow.com/questions/18849756/…
                  – Stefan Steiger
                  Sep 17 '13 at 15:09












                  @Quandary: Interesting, thanks. Looks like there's still no bulletproof method to do it, other than storing creation times yourself.
                  – Alex Korban
                  Sep 19 '13 at 4:42




                  @Quandary: Interesting, thanks. Looks like there's still no bulletproof method to do it, other than storing creation times yourself.
                  – Alex Korban
                  Sep 19 '13 at 4:42












                  I need to be a master user? Can you express a are a query (that works), like the @Manoj's?
                  – Peter Krauss
                  Jan 13 '14 at 0:01




                  I need to be a master user? Can you express a are a query (that works), like the @Manoj's?
                  – Peter Krauss
                  Jan 13 '14 at 0:01












                  Have a look at @Quandary's link: stackoverflow.com/questions/18849756/…
                  – Alex Korban
                  Jan 13 '14 at 4:07




                  Have a look at @Quandary's link: stackoverflow.com/questions/18849756/…
                  – Alex Korban
                  Jan 13 '14 at 4:07












                  up vote
                  17
                  down vote













                  You can't - the information isn't recorded anywhere. Looking at the table files won't necessarily give you the right information - there are table operations that will create a new file for you, in which case the date would reset.






                  share|improve this answer

























                    up vote
                    17
                    down vote













                    You can't - the information isn't recorded anywhere. Looking at the table files won't necessarily give you the right information - there are table operations that will create a new file for you, in which case the date would reset.






                    share|improve this answer























                      up vote
                      17
                      down vote










                      up vote
                      17
                      down vote









                      You can't - the information isn't recorded anywhere. Looking at the table files won't necessarily give you the right information - there are table operations that will create a new file for you, in which case the date would reset.






                      share|improve this answer












                      You can't - the information isn't recorded anywhere. Looking at the table files won't necessarily give you the right information - there are table operations that will create a new file for you, in which case the date would reset.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Apr 5 '10 at 9:15









                      Magnus Hagander

                      16.2k34336




                      16.2k34336






















                          up vote
                          2
                          down vote













                          I don't think it's possible from within PostgreSQL, but you'll probably find it in the underlying table file's creation time.






                          share|improve this answer

























                            up vote
                            2
                            down vote













                            I don't think it's possible from within PostgreSQL, but you'll probably find it in the underlying table file's creation time.






                            share|improve this answer























                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote









                              I don't think it's possible from within PostgreSQL, but you'll probably find it in the underlying table file's creation time.






                              share|improve this answer












                              I don't think it's possible from within PostgreSQL, but you'll probably find it in the underlying table file's creation time.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Apr 5 '10 at 6:48









                              Marcelo Cantos

                              142k31282328




                              142k31282328






















                                  up vote
                                  0
                                  down vote













                                  Suggested here :



                                  SELECT oid FROM pg_database WHERE datname = 'mydb';


                                  Then (assuming the oid is 12345) :



                                  ls -l $PGDATA/base/12345/PG_VERSION


                                  This workaround assumes that PG_VERSION is the least likely to be modified after the creation.



                                  NB : If PGDATA is not defined, check Where does PostgreSQL store the database?






                                  share|improve this answer



























                                    up vote
                                    0
                                    down vote













                                    Suggested here :



                                    SELECT oid FROM pg_database WHERE datname = 'mydb';


                                    Then (assuming the oid is 12345) :



                                    ls -l $PGDATA/base/12345/PG_VERSION


                                    This workaround assumes that PG_VERSION is the least likely to be modified after the creation.



                                    NB : If PGDATA is not defined, check Where does PostgreSQL store the database?






                                    share|improve this answer

























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      Suggested here :



                                      SELECT oid FROM pg_database WHERE datname = 'mydb';


                                      Then (assuming the oid is 12345) :



                                      ls -l $PGDATA/base/12345/PG_VERSION


                                      This workaround assumes that PG_VERSION is the least likely to be modified after the creation.



                                      NB : If PGDATA is not defined, check Where does PostgreSQL store the database?






                                      share|improve this answer














                                      Suggested here :



                                      SELECT oid FROM pg_database WHERE datname = 'mydb';


                                      Then (assuming the oid is 12345) :



                                      ls -l $PGDATA/base/12345/PG_VERSION


                                      This workaround assumes that PG_VERSION is the least likely to be modified after the creation.



                                      NB : If PGDATA is not defined, check Where does PostgreSQL store the database?







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited May 23 '17 at 11:54


























                                      community wiki





                                      2 revs
                                      Skippy le Grand Gourou























                                          up vote
                                          0
                                          down vote













                                          I'm trying to follow a different way for obtain this.
                                          Starting from this discussion my solution was:



                                          DROP TABLE IF EXISTS t_create_history CASCADE;
                                          CREATE TABLE t_create_history (
                                          gid serial primary key,
                                          object_type varchar(20),
                                          schema_name varchar(50),
                                          object_identity varchar(200),
                                          creation_date timestamp without time zone
                                          );



                                          --delete event trigger before dropping function
                                          DROP EVENT TRIGGER IF EXISTS t_create_history_trigger;

                                          --create history function
                                          DROP FUNCTION IF EXISTS public.t_create_history_func();

                                          CREATE OR REPLACE FUNCTION t_create_history_func()
                                          RETURNS event_trigger
                                          LANGUAGE plpgsql
                                          AS $$
                                          DECLARE
                                          obj record;
                                          BEGIN
                                          FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands () WHERE command_tag in ('SELECT INTO','CREATE TABLE','CREATE TABLE AS')
                                          LOOP
                                          INSERT INTO public.t_create_history (object_type, schema_name, object_identity, creation_date) SELECT obj.object_type, obj.schema_name, obj.object_identity, now();
                                          END LOOP;

                                          END;
                                          $$;


                                          --ALTER EVENT TRIGGER t_create_history_trigger DISABLE;
                                          --DROP EVENT TRIGGER t_create_history_trigger;

                                          CREATE EVENT TRIGGER t_create_history_trigger ON ddl_command_end
                                          WHEN TAG IN ('SELECT INTO','CREATE TABLE','CREATE TABLE AS')
                                          EXECUTE PROCEDURE t_create_history_func();


                                          In this way you obtain a table that records all the creation tables.






                                          share|improve this answer

























                                            up vote
                                            0
                                            down vote













                                            I'm trying to follow a different way for obtain this.
                                            Starting from this discussion my solution was:



                                            DROP TABLE IF EXISTS t_create_history CASCADE;
                                            CREATE TABLE t_create_history (
                                            gid serial primary key,
                                            object_type varchar(20),
                                            schema_name varchar(50),
                                            object_identity varchar(200),
                                            creation_date timestamp without time zone
                                            );



                                            --delete event trigger before dropping function
                                            DROP EVENT TRIGGER IF EXISTS t_create_history_trigger;

                                            --create history function
                                            DROP FUNCTION IF EXISTS public.t_create_history_func();

                                            CREATE OR REPLACE FUNCTION t_create_history_func()
                                            RETURNS event_trigger
                                            LANGUAGE plpgsql
                                            AS $$
                                            DECLARE
                                            obj record;
                                            BEGIN
                                            FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands () WHERE command_tag in ('SELECT INTO','CREATE TABLE','CREATE TABLE AS')
                                            LOOP
                                            INSERT INTO public.t_create_history (object_type, schema_name, object_identity, creation_date) SELECT obj.object_type, obj.schema_name, obj.object_identity, now();
                                            END LOOP;

                                            END;
                                            $$;


                                            --ALTER EVENT TRIGGER t_create_history_trigger DISABLE;
                                            --DROP EVENT TRIGGER t_create_history_trigger;

                                            CREATE EVENT TRIGGER t_create_history_trigger ON ddl_command_end
                                            WHEN TAG IN ('SELECT INTO','CREATE TABLE','CREATE TABLE AS')
                                            EXECUTE PROCEDURE t_create_history_func();


                                            In this way you obtain a table that records all the creation tables.






                                            share|improve this answer























                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              I'm trying to follow a different way for obtain this.
                                              Starting from this discussion my solution was:



                                              DROP TABLE IF EXISTS t_create_history CASCADE;
                                              CREATE TABLE t_create_history (
                                              gid serial primary key,
                                              object_type varchar(20),
                                              schema_name varchar(50),
                                              object_identity varchar(200),
                                              creation_date timestamp without time zone
                                              );



                                              --delete event trigger before dropping function
                                              DROP EVENT TRIGGER IF EXISTS t_create_history_trigger;

                                              --create history function
                                              DROP FUNCTION IF EXISTS public.t_create_history_func();

                                              CREATE OR REPLACE FUNCTION t_create_history_func()
                                              RETURNS event_trigger
                                              LANGUAGE plpgsql
                                              AS $$
                                              DECLARE
                                              obj record;
                                              BEGIN
                                              FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands () WHERE command_tag in ('SELECT INTO','CREATE TABLE','CREATE TABLE AS')
                                              LOOP
                                              INSERT INTO public.t_create_history (object_type, schema_name, object_identity, creation_date) SELECT obj.object_type, obj.schema_name, obj.object_identity, now();
                                              END LOOP;

                                              END;
                                              $$;


                                              --ALTER EVENT TRIGGER t_create_history_trigger DISABLE;
                                              --DROP EVENT TRIGGER t_create_history_trigger;

                                              CREATE EVENT TRIGGER t_create_history_trigger ON ddl_command_end
                                              WHEN TAG IN ('SELECT INTO','CREATE TABLE','CREATE TABLE AS')
                                              EXECUTE PROCEDURE t_create_history_func();


                                              In this way you obtain a table that records all the creation tables.






                                              share|improve this answer












                                              I'm trying to follow a different way for obtain this.
                                              Starting from this discussion my solution was:



                                              DROP TABLE IF EXISTS t_create_history CASCADE;
                                              CREATE TABLE t_create_history (
                                              gid serial primary key,
                                              object_type varchar(20),
                                              schema_name varchar(50),
                                              object_identity varchar(200),
                                              creation_date timestamp without time zone
                                              );



                                              --delete event trigger before dropping function
                                              DROP EVENT TRIGGER IF EXISTS t_create_history_trigger;

                                              --create history function
                                              DROP FUNCTION IF EXISTS public.t_create_history_func();

                                              CREATE OR REPLACE FUNCTION t_create_history_func()
                                              RETURNS event_trigger
                                              LANGUAGE plpgsql
                                              AS $$
                                              DECLARE
                                              obj record;
                                              BEGIN
                                              FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands () WHERE command_tag in ('SELECT INTO','CREATE TABLE','CREATE TABLE AS')
                                              LOOP
                                              INSERT INTO public.t_create_history (object_type, schema_name, object_identity, creation_date) SELECT obj.object_type, obj.schema_name, obj.object_identity, now();
                                              END LOOP;

                                              END;
                                              $$;


                                              --ALTER EVENT TRIGGER t_create_history_trigger DISABLE;
                                              --DROP EVENT TRIGGER t_create_history_trigger;

                                              CREATE EVENT TRIGGER t_create_history_trigger ON ddl_command_end
                                              WHEN TAG IN ('SELECT INTO','CREATE TABLE','CREATE TABLE AS')
                                              EXECUTE PROCEDURE t_create_history_func();


                                              In this way you obtain a table that records all the creation tables.







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Sep 17 at 12:52









                                              Fatal

                                              1012




                                              1012






















                                                  up vote
                                                  -3
                                                  down vote













                                                  You can get this from pg_stat_last_operation. Here is how to do it:



                                                  select * from pg_stat_last_operation where objid = 'table_name'::regclass order by statime;


                                                  This table stores following operations:



                                                  select distinct staactionname from pg_stat_last_operation;

                                                  staactionname
                                                  ---------------
                                                  ALTER

                                                  ANALYZE

                                                  CREATE

                                                  PARTITION

                                                  PRIVILEGE

                                                  VACUUM
                                                  (6 rows)





                                                  share|improve this answer



















                                                  • 5




                                                    ERROR: "pg_stat_last_operation not exist", I am using pg9.2.4.
                                                    – Peter Krauss
                                                    Jan 12 '14 at 23:58






                                                  • 3




                                                    pg_stat_last_operation is Greenplum.
                                                    – Peter Eisentraut
                                                    Apr 15 '14 at 4:38










                                                  • Table pg_stat_all_tables could also help.
                                                    – Hans Ginzel
                                                    Jun 15 '15 at 8:12






                                                  • 1




                                                    this does not work in PostgreSQL
                                                    – a1an
                                                    Jun 19 '15 at 14:34















                                                  up vote
                                                  -3
                                                  down vote













                                                  You can get this from pg_stat_last_operation. Here is how to do it:



                                                  select * from pg_stat_last_operation where objid = 'table_name'::regclass order by statime;


                                                  This table stores following operations:



                                                  select distinct staactionname from pg_stat_last_operation;

                                                  staactionname
                                                  ---------------
                                                  ALTER

                                                  ANALYZE

                                                  CREATE

                                                  PARTITION

                                                  PRIVILEGE

                                                  VACUUM
                                                  (6 rows)





                                                  share|improve this answer



















                                                  • 5




                                                    ERROR: "pg_stat_last_operation not exist", I am using pg9.2.4.
                                                    – Peter Krauss
                                                    Jan 12 '14 at 23:58






                                                  • 3




                                                    pg_stat_last_operation is Greenplum.
                                                    – Peter Eisentraut
                                                    Apr 15 '14 at 4:38










                                                  • Table pg_stat_all_tables could also help.
                                                    – Hans Ginzel
                                                    Jun 15 '15 at 8:12






                                                  • 1




                                                    this does not work in PostgreSQL
                                                    – a1an
                                                    Jun 19 '15 at 14:34













                                                  up vote
                                                  -3
                                                  down vote










                                                  up vote
                                                  -3
                                                  down vote









                                                  You can get this from pg_stat_last_operation. Here is how to do it:



                                                  select * from pg_stat_last_operation where objid = 'table_name'::regclass order by statime;


                                                  This table stores following operations:



                                                  select distinct staactionname from pg_stat_last_operation;

                                                  staactionname
                                                  ---------------
                                                  ALTER

                                                  ANALYZE

                                                  CREATE

                                                  PARTITION

                                                  PRIVILEGE

                                                  VACUUM
                                                  (6 rows)





                                                  share|improve this answer














                                                  You can get this from pg_stat_last_operation. Here is how to do it:



                                                  select * from pg_stat_last_operation where objid = 'table_name'::regclass order by statime;


                                                  This table stores following operations:



                                                  select distinct staactionname from pg_stat_last_operation;

                                                  staactionname
                                                  ---------------
                                                  ALTER

                                                  ANALYZE

                                                  CREATE

                                                  PARTITION

                                                  PRIVILEGE

                                                  VACUUM
                                                  (6 rows)






                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited May 1 '13 at 22:01









                                                  tessi

                                                  11.2k32844




                                                  11.2k32844










                                                  answered May 1 '13 at 21:36









                                                  Manoj

                                                  191




                                                  191








                                                  • 5




                                                    ERROR: "pg_stat_last_operation not exist", I am using pg9.2.4.
                                                    – Peter Krauss
                                                    Jan 12 '14 at 23:58






                                                  • 3




                                                    pg_stat_last_operation is Greenplum.
                                                    – Peter Eisentraut
                                                    Apr 15 '14 at 4:38










                                                  • Table pg_stat_all_tables could also help.
                                                    – Hans Ginzel
                                                    Jun 15 '15 at 8:12






                                                  • 1




                                                    this does not work in PostgreSQL
                                                    – a1an
                                                    Jun 19 '15 at 14:34














                                                  • 5




                                                    ERROR: "pg_stat_last_operation not exist", I am using pg9.2.4.
                                                    – Peter Krauss
                                                    Jan 12 '14 at 23:58






                                                  • 3




                                                    pg_stat_last_operation is Greenplum.
                                                    – Peter Eisentraut
                                                    Apr 15 '14 at 4:38










                                                  • Table pg_stat_all_tables could also help.
                                                    – Hans Ginzel
                                                    Jun 15 '15 at 8:12






                                                  • 1




                                                    this does not work in PostgreSQL
                                                    – a1an
                                                    Jun 19 '15 at 14:34








                                                  5




                                                  5




                                                  ERROR: "pg_stat_last_operation not exist", I am using pg9.2.4.
                                                  – Peter Krauss
                                                  Jan 12 '14 at 23:58




                                                  ERROR: "pg_stat_last_operation not exist", I am using pg9.2.4.
                                                  – Peter Krauss
                                                  Jan 12 '14 at 23:58




                                                  3




                                                  3




                                                  pg_stat_last_operation is Greenplum.
                                                  – Peter Eisentraut
                                                  Apr 15 '14 at 4:38




                                                  pg_stat_last_operation is Greenplum.
                                                  – Peter Eisentraut
                                                  Apr 15 '14 at 4:38












                                                  Table pg_stat_all_tables could also help.
                                                  – Hans Ginzel
                                                  Jun 15 '15 at 8:12




                                                  Table pg_stat_all_tables could also help.
                                                  – Hans Ginzel
                                                  Jun 15 '15 at 8:12




                                                  1




                                                  1




                                                  this does not work in PostgreSQL
                                                  – a1an
                                                  Jun 19 '15 at 14:34




                                                  this does not work in PostgreSQL
                                                  – a1an
                                                  Jun 19 '15 at 14:34










                                                  up vote
                                                  -3
                                                  down vote













                                                  --query



                                                  select pslo.stasubtype, pc.relname, pslo.statime
                                                  from pg_stat_last_operation pslo
                                                  join pg_class pc on(pc.relfilenode = pslo.objid)
                                                  and pslo.staactionname = 'CREATE'
                                                  Order By pslo.statime desc


                                                  will help to accomplish desired results



                                                  (tried it on greenplum)






                                                  share|improve this answer





















                                                  • This is the same answer as Manoj's and greenplum specific.
                                                    – dezso
                                                    Nov 7 '14 at 14:12










                                                  • I am using two tables
                                                    – Gurupreet Singh Bhatia
                                                    Dec 8 '14 at 10:59






                                                  • 1




                                                    this does not work in PostgreSQL
                                                    – a1an
                                                    Jun 19 '15 at 14:34















                                                  up vote
                                                  -3
                                                  down vote













                                                  --query



                                                  select pslo.stasubtype, pc.relname, pslo.statime
                                                  from pg_stat_last_operation pslo
                                                  join pg_class pc on(pc.relfilenode = pslo.objid)
                                                  and pslo.staactionname = 'CREATE'
                                                  Order By pslo.statime desc


                                                  will help to accomplish desired results



                                                  (tried it on greenplum)






                                                  share|improve this answer





















                                                  • This is the same answer as Manoj's and greenplum specific.
                                                    – dezso
                                                    Nov 7 '14 at 14:12










                                                  • I am using two tables
                                                    – Gurupreet Singh Bhatia
                                                    Dec 8 '14 at 10:59






                                                  • 1




                                                    this does not work in PostgreSQL
                                                    – a1an
                                                    Jun 19 '15 at 14:34













                                                  up vote
                                                  -3
                                                  down vote










                                                  up vote
                                                  -3
                                                  down vote









                                                  --query



                                                  select pslo.stasubtype, pc.relname, pslo.statime
                                                  from pg_stat_last_operation pslo
                                                  join pg_class pc on(pc.relfilenode = pslo.objid)
                                                  and pslo.staactionname = 'CREATE'
                                                  Order By pslo.statime desc


                                                  will help to accomplish desired results



                                                  (tried it on greenplum)






                                                  share|improve this answer












                                                  --query



                                                  select pslo.stasubtype, pc.relname, pslo.statime
                                                  from pg_stat_last_operation pslo
                                                  join pg_class pc on(pc.relfilenode = pslo.objid)
                                                  and pslo.staactionname = 'CREATE'
                                                  Order By pslo.statime desc


                                                  will help to accomplish desired results



                                                  (tried it on greenplum)







                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Nov 7 '14 at 13:51









                                                  Gurupreet Singh Bhatia

                                                  447510




                                                  447510












                                                  • This is the same answer as Manoj's and greenplum specific.
                                                    – dezso
                                                    Nov 7 '14 at 14:12










                                                  • I am using two tables
                                                    – Gurupreet Singh Bhatia
                                                    Dec 8 '14 at 10:59






                                                  • 1




                                                    this does not work in PostgreSQL
                                                    – a1an
                                                    Jun 19 '15 at 14:34


















                                                  • This is the same answer as Manoj's and greenplum specific.
                                                    – dezso
                                                    Nov 7 '14 at 14:12










                                                  • I am using two tables
                                                    – Gurupreet Singh Bhatia
                                                    Dec 8 '14 at 10:59






                                                  • 1




                                                    this does not work in PostgreSQL
                                                    – a1an
                                                    Jun 19 '15 at 14:34
















                                                  This is the same answer as Manoj's and greenplum specific.
                                                  – dezso
                                                  Nov 7 '14 at 14:12




                                                  This is the same answer as Manoj's and greenplum specific.
                                                  – dezso
                                                  Nov 7 '14 at 14:12












                                                  I am using two tables
                                                  – Gurupreet Singh Bhatia
                                                  Dec 8 '14 at 10:59




                                                  I am using two tables
                                                  – Gurupreet Singh Bhatia
                                                  Dec 8 '14 at 10:59




                                                  1




                                                  1




                                                  this does not work in PostgreSQL
                                                  – a1an
                                                  Jun 19 '15 at 14:34




                                                  this does not work in PostgreSQL
                                                  – a1an
                                                  Jun 19 '15 at 14:34


















                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2577168%2fpostgresql-table-creation-time%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                                  Alcedinidae

                                                  Origin of the phrase “under your belt”?