Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →MAX(number) + 1 can assign the same invoice number twice because two transactions may read the same maximum before either inserts a row. In PostgreSQL, a unique constraint can stop both copies from being committed, but it cannot generate the next number or guarantee a gapless series. Choose the numbering method based on whether you need unique values or a gapless committed sequence.
Why does MAX() + 1 give duplicate numbers?
It is a read-then-write race. Suppose a tenant’s highest invoice number is 41. Two requests run at nearly the same time:
- Transaction A reads
MAX(number)and gets 41. - Transaction B reads the same maximum and also gets 41.
- Both calculate 42 and try to insert it.
At PostgreSQL’s default isolation level, ordinary reads do not reserve the value they return. Unless the application uses additional coordination, both transactions can proceed with the same result. A unique constraint on the business key—for example, (tenant_id, number)—prevents duplicate rows from being committed, but one request then fails rather than receiving a safely allocated next number.
In an eight-session, 15-second pgbench test on PostgreSQL 17.10, Now-Next reported that default-isolation MAX(number) + 1 issued a previously used number for 140,977 of 161,479 rows. Those rows contained 20,502 distinct values; the article described 87% of invoices in that test as duplicates. These are the publisher’s measurements on one machine with default settings, not a general duplicate rate or a benchmark independently replicated. The test did not cover crashes, replication, or more than eight sessions. See Chris van Eijk’s Now-Next article.
#1 Best Overall
- Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
- Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
- Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
- Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
- 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
Uniqueness and gaplessness are different requirements
Uniqueness means no two issued invoices in the relevant series share a number. Gaplessness means the committed series has no missing numbers. A constraint can enforce uniqueness among stored rows; it cannot ensure that every number in a series is present. A numbering mechanism may provide unique values while leaving gaps after a failed transaction.
The legal and administrative requirement depends on jurisdiction and invoice policy. The Dutch Tax and Customs Administration says to use consecutive invoice numbers in one or more series and that each invoice number may occur only once. That is Dutch guidance, not a universal statement of what every jurisdiction requires. See Belastingdienst guidance on invoice requirements.
Rank #2
- 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
- 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
- Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
- 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
- What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.
Which PostgreSQL numbering approach fits?
| Approach | Concurrent uniqueness | Gaps after rollback | Scope and contention | Failure handling |
|---|---|---|---|---|
MAX(number) + 1 without coordination |
Not safe by itself; concurrent transactions can choose the same value. A unique constraint can reject a collision. | Not a reliable gapless allocator; a rejected request does not resolve the race. | Can be scoped by tenant or series in the query, but that does not prevent concurrent collisions. | Application must handle unique-constraint errors; retrying may be necessary and is not a substitute for a sound allocation design. |
| PostgreSQL sequence | nextval allocates distinct values across concurrent sessions. |
Gaps are possible; values are not reclaimed after transaction aborts, among other documented behavior. | A sequence can be shared or separate sequences can be defined for different scopes; a shared sequence can become a common allocation point. | Usually no collision retry is needed for sequence allocation; the application still handles other insert failures. |
| Counter row updated in the invoice transaction | Safe when each series uses one counter row and the increment and invoice insert commit together. | Rollback undoes both the counter update and invoice insert, allowing the number to be reused. | Requests for the same series serialize on its row; separate tenants or series can update separate rows. | Handle transaction errors normally; keep the lock-holding transaction short. |
MAX(number) + 1 at SERIALIZABLE |
Serialization conflicts prevent unsafe concurrent outcomes if failures are handled correctly. | Can avoid gaps in the reported test when transactions succeed, but the approach incurs aborts and retries. | Conflicting work may serialize or abort; behavior depends on workload. | Application must retry serialization failures. In the cited test, some transactions still failed after the article’s retry limit. |
How do you number invoices per tenant in PostgreSQL?
For a gapless committed series, keep a counter row for each tenant and, if needed, each series. Increment that row and insert the invoice in the same transaction. The row lock coordinates requests for that series; the transaction ensures that a rollback undoes both changes.
Define the series and protect the business key
Decide whether numbering is per tenant, per tenant and year, or another series before writing the allocator. Represent that scope in the counter’s key and the invoice table’s uniqueness rule. For example, use (tenant_id, series) as the counter key and a unique constraint on (tenant_id, series, number) for invoices. If there is only one series per tenant, (tenant_id, number) may be the business key. The unique constraint remains an essential final integrity boundary even when allocation is correct.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Rank #3
- Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
- Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
- Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
- Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
- What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
Allocate and insert in one short transaction
A PostgreSQL pattern is to use an upsert that increments the scoped counter and returns the allocated number, then insert the invoice before committing:
BEGIN;
INSERT INTO invoice_counters (tenant_id, series, last_number)
VALUES (42, '2026', 1)
ON CONFLICT (tenant_id, series)
DO UPDATE SET last_number = invoice_counters.last_number + 1
RETURNING last_number;
-- Use the returned value in the invoice insert.
INSERT INTO invoices (tenant_id, series, number, status)
VALUES (42, '2026', :returned_number, 'issued');
COMMIT;
The first successful insert for a counter key starts at 1 in this example; a conflicting insert increments the existing counter by one. The returned value must be passed to the invoice insert within the same transaction. If the invoice insert fails or the transaction rolls back, the counter update rolls back too. Keep unrelated work—such as slow document generation—outside the transaction so the counter-row lock is held for as little time as practical.
Rank #4
- Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
- Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
- Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
- Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
- Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
Leave drafts unnumbered until issuance
If draft invoices do not need official numbers, create them without one and allocate the number only when finalizing or issuing the invoice. This avoids holding a series number while a draft is edited or while other slow work occurs. Define separately how voided invoices, canceled issuance attempts, and failed issuance are recorded; the database allocation pattern alone does not set those accounting policies.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.When is a sequence the better choice?
Use a PostgreSQL sequence when the requirement is distinct identifiers and occasional gaps are acceptable. PostgreSQL documents that nextval is atomic across concurrent sessions, so concurrent callers receive distinct values. It also explicitly warns: “PostgreSQL sequence objects cannot be used to obtain ‘gapless’ sequences.” Allocated values are not reclaimed after an abort, because doing so could block concurrent transactions. See the PostgreSQL 17 Sequence Manipulation Functions documentation.
Free tools Windows power users keep installed
One-click scans. No signup required.
Best Value
- 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
- Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
- Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
- HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
- What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
A sequence is therefore a good fit for surrogate keys or invoice identifiers where uniqueness matters more than a consecutive issued series. It is not a substitute for the counter-row transaction when policy requires gaplessness.
What does the performance evidence show?
Now-Next’s reported figures come from eight concurrent sessions running for 15 seconds with pgbench on PostgreSQL 17.10, on one machine with default settings. They are workload-specific measurements, not universal throughput guarantees:
| Method and test condition | Reported throughput | Reported outcome |
|---|---|---|
| One sequence, with 10% rollbacks | 12,131 invoices per second | 17,973 of 181,937 values skipped. |
MAX(number) + 1, default isolation |
10,769 invoices per second | 140,977 of 161,479 rows reused a number already issued in the test. |
MAX(number) + 1 under SERIALIZABLE, up to 20 retries |
1,475 invoices per second | No reported duplicates or gaps, but 26.6% of transactions failed. |
| Counter row, one tenant | 2,143 invoices per second | No reported duplicates or gaps in this test case. |
| Counter rows, requests spread over 1,000 tenants | 10,787 invoices per second | No reported duplicates or gaps in this test case. |
| Counter row with 10 ms of other work after taking the number | 94 invoices per second | The additional work extended the time the counter was held. |
| Counter row with 10 ms of other work before taking the number | 746 invoices per second | Moving the other work before allocation raised reported throughput in this added test. |
The contrast between one tenant and 1,000 tenants illustrates the contention trade-off in that workload: requests for one series share a counter row, while different tenants can update different rows. The benchmark did not test beyond eight sessions or examine crashes and replication, so it cannot establish how these designs behave under those conditions. Full benchmark details are in the Now-Next article.
How can you audit existing invoice numbers?
Run checks against the real numbering scope. If numbering is per tenant and series, partition or group by both; if the expected first number is 1, check that separately because a gap-between-rows query cannot detect a missing first number.
Find repeated numbers
SELECT tenant_id, series, number, COUNT(*) AS copies
FROM invoices
GROUP BY tenant_id, series, number
HAVING COUNT(*) > 1;
Find gaps between existing numbers
WITH ordered AS (
SELECT tenant_id,
series,
number,
LEAD(number) OVER (
PARTITION BY tenant_id, series
ORDER BY number
) AS next_number
FROM invoices
)
SELECT tenant_id, series, number + 1 AS first_missing,
next_number - 1 AS last_missing
FROM ordered
WHERE next_number > number + 1;
This identifies ranges missing between extant numbers. It does not determine whether a missing number is an error: that depends on rules for voided invoices, canceled documents, series boundaries, and failed issuance. Validate the first expected number and interpret findings against the policy for each series.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




