{"id":16002390,"url":"https://github.com/modfin/pg-xid","last_synced_at":"2025-04-09T06:13:18.414Z","repository":{"id":37239298,"uuid":"478630516","full_name":"modfin/pg-xid","owner":"modfin","description":"pg-xid is a globally unique id generator for postgres","archived":false,"fork":false,"pushed_at":"2025-03-13T09:47:26.000Z","size":139,"stargazers_count":100,"open_issues_count":0,"forks_count":3,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-02T02:14:57.892Z","etag":null,"topics":["guid","postgres","postgresql","xid"],"latest_commit_sha":null,"homepage":"","language":"Go","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/modfin.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-04-06T15:59:44.000Z","updated_at":"2025-03-31T16:12:18.000Z","dependencies_parsed_at":"2023-01-21T04:02:16.187Z","dependency_job_id":"233873bb-9f62-41b3-8176-94560d8e7bdb","html_url":"https://github.com/modfin/pg-xid","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modfin%2Fpg-xid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modfin%2Fpg-xid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modfin%2Fpg-xid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/modfin%2Fpg-xid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/modfin","download_url":"https://codeload.github.com/modfin/pg-xid/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247987285,"owners_count":21028895,"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":["guid","postgres","postgresql","xid"],"created_at":"2024-10-08T10:01:38.506Z","updated_at":"2025-04-09T06:13:18.378Z","avatar_url":"https://github.com/modfin.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# XID for Postgres, Globally Unique ID Generator\n\n[![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/modfin/pg-xid/master/LICENSE)\n\n\n###### This project is a Postgres implementation of the Go Lang library found here: [https://github.com/rs/xid](https://github.com/rs/xid)\n\n---\n\n## Description\n\n`Xid` is a globally unique id generator functions. They are small and ordered.\n\nXid uses the *Mongo Object ID* algorithm to generate globally unique ids with a different serialization (base32) to make it shorter when transported as a string:\nhttps://docs.mongodb.org/manual/reference/object-id/\n\n\u003ctable border=\"1\"\u003e\n\u003ccaption\u003eXid layout\u003c/caption\u003e\n\u003ctr\u003e\n\u003ctd\u003e0\u003c/td\u003e\u003ctd\u003e1\u003c/td\u003e\u003ctd\u003e2\u003c/td\u003e\u003ctd\u003e3\u003c/td\u003e\u003ctd\u003e4\u003c/td\u003e\u003ctd\u003e5\u003c/td\u003e\u003ctd\u003e6\u003c/td\u003e\u003ctd\u003e7\u003c/td\u003e\u003ctd\u003e8\u003c/td\u003e\u003ctd\u003e9\u003c/td\u003e\u003ctd\u003e10\u003c/td\u003e\u003ctd\u003e11\u003c/td\u003e\n\u003c/tr\u003e\n\u003ctr\u003e\n\u003ctd colspan=\"4\"\u003etime\u003c/td\u003e\u003ctd colspan=\"3\"\u003emachine id\u003c/td\u003e\u003ctd colspan=\"2\"\u003epid\u003c/td\u003e\u003ctd colspan=\"3\"\u003ecounter\u003c/td\u003e\n\u003c/tr\u003e\n\u003c/table\u003e\n\n- 4-byte value representing the seconds since the Unix epoch,\n- 3-byte machine identifier,\n- 2-byte process id, and\n- 3-byte counter, starting with a random value.\n\nThe binary representation of the id is compatible with Mongo 12 bytes Object IDs.\nThe string representation is using [base32 hex (w/o padding)](https://tools.ietf.org/html/rfc4648#page-10) for better space efficiency when stored in that form (20 bytes). The hex variant of base32 is used to retain the\nsortable property of the id.\n\n`Xid`s simply offer uniqueness and speed, but they are not cryptographically secure. They are predictable and can be *brute forced* given enough time.\n\n## Features\n- Size: 12 bytes (96 bits), smaller than UUID, larger than [Twitter Snowflake](https://blog.twitter.com/2010/announcing-snowflake)\n- Base32 hex encoded by default (20 chars when transported as printable string, still sortable)\n- Configuration free: there is no need to set a unique machine and/or data center id\n- K-ordered\n- Embedded time with 1 second precision\n- Unicity guaranteed for 16,777,216 (24 bits) unique ids per second and per host/process\n- Lock-free (unlike UUIDv1 and v2)\n\n## Comparison\n\n| Name        | Binary Size | String Size    | Features\n|-------------|-------------|----------------|----------------\n| [UUID]      | 16 bytes    | 36 chars       | configuration free, not sortable\n| [shortuuid] | 16 bytes    | 22 chars       | configuration free, not sortable\n| [Snowflake] | 8 bytes     | up to 20 chars | needs machine/DC configuration, needs central server, sortable\n| [MongoID]   | 12 bytes    | 24 chars       | configuration free, sortable\n| xid         | 12 bytes    | 20 chars       | configuration free, sortable\n\n[UUID]: https://en.wikipedia.org/wiki/Universally_unique_identifier\n[shortuuid]: https://github.com/stochastic-technologies/shortuuid\n[Snowflake]: https://blog.twitter.com/2010/announcing-snowflake\n[MongoID]: https://docs.mongodb.org/manual/reference/object-id/\n\n## Installation\n\n```bash\ncat ./xid.sql | psql your-database\n```\n\n## Usage\nGenerate a `xid` as base32Hex\n```sql\nSELECT xid() ;\n\n-- +--------------------+\n-- |xid                 |\n-- +--------------------+\n-- |c96r3a88u0k0b3e6hft0|\n-- +--------------------+\n```\n\nGenerate a `xid` as a base32Hex at a specific time\n```sql\nSELECT xid, xid_time(xid) FROM(\n  SELECT xid( _at =\u003e CURRENT_TIMESTAMP - INTERVAL '10 YEAR')\n) a\n\n-- +--------------------+---------------------------------+\n-- |xid                 |xid_time                         |\n-- +--------------------+---------------------------------+\n-- |9tvgr208u0k0b3e6hgb0|2012-04-06 15:36:40.000000 +00:00|\n-- +--------------------+---------------------------------+\n\n```\n\n\nUse a xid as column type\n\n```sql\n\nCREATE TABLE users (\n    id public.xid PRIMARY KEY DEFAULT xid(),\n    email text\n);\n\nINSERT INTO users (email)\nVALUES\n('user1@example.com'),\n('user2@example.com');\n\nSELECT * FROM users;\n\n-- +--------------------+-----------------+\n-- |id                  |email            |\n-- +--------------------+-----------------+\n-- |c96r9eo8u0k0b3e6hgcg|user1@example.com|\n-- |c96r9eo8u0k0b3e6hgd0|user2@example.com|\n-- +--------------------+-----------------+\n\n\n```\n\nDecode a `xid` as byte array \n```sql\nSELECT xid_decode(xid())\n-- +---------------------------------------+\n-- |xid_decode                             |\n-- +---------------------------------------+\n-- |{98,77,178,53,8,240,40,5,141,198,140,2}|\n-- +---------------------------------------+\n\n```\n\nEncode byte array as `xid`\n```sql\nSELECT xid_encode('{98,77,178,53,8,240,40,5,141,198,140,2}'::INT[]);\n-- +---------------------+\n-- |xid_encode           |\n-- +---------------------+\n-- |c96r4d88u0k0b3e6hg10 |\n-- +---------------------+\n\n\n```\n\n\nInspect a `xid`\n```sql\nSELECT id, xid_time(id), xid_machine(id), xid_pid(id), xid_counter(id) \nFROM (\n    SELECT xid() id FROM generate_series(1, 3)\n) a\n\n-- +--------------------+---------------------------------+-----------+-------+-----------+\n-- |id                  |xid_time                         |xid_machine|xid_pid|xid_counter|\n-- +--------------------+---------------------------------+-----------+-------+-----------+\n-- |c96r2ho8u0k0b3e6hfr0|2022-04-06 15:27:03.000000 +00:00|{8,240,40} |1421   |13011958   |\n-- |c96r2ho8u0k0b3e6hfrg|2022-04-06 15:27:03.000000 +00:00|{8,240,40} |1421   |13011959   |\n-- |c96r2ho8u0k0b3e6hfs0|2022-04-06 15:27:03.000000 +00:00|{8,240,40} |1421   |13011960   |\n-- +--------------------+---------------------------------+-----------+-------+-----------+\n```\n\n\n## Test\nTo run the tests you'll need to install docker along with go. Run `go test -v ./xid_test.go`\n\n## Licenses\nThe source code is licensed under the [MIT License](https://github.com/modfin/pg-xid/master/LICENSE).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmodfin%2Fpg-xid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmodfin%2Fpg-xid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmodfin%2Fpg-xid/lists"}