Hash-based sharding is a method of distributing data across multiple database shards by applying a hash function to a specific key. The hash function generates a hash value, which is then used to determine the shard where the data should be stored. This approach ensures a more uniform distribution of data compared to direct key sharding.
Hash-based sharding still uses key ranges, just like direct key sharding. The difference is that your key is first transformed by a hash function (e.g., MURMUR or CITY), and the resulting hash value is then routed using key ranges defined over the hash space.
For example, if you are sharding based on user IDs, the hash function will take a user ID as input and produce a hash value. This hash value is then mapped to one of the available shards using key ranges. The same hash function is used consistently to ensure that the same key always maps to the same shard.Hash-based sharding helps to evenly distribute the data and load across all shards, reducing the risk of hotspots and ensuring better performance and scalability. However, it can make range queries more complex, as the data for a given range of keys may be spread across multiple shards.SPQR also supports composite (multi-column) sharding keys with hash functions, which is particularly useful when you have a low-cardinality first column combined with a high-cardinality second column.
To use a hash function as your distribution(sharding) key, you need to specify it in the administrative router or coordination console using the HASH FUNCTION keyword:
ALTER DISTRIBUTION ds1 ATTACH RELATION r4 DISTRIBUTION KEY col1 HASH FUNCTION CITY; attach table------------------------ relation name -> r4 distribution id -> ds1(2 rows)
Then, you create key ranges over the hash space. For hash functions like MURMUR, the output is a 32-bit unsigned integer, so the hash space is 0 to 4294967295:
-- Split hash space between two shards (create in descending order by lower bound)-- Second half: 2147483648 to 4294967295 -> shard02-- First half: 0 to 2147483647 -> shard01CREATE KEY RANGE krid2 FROM 2147483648 ROUTE TO shard02 FOR DISTRIBUTION ds1; add key range--------------- bound -> 2147483648(1 row)CREATE KEY RANGE krid1 FROM 0 ROUTE TO shard01 FOR DISTRIBUTION ds1; add key range--------------- bound -> 0(1 row)
When creating multiple key ranges on different shards, you must create them in descending order by lower bound (highest first). Creating them in ascending order will result in key range ... intersects with key range ... errors.
{"docsConfig":{"$schema":"https://mintlify.com/docs.json","theme":"mint","name":"SPQR","colors":{"primary":"#5382ff","light":"#e2ebff","dark":"#5382ff"},"favicon":"https://mintcdn.com/pg-shardingspqr/55SknXFsB6PObVk2/logo/spqr_icon-main.svg?fit=max\u0026auto=format\u0026n=55SknXFsB6PObVk2\u0026q=85\u0026s=d6c32807f9b0a07418733c3d548908d3","logo":{"light":"https://mintcdn.com/pg-shardingspqr/55SknXFsB6PObVk2/logo/spqr_icon-main.svg?fit=max\u0026auto=format\u0026n=55SknXFsB6PObVk2\u0026q=85\u0026s=d6c32807f9b0a07418733c3d548908d3","dark":"https://mintcdn.com/pg-shardingspqr/55SknXFsB6PObVk2/logo/spqr_icon-main.svg?fit=max\u0026auto=format\u0026n=55SknXFsB6PObVk2\u0026q=85\u0026s=d6c32807f9b0a07418733c3d548908d3","href":"https://pg-sharding.tech/"},"appearance":{"default":"light","strict":null},"navbar":{"primary":{"type":"github","href":"https://github.com/pg-sharding/spqr"}},"styling":{"latex":false},"navigation":{"pages":[]}},"deployment":{"deploymentMetadataValue":{"subdomain":"pg-shardingspqr","hasAudioNarration":false,"actualSubdomain":"pg-shardingspqr","customDomains":["docs.pg-sharding.tech"],"gitSource":{"type":"github","owner":"pg-sharding","repo":"spqr","deployBranch":"master","contentDirectory":"docs","isPrivate":false,"mountPath":"","label":""},"chroma":{"liveDeploymentHistoryId":"6ae34cd6af12d5279fa8389f","collectionId":"1d1ce3cd-bfbd-4327-881b-81bd5976a9ed","schemaVersion":"v3","latestBuiltDeploymentHistoryId":"6ae34cd6af12d5279fa8389f"},"feedback":{"thumbs":true,"edits":true},"entitlements":{},"buildId":"6ae34cd6af12d5279fa8389f","clientVersion":"0.0.4043","searchFilterCounts":[{"language":"default","tag":"Documentation","count":102},{"language":"default","tag":"Sharding","count":19},{"language":"default","tag":"Features","count":16},{"language":"default","tag":"Configuration Guide","count":6},{"language":"default","tag":"Deployment Guide","count":5},{"language":"default","tag":"Cluster Components","count":4},{"language":"default","tag":"Balancing","count":3},{"language":"default","tag":"Admin Console","count":3},{"language":"default","tag":"Query routing","count":1}]},"assistantConfig":{},"basePath":"","hasChatPermissions":false,"hasWebMcpPermissions":false,"toggles":[],"googleFontsInlined":false},"customCss":[],"page":{"pagePath":"sharding/hashed","slug":"/sharding/hashed","pageMetadata":{"title":"Hash-based sharding","description":null,"href":"/sharding/hashed"},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{Fragment:_Fragment,jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={a:`a`,code:`code`,p:`p`,pre:`pre`,span:`span`,strong:`strong`,..._provideComponents(),...props.components},{CodeBlock,Heading,Note,OptimizedImage,Warning}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Note)_missingMdxReference(`Note`,true);if(!OptimizedImage)_missingMdxReference(`OptimizedImage`,true);if(!Warning)_missingMdxReference(`Warning`,true);return _jsxs(_Fragment,{children:[_jsxs(_components.p,{children:[`Hash-based sharding is a method of distributing data across multiple database shards by applying a hash function to a specific key. The hash function generates a hash value, which is then used to determine the shard where the data should be stored. This approach ensures a more uniform distribution of data compared to `,_jsx(_components.a,{href:`/sharding/ranged`,children:`direct key sharding`}),`.`]}),`\n`,_jsx(Note,{children:_jsxs(_components.p,{children:[`Hash-based sharding `,_jsx(_components.strong,{children:`still uses key ranges`}),`, just like direct key sharding. The difference is that your key is first transformed by a hash function (e.g., MURMUR or CITY), and the resulting hash value is then routed using key ranges defined over the hash space.`]})}),`\n`,_jsx(_components.p,{children:`For example, if you are sharding based on user IDs, the hash function will take a user ID as input and produce a hash value. This hash value is then mapped to one of the available shards using key ranges. The same hash function is used consistently to ensure that the same key always maps to the same shard.`}),`\n`,_jsx(_components.p,{children:`Hash-based sharding helps to evenly distribute the data and load across all shards, reducing the risk of hotspots and ensuring better performance and scalability. However, it can make range queries more complex, as the data for a given range of keys may be spread across multiple shards.`}),`\n`,_jsxs(_components.p,{children:[`SPQR also supports `,_jsx(_components.a,{href:`/sharding/composite_keys`,children:`composite (multi-column) sharding keys`}),` with hash functions, which is particularly useful when you have a low-cardinality first column combined with a high-cardinality second column.`]}),`\n`,_jsx(OptimizedImage,{src:`https://mintcdn.com/pg-shardingspqr/9v2nyWcfr6Uwa9yC/images/balancing_hashed.png?fit=max\u0026auto=format\u0026n=9v2nyWcfr6Uwa9yC\u0026q=85\u0026s=eef2aa052ebde94bc454f4e7df279b5c`,alt:`hashed`,\"data-og-width\":`2377`,width:`2377`,\"data-og-height\":`1476`,height:`1476`,\"data-path\":`images/balancing_hashed.png`,\"data-optimize\":`true`,\"data-opv\":`3`,srcset:`https://mintcdn.com/pg-shardingspqr/9v2nyWcfr6Uwa9yC/images/balancing_hashed.png?w=280\u0026fit=max\u0026auto=format\u0026n=9v2nyWcfr6Uwa9yC\u0026q=85\u0026s=c41e84a5707c195264f0d9fa2ccf8d3a 280w, https://mintcdn.com/pg-shardingspqr/9v2nyWcfr6Uwa9yC/images/balancing_hashed.png?w=560\u0026fit=max\u0026auto=format\u0026n=9v2nyWcfr6Uwa9yC\u0026q=85\u0026s=bca96360b749546aa6a71d7f447f41ec 560w, https://mintcdn.com/pg-shardingspqr/9v2nyWcfr6Uwa9yC/images/balancing_hashed.png?w=840\u0026fit=max\u0026auto=format\u0026n=9v2nyWcfr6Uwa9yC\u0026q=85\u0026s=0d7c99c9d7c095d0c060c7cfa3c62063 840w, https://mintcdn.com/pg-shardingspqr/9v2nyWcfr6Uwa9yC/images/balancing_hashed.png?w=1100\u0026fit=max\u0026auto=format\u0026n=9v2nyWcfr6Uwa9yC\u0026q=85\u0026s=4f96400e414665da223fb2b87abe34ec 1100w, https://mintcdn.com/pg-shardingspqr/9v2nyWcfr6Uwa9yC/images/balancing_hashed.png?w=1650\u0026fit=max\u0026auto=format\u0026n=9v2nyWcfr6Uwa9yC\u0026q=85\u0026s=c2ce463e77e5138cf456a1be59f4ad2d 1650w, https://mintcdn.com/pg-shardingspqr/9v2nyWcfr6Uwa9yC/images/balancing_hashed.png?w=2500\u0026fit=max\u0026auto=format\u0026n=9v2nyWcfr6Uwa9yC\u0026q=85\u0026s=0171e09e98d6c74a4c2a436e1cb8e777 2500w`}),`\n`,_jsx(Heading,{level:`2`,id:`how-to-use-it`,children:`How to use it?`}),`\n`,_jsxs(_components.p,{children:[`To use a hash function as your distribution(sharding) key, you need to specify it in the administrative router or coordination console using the `,_jsx(_components.code,{children:`HASH FUNCTION`}),` keyword:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`6`,language:`sql`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`sql`,children:_jsxs(_components.code,{language:`sql`,numberOfLines:`6`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`ALTER`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` DISTRIBUTION`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ds1 `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`ATTACH`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` RELATION r4 `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`DISTRIBUTION`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` KEY`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` col1 `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`HASH`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` FUNCTION`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` CITY;`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` attach`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` table`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` `})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`------------------------`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` relation `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`name`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:` ->`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` r4`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` distribution`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` id `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`->`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ds1`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`2`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` rows`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[`Then, you create key ranges over the hash space. For hash functions like MURMUR, the output is a 32-bit unsigned integer, so the hash space is `,_jsx(_components.code,{children:`0`}),` to `,_jsx(_components.code,{children:`4294967295`}),`:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`14`,language:`sql`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`sql`,children:_jsxs(_components.code,{language:`sql`,numberOfLines:`14`,children:[_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`-- Split hash space between two shards (create in descending order by lower bound)`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`-- Second half: 2147483648 to 4294967295 -> shard02`})}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`-- First half: 0 to 2147483647 -> shard01`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`CREATE`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` KEY`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` RANGE`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` krid2 `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`FROM`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 2147483648`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` ROUTE`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` TO`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` shard02 `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`FOR`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` DISTRIBUTION`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ds1;`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` add`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` key`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` range`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`---------------`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` bound `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`->`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 2147483648`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` row`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`CREATE`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` KEY`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` RANGE`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` krid1 `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`FROM`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 0`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` ROUTE`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` TO`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` shard01 `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`FOR`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` DISTRIBUTION`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` ds1;`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` add`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` key`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` range`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`---------------`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` bound `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`->`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 0`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` row`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`]})})}),`\n`,_jsx(Warning,{children:_jsxs(_components.p,{children:[`When creating multiple key ranges on different shards, you must create them in descending order by lower bound (highest first). Creating them in ascending order will result in `,_jsx(_components.code,{children:`key range ... intersects with key range ...`}),` errors.`]})}),`\n`,_jsxs(_components.p,{children:[`For more examples, see `,_jsx(_components.a,{href:`https://github.com/pg-sharding/spqr/blob/master/test/regress/tests/console/expected/hash.out`,children:`this test`}),`.`]})]})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"config":{},"pageMetadata":{"title":"Hash-based sharding","description":null,"href":"/sharding/hashed"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"How to use it?","slug":"how-to-use-it","depth":2,"children":[]}],"codeExamples":{}},"jsLinks":[],"subdomain":"pg-shardingspqr","siteName":"SPQR","title":"Hash-based sharding - SPQR","navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2026-10-21T13:04:01.432Z","originalFileLocation":"sharding/hashed.mdx"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"light\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"application-name\" content=\"SPQR\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon/android-chrome-192x192.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/png\" sizes=\"180x180\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon/apple-touch-icon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon/favicon-16x16.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon/favicon-32x32.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon/favicon.ico\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon-dark/android-chrome-192x192.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon-dark/favicon-16x16.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon-dark/favicon-32x32.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon-dark/favicon.ico\" media=\"(prefers-color-scheme: dark)\">\u003cmeta data-mint-head property=\"og:title\" content=\"Hash-based sharding - SPQR\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"SPQR\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://docs.pg-sharding.tech/sharding/hashed\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://pg-shardingspqr.mintlify.app/_mintlify/api/og/v2?division=Sharding\u0026amp;title=Hash-based+sharding\u0026amp;theme=e465a5f6c0c6956ccd8133e1\u0026amp;signature=a52ea5baee34e4cce0b31e39f4c4f3f77798df26e94fdaa5c87c5fcbfc75a9db\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"Hash-based sharding - SPQR\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://pg-shardingspqr.mintlify.app/_mintlify/api/og/v2?division=Sharding\u0026amp;title=Hash-based+sharding\u0026amp;theme=e465a5f6c0c6956ccd8133e1\u0026amp;signature=a52ea5baee34e4cce0b31e39f4c4f3f77798df26e94fdaa5c87c5fcbfc75a9db\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"/mintlify-assets/_mintlify/favicons/pg-shardingspqr/nzsKKGyE6s8yEmTB/_generated/favicon/browserconfig.xml\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"SPQR\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#5382ff\">\u003clink data-mint-head rel=\"canonical\" href=\"https://docs.pg-sharding.tech/sharding/hashed\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/sharding/hashed.md\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://docs.pg-sharding.tech/#organization\",\"name\":\"SPQR\",\"url\":\"https://docs.pg-sharding.tech\",\"logo\":{\"@type\":\"ImageObject\",\"url\":\"https://mintcdn.com/pg-shardingspqr/55SknXFsB6PObVk2/logo/spqr_icon-main.svg?fit=max\u0026auto=format\u0026n=55SknXFsB6PObVk2\u0026q=85\u0026s=d6c32807f9b0a07418733c3d548908d3\"}},{\"@type\":\"WebSite\",\"@id\":\"https://docs.pg-sharding.tech#website\",\"name\":\"SPQR\",\"url\":\"https://docs.pg-sharding.tech\",\"publisher\":{\"@id\":\"https://docs.pg-sharding.tech/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://docs.pg-sharding.tech/sharding/hashed#webpage\",\"url\":\"https://docs.pg-sharding.tech/sharding/hashed\",\"name\":\"Hash-based sharding\",\"dateModified\":\"2026-10-21T13:04:01.432Z\",\"isPartOf\":{\"@id\":\"https://docs.pg-sharding.tech#website\"},\"breadcrumb\":{\"@id\":\"https://docs.pg-sharding.tech/sharding/hashed#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://docs.pg-sharding.tech/sharding/hashed#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Documentation\",\"item\":\"https://docs.pg-sharding.tech/welcome/index\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sharding\",\"item\":\"https://docs.pg-sharding.tech/welcome/get_started\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Hash-based sharding\",\"item\":\"https://docs.pg-sharding.tech/sharding/hashed\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://docs.pg-sharding.tech/sharding/hashed#article\",\"headline\":\"Hash-based sharding\",\"name\":\"Hash-based sharding\",\"url\":\"https://docs.pg-sharding.tech/sharding/hashed\",\"mainEntityOfPage\":{\"@id\":\"https://docs.pg-sharding.tech/sharding/hashed#webpage\"},\"dateModified\":\"2026-10-21T13:04:01.432Z\",\"publisher\":{\"@id\":\"https://docs.pg-sharding.tech/#organization\"},\"isPartOf\":{\"@id\":\"https://docs.pg-sharding.tech#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"SPQR\",\"creator\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","siteName":"SPQR","nav":{"global":null,"anchors":[{"anchor":"Documentation","icon":"book-open-cover","groups":[{"group":"Documentation","pages":[{"title":"Welcome to SPQR","href":"/welcome"},{"title":"Get Started","href":"/welcome/get_started"},{"group":"Features","pages":[{"title":"Sharding","href":"/features/sharding"},{"title":"Default shard","href":"/features/default_shard"},{"title":"Reference tables","href":"/features/reference_tables"},{"title":"Distributed Transactions","href":"/features/distributed_transactions"},{"title":"Transaction and session pooling","href":"/features/pooling"},{"title":"Fault Tolerance","href":"/features/fault_tolerance"},{"title":"Data Balancing","href":"/features/data_balancing"},{"title":"Cross-shard queries","href":"/features/cross_shard_queries"},{"title":"Multiple servers and failover","href":"/features/multiple_servers_and_failover"},{"title":"Works over PostgreSQL protocol","href":"/features/protocol"},{"title":"Dedicated read-only mode","href":"/features/read_only"},{"title":"Performance","href":"/features/performance"},{"title":"Various authentication types","href":"/features/authentication"},{"title":"Live configuration reloading","href":"/features/config_reloading"},{"title":"Statistics","href":"/features/statistics"},{"title":"Metrics","href":"/features/metrics"}]},{"group":"Deployment Guide","pages":[{"title":"Overview","href":"/deployment/overview"},{"title":"Router with Init SQL","href":"/deployment/bare_router"},{"title":"Router + Coordinator","href":"/deployment/router_coordinator"},{"title":"SPQR Infra Mode","href":"/deployment/spqrinfra"},{"title":"Prevent direct writes to shards","href":"/deployment/spqrguard"}]},{"group":"Configuration Guide","pages":[{"title":"Router","href":"/configuration/router"},{"title":"Virtual functions","href":"/configuration/virtual_function"},{"title":"Coordinator","href":"/configuration/coordinator"},{"title":"Balancer","href":"/configuration/balancer"},{"title":"Auth settings","href":"/configuration/auth"},{"title":"Roles settings","href":"/configuration/roles"}]},{"group":"Sharding","pages":[{"title":"Get Started","href":"/welcome/get_started"},{"title":"Direct key sharding","description":null,"href":"/sharding/ranged"},{"title":"Hash-based sharding","href":"/sharding/hashed"},{"title":"Sharded Tables","description":null,"href":"/sharding/sharded_tables"},{"title":"Reference Tables","href":"/sharding/reference_tables"},{"title":"Composite Sharding Keys","href":"/sharding/composite_keys"},{"title":"Distributed Transactions","href":"/sharding/distributed_transactions"},{"group":"Cluster Components","pages":[{"title":"Overview","href":"/sharding/cluster_components/overview"},{"title":"Shard","href":"/sharding/cluster_components/shard"},{"title":"Router","href":"/sharding/cluster_components/router"},{"title":"Coordinator","href":"/sharding/cluster_components/coordinator"}]},{"group":"Balancing","pages":[{"title":"Overview","href":"/sharding/balancing/overview"},{"title":"Details","href":"/sharding/balancing/details"},{"title":"Manual shard rebalancing","href":"/sharding/manual_rebalancing"}]},{"group":"Admin Console","pages":[{"title":"How to connect","href":"/sharding/console/how_to_connect"},{"title":"Concepts","href":"/sharding/console/concepts"},{"title":"SQL commands","href":"/sharding/console/sql_commands"}]},{"title":"Bulk Operations","href":"/sharding/bulk"},{"title":"Default shard","href":"/sharding/default_shard"}]},{"group":"Query routing","pages":[{"title":"Routing hints","href":"/routing/hints"}]},{"title":"Design Goals","href":"/welcome/design"},{"title":"Talks","href":"/welcome/talks"},{"title":"FAQ","href":"/welcome/faq"}]}]},{"anchor":"Community","href":"https://t.me/+jMGhyjwicpI3ZWQy","icon":"telegram"},{"anchor":"Releases","href":"https://github.com/pg-sharding/spqr/releases","icon":"clock"}]},"basePath":"","title":"Hash-based sharding - SPQR","buildId":"dpl_G9nrg4qeJSZtah1Ziwbc2n6WkLch","deploymentId":"dpl_G9nrg4qeJSZtah1Ziwbc2n6WkLch","dynamics":["a74be98ba7"],"chunks":["zoom-image-lightbox-DL4wrF0w.js","lightbox-handle-r17bVUz3.js"]}