Posts

Showing posts with the label backend engineering

Advisory Lock

Image
Introduction Unlike Level Locks created by the database at the physical layer, developers can freely create Advisory Locks based on custom logic at the application level The database does not know the meaning of this lock and it only acts as an intermediary to hold the lock (usually a bigint number) The process that arrives first is granted the lock for processing Meanwhile, subsequent processes must line up and wait to acquire the lock When a process finishes processing, it returns the lock to the next process Advantages When using other types of locks (such as Table level lock, Row level lock or Page level lock), you must rely on an actual existing data row in the table to lock, whereas Advisory Lock does not require any available data row, you can use any random number to lock It is extremely lightweight because it only exists on the Database RAM, does not write to the hard drive and does not generate redundant data (dead tuples) Functions To create an advisory lock, Postgres provid...

Composite Index

Image
Introduction A Composite Index is an index type that contains two or more columns on the same table. It includes the following characteristics A Composite Index can be created for a maximum of 32 columns This is the default configuration according to the INDEX_MAX_KEYS constant of Postgres In practice, you should not create an index with more than 3 to 4 columns because it increases the index size, slowing down INSERT/UPDATE/DELETE operations The column order is sorted ascendingly by default However, unlike a standard index where you can sort ascending or descending at will, when using a Composite Index, you can only sort the columns all ascending, all descending or in the exact order specified at creation time For example, when using ON table (c1, c2) (default is all ascending), then You can query ORDER BY c1 ASC, c2 ASC You can query ORDER BY c1 DESC, c2 DESC But the index will not work with the query ORDER BY c1 ASC, c2 DESC Operation Order When using a Composite Index, you must ...