{"id":26239847,"url":"https://github.com/pgsuite/pghist","last_synced_at":"2025-04-23T02:12:50.208Z","repository":{"id":49395261,"uuid":"509416409","full_name":"PGSuite/PGHist","owner":"PGSuite","description":"History and audit of table changes in PostgreSQL","archived":false,"fork":false,"pushed_at":"2025-03-23T17:32:32.000Z","size":418,"stargazers_count":33,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-23T02:12:39.459Z","etag":null,"topics":["audit","changes","history","log","logging","postgresql","table"],"latest_commit_sha":null,"homepage":"http://pghist.org","language":"PLpgSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PGSuite.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-07-01T10:39:56.000Z","updated_at":"2025-04-16T13:38:53.000Z","dependencies_parsed_at":"2024-02-28T18:29:39.454Z","dependency_job_id":"121276f2-bdfa-4c1b-8ffb-6b1da2dbb0cb","html_url":"https://github.com/PGSuite/PGHist","commit_stats":null,"previous_names":["pgsuite/pghist"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PGSuite%2FPGHist","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PGSuite%2FPGHist/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PGSuite%2FPGHist/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PGSuite%2FPGHist/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PGSuite","download_url":"https://codeload.github.com/PGSuite/PGHist/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250354512,"owners_count":21416751,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["audit","changes","history","log","logging","postgresql","table"],"created_at":"2025-03-13T07:17:28.626Z","updated_at":"2025-04-23T02:12:50.201Z","avatar_url":"https://github.com/PGSuite.png","language":"PLpgSQL","funding_links":[],"categories":[],"sub_categories":[],"readme":"## PGHist | History and audit of table changes in PostgreSQL\n\nTool `PGHist` keeps history of table changes and allows to get log(audit) of changes by row,\nlist of changes by field indicating user, time of the change, SQL query, transaction, other technical information\nand table as of date-time in the past (versioning).\nTo display information in user interface, SQL expressions are defined to describe changed table rows and fields.\nIt is possible to override the operation name and username functions.\n\n### Design and working principle ###\n\nPGHIST is a schema with procedures and common tables: transactions, SQL expressions etc.\nWhen history is enabled (procedure `pghist.hist_enable`), for specified table created additional table, triggers for insert,update,delete,truncate, stored procedures and view for obtaining data.\nWhen a table is changed, triggers are fired that modify the history table.\nThere are also event triggers that rebuild the history table and recreate the stored procedures.\n\n### Installation ###\n\nThe installer is [pghist_init.sql](https://github.com/PGHist/PGHist/raw/main/pghist_init.sql) file that creates `pghist` schema.  \nThe installation consists in executing it in the psql terminal client or SQL manager, for example:  \n\n```bash\nwget -O - -q https://github.com/PGHist/PGHist/raw/main/pghist_init.sql | psql -d [database]\n```\n\nOptional. If the developers are not superusers, need to grant them privileges on the pghist schema and its procedures.\nTo do this, use the SQL script [pghist_grants.sql](https://github.com/PGHist/PGHist/raw/main/pghist_grants.sql) with the roles variable. For example:\n\n```bash\nwget -O - -q https://github.com/PGHist/PGHist/raw/main/pghist_grants.sql | psql -d [database] -v roles=[developers]\n```\n\n### Extension ###\n\nTo install `PGHist` as an extension, unpack the [pghist_extension.tar](https://github.com/PGHist/PGHist/raw/main/extension/pghist_extension.tar) archive into the [sharedir]/extension directory of the postgres installation, for example (run as root):\n\n```bash\nwget -O - -q https://github.com/PGHist/PGHist/raw/main/extension/pghist_extension.tar | tar x -C `su - postgres -c \"pg_config --sharedir\"`/extension\n```\nMore info on page [download](https://pghist.org/en/download/)\n\n### Simple example ###\n\n```sql\n-- Create table\ncreate table example(\n  id int primary key,\n  name varchar(20),\n  number numeric(10,2),\n  date date\n);\n\n-- Enable keeping history\ncall pghist.hist_enable('example');\n\n-- Change table\ninsert into example values (1, 'Example', 10, current_date);\nupdate example set number=20, date=date-1;\n\n-- View change log by row\nselect * from example_hist;\n\n-- View changes by field\nselect * from example_changes();\n\n-- View table at timestamp \nselect * from example_at_timestamp(now()-interval '10 second');\n```\n\nAll examples in directory [example](https://github.com/PGHist/PGHist/tree/main/example)\n\n### Main functions and view ###\n  \n*   **pghist.hist\\_enable(\\[schema\\],\\[table\\])** - enable history keeping \n\n*   **\\[schema\\].\\[table\\]_hist** - log(audit) of changes by row, optimized for analysis\n  \n*   **\\[schema\\].\\[table\\]\\_changes** - list of changes by field, optimized for display to the user\n\n*   **\\[schema\\].\\[table\\]\\_at\\_timestamp** - table at date-time in the past (versioning)\n\nDocumentation in file [documentation/documentation.html](https://htmlpreview.github.io/?https://github.com/PGSuite/PGHist/blob/main/documentation/documentation.html)  \n\n### Important qualities ### \n\n*   **Storage optimization** - saving only old values of changed fields and primary key. The Transaction-Expression-Row storage structure matches the operation of the DBMS and minimizes redundancy.\n*   **Versatility** - possible to get change log, list of only changed data from several tables and table at a point in time.\n*   **Descriptions** - for each table and its columns, it is possible to define SQL expressions to describe changed rows and field values. By default, descriptions are created for foreign key fields and table rows\n*   **Inheritance** - stored procedures have parameter \"cascade\" that allows you to get data with or without inheritance\n*   **Transaction and SQL statements** - changes have a reference to SQL statements, that references a transaction. Can get all the changes made within a single transaction or rows within one expression\n*   **Indexes** - for a history table, an index is built on the primary key column(s), for a table at a point in time - standard indexes on columns\n*   **Condition (optional)** - when getting a list of changes, you specify a condition on the primary key or foreign key referencing the master table\n*   **Autocorrection** - when performing DDL operations on a table (alter table, create index, etc.), a trigger fires, that corrects the history keeping. When a table is deleted, its history is also deleted\n\nOverview on site [pghist.org](https://pghist.org/en/)\n\n### Support ### \n\nOf course you can create an issue, I will answer all requests.  \nAlso I will help to install and use the tool.  \nWelcome to discussion !  \n\nWhatsApp: [PGSuite (+7-936-1397626)](https://wa.me/79361397626)  \nemail: [support\\@pgsuite.org](mailto:support@pgsuite.org?subject=PGXLS)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgsuite%2Fpghist","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpgsuite%2Fpghist","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpgsuite%2Fpghist/lists"}