{
    "content": [
        {
            "type": "text",
            "text": "# LOCK(7) (man)\n\n**Summary:** LOCK - lock a table\n\n**Synopsis:** LOCK [ TABLE ] [ ONLY ] name [ * ] [, ...] [ IN lockmode MODE ] [ NOWAIT ]\nwhere lockmode is one of:\nACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE\n| SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE\n\n## Examples\n\n- `Obtain a SHARE lock on a primary key table when going to perform inserts into a foreign key`\n- `table:`\n- `BEGIN WORK;`\n- `LOCK TABLE films IN SHARE MODE;`\n- `SELECT id FROM films`\n- `WHERE name = 'Star Wars: Episode I - The Phantom Menace';`\n- `-- Do ROLLBACK if record was not returned`\n- `INSERT INTO filmsusercomments VALUES`\n- `(id, 'GREAT! I was waiting for it for so long!');`\n- `COMMIT WORK;`\n- `Take a SHARE ROW EXCLUSIVE lock on a primary key table when going to perform a delete`\n- `operation:`\n- `BEGIN WORK;`\n- `LOCK TABLE films IN SHARE ROW EXCLUSIVE MODE;`\n- `DELETE FROM filmsusercomments WHERE id IN`\n- `(SELECT id FROM films WHERE rating < 5);`\n- `DELETE FROM films WHERE rating < 5;`\n- `COMMIT WORK;`\n\n## Section Outline\n\n- **NAME** (2 lines)\n- **SYNOPSIS** (7 lines)\n- **DESCRIPTION** (40 lines)\n- **PARAMETERS** (20 lines)\n- **NOTES** (22 lines)\n- **EXAMPLES** (22 lines)\n- **COMPATIBILITY** (10 lines)\n\n## Full Content\n\n### NAME\n\nLOCK - lock a table\n\n### SYNOPSIS\n\nLOCK [ TABLE ] [ ONLY ] name [ * ] [, ...] [ IN lockmode MODE ] [ NOWAIT ]\n\nwhere lockmode is one of:\n\nACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE\n| SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE\n\n### DESCRIPTION\n\nLOCK TABLE obtains a table-level lock, waiting if necessary for any conflicting locks to be\nreleased. If NOWAIT is specified, LOCK TABLE does not wait to acquire the desired lock: if it\ncannot be acquired immediately, the command is aborted and an error is emitted. Once\nobtained, the lock is held for the remainder of the current transaction. (There is no UNLOCK\nTABLE command; locks are always released at transaction end.)\n\nWhen a view is locked, all relations appearing in the view definition query are also locked\nrecursively with the same lock mode.\n\nWhen acquiring locks automatically for commands that reference tables, PostgreSQL always uses\nthe least restrictive lock mode possible.  LOCK TABLE provides for cases when you might need\nmore restrictive locking. For example, suppose an application runs a transaction at the READ\nCOMMITTED isolation level and needs to ensure that data in a table remains stable for the\nduration of the transaction. To achieve this you could obtain SHARE lock mode over the table\nbefore querying. This will prevent concurrent data changes and ensure subsequent reads of the\ntable see a stable view of committed data, because SHARE lock mode conflicts with the ROW\nEXCLUSIVE lock acquired by writers, and your LOCK TABLE name IN SHARE MODE statement will\nwait until any concurrent holders of ROW EXCLUSIVE mode locks commit or roll back. Thus, once\nyou obtain the lock, there are no uncommitted writes outstanding; furthermore none can begin\nuntil you release the lock.\n\nTo achieve a similar effect when running a transaction at the REPEATABLE READ or SERIALIZABLE\nisolation level, you have to execute the LOCK TABLE statement before executing any SELECT or\ndata modification statement. A REPEATABLE READ or SERIALIZABLE transaction's view of data\nwill be frozen when its first SELECT or data modification statement begins. A LOCK TABLE\nlater in the transaction will still prevent concurrent writes — but it won't ensure that what\nthe transaction reads corresponds to the latest committed values.\n\nIf a transaction of this sort is going to change the data in the table, then it should use\nSHARE ROW EXCLUSIVE lock mode instead of SHARE mode. This ensures that only one transaction\nof this type runs at a time. Without this, a deadlock is possible: two transactions might\nboth acquire SHARE mode, and then be unable to also acquire ROW EXCLUSIVE mode to actually\nperform their updates. (Note that a transaction's own locks never conflict, so a transaction\ncan acquire ROW EXCLUSIVE mode when it holds SHARE mode — but not if anyone else holds SHARE\nmode.) To avoid deadlocks, make sure all transactions acquire locks on the same objects in\nthe same order, and if multiple lock modes are involved for a single object, then\ntransactions should always acquire the most restrictive mode first.\n\nMore information about the lock modes and locking strategies can be found in Section 13.3.\n\n### PARAMETERS\n\nname\nThe name (optionally schema-qualified) of an existing table to lock. If ONLY is specified\nbefore the table name, only that table is locked. If ONLY is not specified, the table and\nall its descendant tables (if any) are locked. Optionally, * can be specified after the\ntable name to explicitly indicate that descendant tables are included.\n\nThe command LOCK TABLE a, b; is equivalent to LOCK TABLE a; LOCK TABLE b;. The tables are\nlocked one-by-one in the order specified in the LOCK TABLE command.\n\nlockmode\nThe lock mode specifies which locks this lock conflicts with. Lock modes are described in\nSection 13.3.\n\nIf no lock mode is specified, then ACCESS EXCLUSIVE, the most restrictive mode, is used.\n\nNOWAIT\nSpecifies that LOCK TABLE should not wait for any conflicting locks to be released: if\nthe specified lock(s) cannot be acquired immediately without waiting, the transaction is\naborted.\n\n### NOTES\n\nLOCK TABLE ... IN ACCESS SHARE MODE requires SELECT privileges on the target table.  LOCK\nTABLE ... IN ROW EXCLUSIVE MODE requires INSERT, UPDATE, DELETE, or TRUNCATE privileges on\nthe target table. All other forms of LOCK require table-level UPDATE, DELETE, or TRUNCATE\nprivileges.\n\nThe user performing the lock on the view must have the corresponding privilege on the view.\nIn addition the view's owner must have the relevant privileges on the underlying base\nrelations, but the user performing the lock does not need any permissions on the underlying\nbase relations.\n\nLOCK TABLE is useless outside a transaction block: the lock would remain held only to the\ncompletion of the statement. Therefore PostgreSQL reports an error if LOCK is used outside a\ntransaction block. Use BEGIN and COMMIT (or ROLLBACK) to define a transaction block.\n\nLOCK TABLE only deals with table-level locks, and so the mode names involving ROW are all\nmisnomers. These mode names should generally be read as indicating the intention of the user\nto acquire row-level locks within the locked table. Also, ROW EXCLUSIVE mode is a shareable\ntable lock. Keep in mind that all the lock modes have identical semantics so far as LOCK\nTABLE is concerned, differing only in the rules about which modes conflict with which. For\ninformation on how to acquire an actual row-level lock, see Section 13.3.2 and The Locking\nClause in the SELECT(7) documentation.\n\n### EXAMPLES\n\nObtain a SHARE lock on a primary key table when going to perform inserts into a foreign key\ntable:\n\nBEGIN WORK;\nLOCK TABLE films IN SHARE MODE;\nSELECT id FROM films\nWHERE name = 'Star Wars: Episode I - The Phantom Menace';\n-- Do ROLLBACK if record was not returned\nINSERT INTO filmsusercomments VALUES\n(id, 'GREAT! I was waiting for it for so long!');\nCOMMIT WORK;\n\nTake a SHARE ROW EXCLUSIVE lock on a primary key table when going to perform a delete\noperation:\n\nBEGIN WORK;\nLOCK TABLE films IN SHARE ROW EXCLUSIVE MODE;\nDELETE FROM filmsusercomments WHERE id IN\n(SELECT id FROM films WHERE rating < 5);\nDELETE FROM films WHERE rating < 5;\nCOMMIT WORK;\n\n### COMPATIBILITY\n\nThere is no LOCK TABLE in the SQL standard, which instead uses SET TRANSACTION to specify\nconcurrency levels on transactions.  PostgreSQL supports that too; see SET TRANSACTION\n(SETTRANSACTION(7)) for details.\n\nExcept for ACCESS SHARE, ACCESS EXCLUSIVE, and SHARE UPDATE EXCLUSIVE lock modes, the\nPostgreSQL lock modes and the LOCK TABLE syntax are compatible with those present in Oracle.\n\n\n\nPostgreSQL 14.23                                2026                                         LOCK(7)\n\n"
        }
    ],
    "structuredContent": {
        "command": "LOCK",
        "section": "7",
        "mode": "man",
        "summary": "LOCK - lock a table",
        "synopsis": "LOCK [ TABLE ] [ ONLY ] name [ * ] [, ...] [ IN lockmode MODE ] [ NOWAIT ]\nwhere lockmode is one of:\nACCESS SHARE | ROW SHARE | ROW EXCLUSIVE | SHARE UPDATE EXCLUSIVE\n| SHARE | SHARE ROW EXCLUSIVE | EXCLUSIVE | ACCESS EXCLUSIVE",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [
            "Obtain a SHARE lock on a primary key table when going to perform inserts into a foreign key",
            "table:",
            "BEGIN WORK;",
            "LOCK TABLE films IN SHARE MODE;",
            "SELECT id FROM films",
            "WHERE name = 'Star Wars: Episode I - The Phantom Menace';",
            "-- Do ROLLBACK if record was not returned",
            "INSERT INTO filmsusercomments VALUES",
            "(id, 'GREAT! I was waiting for it for so long!');",
            "COMMIT WORK;",
            "Take a SHARE ROW EXCLUSIVE lock on a primary key table when going to perform a delete",
            "operation:",
            "BEGIN WORK;",
            "LOCK TABLE films IN SHARE ROW EXCLUSIVE MODE;",
            "DELETE FROM filmsusercomments WHERE id IN",
            "(SELECT id FROM films WHERE rating < 5);",
            "DELETE FROM films WHERE rating < 5;",
            "COMMIT WORK;"
        ],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 7,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 40,
                "subsections": []
            },
            {
                "name": "PARAMETERS",
                "lines": 20,
                "subsections": []
            },
            {
                "name": "NOTES",
                "lines": 22,
                "subsections": []
            },
            {
                "name": "EXAMPLES",
                "lines": 22,
                "subsections": []
            },
            {
                "name": "COMPATIBILITY",
                "lines": 10,
                "subsections": []
            }
        ]
    }
}