Currently Available: Need a skilled Software Developer for your next project?
Categories
Databases PostgreSQL

Creating Indexes and Schemas in PostgreSQL 7.3

In PostgreSQL, a schema groups tables and other objects into separate namespaces, so you can give objects in different schemas the same name. An index is a separate data structure that PostgreSQL builds from table values to locate matching rows faster. When you run a query, PostgreSQL’s query planner chooses the execution plan and decides whether an index will help.

The examples below follow the PostgreSQL 7.3 documentation for version 7.3.21, which is no longer supported. You can use them to understand or maintain that historical release, and you should check the documentation for your own server version before applying this syntax or behavior to a newer PostgreSQL installation.

Create a schema and name objects explicitly

You create a schema to group related tables and other database objects. In PostgreSQL 7.3, each schema belongs to the current database and does not create a separate database. To create one, run CREATE SCHEMA:

CREATE SCHEMA app;

The 7.3 syntax also lets you assign an owner:

CREATE SCHEMA reporting AUTHORIZATION analyst_role;

To create a schema owned by another user, PostgreSQL 7.3 requires you to be a superuser, and you also need the CREATE privilege on the current database. The 7.3 CREATE SCHEMA reference lists CREATE TABLE, CREATE VIEW, and GRANT as supported subcommands inside a schema definition. It omits CREATE INDEX, so you create the table first and then create the index separately.

CREATE SCHEMA app;

CREATE TABLE app.orders (
    order_id     integer,
    customer_id  integer,
    created_at   date,
    status       text
);

CREATE INDEX orders_customer_created_idx
    ON app.orders (customer_id, created_at);

The name app.orders is a qualified name because it includes both the schema (app) and the table (orders). Using qualified names in setup scripts makes the target explicit and keeps PostgreSQL from depending on the active search path.

The search path is the ordered list of schemas PostgreSQL checks when you write an object name without a schema prefix. PostgreSQL 7.3 documents the default as $user, public. When you create an object without a schema name, PostgreSQL places it in the first schema on that list that exists. You can inspect the current setting with SHOW search_path;, or set it yourself:

SET search_path TO app, public;

SELECT order_id
FROM orders;

With this setting, PostgreSQL checks app before public when it looks up orders. The 7.3 schema documentation explains how schema order controls both name lookup and object creation.

Build indexes for specific queries

An index lets PostgreSQL find rows without scanning every row in a table, though the query planner may still choose a table scan. PostgreSQL 7.3 recommends running ANALYZE to keep planner statistics up to date. Indexes also slow down inserts, updates, and deletes, because PostgreSQL has to update every index on a table whenever it modifies that table’s rows. Create indexes for the queries that benefit from them, then check whether PostgreSQL actually uses them.

The earlier example creates a composite index on two columns, customer_id and created_at. It targets queries that match a specific customer_id and also filter or sort by created_at, such as:

SELECT order_id, created_at
FROM app.orders
WHERE customer_id = 42;

That index can help this query, though PostgreSQL may still pick a different plan. In version 7.3, PostgreSQL can use at most one index per table in a single query or data-change command. That limit applies to 7.3 and does not reflect newer PostgreSQL releases. See the PostgreSQL 7.3 index guidance for the documented trade-offs.

PostgreSQL 7.3 supports ordinary, unique, function-based, and partial indexes. A unique index blocks duplicate key values, including any duplicates already in the table when PostgreSQL builds the index. A function-based index stores the output of a function applied to a column. A partial index includes only the rows that match its WHERE condition.

CREATE UNIQUE INDEX orders_order_id_idx
    ON app.orders (order_id);

CREATE INDEX people_upper_name_idx
    ON app.people (upper(name));

CREATE INDEX open_orders_customer_idx
    ON app.orders (customer_id)
    WHERE status = 'open';

The people example assumes app.people already exists with a name column. In 7.3, any function or operator you use in an index definition must be immutable, meaning it always returns the same result for the same inputs and cannot depend on outside state such as the current time or another table’s rows. A partial-index WHERE clause can reference columns from the indexed table, and it cannot include a subquery or an aggregate expression. The 7.3 CREATE INDEX reference covers these rules in detail.

Choose an index method for the data

PostgreSQL 7.3 documents four index access methods for organizing and searching index data: B-tree, R-tree, Hash, and GiST. B-tree is the default method and handles standard comparisons, including equality and range checks for smaller or larger values. The 7.3 documentation recommends R-tree for spatial data. Hash indexes handle equality comparisons, though the 7.3 documentation discourages using them. Its tests found Hash indexes to be equal to or slower than B-tree indexes, larger on disk, slower to build, and less effective under high concurrency. That warning applies specifically to the 7.3 release.

For multicolumn indexes, PostgreSQL 7.3 supports B-tree and GiST. Its default maximum is 32 keys, though you could change that limit when compiling PostgreSQL. Only B-tree supports unique indexes in 7.3. Because these limits and behaviors belong to a historical release, check the documentation for your own PostgreSQL version before relying on them. The 7.3 index-type reference describes each method.

Check schema privileges and search-path risks

Schema privileges control who can look up objects in a schema and who can create new ones there. In PostgreSQL 7.3, granting USAGE lets a user look up objects in the schema, while table or object privileges still control whether that user can read or modify them. Granting CREATE lets a user create objects inside the schema. Having access to a schema never grants permission to read or change its tables on its own.

PostgreSQL 7.3 documents that every user has CREATE on the public schema by default. An administrator can revoke that permission with:

REVOKE CREATE ON public FROM PUBLIC;

In this command, the lowercase public names the schema, and PUBLIC refers to all users. Newer or upgraded PostgreSQL databases may not share this historical default. Current PostgreSQL schema documentation also warns against leaving a schema that untrusted users can write to in search_path. If you run a query with an unqualified object name, PostgreSQL could resolve it to an object that another user placed in that schema.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *