Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For most invoice systems, allocate numbers with a database sequence or identity mechanism and enforce a UNIQUE constraint on the stored invoice number. Avoid an unprotected SELECT MAX(invoice_number) + 1: concurrent users can calculate the same value. Sequences prevent that allocation race, but they can leave gaps. If committed invoice numbers must be gapless, use a transactionally locked counter and accept reduced concurrency; confirm any legal numbering rules for your jurisdiction separately.

How database sequences vs. MAX()+1 for generating invoice numbers compare

Approach Concurrent allocation Gaps Concurrency trade-off When it fits
Unprotected MAX()+1 Two transactions can read the same maximum and calculate the same next value. Without a unique constraint, duplicates can be stored; with one, one insert fails. Depends on application behavior and failed or abandoned operations; it does not itself provide a controlled numbering policy. Can appear simple, but is unsafe under concurrent use unless allocation is serialized. Not recommended as an unprotected allocator.
Database sequence or identity allocator Allocates distinct candidates concurrently. A UNIQUE constraint should still protect stored values. Gaps can result from rollback, unused allocations, conflicts, or crashes, depending on engine. Designed for concurrent allocation; exact behavior varies by engine and configuration. The common choice when unique invoice identifiers matter more than gapless numbering.
Transactionally locked counter row Serializes allocation so each transaction receives the next committed counter value. Can provide gapless assignment for committed allocations when counter update and invoice insert share the transaction; voids and cancellations still need an explicit business policy. More contention and lower concurrency as requests queue for the counter. Only when gapless committed numbering is a genuine requirement.

Can two users get the same invoice number at the same time?

Yes, if both rely on an unprotected MAX()+1. Suppose the current maximum is 104. Transaction A reads 104 and computes 105. Before A inserts, transaction B also reads 104 and computes 105. Both then try to insert the same number. A database unique constraint makes one insert fail rather than allowing duplicate stored numbers; without that constraint, both may succeed. PostgreSQL documents that unique constraints enforce uniqueness and create an index automatically: PostgreSQL CREATE TABLE documentation.

Use a unique constraint as a database invariant, not just a check in application code. A query such as “does this number already exist?” followed by an insert can itself race: two requests can both check before either has stored the value.

Why use a sequence or identity allocator?

A database sequence is built to allocate candidate values safely across concurrent sessions. PostgreSQL documents nextval as atomic across sessions, while SQL Server provides independent sequence objects. These features solve the candidate-allocation race, not the separate question of whether invoice rows contain unique values. Keep a unique constraint on the invoice-number column as well.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • 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.

Use an identity-style column when the database should assign a value as part of inserting a row; use a sequence object when you need to request a value separately or apply it in a broader allocation design. The right mechanism and its exact behavior depend on the database engine and configuration.

Why are there gaps in my sequence?

Gaps are an expected consequence of common sequence mechanisms, not proof that an invoice row is missing. A number may be allocated and then not used because the surrounding transaction rolls back, an insert encounters a conflict, or the application abandons the operation. PostgreSQL explicitly says that nextval values are not reclaimed when a transaction aborts, to avoid blocking concurrent transactions. PostgreSQL also notes gaps from crashes and conflict handling; SQL Server identifies rollbacks and unused allocations as causes.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 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.

In PostgreSQL’s words, “the value obtained by nextval is not reclaimed for re-use if the calling transaction later aborts.” See PostgreSQL 15 sequence documentation and Microsoft’s SQL Server sequence documentation.

Do not try to fill a gap by reusing an earlier number unless the applicable accounting rules and your system’s records support that practice. A numbering gap and a missing invoice are different facts.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • 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.

Do invoice numbers have to be gapless?

That is a jurisdiction-specific compliance question, not something database behavior can answer. The database manuals cited here explain allocation and concurrency; they do not establish whether a particular tax authority permits gaps, requires a numbering scope or format, or specifies how voided invoices must be treated. Check the relevant local tax or accounting rules, or consult a qualified adviser.

If your business requirement is specifically gapless committed numbering, allocate from a counter row inside the same transaction as the invoice insert. The transaction must hold the counter lock through the insert and commit; otherwise another transaction could advance the counter while the first invoice fails, or the allocation could escape the intended serialization. PostgreSQL documents this pattern as possible but much more expensive than sequence objects, especially with many concurrent requests: PostgreSQL CREATE SEQUENCE documentation.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • 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

Agree on how cancellations and voids are represented before implementing the counter. A gapless allocator cannot determine the accounting treatment of an invoice that has been issued and later cancelled.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What changes across database engines?

Do not assume every engine has identical allocation, locking, gap, or replication behavior. MySQL InnoDB, for example, has configurable AUTO_INCREMENT lock modes. More concurrent modes can allow interleaving and gaps in values produced by a statement, and the locking choice can interact with replication requirements. Review the exact engine version and configuration in use: MySQL 8.4 InnoDB AUTO_INCREMENT handling.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 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.

Before choosing an allocator, establish whether a number is needed before an invoice row is inserted, what ordering the business actually requires, and whether replicas or failover configurations affect allocation. A sequence’s uniqueness guarantee should not be mistaken for a guarantee of gaplessness or ordering by transaction commit time.

Implementation choices and safeguards

  • Typical system: use the engine’s sequence or identity-style allocator, and add a unique constraint on the invoice-number field.
  • Gapless committed numbers required: update a counter row and insert the invoice within one transaction, keeping the lock until commit; assess lock contention under expected concurrent demand.
  • Retry after failure: retry only when the database reports the specifically identified uniqueness conflict, and only if the whole operation is safe to repeat. Do not retry every error as if it were a numbering collision.
  • Compliance uncertain: verify the governing jurisdiction’s rules for number scope, format, gaps, and voids before treating a technical design as compliant.

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.