PostgreSQL: Table creation time












33















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





























    33















    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



























      33












      33








      33


      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

      43.6k169095




      43.6k169095










      asked Apr 5 '10 at 6:20









      kiruthikakiruthika

      92962031




      92962031
























          7 Answers
          7






          active

          oldest

          votes


















          14














          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



















          18














          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































            2














            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































              0














              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

































                0














                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































                  -3














                  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



















                  -3














                  --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',
                  autoActivateHeartbeat: false,
                  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









                  14














                  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
















                  14














                  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














                  14












                  14








                  14







                  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 KorbanAlex 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













                  18














                  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




























                    18














                    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


























                      18












                      18








                      18







                      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 HaganderMagnus Hagander

                      16.3k34437




                      16.3k34437























                          2














                          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




























                            2














                            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


























                              2












                              2








                              2







                              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 CantosMarcelo Cantos

                              143k31284328




                              143k31284328























                                  0














                                  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






























                                    0














                                    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




























                                      0












                                      0








                                      0







                                      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
























                                          0














                                          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




























                                            0














                                            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


























                                              0












                                              0








                                              0







                                              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 '18 at 12:52









                                              FatalFatal

                                              1012




                                              1012























                                                  -3














                                                  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
















                                                  -3














                                                  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














                                                  -3












                                                  -3








                                                  -3







                                                  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.2k32944




                                                  11.2k32944










                                                  answered May 1 '13 at 21:36









                                                  ManojManoj

                                                  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











                                                  -3














                                                  --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
















                                                  -3














                                                  --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














                                                  -3












                                                  -3








                                                  -3







                                                  --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 BhatiaGurupreet 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.




                                                  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

                                                  How to change which sound is reproduced for terminal bell?

                                                  Can I use Tabulator js library in my java Spring + Thymeleaf project?

                                                  Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents