Skip to main content

BUILD DEFERRED (SQL)

Populates one or more tables with data that had been inserted or updated, but not filed.

Synopsis

BUILD DEFERRED [ALL] [%NOLOCK] [%NOJOURN] FOR TABLE table-name

BUILD DEFERRED [ALL] [%NOLOCK] [%NOJOURN] FOR SCHEMA schema-name

BUILD DEFERRED [ALL] [%NOLOCK] [%NOJOURN] FOR ALL

Description

The BUILD DEFERRED statement provides the primary interface for executing the backfilling operation that is necessary to file properties and indexes that have been deferred.

Deferring the filing operation reduces the computational resources that are required at the time of inserting or updating your data. You can then execute the backfilling operation (which files the deferred records and builds their associated indexes) at a time when more system resources are available—for example, you can file records in bulk at a scheduled time when there are fewer queries being executed on your system. In this way, deferred filing can improve the operational efficiency of your SQL architecture.

This statement is only needed when a table has fields or indexes defined as Deferred or if you execute an INSERT %DEFER ... statement.

BUILD DEFERRED provides three syntax forms for filing records that were inserted or updated into the table, but that had filing work deferred until a later point:

  • BUILD DEFERRED FOR TABLE table-name files fields and indexes within the specified table which are defined as Deferred.

  • BUILD DEFERRED FOR SCHEMA schema-name files fields and indexes which are defined as Deferred within all tables in a schema.

  • BUILD DEFERRED FOR ALL files fields and indexes which are defined as Deferred within all tables in the current namespace.

By default, BUILD DEFERRED files only the computed fields and indexes that are defined as Deferred. It does not file records that have been deferred using an INSERT %DEFER statement, which defers all fields and indexes which support deferred filing, whether or not they are defined as Deferred. To build these records as well, you must specify the ALL keyword.

Until you perform a backfilling operation with BUILD DEFERRED, tables that defer their filing on insert or update may yield inaccurate query results. Upon completion of the backfilling operation, all deferred data which was added or updated since the last execution of BUILD DEFERRED becomes available to queries.

Keywords and Arguments

ALL

A command which specifies the ALL keyword files every computed field and index that supports deferred filing. This includes fields and indexes which are not defined as Deferred, but which were deferred using an INSERT %DEFER statement. (Without the ALL keyword, BUILD DEFERRED ignores these records.) You can use the ALL keyword at any supported scope (FOR TABLE, FOR SCHEMA, or FOR ALL).

table-name

The name of an existing table. A command which specifies a table-name with the FOR TABLE clause files the outstanding deferred records for the specified table. A table-name can be qualified (schema.table), or unqualified (table). An unqualified table name takes the default schema name.

schema-name

The name of an existing schema. A command which specifies a schema-name with the FOR SCHEMA clause files the outstanding deferred records for all tables in the specified schema.

Transaction Considerations

Atomicity

BUILD DEFERRED behaves as if the current process uses the IMPLICIT (autocommit on) commit mode, even if the process uses the EXPLICIT (autocommit off) commit mode. Each row that BUILD DEFERRED files is committed in its own transaction, so a backfilling operation cannot be rolled back. To execute a backfilling operation entirely within a single transaction, explicitly start a transaction and then call the %BuildDeferred method with 0 specified as the value for parallel.

Locking and Journaling

BUILD DEFERRED files each row that requires filing as a separate transaction, acquiring an independent lock on the target row for each transaction. This ensures that a failure while filing one row does not affect filing work for previous or subsequent rows. You can specify %NOLOCK to file rows without acquiring row locks.

By default, the BUILD DEFERRED statement uses the journaling setting for the current process. You can specify %NOJOURN to prevent journaling; this option also disables transaction management for each constituent filing operation.

If you run BUILD DEFERRED while your process has an open transaction, and that transaction has inserted or updated some of the rows that require filing, BUILD DEFERRED may fail to acquire locks on those rows. A row that fails to file because of a lock failure remains unfiled, and the next BUILD DEFERRED operation attempts to file it again. To file such rows within a long-running transaction, specify %NOLOCK and %NOJOURN.

To specify the %NOLOCK or %NOJOURN option, you must have the corresponding SQL administrative privilege. To determine if you have the appropriate privileges, use %CHECKPRIV. To assign these privileges to a user, use GRANT. For more details, see Privileges.

Return Codes

BUILD DEFERRED returns SQLCODE 0 if the backfilling operation completed. If the build operation failed to file any individual records, it notes this failure as a message in the SQL Diagnostics Log (%SQL_Diag.Message), in the row which is associated with the operation, using the appropriate SQLCODE for that transaction’s failure. The filing of these records remains as pending work for a subsequent backfilling operation.

If BUILD DEFERRED does not find any deferred records to file in the specified schema, table, or namespace, it returns SQLCODE 100.

If the operation encounters any error which prevents it from completing, the operation returns an SQLCODE with a value less than zero. These include:

  • -30 or -473: the specified table or schema was not found.

  • -168: the specified table does not contain any deferrable fields or indexes.

  • -400: the specified table was skipped for one of the following reasons:

    • Another backfilling operation is already being executed on the table.

    • A LOAD DATA operation is currently being executed on the table.

    • The table’s extent is currently locked.

    • The operation was unable to obtain a lock on the flag which identifies that a backfilling operation is in progress.

    The error message provides additional details.

    Note:

    If you have specified FOR SCHEMA or FOR ALL, the operation does not return an error code if a table is skipped. However, it records this event (along with the reason) in the %SQL_Diag.Result table.

Alternatives

To file the outstanding deferred records for a single table in ObjectScript, call the %BuildDeferred()Opens in a new tab class method of the class that defines the table:

set status=##class(classname).%BuildDeferred(all, nolock, nojourn, parallel, .SQLCODE)

The all, nolock, and nojourn parameters correspond to the ALL keyword and the %NOLOCK and %NOJOURN options of BUILD DEFERRED, respectively. Each of these parameters default to 0. The parallel parameter defaults to 1.

%BuildDeferred() returns a %StatusOpens in a new tab value; you can use the SQLCODE parameter (passed by reference) to obtain the specific SQLCODE that the operation returns.

See Also

FeedbackOpens in a new tab