PostgreSQL · Execute

Progress messages from a procedure

The messages a routine sends while it runs, in two scenarios: RAISE NOTICE and WARNING come back in Messages, in order, for the application to show or log as it chooses (ARX never writes them to a log); CollectMessages = false leaves them out.

Set up for this page

The scenarios build on the sample application: SampleContext.cs (the dbContext every sample is given); SampleOutput.cs (Step and Show, which print); SampleContext.Application.cs (dbContext.Service, the application's ARX service); SampleCatalog.cs (the tables' ids, keys, lookups and display names).

1 The messages, in order

An import of 1,000 rows: a notice every 250, then a warning because it's large.

Already in scope: service (ArxPgDbService)

C#
var result = await service.ExecuteAsync(Import(1_000));

foreach (var message in result.Messages)
{
    Console.WriteLine($"{message.Severity,-7} {message.Text}");
}
Used above

Defined once in this sample.

private static ArxProcedureExpression Import(int rows)
{
    var procedure = new ArxProcedureExpression("sp_import_progress");
    procedure.AddParameter("p_rows", rows);
    return procedure;
}

Check it in the database

SQL

Run it in any SQL tool connected to the database, after the code has run.

SELECT proname, prokind
FROM pg_proc
WHERE proname = 'sp_import_progress';

Expect: sp_import_progress, p (the messages themselves are only seen by the session that ran the call).

2 Not collected

The same call without collecting them.

Already in scope: service (ArxPgDbService)

C#
var result = await service.ExecuteAsync(Import(1_000), new ArxExecuteOptions { CollectMessages = false });
Console.WriteLine($"{result.Messages.Count} messages");
Used above

Defined once in this sample.

private static ArxProcedureExpression Import(int rows)
{
    var procedure = new ArxProcedureExpression("sp_import_progress");
    procedure.AddParameter("p_rows", rows);
    return procedure;
}

Check it in the database

SQL

Run it in any SQL tool connected to the database, after the code has run.

SELECT count(*)
FROM pg_proc
WHERE proname = 'sp_import_progress' AND prokind = 'p';

Expect: 1.

Run the complete sample

The code above comes from one program, built in a fresh project and run against the database. To run it yourself, here is every file and step.

The program, its files and how to run it

Run it locally

  1. .NET SDK: .NET SDK 10.0.100 or later 10.0.x (target net10.0).
  2. Create the project:
    Terminal
    dotnet new console -n ArxSample -f net10.0
    cd ArxSample
  3. Install the packages:
    Terminal
    dotnet add package ARX.SDK.PostgreSql --version 1.0.0-rc.1
  4. Database: a PostgreSQL 16 (tested with 16.15) database with the sample schema.
  5. Configuration: set ARX_SAMPLES_POSTGRESQL to its connection string. Nothing is hard-coded.
  6. Source code: add the files of the sample application this sample uses (SampleContext.cs, SampleOutput.cs, SampleContext.Application.cs, SampleCatalog.cs), then replace Program.cs and add the other file below, with the file names shown.
  7. Run it:
    Terminal
    dotnet run
  8. Expected output: below.

The files

Program.cs

Runs the sample. Generated for this page; it reads the connection string from the environment.

using ArxSamples.PostgreSql;
using ArxSamples.PostgreSql.Operations.Execute.RealCases;

// The connection string comes from the environment, never from the code (see "Configure the connection").
var connectionString = Environment.GetEnvironmentVariable("ARX_SAMPLES_POSTGRESQL")
    ?? throw new InvalidOperationException("Set the ARX_SAMPLES_POSTGRESQL environment variable to your PostgreSQL connection string.");

await new RealCase6_ProgressMessages().RunAsync(new SampleContext(connectionString));
RealCase6_ProgressMessages.cs

The sample, as it is in the repository.

using ARX.SDK.Query;
using ARX.SDK.Services;
using static ArxSamples.PostgreSql.SampleOutput;

namespace ArxSamples.PostgreSql.Operations.Execute.RealCases;

/// <summary>
/// The messages a routine sends while it runs, in two scenarios: <c>RAISE NOTICE</c> and <c>WARNING</c> come
/// back in <see cref="ArxExecuteResult.Messages"/>, in order, for the application to show or log as it chooses (ARX never
/// writes them to a log); <c>CollectMessages = false</c> leaves them out.
/// </summary>
public sealed class RealCase6_ProgressMessages : ISample
{
    public string Name => "execute-real-6-progress-messages";

    public string Title => "Progress messages from a procedure";

    public async Task RunAsync(SampleContext dbContext)
    {
        await Scenario1_TheMessagesInOrderAsync(dbContext);

        await Scenario2_NotCollectedAsync(dbContext);
    }

    private static ArxProcedureExpression Import(int rows)
    {
        var procedure = new ArxProcedureExpression("sp_import_progress");
        procedure.AddParameter("p_rows", rows);
        return procedure;
    }

    /// <summary>1. An import of 1,000 rows: a notice every 250, then a warning because it's large.</summary>
    private static async Task Scenario1_TheMessagesInOrderAsync(SampleContext dbContext)
    {
        Step("1. The messages, in order");

        var result = await dbContext.Service.ExecuteAsync(Import(1_000));

        foreach (var message in result.Messages)
        {
            Show($"{message.Severity,-7} {message.Text}");
        }

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * SELECT proname, prokind
         * FROM pg_proc
         * WHERE proname = 'sp_import_progress';
         * -- Expect: sp_import_progress, p (the messages themselves are only seen by the session that ran the call).
         * ===========================================================
         */
    }

    /// <summary>2. The same call without collecting them.</summary>
    private static async Task Scenario2_NotCollectedAsync(SampleContext dbContext)
    {
        Step("2. Not collected");

        var result = await dbContext.Service.ExecuteAsync(Import(1_000), new ArxExecuteOptions { CollectMessages = false });
        Show($"{result.Messages.Count} messages");

        /* ===========================================================
         * [DIRECT QUERY CHECK]
         * -----------------------------------------------------------
         * SELECT count(*)
         * FROM pg_proc
         * WHERE proname = 'sp_import_progress' AND prokind = 'p';
         * -- Expect: 1.
         * ===========================================================
         */
    }
}

Expected output

What dotnet run printed when the sample was verified, on a freshly created sample schema. Ids differ when the tables already hold rows.

Output
  > 1. The messages, in order
      NOTICE  imported 250 of 1000 rows
      NOTICE  imported 500 of 1000 rows
      NOTICE  imported 750 of 1000 rows
      NOTICE  imported 1000 of 1000 rows
      WARNING a large import: 1000 rows
  > 2. Not collected
      0 messages